@@ -105,8 +105,8 @@ |
||
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | /** |
| 108 | - * @param $limit |
|
| 109 | - * @param $offset |
|
| 108 | + * @param integer $limit |
|
| 109 | + * @param integer $offset |
|
| 110 | 110 | * |
| 111 | 111 | * @return int|\WP_Error|\WP_Term[] |
| 112 | 112 | */ |
@@ -4,7 +4,6 @@ |
||
| 4 | 4 | |
| 5 | 5 | |
| 6 | 6 | use Wordlift\Vocabulary\Analysis_Background_Service; |
| 7 | -use Wordlift\Vocabulary\Analysis_Service; |
|
| 8 | 7 | use Wordlift\Vocabulary\Data\Term_Data\Term_Data_Factory; |
| 9 | 8 | use WP_REST_Server; |
| 10 | 9 | |
@@ -14,130 +14,130 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Tag_Rest_Endpoint { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @var Term_Data_Factory |
|
| 19 | - */ |
|
| 20 | - private $term_data_factory; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * Tag_Rest_Endpoint constructor. |
|
| 24 | - * |
|
| 25 | - * @param Term_Data_Factory $term_data_factory |
|
| 26 | - */ |
|
| 27 | - public function __construct( $term_data_factory ) { |
|
| 28 | - |
|
| 29 | - $this->term_data_factory = $term_data_factory; |
|
| 30 | - |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - |
|
| 34 | - public function register_routes() { |
|
| 35 | - $that = $this; |
|
| 36 | - add_action( 'rest_api_init', |
|
| 37 | - function () use ( $that ) { |
|
| 38 | - register_rest_route( |
|
| 39 | - Api_Config::REST_NAMESPACE, |
|
| 40 | - '/tags', |
|
| 41 | - array( |
|
| 42 | - 'methods' => WP_REST_Server::CREATABLE, |
|
| 43 | - 'callback' => array( $that, 'get_tags' ), |
|
| 44 | - //@todo : review the permission level |
|
| 45 | - 'permission_callback' => function () { |
|
| 46 | - return current_user_can( 'manage_options' ); |
|
| 47 | - }, |
|
| 48 | - 'args' => array( |
|
| 49 | - 'limit' => array( |
|
| 50 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 51 | - return is_numeric( $param ) && $param; |
|
| 52 | - }, |
|
| 53 | - 'required' => true, |
|
| 54 | - ), |
|
| 55 | - 'offset' => array( |
|
| 56 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 57 | - return is_numeric( $param ); |
|
| 58 | - }, |
|
| 59 | - 'required' => true, |
|
| 60 | - ), |
|
| 61 | - ), |
|
| 62 | - ) |
|
| 63 | - ); |
|
| 64 | - } ); |
|
| 65 | - |
|
| 66 | - |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - public function get_tags( $request ) { |
|
| 71 | - |
|
| 72 | - $data = $request->get_params(); |
|
| 73 | - $offset = (int) $data['offset']; |
|
| 74 | - $limit = (int) $data['limit']; |
|
| 75 | - $tags = $this->get_tags_from_db( $limit, $offset ); |
|
| 76 | - $term_data_list = array(); |
|
| 77 | - |
|
| 78 | - foreach ( $tags as $tag ) { |
|
| 79 | - |
|
| 80 | - if ( $this->is_tag_excluded_from_ui( $tag ) ) { |
|
| 81 | - continue; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @param $tag \WP_Term |
|
| 86 | - */ |
|
| 87 | - $term_data_instance = $this->term_data_factory->get_term_data($tag); |
|
| 88 | - $term_data = $term_data_instance->get_data(); |
|
| 89 | - if ( $term_data['entities'] ) { |
|
| 90 | - $term_data_list[] = $term_data; |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - |
|
| 95 | - return $term_data_list; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @param \WP_Term $tag |
|
| 100 | - * |
|
| 101 | - * @return bool |
|
| 102 | - */ |
|
| 103 | - private function is_tag_excluded_from_ui( $tag ) { |
|
| 104 | - return (int) get_term_meta( $tag->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) === 1; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @param $limit |
|
| 109 | - * @param $offset |
|
| 110 | - * |
|
| 111 | - * @return int|\WP_Error|\WP_Term[] |
|
| 112 | - */ |
|
| 113 | - public function get_tags_from_db( $limit, $offset ) { |
|
| 114 | - |
|
| 115 | - |
|
| 116 | - $args = array( |
|
| 117 | - 'taxonomy' => 'post_tag', |
|
| 118 | - 'hide_empty' => false, |
|
| 119 | - 'number' => $limit, |
|
| 120 | - 'offset' => $offset, |
|
| 121 | - 'meta_query' => array( |
|
| 122 | - array( |
|
| 123 | - 'key' => Analysis_Background_Service::ENTITIES_PRESENT_FOR_TERM, |
|
| 124 | - 'compare' => 'EXISTS' |
|
| 125 | - ) |
|
| 126 | - ), |
|
| 127 | - 'orderby' => 'count', |
|
| 128 | - 'order' => 'DESC', |
|
| 129 | - ); |
|
| 130 | - |
|
| 131 | - global $wp_version; |
|
| 132 | - |
|
| 133 | - if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
| 134 | - return get_terms( 'post_tag', $args ); |
|
| 135 | - } else { |
|
| 136 | - return get_terms( $args ); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - |
|
| 140 | - } |
|
| 17 | + /** |
|
| 18 | + * @var Term_Data_Factory |
|
| 19 | + */ |
|
| 20 | + private $term_data_factory; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * Tag_Rest_Endpoint constructor. |
|
| 24 | + * |
|
| 25 | + * @param Term_Data_Factory $term_data_factory |
|
| 26 | + */ |
|
| 27 | + public function __construct( $term_data_factory ) { |
|
| 28 | + |
|
| 29 | + $this->term_data_factory = $term_data_factory; |
|
| 30 | + |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + |
|
| 34 | + public function register_routes() { |
|
| 35 | + $that = $this; |
|
| 36 | + add_action( 'rest_api_init', |
|
| 37 | + function () use ( $that ) { |
|
| 38 | + register_rest_route( |
|
| 39 | + Api_Config::REST_NAMESPACE, |
|
| 40 | + '/tags', |
|
| 41 | + array( |
|
| 42 | + 'methods' => WP_REST_Server::CREATABLE, |
|
| 43 | + 'callback' => array( $that, 'get_tags' ), |
|
| 44 | + //@todo : review the permission level |
|
| 45 | + 'permission_callback' => function () { |
|
| 46 | + return current_user_can( 'manage_options' ); |
|
| 47 | + }, |
|
| 48 | + 'args' => array( |
|
| 49 | + 'limit' => array( |
|
| 50 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
| 51 | + return is_numeric( $param ) && $param; |
|
| 52 | + }, |
|
| 53 | + 'required' => true, |
|
| 54 | + ), |
|
| 55 | + 'offset' => array( |
|
| 56 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
| 57 | + return is_numeric( $param ); |
|
| 58 | + }, |
|
| 59 | + 'required' => true, |
|
| 60 | + ), |
|
| 61 | + ), |
|
| 62 | + ) |
|
| 63 | + ); |
|
| 64 | + } ); |
|
| 65 | + |
|
| 66 | + |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + public function get_tags( $request ) { |
|
| 71 | + |
|
| 72 | + $data = $request->get_params(); |
|
| 73 | + $offset = (int) $data['offset']; |
|
| 74 | + $limit = (int) $data['limit']; |
|
| 75 | + $tags = $this->get_tags_from_db( $limit, $offset ); |
|
| 76 | + $term_data_list = array(); |
|
| 77 | + |
|
| 78 | + foreach ( $tags as $tag ) { |
|
| 79 | + |
|
| 80 | + if ( $this->is_tag_excluded_from_ui( $tag ) ) { |
|
| 81 | + continue; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @param $tag \WP_Term |
|
| 86 | + */ |
|
| 87 | + $term_data_instance = $this->term_data_factory->get_term_data($tag); |
|
| 88 | + $term_data = $term_data_instance->get_data(); |
|
| 89 | + if ( $term_data['entities'] ) { |
|
| 90 | + $term_data_list[] = $term_data; |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + |
|
| 95 | + return $term_data_list; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @param \WP_Term $tag |
|
| 100 | + * |
|
| 101 | + * @return bool |
|
| 102 | + */ |
|
| 103 | + private function is_tag_excluded_from_ui( $tag ) { |
|
| 104 | + return (int) get_term_meta( $tag->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) === 1; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @param $limit |
|
| 109 | + * @param $offset |
|
| 110 | + * |
|
| 111 | + * @return int|\WP_Error|\WP_Term[] |
|
| 112 | + */ |
|
| 113 | + public function get_tags_from_db( $limit, $offset ) { |
|
| 114 | + |
|
| 115 | + |
|
| 116 | + $args = array( |
|
| 117 | + 'taxonomy' => 'post_tag', |
|
| 118 | + 'hide_empty' => false, |
|
| 119 | + 'number' => $limit, |
|
| 120 | + 'offset' => $offset, |
|
| 121 | + 'meta_query' => array( |
|
| 122 | + array( |
|
| 123 | + 'key' => Analysis_Background_Service::ENTITIES_PRESENT_FOR_TERM, |
|
| 124 | + 'compare' => 'EXISTS' |
|
| 125 | + ) |
|
| 126 | + ), |
|
| 127 | + 'orderby' => 'count', |
|
| 128 | + 'order' => 'DESC', |
|
| 129 | + ); |
|
| 130 | + |
|
| 131 | + global $wp_version; |
|
| 132 | + |
|
| 133 | + if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
| 134 | + return get_terms( 'post_tag', $args ); |
|
| 135 | + } else { |
|
| 136 | + return get_terms( $args ); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | 142 | |
| 143 | 143 | } |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * |
| 25 | 25 | * @param Term_Data_Factory $term_data_factory |
| 26 | 26 | */ |
| 27 | - public function __construct( $term_data_factory ) { |
|
| 27 | + public function __construct($term_data_factory) { |
|
| 28 | 28 | |
| 29 | 29 | $this->term_data_factory = $term_data_factory; |
| 30 | 30 | |
@@ -33,28 +33,28 @@ discard block |
||
| 33 | 33 | |
| 34 | 34 | public function register_routes() { |
| 35 | 35 | $that = $this; |
| 36 | - add_action( 'rest_api_init', |
|
| 37 | - function () use ( $that ) { |
|
| 36 | + add_action('rest_api_init', |
|
| 37 | + function() use ($that) { |
|
| 38 | 38 | register_rest_route( |
| 39 | 39 | Api_Config::REST_NAMESPACE, |
| 40 | 40 | '/tags', |
| 41 | 41 | array( |
| 42 | 42 | 'methods' => WP_REST_Server::CREATABLE, |
| 43 | - 'callback' => array( $that, 'get_tags' ), |
|
| 43 | + 'callback' => array($that, 'get_tags'), |
|
| 44 | 44 | //@todo : review the permission level |
| 45 | - 'permission_callback' => function () { |
|
| 46 | - return current_user_can( 'manage_options' ); |
|
| 45 | + 'permission_callback' => function() { |
|
| 46 | + return current_user_can('manage_options'); |
|
| 47 | 47 | }, |
| 48 | 48 | 'args' => array( |
| 49 | 49 | 'limit' => array( |
| 50 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 51 | - return is_numeric( $param ) && $param; |
|
| 50 | + 'validate_callback' => function($param, $request, $key) { |
|
| 51 | + return is_numeric($param) && $param; |
|
| 52 | 52 | }, |
| 53 | 53 | 'required' => true, |
| 54 | 54 | ), |
| 55 | 55 | 'offset' => array( |
| 56 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 57 | - return is_numeric( $param ); |
|
| 56 | + 'validate_callback' => function($param, $request, $key) { |
|
| 57 | + return is_numeric($param); |
|
| 58 | 58 | }, |
| 59 | 59 | 'required' => true, |
| 60 | 60 | ), |
@@ -67,17 +67,17 @@ discard block |
||
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | |
| 70 | - public function get_tags( $request ) { |
|
| 70 | + public function get_tags($request) { |
|
| 71 | 71 | |
| 72 | 72 | $data = $request->get_params(); |
| 73 | 73 | $offset = (int) $data['offset']; |
| 74 | 74 | $limit = (int) $data['limit']; |
| 75 | - $tags = $this->get_tags_from_db( $limit, $offset ); |
|
| 75 | + $tags = $this->get_tags_from_db($limit, $offset); |
|
| 76 | 76 | $term_data_list = array(); |
| 77 | 77 | |
| 78 | - foreach ( $tags as $tag ) { |
|
| 78 | + foreach ($tags as $tag) { |
|
| 79 | 79 | |
| 80 | - if ( $this->is_tag_excluded_from_ui( $tag ) ) { |
|
| 80 | + if ($this->is_tag_excluded_from_ui($tag)) { |
|
| 81 | 81 | continue; |
| 82 | 82 | } |
| 83 | 83 | |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | */ |
| 87 | 87 | $term_data_instance = $this->term_data_factory->get_term_data($tag); |
| 88 | 88 | $term_data = $term_data_instance->get_data(); |
| 89 | - if ( $term_data['entities'] ) { |
|
| 89 | + if ($term_data['entities']) { |
|
| 90 | 90 | $term_data_list[] = $term_data; |
| 91 | 91 | } |
| 92 | 92 | } |
@@ -100,8 +100,8 @@ discard block |
||
| 100 | 100 | * |
| 101 | 101 | * @return bool |
| 102 | 102 | */ |
| 103 | - private function is_tag_excluded_from_ui( $tag ) { |
|
| 104 | - return (int) get_term_meta( $tag->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) === 1; |
|
| 103 | + private function is_tag_excluded_from_ui($tag) { |
|
| 104 | + return (int) get_term_meta($tag->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true) === 1; |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | /** |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | * |
| 111 | 111 | * @return int|\WP_Error|\WP_Term[] |
| 112 | 112 | */ |
| 113 | - public function get_tags_from_db( $limit, $offset ) { |
|
| 113 | + public function get_tags_from_db($limit, $offset) { |
|
| 114 | 114 | |
| 115 | 115 | |
| 116 | 116 | $args = array( |
@@ -130,10 +130,10 @@ discard block |
||
| 130 | 130 | |
| 131 | 131 | global $wp_version; |
| 132 | 132 | |
| 133 | - if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
| 134 | - return get_terms( 'post_tag', $args ); |
|
| 133 | + if (version_compare($wp_version, '4.5', '<')) { |
|
| 134 | + return get_terms('post_tag', $args); |
|
| 135 | 135 | } else { |
| 136 | - return get_terms( $args ); |
|
| 136 | + return get_terms($args); |
|
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | |
@@ -22,7 +22,7 @@ |
||
| 22 | 22 | /** |
| 23 | 23 | * Analysis_Background_Process constructor. |
| 24 | 24 | * |
| 25 | - * @param $analysis_background_service Analysis_Background_Service A {@link Analysis_Background_Service} instance providing the supporting functions to this background process. |
|
| 25 | + * @param Analysis_Background_Service $analysis_background_service Analysis_Background_Service A {@link Analysis_Background_Service} instance providing the supporting functions to this background process. |
|
| 26 | 26 | */ |
| 27 | 27 | public function __construct( $analysis_background_service ) { |
| 28 | 28 | parent::__construct(); |
@@ -4,208 +4,208 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Analysis_Background_Process extends \Wordlift_Plugin_WP_Background_Process { |
| 6 | 6 | |
| 7 | - const WL_CMKG_ANALYSIS_BACKGROUND_PROCESS = '_wl_cmkg_analysis_background_process'; |
|
| 8 | - |
|
| 9 | - |
|
| 10 | - protected $action = 'wl_cmkg_analysis_background__analysis'; |
|
| 11 | - |
|
| 12 | - /** |
|
| 13 | - * @var Analysis_Background_Service |
|
| 14 | - */ |
|
| 15 | - private $analysis_background_service; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * @var \Wordlift_Log_Service |
|
| 19 | - */ |
|
| 20 | - private $log; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * Analysis_Background_Process constructor. |
|
| 24 | - * |
|
| 25 | - * @param $analysis_background_service Analysis_Background_Service A {@link Analysis_Background_Service} instance providing the supporting functions to this background process. |
|
| 26 | - */ |
|
| 27 | - public function __construct( $analysis_background_service ) { |
|
| 28 | - parent::__construct(); |
|
| 29 | - |
|
| 30 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 31 | - |
|
| 32 | - $this->analysis_background_service = $analysis_background_service; |
|
| 33 | - |
|
| 34 | - |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * This function is called: |
|
| 39 | - * - To start a new Synchronization, by passing a {@link Sync_Start_Message} instance. |
|
| 40 | - * - To synchronize a post, by passing a numeric ID. |
|
| 41 | - * |
|
| 42 | - * This function returns the parameter for the next call or NULL if there are no more posts to process. |
|
| 43 | - * |
|
| 44 | - * @param int[] $term_ids An array of term IDs. |
|
| 45 | - * |
|
| 46 | - * @return int[]|false The next term IDs or false if there are no more. |
|
| 47 | - */ |
|
| 48 | - protected function task( $term_ids ) { |
|
| 49 | - |
|
| 50 | - // Check if we must cancel. |
|
| 51 | - if ( $this->must_cancel() ) { |
|
| 52 | - $this->cancel(); |
|
| 53 | - |
|
| 54 | - return false; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - if ( $term_ids && is_array($term_ids) ) { |
|
| 58 | - $this->log->debug( sprintf( "Synchronizing terms %s...", implode( ', ', $term_ids ) ) ); |
|
| 59 | - } |
|
| 60 | - // Sync the item. |
|
| 61 | - return $this->sync_items( $term_ids ); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Start the background processing. |
|
| 66 | - * |
|
| 67 | - * @return bool True if the process has been started, otherwise false. |
|
| 68 | - */ |
|
| 69 | - public function start() { |
|
| 70 | - $this->log->debug( "Trying to start analysis bg service..." ); |
|
| 71 | - // Create a new Sync_Model state of `started`. |
|
| 72 | - if ( ! $this->is_started( self::get_state() ) ) { |
|
| 73 | - $this->log->debug( "Starting..." ); |
|
| 74 | - |
|
| 75 | - $sync_state = new Sync_State( time(), 0, $this->analysis_background_service->count(), time(), 'started' ); |
|
| 76 | - update_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, $sync_state, false ); |
|
| 77 | - |
|
| 78 | - $next = $this->analysis_background_service->next(); |
|
| 79 | - |
|
| 80 | - $this->push_to_queue( $next ); |
|
| 81 | - $this->save()->dispatch(); |
|
| 82 | - |
|
| 83 | - if ( $next && is_array($next) ) { |
|
| 84 | - $this->log->debug( sprintf( 'Started with term IDs %s.', implode( ', ', $next ) ) ); |
|
| 85 | - } |
|
| 86 | - return true; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - return false; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Set the transient to cancel the process. The next time the process runs, it'll check whether this transient is |
|
| 94 | - * set and will stop processing. |
|
| 95 | - */ |
|
| 96 | - public function request_cancel() { |
|
| 97 | - |
|
| 98 | - set_transient( "{$this->action}__cancel", true ); |
|
| 99 | - |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Get the sync state. |
|
| 104 | - * |
|
| 105 | - * @return Sync_State The {@link Sync_State}. |
|
| 106 | - */ |
|
| 107 | - public static function get_state() { |
|
| 108 | - |
|
| 109 | - try { |
|
| 110 | - return get_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, Sync_State::unknown() ); |
|
| 111 | - } catch ( \Exception $e ) { |
|
| 112 | - return Sync_State::unknown(); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Check whether the provided state is `started` or not. |
|
| 119 | - * |
|
| 120 | - * @param Sync_State $state The {@link Sync_State}. |
|
| 121 | - * |
|
| 122 | - * @return bool True if the state is started. |
|
| 123 | - */ |
|
| 124 | - private function is_started( $state ) { |
|
| 125 | - return $state instanceof Sync_State && 'started' === $state->state && 30 > ( time() - $state->last_update ); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Check whether the process must cancel or not. |
|
| 130 | - * |
|
| 131 | - * @return bool Whether to cancel or not the process. |
|
| 132 | - */ |
|
| 133 | - private function must_cancel() { |
|
| 134 | - |
|
| 135 | - return get_transient( "{$this->action}__cancel" ); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Cancels the current process. |
|
| 140 | - */ |
|
| 141 | - public function cancel() { |
|
| 142 | - |
|
| 143 | - $this->log->debug( "Cancelling synchronization..." ); |
|
| 144 | - |
|
| 145 | - // Cleanup the process data. |
|
| 146 | - $this->cancel_process(); |
|
| 147 | - |
|
| 148 | - // Set the state to cancelled. |
|
| 149 | - $state = self::get_state(); |
|
| 150 | - $state->set_state( 'cancelled' ); |
|
| 151 | - update_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, $state, false ); |
|
| 152 | - |
|
| 153 | - // Finally delete the transient. |
|
| 154 | - delete_transient( "{$this->action}__cancel" ); |
|
| 155 | - |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Push the post with the provided ID to the remote platform. |
|
| 160 | - * |
|
| 161 | - * @param int[] $term_ids The term IDs. |
|
| 162 | - * |
|
| 163 | - * @return int[]|false The next term ID to process or false if processing is complete. |
|
| 164 | - */ |
|
| 165 | - private function sync_items( $term_ids ) { |
|
| 166 | - |
|
| 167 | - |
|
| 168 | - $this->log->debug( "sync_items called with term ids : " . join( ",", $term_ids ) ); |
|
| 169 | - |
|
| 170 | - if (! $term_ids ) { |
|
| 171 | - $this->cancel_process(); |
|
| 172 | - return false; |
|
| 173 | - } |
|
| 7 | + const WL_CMKG_ANALYSIS_BACKGROUND_PROCESS = '_wl_cmkg_analysis_background_process'; |
|
| 8 | + |
|
| 9 | + |
|
| 10 | + protected $action = 'wl_cmkg_analysis_background__analysis'; |
|
| 11 | + |
|
| 12 | + /** |
|
| 13 | + * @var Analysis_Background_Service |
|
| 14 | + */ |
|
| 15 | + private $analysis_background_service; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * @var \Wordlift_Log_Service |
|
| 19 | + */ |
|
| 20 | + private $log; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * Analysis_Background_Process constructor. |
|
| 24 | + * |
|
| 25 | + * @param $analysis_background_service Analysis_Background_Service A {@link Analysis_Background_Service} instance providing the supporting functions to this background process. |
|
| 26 | + */ |
|
| 27 | + public function __construct( $analysis_background_service ) { |
|
| 28 | + parent::__construct(); |
|
| 29 | + |
|
| 30 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 31 | + |
|
| 32 | + $this->analysis_background_service = $analysis_background_service; |
|
| 33 | + |
|
| 34 | + |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * This function is called: |
|
| 39 | + * - To start a new Synchronization, by passing a {@link Sync_Start_Message} instance. |
|
| 40 | + * - To synchronize a post, by passing a numeric ID. |
|
| 41 | + * |
|
| 42 | + * This function returns the parameter for the next call or NULL if there are no more posts to process. |
|
| 43 | + * |
|
| 44 | + * @param int[] $term_ids An array of term IDs. |
|
| 45 | + * |
|
| 46 | + * @return int[]|false The next term IDs or false if there are no more. |
|
| 47 | + */ |
|
| 48 | + protected function task( $term_ids ) { |
|
| 49 | + |
|
| 50 | + // Check if we must cancel. |
|
| 51 | + if ( $this->must_cancel() ) { |
|
| 52 | + $this->cancel(); |
|
| 53 | + |
|
| 54 | + return false; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + if ( $term_ids && is_array($term_ids) ) { |
|
| 58 | + $this->log->debug( sprintf( "Synchronizing terms %s...", implode( ', ', $term_ids ) ) ); |
|
| 59 | + } |
|
| 60 | + // Sync the item. |
|
| 61 | + return $this->sync_items( $term_ids ); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Start the background processing. |
|
| 66 | + * |
|
| 67 | + * @return bool True if the process has been started, otherwise false. |
|
| 68 | + */ |
|
| 69 | + public function start() { |
|
| 70 | + $this->log->debug( "Trying to start analysis bg service..." ); |
|
| 71 | + // Create a new Sync_Model state of `started`. |
|
| 72 | + if ( ! $this->is_started( self::get_state() ) ) { |
|
| 73 | + $this->log->debug( "Starting..." ); |
|
| 74 | + |
|
| 75 | + $sync_state = new Sync_State( time(), 0, $this->analysis_background_service->count(), time(), 'started' ); |
|
| 76 | + update_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, $sync_state, false ); |
|
| 77 | + |
|
| 78 | + $next = $this->analysis_background_service->next(); |
|
| 79 | + |
|
| 80 | + $this->push_to_queue( $next ); |
|
| 81 | + $this->save()->dispatch(); |
|
| 82 | + |
|
| 83 | + if ( $next && is_array($next) ) { |
|
| 84 | + $this->log->debug( sprintf( 'Started with term IDs %s.', implode( ', ', $next ) ) ); |
|
| 85 | + } |
|
| 86 | + return true; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + return false; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Set the transient to cancel the process. The next time the process runs, it'll check whether this transient is |
|
| 94 | + * set and will stop processing. |
|
| 95 | + */ |
|
| 96 | + public function request_cancel() { |
|
| 97 | + |
|
| 98 | + set_transient( "{$this->action}__cancel", true ); |
|
| 99 | + |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Get the sync state. |
|
| 104 | + * |
|
| 105 | + * @return Sync_State The {@link Sync_State}. |
|
| 106 | + */ |
|
| 107 | + public static function get_state() { |
|
| 108 | + |
|
| 109 | + try { |
|
| 110 | + return get_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, Sync_State::unknown() ); |
|
| 111 | + } catch ( \Exception $e ) { |
|
| 112 | + return Sync_State::unknown(); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Check whether the provided state is `started` or not. |
|
| 119 | + * |
|
| 120 | + * @param Sync_State $state The {@link Sync_State}. |
|
| 121 | + * |
|
| 122 | + * @return bool True if the state is started. |
|
| 123 | + */ |
|
| 124 | + private function is_started( $state ) { |
|
| 125 | + return $state instanceof Sync_State && 'started' === $state->state && 30 > ( time() - $state->last_update ); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Check whether the process must cancel or not. |
|
| 130 | + * |
|
| 131 | + * @return bool Whether to cancel or not the process. |
|
| 132 | + */ |
|
| 133 | + private function must_cancel() { |
|
| 134 | + |
|
| 135 | + return get_transient( "{$this->action}__cancel" ); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Cancels the current process. |
|
| 140 | + */ |
|
| 141 | + public function cancel() { |
|
| 142 | + |
|
| 143 | + $this->log->debug( "Cancelling synchronization..." ); |
|
| 144 | + |
|
| 145 | + // Cleanup the process data. |
|
| 146 | + $this->cancel_process(); |
|
| 147 | + |
|
| 148 | + // Set the state to cancelled. |
|
| 149 | + $state = self::get_state(); |
|
| 150 | + $state->set_state( 'cancelled' ); |
|
| 151 | + update_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, $state, false ); |
|
| 152 | + |
|
| 153 | + // Finally delete the transient. |
|
| 154 | + delete_transient( "{$this->action}__cancel" ); |
|
| 155 | + |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Push the post with the provided ID to the remote platform. |
|
| 160 | + * |
|
| 161 | + * @param int[] $term_ids The term IDs. |
|
| 162 | + * |
|
| 163 | + * @return int[]|false The next term ID to process or false if processing is complete. |
|
| 164 | + */ |
|
| 165 | + private function sync_items( $term_ids ) { |
|
| 166 | + |
|
| 167 | + |
|
| 168 | + $this->log->debug( "sync_items called with term ids : " . join( ",", $term_ids ) ); |
|
| 169 | + |
|
| 170 | + if (! $term_ids ) { |
|
| 171 | + $this->cancel_process(); |
|
| 172 | + return false; |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | - if ( ! is_array( $term_ids ) ) { |
|
| 176 | - $this->log->error( '$term_ids must be an array, received: ' . var_export( $term_ids, true ) ); |
|
| 175 | + if ( ! is_array( $term_ids ) ) { |
|
| 176 | + $this->log->error( '$term_ids must be an array, received: ' . var_export( $term_ids, true ) ); |
|
| 177 | 177 | |
| 178 | - return false; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - // Sync this item. |
|
| 182 | - if ( $this->analysis_background_service->perform_analysis_for_terms( $term_ids ) ) { |
|
| 183 | - |
|
| 184 | - $next = $this->analysis_background_service->next(); |
|
| 185 | - $next_state = isset( $next ) ? 'started' : 'ended'; |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Update the synchronization meta data, by increasing the current index. |
|
| 189 | - * |
|
| 190 | - * @var Sync_State $sync The {@link Sync_State}. |
|
| 191 | - */ |
|
| 192 | - $state = self::get_state() |
|
| 193 | - ->increment_index( $this->analysis_background_service->get_batch_size() ) |
|
| 194 | - ->set_state( $next_state ); |
|
| 195 | - update_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS . '', $state, false ); |
|
| 178 | + return false; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + // Sync this item. |
|
| 182 | + if ( $this->analysis_background_service->perform_analysis_for_terms( $term_ids ) ) { |
|
| 183 | + |
|
| 184 | + $next = $this->analysis_background_service->next(); |
|
| 185 | + $next_state = isset( $next ) ? 'started' : 'ended'; |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Update the synchronization meta data, by increasing the current index. |
|
| 189 | + * |
|
| 190 | + * @var Sync_State $sync The {@link Sync_State}. |
|
| 191 | + */ |
|
| 192 | + $state = self::get_state() |
|
| 193 | + ->increment_index( $this->analysis_background_service->get_batch_size() ) |
|
| 194 | + ->set_state( $next_state ); |
|
| 195 | + update_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS . '', $state, false ); |
|
| 196 | 196 | |
| 197 | 197 | |
| 198 | - // Return the next IDs or false if there aren't. |
|
| 199 | - return isset( $next ) ? $next : false; |
|
| 200 | - } else { |
|
| 201 | - // Retry. |
|
| 202 | - // @@todo: put a limit to the number of retries. |
|
| 203 | - |
|
| 204 | - $this->log->error( sprintf( "Sync failed for terms %s.", implode( ', ', $term_ids ) ) ); |
|
| 205 | - |
|
| 206 | - return $term_ids; |
|
| 207 | - } |
|
| 198 | + // Return the next IDs or false if there aren't. |
|
| 199 | + return isset( $next ) ? $next : false; |
|
| 200 | + } else { |
|
| 201 | + // Retry. |
|
| 202 | + // @@todo: put a limit to the number of retries. |
|
| 203 | + |
|
| 204 | + $this->log->error( sprintf( "Sync failed for terms %s.", implode( ', ', $term_ids ) ) ); |
|
| 205 | + |
|
| 206 | + return $term_ids; |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | - } |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | 211 | } |
@@ -24,10 +24,10 @@ discard block |
||
| 24 | 24 | * |
| 25 | 25 | * @param $analysis_background_service Analysis_Background_Service A {@link Analysis_Background_Service} instance providing the supporting functions to this background process. |
| 26 | 26 | */ |
| 27 | - public function __construct( $analysis_background_service ) { |
|
| 27 | + public function __construct($analysis_background_service) { |
|
| 28 | 28 | parent::__construct(); |
| 29 | 29 | |
| 30 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 30 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
| 31 | 31 | |
| 32 | 32 | $this->analysis_background_service = $analysis_background_service; |
| 33 | 33 | |
@@ -45,20 +45,20 @@ discard block |
||
| 45 | 45 | * |
| 46 | 46 | * @return int[]|false The next term IDs or false if there are no more. |
| 47 | 47 | */ |
| 48 | - protected function task( $term_ids ) { |
|
| 48 | + protected function task($term_ids) { |
|
| 49 | 49 | |
| 50 | 50 | // Check if we must cancel. |
| 51 | - if ( $this->must_cancel() ) { |
|
| 51 | + if ($this->must_cancel()) { |
|
| 52 | 52 | $this->cancel(); |
| 53 | 53 | |
| 54 | 54 | return false; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - if ( $term_ids && is_array($term_ids) ) { |
|
| 58 | - $this->log->debug( sprintf( "Synchronizing terms %s...", implode( ', ', $term_ids ) ) ); |
|
| 57 | + if ($term_ids && is_array($term_ids)) { |
|
| 58 | + $this->log->debug(sprintf("Synchronizing terms %s...", implode(', ', $term_ids))); |
|
| 59 | 59 | } |
| 60 | 60 | // Sync the item. |
| 61 | - return $this->sync_items( $term_ids ); |
|
| 61 | + return $this->sync_items($term_ids); |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | /** |
@@ -67,21 +67,21 @@ discard block |
||
| 67 | 67 | * @return bool True if the process has been started, otherwise false. |
| 68 | 68 | */ |
| 69 | 69 | public function start() { |
| 70 | - $this->log->debug( "Trying to start analysis bg service..." ); |
|
| 70 | + $this->log->debug("Trying to start analysis bg service..."); |
|
| 71 | 71 | // Create a new Sync_Model state of `started`. |
| 72 | - if ( ! $this->is_started( self::get_state() ) ) { |
|
| 73 | - $this->log->debug( "Starting..." ); |
|
| 72 | + if ( ! $this->is_started(self::get_state())) { |
|
| 73 | + $this->log->debug("Starting..."); |
|
| 74 | 74 | |
| 75 | - $sync_state = new Sync_State( time(), 0, $this->analysis_background_service->count(), time(), 'started' ); |
|
| 76 | - update_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, $sync_state, false ); |
|
| 75 | + $sync_state = new Sync_State(time(), 0, $this->analysis_background_service->count(), time(), 'started'); |
|
| 76 | + update_option(self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, $sync_state, false); |
|
| 77 | 77 | |
| 78 | 78 | $next = $this->analysis_background_service->next(); |
| 79 | 79 | |
| 80 | - $this->push_to_queue( $next ); |
|
| 80 | + $this->push_to_queue($next); |
|
| 81 | 81 | $this->save()->dispatch(); |
| 82 | 82 | |
| 83 | - if ( $next && is_array($next) ) { |
|
| 84 | - $this->log->debug( sprintf( 'Started with term IDs %s.', implode( ', ', $next ) ) ); |
|
| 83 | + if ($next && is_array($next)) { |
|
| 84 | + $this->log->debug(sprintf('Started with term IDs %s.', implode(', ', $next))); |
|
| 85 | 85 | } |
| 86 | 86 | return true; |
| 87 | 87 | } |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | */ |
| 96 | 96 | public function request_cancel() { |
| 97 | 97 | |
| 98 | - set_transient( "{$this->action}__cancel", true ); |
|
| 98 | + set_transient("{$this->action}__cancel", true); |
|
| 99 | 99 | |
| 100 | 100 | } |
| 101 | 101 | |
@@ -107,8 +107,8 @@ discard block |
||
| 107 | 107 | public static function get_state() { |
| 108 | 108 | |
| 109 | 109 | try { |
| 110 | - return get_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, Sync_State::unknown() ); |
|
| 111 | - } catch ( \Exception $e ) { |
|
| 110 | + return get_option(self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, Sync_State::unknown()); |
|
| 111 | + } catch (\Exception $e) { |
|
| 112 | 112 | return Sync_State::unknown(); |
| 113 | 113 | } |
| 114 | 114 | |
@@ -121,8 +121,8 @@ discard block |
||
| 121 | 121 | * |
| 122 | 122 | * @return bool True if the state is started. |
| 123 | 123 | */ |
| 124 | - private function is_started( $state ) { |
|
| 125 | - return $state instanceof Sync_State && 'started' === $state->state && 30 > ( time() - $state->last_update ); |
|
| 124 | + private function is_started($state) { |
|
| 125 | + return $state instanceof Sync_State && 'started' === $state->state && 30 > (time() - $state->last_update); |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | /** |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | */ |
| 133 | 133 | private function must_cancel() { |
| 134 | 134 | |
| 135 | - return get_transient( "{$this->action}__cancel" ); |
|
| 135 | + return get_transient("{$this->action}__cancel"); |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | /** |
@@ -140,18 +140,18 @@ discard block |
||
| 140 | 140 | */ |
| 141 | 141 | public function cancel() { |
| 142 | 142 | |
| 143 | - $this->log->debug( "Cancelling synchronization..." ); |
|
| 143 | + $this->log->debug("Cancelling synchronization..."); |
|
| 144 | 144 | |
| 145 | 145 | // Cleanup the process data. |
| 146 | 146 | $this->cancel_process(); |
| 147 | 147 | |
| 148 | 148 | // Set the state to cancelled. |
| 149 | 149 | $state = self::get_state(); |
| 150 | - $state->set_state( 'cancelled' ); |
|
| 151 | - update_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, $state, false ); |
|
| 150 | + $state->set_state('cancelled'); |
|
| 151 | + update_option(self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, $state, false); |
|
| 152 | 152 | |
| 153 | 153 | // Finally delete the transient. |
| 154 | - delete_transient( "{$this->action}__cancel" ); |
|
| 154 | + delete_transient("{$this->action}__cancel"); |
|
| 155 | 155 | |
| 156 | 156 | } |
| 157 | 157 | |
@@ -162,27 +162,27 @@ discard block |
||
| 162 | 162 | * |
| 163 | 163 | * @return int[]|false The next term ID to process or false if processing is complete. |
| 164 | 164 | */ |
| 165 | - private function sync_items( $term_ids ) { |
|
| 165 | + private function sync_items($term_ids) { |
|
| 166 | 166 | |
| 167 | 167 | |
| 168 | - $this->log->debug( "sync_items called with term ids : " . join( ",", $term_ids ) ); |
|
| 168 | + $this->log->debug("sync_items called with term ids : ".join(",", $term_ids)); |
|
| 169 | 169 | |
| 170 | - if (! $term_ids ) { |
|
| 170 | + if ( ! $term_ids) { |
|
| 171 | 171 | $this->cancel_process(); |
| 172 | 172 | return false; |
| 173 | 173 | } |
| 174 | 174 | |
| 175 | - if ( ! is_array( $term_ids ) ) { |
|
| 176 | - $this->log->error( '$term_ids must be an array, received: ' . var_export( $term_ids, true ) ); |
|
| 175 | + if ( ! is_array($term_ids)) { |
|
| 176 | + $this->log->error('$term_ids must be an array, received: '.var_export($term_ids, true)); |
|
| 177 | 177 | |
| 178 | 178 | return false; |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | // Sync this item. |
| 182 | - if ( $this->analysis_background_service->perform_analysis_for_terms( $term_ids ) ) { |
|
| 182 | + if ($this->analysis_background_service->perform_analysis_for_terms($term_ids)) { |
|
| 183 | 183 | |
| 184 | 184 | $next = $this->analysis_background_service->next(); |
| 185 | - $next_state = isset( $next ) ? 'started' : 'ended'; |
|
| 185 | + $next_state = isset($next) ? 'started' : 'ended'; |
|
| 186 | 186 | |
| 187 | 187 | /** |
| 188 | 188 | * Update the synchronization meta data, by increasing the current index. |
@@ -190,18 +190,18 @@ discard block |
||
| 190 | 190 | * @var Sync_State $sync The {@link Sync_State}. |
| 191 | 191 | */ |
| 192 | 192 | $state = self::get_state() |
| 193 | - ->increment_index( $this->analysis_background_service->get_batch_size() ) |
|
| 194 | - ->set_state( $next_state ); |
|
| 195 | - update_option( self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS . '', $state, false ); |
|
| 193 | + ->increment_index($this->analysis_background_service->get_batch_size()) |
|
| 194 | + ->set_state($next_state); |
|
| 195 | + update_option(self::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS.'', $state, false); |
|
| 196 | 196 | |
| 197 | 197 | |
| 198 | 198 | // Return the next IDs or false if there aren't. |
| 199 | - return isset( $next ) ? $next : false; |
|
| 199 | + return isset($next) ? $next : false; |
|
| 200 | 200 | } else { |
| 201 | 201 | // Retry. |
| 202 | 202 | // @@todo: put a limit to the number of retries. |
| 203 | 203 | |
| 204 | - $this->log->error( sprintf( "Sync failed for terms %s.", implode( ', ', $term_ids ) ) ); |
|
| 204 | + $this->log->error(sprintf("Sync failed for terms %s.", implode(', ', $term_ids))); |
|
| 205 | 205 | |
| 206 | 206 | return $term_ids; |
| 207 | 207 | } |
@@ -7,14 +7,14 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Term_Count_Factory { |
| 9 | 9 | |
| 10 | - const CACHED_TERM_COUNT = 'cached_term_count'; |
|
| 10 | + const CACHED_TERM_COUNT = 'cached_term_count'; |
|
| 11 | 11 | |
| 12 | - public static function get_instance( $type ) { |
|
| 13 | - if ($type === self::CACHED_TERM_COUNT) { |
|
| 14 | - return new Cached_Term_Count( new Default_Term_Count() ); |
|
| 15 | - } |
|
| 16 | - return null; |
|
| 17 | - } |
|
| 12 | + public static function get_instance( $type ) { |
|
| 13 | + if ($type === self::CACHED_TERM_COUNT) { |
|
| 14 | + return new Cached_Term_Count( new Default_Term_Count() ); |
|
| 15 | + } |
|
| 16 | + return null; |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | 19 | |
| 20 | 20 | |
@@ -9,9 +9,9 @@ |
||
| 9 | 9 | |
| 10 | 10 | const CACHED_TERM_COUNT = 'cached_term_count'; |
| 11 | 11 | |
| 12 | - public static function get_instance( $type ) { |
|
| 13 | - if ($type === self::CACHED_TERM_COUNT) { |
|
| 14 | - return new Cached_Term_Count( new Default_Term_Count() ); |
|
| 12 | + public static function get_instance($type) { |
|
| 13 | + if ($type === self::CACHED_TERM_COUNT) { |
|
| 14 | + return new Cached_Term_Count(new Default_Term_Count()); |
|
| 15 | 15 | } |
| 16 | 16 | return null; |
| 17 | 17 | } |
@@ -9,11 +9,11 @@ |
||
| 9 | 9 | |
| 10 | 10 | interface Term_Count { |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * Return the term count which needs to be processed by the editor. |
|
| 14 | - * @return int |
|
| 15 | - */ |
|
| 16 | - public function get_term_count(); |
|
| 12 | + /** |
|
| 13 | + * Return the term count which needs to be processed by the editor. |
|
| 14 | + * @return int |
|
| 15 | + */ |
|
| 16 | + public function get_term_count(); |
|
| 17 | 17 | |
| 18 | 18 | } |
| 19 | 19 | |
@@ -7,16 +7,16 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Cached_Term_count_Manager { |
| 9 | 9 | |
| 10 | - public function connect_hook() { |
|
| 10 | + public function connect_hook() { |
|
| 11 | 11 | |
| 12 | - add_action( 'wordlift_vocabulary_analysis_complete_for_terms_batch', function () { |
|
| 13 | - delete_transient( Cached_Term_Count::TRANSIENT_KEY ); |
|
| 14 | - } ); |
|
| 12 | + add_action( 'wordlift_vocabulary_analysis_complete_for_terms_batch', function () { |
|
| 13 | + delete_transient( Cached_Term_Count::TRANSIENT_KEY ); |
|
| 14 | + } ); |
|
| 15 | 15 | |
| 16 | - add_action('created_post_tag', function () { |
|
| 17 | - delete_transient( Cached_Term_Count::TRANSIENT_KEY ); |
|
| 18 | - }); |
|
| 16 | + add_action('created_post_tag', function () { |
|
| 17 | + delete_transient( Cached_Term_Count::TRANSIENT_KEY ); |
|
| 18 | + }); |
|
| 19 | 19 | |
| 20 | - } |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | 22 | } |
| 23 | 23 | \ No newline at end of file |
@@ -9,12 +9,12 @@ |
||
| 9 | 9 | |
| 10 | 10 | public function connect_hook() { |
| 11 | 11 | |
| 12 | - add_action( 'wordlift_vocabulary_analysis_complete_for_terms_batch', function () { |
|
| 13 | - delete_transient( Cached_Term_Count::TRANSIENT_KEY ); |
|
| 12 | + add_action('wordlift_vocabulary_analysis_complete_for_terms_batch', function() { |
|
| 13 | + delete_transient(Cached_Term_Count::TRANSIENT_KEY); |
|
| 14 | 14 | } ); |
| 15 | 15 | |
| 16 | - add_action('created_post_tag', function () { |
|
| 17 | - delete_transient( Cached_Term_Count::TRANSIENT_KEY ); |
|
| 16 | + add_action('created_post_tag', function() { |
|
| 17 | + delete_transient(Cached_Term_Count::TRANSIENT_KEY); |
|
| 18 | 18 | }); |
| 19 | 19 | |
| 20 | 20 | } |
@@ -9,32 +9,32 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Cached_Term_Count implements Term_Count { |
| 11 | 11 | |
| 12 | - const TRANSIENT_KEY = '_wl_vocabulary_term_count_transient'; |
|
| 13 | - /** |
|
| 14 | - * @var Term_Count |
|
| 15 | - */ |
|
| 16 | - private $term_count; |
|
| 17 | - |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Cached_Term_Count constructor. |
|
| 21 | - * |
|
| 22 | - * @param $term_count Term_Count |
|
| 23 | - */ |
|
| 24 | - public function __construct( $term_count ) { |
|
| 25 | - $this->term_count = $term_count; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - public function get_term_count() { |
|
| 29 | - $data = get_transient( self::TRANSIENT_KEY ); |
|
| 30 | - |
|
| 31 | - if ( ! $data ) { |
|
| 32 | - $data = $this->term_count->get_term_count(); |
|
| 33 | - set_transient( self::TRANSIENT_KEY, $data, 8 * 60 * 60 ); |
|
| 34 | - |
|
| 35 | - return $data; |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - return $data; |
|
| 39 | - } |
|
| 12 | + const TRANSIENT_KEY = '_wl_vocabulary_term_count_transient'; |
|
| 13 | + /** |
|
| 14 | + * @var Term_Count |
|
| 15 | + */ |
|
| 16 | + private $term_count; |
|
| 17 | + |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Cached_Term_Count constructor. |
|
| 21 | + * |
|
| 22 | + * @param $term_count Term_Count |
|
| 23 | + */ |
|
| 24 | + public function __construct( $term_count ) { |
|
| 25 | + $this->term_count = $term_count; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + public function get_term_count() { |
|
| 29 | + $data = get_transient( self::TRANSIENT_KEY ); |
|
| 30 | + |
|
| 31 | + if ( ! $data ) { |
|
| 32 | + $data = $this->term_count->get_term_count(); |
|
| 33 | + set_transient( self::TRANSIENT_KEY, $data, 8 * 60 * 60 ); |
|
| 34 | + |
|
| 35 | + return $data; |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + return $data; |
|
| 39 | + } |
|
| 40 | 40 | } |
@@ -21,16 +21,16 @@ |
||
| 21 | 21 | * |
| 22 | 22 | * @param $term_count Term_Count |
| 23 | 23 | */ |
| 24 | - public function __construct( $term_count ) { |
|
| 24 | + public function __construct($term_count) { |
|
| 25 | 25 | $this->term_count = $term_count; |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | public function get_term_count() { |
| 29 | - $data = get_transient( self::TRANSIENT_KEY ); |
|
| 29 | + $data = get_transient(self::TRANSIENT_KEY); |
|
| 30 | 30 | |
| 31 | - if ( ! $data ) { |
|
| 31 | + if ( ! $data) { |
|
| 32 | 32 | $data = $this->term_count->get_term_count(); |
| 33 | - set_transient( self::TRANSIENT_KEY, $data, 8 * 60 * 60 ); |
|
| 33 | + set_transient(self::TRANSIENT_KEY, $data, 8 * 60 * 60); |
|
| 34 | 34 | |
| 35 | 35 | return $data; |
| 36 | 36 | } |
@@ -12,42 +12,42 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Default_Term_Count implements Term_Count { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Return count of terms which have entities and also not matched by editor, currently |
|
| 17 | - * it is used for the menu icon badge. |
|
| 18 | - * @return int |
|
| 19 | - */ |
|
| 20 | - public function get_term_count() { |
|
| 21 | - /** |
|
| 22 | - * @todo: add support for all terms, currently we add only |
|
| 23 | - * post_tag. |
|
| 24 | - */ |
|
| 25 | - return count( $this->get_terms_compat( 'post_tag', array( |
|
| 26 | - 'taxonomy' => 'post_tag', |
|
| 27 | - 'hide_empty' => false, |
|
| 28 | - 'fields' => 'ids', |
|
| 29 | - 'meta_query' => array( |
|
| 30 | - array( |
|
| 31 | - 'key' => Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, |
|
| 32 | - 'compare' => 'NOT EXISTS', |
|
| 33 | - ), |
|
| 34 | - array( |
|
| 35 | - 'key' => Analysis_Background_Service::ENTITIES_PRESENT_FOR_TERM, |
|
| 36 | - 'compare' => 'EXISTS' |
|
| 37 | - ) |
|
| 38 | - ), |
|
| 39 | - ) ) ); |
|
| 40 | - |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - private function get_terms_compat( $taxonomy, $args_with_taxonomy_key ) { |
|
| 44 | - global $wp_version; |
|
| 45 | - |
|
| 46 | - if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
| 47 | - return get_terms( $taxonomy, $args_with_taxonomy_key ); |
|
| 48 | - } else { |
|
| 49 | - return get_terms( $args_with_taxonomy_key ); |
|
| 50 | - } |
|
| 51 | - } |
|
| 15 | + /** |
|
| 16 | + * Return count of terms which have entities and also not matched by editor, currently |
|
| 17 | + * it is used for the menu icon badge. |
|
| 18 | + * @return int |
|
| 19 | + */ |
|
| 20 | + public function get_term_count() { |
|
| 21 | + /** |
|
| 22 | + * @todo: add support for all terms, currently we add only |
|
| 23 | + * post_tag. |
|
| 24 | + */ |
|
| 25 | + return count( $this->get_terms_compat( 'post_tag', array( |
|
| 26 | + 'taxonomy' => 'post_tag', |
|
| 27 | + 'hide_empty' => false, |
|
| 28 | + 'fields' => 'ids', |
|
| 29 | + 'meta_query' => array( |
|
| 30 | + array( |
|
| 31 | + 'key' => Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, |
|
| 32 | + 'compare' => 'NOT EXISTS', |
|
| 33 | + ), |
|
| 34 | + array( |
|
| 35 | + 'key' => Analysis_Background_Service::ENTITIES_PRESENT_FOR_TERM, |
|
| 36 | + 'compare' => 'EXISTS' |
|
| 37 | + ) |
|
| 38 | + ), |
|
| 39 | + ) ) ); |
|
| 40 | + |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + private function get_terms_compat( $taxonomy, $args_with_taxonomy_key ) { |
|
| 44 | + global $wp_version; |
|
| 45 | + |
|
| 46 | + if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
| 47 | + return get_terms( $taxonomy, $args_with_taxonomy_key ); |
|
| 48 | + } else { |
|
| 49 | + return get_terms( $args_with_taxonomy_key ); |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | 53 | } |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | * @todo: add support for all terms, currently we add only |
| 23 | 23 | * post_tag. |
| 24 | 24 | */ |
| 25 | - return count( $this->get_terms_compat( 'post_tag', array( |
|
| 25 | + return count($this->get_terms_compat('post_tag', array( |
|
| 26 | 26 | 'taxonomy' => 'post_tag', |
| 27 | 27 | 'hide_empty' => false, |
| 28 | 28 | 'fields' => 'ids', |
@@ -36,17 +36,17 @@ discard block |
||
| 36 | 36 | 'compare' => 'EXISTS' |
| 37 | 37 | ) |
| 38 | 38 | ), |
| 39 | - ) ) ); |
|
| 39 | + ))); |
|
| 40 | 40 | |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - private function get_terms_compat( $taxonomy, $args_with_taxonomy_key ) { |
|
| 43 | + private function get_terms_compat($taxonomy, $args_with_taxonomy_key) { |
|
| 44 | 44 | global $wp_version; |
| 45 | 45 | |
| 46 | - if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
| 47 | - return get_terms( $taxonomy, $args_with_taxonomy_key ); |
|
| 46 | + if (version_compare($wp_version, '4.5', '<')) { |
|
| 47 | + return get_terms($taxonomy, $args_with_taxonomy_key); |
|
| 48 | 48 | } else { |
| 49 | - return get_terms( $args_with_taxonomy_key ); |
|
| 49 | + return get_terms($args_with_taxonomy_key); |
|
| 50 | 50 | } |
| 51 | 51 | } |
| 52 | 52 | |
@@ -4,27 +4,27 @@ |
||
| 4 | 4 | use Wordlift\Vocabulary\Analysis_Service; |
| 5 | 5 | |
| 6 | 6 | class Term_Data_Factory { |
| 7 | - /** |
|
| 8 | - * @var Analysis_Service |
|
| 9 | - */ |
|
| 10 | - private $analysis_service; |
|
| 7 | + /** |
|
| 8 | + * @var Analysis_Service |
|
| 9 | + */ |
|
| 10 | + private $analysis_service; |
|
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * @var Analysis_Service |
|
| 14 | - */ |
|
| 15 | - public function __construct( $analysis_service ) { |
|
| 16 | - $this->analysis_service = $analysis_service; |
|
| 17 | - } |
|
| 12 | + /** |
|
| 13 | + * @var Analysis_Service |
|
| 14 | + */ |
|
| 15 | + public function __construct( $analysis_service ) { |
|
| 16 | + $this->analysis_service = $analysis_service; |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @param $term \WP_Term |
|
| 21 | - * |
|
| 22 | - * @return Term_Data |
|
| 23 | - */ |
|
| 24 | - public function get_term_data( $term ) { |
|
| 25 | - $entities = $this->analysis_service->get_entities( $term ); |
|
| 26 | - return new Default_Term_Data( $term, $entities ); |
|
| 27 | - } |
|
| 19 | + /** |
|
| 20 | + * @param $term \WP_Term |
|
| 21 | + * |
|
| 22 | + * @return Term_Data |
|
| 23 | + */ |
|
| 24 | + public function get_term_data( $term ) { |
|
| 25 | + $entities = $this->analysis_service->get_entities( $term ); |
|
| 26 | + return new Default_Term_Data( $term, $entities ); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | 29 | |
| 30 | 30 | } |
| 31 | 31 | \ No newline at end of file |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | /** |
| 13 | 13 | * @var Analysis_Service |
| 14 | 14 | */ |
| 15 | - public function __construct( $analysis_service ) { |
|
| 15 | + public function __construct($analysis_service) { |
|
| 16 | 16 | $this->analysis_service = $analysis_service; |
| 17 | 17 | } |
| 18 | 18 | |
@@ -21,9 +21,9 @@ discard block |
||
| 21 | 21 | * |
| 22 | 22 | * @return Term_Data |
| 23 | 23 | */ |
| 24 | - public function get_term_data( $term ) { |
|
| 25 | - $entities = $this->analysis_service->get_entities( $term ); |
|
| 26 | - return new Default_Term_Data( $term, $entities ); |
|
| 24 | + public function get_term_data($term) { |
|
| 25 | + $entities = $this->analysis_service->get_entities($term); |
|
| 26 | + return new Default_Term_Data($term, $entities); |
|
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | |
@@ -8,10 +8,10 @@ |
||
| 8 | 8 | |
| 9 | 9 | interface Term_Data { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * Should return an array which can be used by ui components. |
|
| 13 | - * @return array |
|
| 14 | - */ |
|
| 15 | - public function get_data(); |
|
| 11 | + /** |
|
| 12 | + * Should return an array which can be used by ui components. |
|
| 13 | + * @return array |
|
| 14 | + */ |
|
| 15 | + public function get_data(); |
|
| 16 | 16 | |
| 17 | 17 | } |
| 18 | 18 | \ No newline at end of file |