Skip to content

Commit

Permalink
Merge pull request #586 from x-team/connector-based-labels-refresh
Browse files Browse the repository at this point in the history
Bring back usage of cached labels
  • Loading branch information
frankiejarrett committed Jul 18, 2014
2 parents bed8bd1 + 764fa9e commit 56cdb32
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
18 changes: 18 additions & 0 deletions connectors/posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 )
*
Expand Down
32 changes: 23 additions & 9 deletions connectors/taxonomies.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
*
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -174,16 +190,14 @@ 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!
$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(
Expand Down

0 comments on commit 56cdb32

Please sign in to comment.