diff --git a/connectors/posts.php b/connectors/posts.php index c1226e2aa..aa245fb22 100644 --- a/connectors/posts.php +++ b/connectors/posts.php @@ -52,6 +52,9 @@ public static function get_context_labels() { global $wp_post_types; $post_types = wp_filter_object_list( $wp_post_types, array(), null, 'label' ); $post_types = array_diff_key( $post_types, array_flip( self::get_ignored_post_types() ) ); + + add_action( 'registered_post_type', array( __CLASS__, '_registered_post_type' ), 10, 2 ); + return $post_types; } @@ -110,6 +113,21 @@ public static function action_links( $links, $record ) { return $links; } + /** + * Catch registeration of post_types after initial loading, to cache its labels + * + * @action registered_post_type + * + * @param string $post_type Post type slug + * @param array $args Arguments used to register the post type + */ + public static function _registered_post_type( $post_type, $args ) { + $post_type_obj = get_post_type_object( $post_type ); + $label = $post_type_obj->label; + + WP_Stream_Connectors::$term_labels['stream_context'][ $post_type ] = $label; + } + /** * Log all post status changes ( creating / updating / trashing ) * diff --git a/connectors/taxonomies.php b/connectors/taxonomies.php index d7d8217ba..82a033ac1 100644 --- a/connectors/taxonomies.php +++ b/connectors/taxonomies.php @@ -78,6 +78,8 @@ public static function get_context_labels() { self::$context_labels = wp_list_pluck( $labels, 'name' ); + add_action( 'registered_taxonomy', array( __CLASS__, '_registered_taxonomy' ), 10, 3 ); + return self::$context_labels; } @@ -103,6 +105,24 @@ public static function action_links( $links, $record ) { return $links; } + /** + * Catch registration of taxonomies after inital loading, so we can cache its labels + * + * @action registered_taxonomy + * + * @param string $taxonomy Taxonomy slug + * @param array|string $object_type Object type or array of object types + * @param array|string $args Array or string of taxonomy registration arguments + */ + public static function _registered_taxonomy( $taxonomy, $object_type, $args ) { + $taxonomy_obj = get_taxonomy( $taxonomy ); + $label = get_taxonomy_labels( $taxonomy_obj )->name; + + self::$context_labels[ $taxonomy ] = $label; + + WP_Stream_Connectors::$term_labels['stream_context'][ $taxonomy ] = $label; + } + /** * Tracks creation of terms * @@ -113,11 +133,9 @@ public static function callback_created_term( $term_id, $tt_id, $taxonomy ) { return; } - global $wp_taxonomies; - $term = get_term( $term_id, $taxonomy ); $term_name = $term->name; - $taxonomy_label = strtolower( $wp_taxonomies[ $taxonomy ]->labels->singular_name ); + $taxonomy_label = strtolower( self::$context_labels[ $taxonomy ] ); $term_parent = $term->parent; self::log( @@ -142,11 +160,9 @@ public static function callback_delete_term( $term_id, $tt_id, $taxonomy, $delet return; } - global $wp_taxonomies; - $term_name = $deleted_term->name; $term_parent = $deleted_term->parent; - $taxonomy_label = strtolower( $wp_taxonomies[ $taxonomy ]->labels->singular_name ); + $taxonomy_label = strtolower( self::$context_labels[ $taxonomy ] ); self::log( _x( @@ -174,8 +190,6 @@ public static function callback_edited_term( $term_id, $tt_id, $taxonomy ) { return; } - global $wp_taxonomies; - $term = self::$cached_term_before_update; if ( ! $term ) { // For some reason! @@ -183,7 +197,7 @@ public static function callback_edited_term( $term_id, $tt_id, $taxonomy ) { } $term_name = $term->name; - $taxonomy_label = strtolower( $wp_taxonomies[ $taxonomy ]->labels->singular_name ); + $taxonomy_label = strtolower( self::$context_labels[ $taxonomy ] ); $term_parent = $term->parent; self::log(