@@ -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 Wordlift\Vocabulary\Terms_Compat; |
| 10 | 9 | use WP_REST_Server; |
@@ -15,121 +15,121 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Tag_Rest_Endpoint { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var Term_Data_Factory |
|
| 20 | - */ |
|
| 21 | - private $term_data_factory; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Tag_Rest_Endpoint constructor. |
|
| 25 | - * |
|
| 26 | - * @param Term_Data_Factory $term_data_factory |
|
| 27 | - */ |
|
| 28 | - public function __construct( $term_data_factory ) { |
|
| 29 | - |
|
| 30 | - $this->term_data_factory = $term_data_factory; |
|
| 31 | - |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - |
|
| 35 | - public function register_routes() { |
|
| 36 | - $that = $this; |
|
| 37 | - add_action( 'rest_api_init', |
|
| 38 | - function () use ( $that ) { |
|
| 39 | - register_rest_route( |
|
| 40 | - Api_Config::REST_NAMESPACE, |
|
| 41 | - '/tags', |
|
| 42 | - array( |
|
| 43 | - 'methods' => WP_REST_Server::CREATABLE, |
|
| 44 | - 'callback' => array( $that, 'get_tags' ), |
|
| 45 | - //@todo : review the permission level |
|
| 46 | - 'permission_callback' => function () { |
|
| 47 | - return current_user_can( 'manage_options' ); |
|
| 48 | - }, |
|
| 49 | - 'args' => array( |
|
| 50 | - 'limit' => array( |
|
| 51 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 52 | - return is_numeric( $param ) && $param; |
|
| 53 | - }, |
|
| 54 | - 'required' => true, |
|
| 55 | - ), |
|
| 56 | - 'offset' => array( |
|
| 57 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 58 | - return is_numeric( $param ); |
|
| 59 | - }, |
|
| 60 | - 'required' => true, |
|
| 61 | - ), |
|
| 62 | - ), |
|
| 63 | - ) |
|
| 64 | - ); |
|
| 65 | - } ); |
|
| 66 | - |
|
| 67 | - |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - |
|
| 71 | - public function get_tags( $request ) { |
|
| 72 | - |
|
| 73 | - $data = $request->get_params(); |
|
| 74 | - $offset = (int) $data['offset']; |
|
| 75 | - $limit = (int) $data['limit']; |
|
| 76 | - $tags = $this->get_terms_from_db( $limit, $offset ); |
|
| 77 | - $term_data_list = array(); |
|
| 78 | - |
|
| 79 | - foreach ( $tags as $tag ) { |
|
| 80 | - |
|
| 81 | - if ( $this->is_tag_excluded_from_ui( $tag ) ) { |
|
| 82 | - continue; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param $tag \WP_Term |
|
| 87 | - */ |
|
| 88 | - $term_data_instance = $this->term_data_factory->get_term_data($tag); |
|
| 89 | - $term_data = $term_data_instance->get_data(); |
|
| 90 | - if ( $term_data['entities'] ) { |
|
| 91 | - $term_data_list[] = $term_data; |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - |
|
| 96 | - return $term_data_list; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * @param \WP_Term $tag |
|
| 101 | - * |
|
| 102 | - * @return bool |
|
| 103 | - */ |
|
| 104 | - private function is_tag_excluded_from_ui( $tag ) { |
|
| 105 | - return (int) get_term_meta( $tag->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) === 1; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @param $limit |
|
| 110 | - * @param $offset |
|
| 111 | - * |
|
| 112 | - * @return int|\WP_Error|\WP_Term[] |
|
| 113 | - */ |
|
| 114 | - public function get_terms_from_db( $limit, $offset ) { |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - return Terms_Compat::get_terms(Terms_Compat::get_public_taxonomies(), array( |
|
| 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 | - |
|
| 132 | - } |
|
| 18 | + /** |
|
| 19 | + * @var Term_Data_Factory |
|
| 20 | + */ |
|
| 21 | + private $term_data_factory; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Tag_Rest_Endpoint constructor. |
|
| 25 | + * |
|
| 26 | + * @param Term_Data_Factory $term_data_factory |
|
| 27 | + */ |
|
| 28 | + public function __construct( $term_data_factory ) { |
|
| 29 | + |
|
| 30 | + $this->term_data_factory = $term_data_factory; |
|
| 31 | + |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + |
|
| 35 | + public function register_routes() { |
|
| 36 | + $that = $this; |
|
| 37 | + add_action( 'rest_api_init', |
|
| 38 | + function () use ( $that ) { |
|
| 39 | + register_rest_route( |
|
| 40 | + Api_Config::REST_NAMESPACE, |
|
| 41 | + '/tags', |
|
| 42 | + array( |
|
| 43 | + 'methods' => WP_REST_Server::CREATABLE, |
|
| 44 | + 'callback' => array( $that, 'get_tags' ), |
|
| 45 | + //@todo : review the permission level |
|
| 46 | + 'permission_callback' => function () { |
|
| 47 | + return current_user_can( 'manage_options' ); |
|
| 48 | + }, |
|
| 49 | + 'args' => array( |
|
| 50 | + 'limit' => array( |
|
| 51 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
| 52 | + return is_numeric( $param ) && $param; |
|
| 53 | + }, |
|
| 54 | + 'required' => true, |
|
| 55 | + ), |
|
| 56 | + 'offset' => array( |
|
| 57 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
| 58 | + return is_numeric( $param ); |
|
| 59 | + }, |
|
| 60 | + 'required' => true, |
|
| 61 | + ), |
|
| 62 | + ), |
|
| 63 | + ) |
|
| 64 | + ); |
|
| 65 | + } ); |
|
| 66 | + |
|
| 67 | + |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + |
|
| 71 | + public function get_tags( $request ) { |
|
| 72 | + |
|
| 73 | + $data = $request->get_params(); |
|
| 74 | + $offset = (int) $data['offset']; |
|
| 75 | + $limit = (int) $data['limit']; |
|
| 76 | + $tags = $this->get_terms_from_db( $limit, $offset ); |
|
| 77 | + $term_data_list = array(); |
|
| 78 | + |
|
| 79 | + foreach ( $tags as $tag ) { |
|
| 80 | + |
|
| 81 | + if ( $this->is_tag_excluded_from_ui( $tag ) ) { |
|
| 82 | + continue; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param $tag \WP_Term |
|
| 87 | + */ |
|
| 88 | + $term_data_instance = $this->term_data_factory->get_term_data($tag); |
|
| 89 | + $term_data = $term_data_instance->get_data(); |
|
| 90 | + if ( $term_data['entities'] ) { |
|
| 91 | + $term_data_list[] = $term_data; |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + |
|
| 96 | + return $term_data_list; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * @param \WP_Term $tag |
|
| 101 | + * |
|
| 102 | + * @return bool |
|
| 103 | + */ |
|
| 104 | + private function is_tag_excluded_from_ui( $tag ) { |
|
| 105 | + return (int) get_term_meta( $tag->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) === 1; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @param $limit |
|
| 110 | + * @param $offset |
|
| 111 | + * |
|
| 112 | + * @return int|\WP_Error|\WP_Term[] |
|
| 113 | + */ |
|
| 114 | + public function get_terms_from_db( $limit, $offset ) { |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + return Terms_Compat::get_terms(Terms_Compat::get_public_taxonomies(), array( |
|
| 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 | + |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | 134 | |
| 135 | 135 | } |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | * |
| 26 | 26 | * @param Term_Data_Factory $term_data_factory |
| 27 | 27 | */ |
| 28 | - public function __construct( $term_data_factory ) { |
|
| 28 | + public function __construct($term_data_factory) { |
|
| 29 | 29 | |
| 30 | 30 | $this->term_data_factory = $term_data_factory; |
| 31 | 31 | |
@@ -34,28 +34,28 @@ discard block |
||
| 34 | 34 | |
| 35 | 35 | public function register_routes() { |
| 36 | 36 | $that = $this; |
| 37 | - add_action( 'rest_api_init', |
|
| 38 | - function () use ( $that ) { |
|
| 37 | + add_action('rest_api_init', |
|
| 38 | + function() use ($that) { |
|
| 39 | 39 | register_rest_route( |
| 40 | 40 | Api_Config::REST_NAMESPACE, |
| 41 | 41 | '/tags', |
| 42 | 42 | array( |
| 43 | 43 | 'methods' => WP_REST_Server::CREATABLE, |
| 44 | - 'callback' => array( $that, 'get_tags' ), |
|
| 44 | + 'callback' => array($that, 'get_tags'), |
|
| 45 | 45 | //@todo : review the permission level |
| 46 | - 'permission_callback' => function () { |
|
| 47 | - return current_user_can( 'manage_options' ); |
|
| 46 | + 'permission_callback' => function() { |
|
| 47 | + return current_user_can('manage_options'); |
|
| 48 | 48 | }, |
| 49 | 49 | 'args' => array( |
| 50 | 50 | 'limit' => array( |
| 51 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 52 | - return is_numeric( $param ) && $param; |
|
| 51 | + 'validate_callback' => function($param, $request, $key) { |
|
| 52 | + return is_numeric($param) && $param; |
|
| 53 | 53 | }, |
| 54 | 54 | 'required' => true, |
| 55 | 55 | ), |
| 56 | 56 | 'offset' => array( |
| 57 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 58 | - return is_numeric( $param ); |
|
| 57 | + 'validate_callback' => function($param, $request, $key) { |
|
| 58 | + return is_numeric($param); |
|
| 59 | 59 | }, |
| 60 | 60 | 'required' => true, |
| 61 | 61 | ), |
@@ -68,17 +68,17 @@ discard block |
||
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | |
| 71 | - public function get_tags( $request ) { |
|
| 71 | + public function get_tags($request) { |
|
| 72 | 72 | |
| 73 | 73 | $data = $request->get_params(); |
| 74 | 74 | $offset = (int) $data['offset']; |
| 75 | 75 | $limit = (int) $data['limit']; |
| 76 | - $tags = $this->get_terms_from_db( $limit, $offset ); |
|
| 76 | + $tags = $this->get_terms_from_db($limit, $offset); |
|
| 77 | 77 | $term_data_list = array(); |
| 78 | 78 | |
| 79 | - foreach ( $tags as $tag ) { |
|
| 79 | + foreach ($tags as $tag) { |
|
| 80 | 80 | |
| 81 | - if ( $this->is_tag_excluded_from_ui( $tag ) ) { |
|
| 81 | + if ($this->is_tag_excluded_from_ui($tag)) { |
|
| 82 | 82 | continue; |
| 83 | 83 | } |
| 84 | 84 | |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | */ |
| 88 | 88 | $term_data_instance = $this->term_data_factory->get_term_data($tag); |
| 89 | 89 | $term_data = $term_data_instance->get_data(); |
| 90 | - if ( $term_data['entities'] ) { |
|
| 90 | + if ($term_data['entities']) { |
|
| 91 | 91 | $term_data_list[] = $term_data; |
| 92 | 92 | } |
| 93 | 93 | } |
@@ -101,8 +101,8 @@ discard block |
||
| 101 | 101 | * |
| 102 | 102 | * @return bool |
| 103 | 103 | */ |
| 104 | - private function is_tag_excluded_from_ui( $tag ) { |
|
| 105 | - return (int) get_term_meta( $tag->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) === 1; |
|
| 104 | + private function is_tag_excluded_from_ui($tag) { |
|
| 105 | + return (int) get_term_meta($tag->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true) === 1; |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | /** |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | * |
| 112 | 112 | * @return int|\WP_Error|\WP_Term[] |
| 113 | 113 | */ |
| 114 | - public function get_terms_from_db( $limit, $offset ) { |
|
| 114 | + public function get_terms_from_db($limit, $offset) { |
|
| 115 | 115 | |
| 116 | 116 | |
| 117 | 117 | return Terms_Compat::get_terms(Terms_Compat::get_public_taxonomies(), array( |
@@ -98,7 +98,7 @@ |
||
| 98 | 98 | * Formats the entity url from https://foo.com/some/path to |
| 99 | 99 | * https/foo.com/some/path |
| 100 | 100 | * |
| 101 | - * @return bool|string |
|
| 101 | + * @return false|string |
|
| 102 | 102 | */ |
| 103 | 103 | public static function format_entity_url( $entity_url ) { |
| 104 | 104 | $result = parse_url( $entity_url ); |
@@ -13,161 +13,161 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Analysis_Service { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * @var Default_Api_Service |
|
| 18 | - */ |
|
| 19 | - private $api_service; |
|
| 20 | - /** |
|
| 21 | - * @var Cache |
|
| 22 | - */ |
|
| 23 | - private $cache_service; |
|
| 24 | - /** |
|
| 25 | - * @var \Wordlift_Log_Service |
|
| 26 | - */ |
|
| 27 | - private $log; |
|
| 16 | + /** |
|
| 17 | + * @var Default_Api_Service |
|
| 18 | + */ |
|
| 19 | + private $api_service; |
|
| 20 | + /** |
|
| 21 | + * @var Cache |
|
| 22 | + */ |
|
| 23 | + private $cache_service; |
|
| 24 | + /** |
|
| 25 | + * @var \Wordlift_Log_Service |
|
| 26 | + */ |
|
| 27 | + private $log; |
|
| 28 | 28 | |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Tag_Rest_Endpoint constructor. |
|
| 32 | - * |
|
| 33 | - * @param Default_Api_Service $api_service |
|
| 34 | - * @param Cache $cache_service |
|
| 35 | - */ |
|
| 36 | - public function __construct( $api_service, $cache_service ) { |
|
| 30 | + /** |
|
| 31 | + * Tag_Rest_Endpoint constructor. |
|
| 32 | + * |
|
| 33 | + * @param Default_Api_Service $api_service |
|
| 34 | + * @param Cache $cache_service |
|
| 35 | + */ |
|
| 36 | + public function __construct( $api_service, $cache_service ) { |
|
| 37 | 37 | |
| 38 | - $this->api_service = $api_service; |
|
| 38 | + $this->api_service = $api_service; |
|
| 39 | 39 | |
| 40 | - $this->cache_service = $cache_service; |
|
| 40 | + $this->cache_service = $cache_service; |
|
| 41 | 41 | |
| 42 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 43 | - |
|
| 44 | - } |
|
| 42 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 43 | + |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Check if entities are in cache, if not return the results from |
|
| 49 | - * cache service. |
|
| 50 | - * |
|
| 51 | - * @param $tag \WP_Term |
|
| 52 | - */ |
|
| 53 | - public function get_entities( $tag ) { |
|
| 47 | + /** |
|
| 48 | + * Check if entities are in cache, if not return the results from |
|
| 49 | + * cache service. |
|
| 50 | + * |
|
| 51 | + * @param $tag \WP_Term |
|
| 52 | + */ |
|
| 53 | + public function get_entities( $tag ) { |
|
| 54 | 54 | |
| 55 | - $cache_key = $tag->term_id; |
|
| 56 | - $cache_result = $this->cache_service->get( $cache_key ); |
|
| 57 | - if ( $cache_result !== false ) { |
|
| 58 | - return $cache_result; |
|
| 59 | - } |
|
| 55 | + $cache_key = $tag->term_id; |
|
| 56 | + $cache_result = $this->cache_service->get( $cache_key ); |
|
| 57 | + if ( $cache_result !== false ) { |
|
| 58 | + return $cache_result; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - // send the request. |
|
| 62 | - $response = $this->api_service->request( |
|
| 63 | - 'POST', |
|
| 64 | - "/analysis/single", |
|
| 65 | - array( 'Content-Type' => 'application/json' ), |
|
| 66 | - wp_json_encode( array( |
|
| 67 | - "content" => $tag->name, |
|
| 68 | - "contentType" => "text/plain", |
|
| 69 | - "version" => "1.0.0", |
|
| 70 | - "contentLanguage" => "en", |
|
| 71 | - "scope" => "all", |
|
| 72 | - ) ) |
|
| 73 | - ); |
|
| 61 | + // send the request. |
|
| 62 | + $response = $this->api_service->request( |
|
| 63 | + 'POST', |
|
| 64 | + "/analysis/single", |
|
| 65 | + array( 'Content-Type' => 'application/json' ), |
|
| 66 | + wp_json_encode( array( |
|
| 67 | + "content" => $tag->name, |
|
| 68 | + "contentType" => "text/plain", |
|
| 69 | + "version" => "1.0.0", |
|
| 70 | + "contentLanguage" => "en", |
|
| 71 | + "scope" => "all", |
|
| 72 | + ) ) |
|
| 73 | + ); |
|
| 74 | 74 | |
| 75 | 75 | |
| 76 | - if ( ! $response->is_success() ) { |
|
| 77 | - return false; |
|
| 78 | - } |
|
| 76 | + if ( ! $response->is_success() ) { |
|
| 77 | + return false; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - $response = json_decode( $response->get_body(), true ); |
|
| 80 | + $response = json_decode( $response->get_body(), true ); |
|
| 81 | 81 | |
| 82 | - if ( ! array_key_exists( 'entities', $response ) ) { |
|
| 83 | - return false; |
|
| 84 | - } |
|
| 82 | + if ( ! array_key_exists( 'entities', $response ) ) { |
|
| 83 | + return false; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | 86 | |
| 87 | - $entities = $this->get_meta_for_entities( $response['entities'] ); |
|
| 87 | + $entities = $this->get_meta_for_entities( $response['entities'] ); |
|
| 88 | 88 | |
| 89 | - $this->cache_service->put( $cache_key, $entities ); |
|
| 89 | + $this->cache_service->put( $cache_key, $entities ); |
|
| 90 | 90 | |
| 91 | - return $entities; |
|
| 91 | + return $entities; |
|
| 92 | 92 | |
| 93 | - } |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * @param $entity_url string |
|
| 98 | - * Formats the entity url from https://foo.com/some/path to |
|
| 99 | - * https/foo.com/some/path |
|
| 100 | - * |
|
| 101 | - * @return bool|string |
|
| 102 | - */ |
|
| 103 | - public static function format_entity_url( $entity_url ) { |
|
| 104 | - $result = parse_url( $entity_url ); |
|
| 105 | - if ( ! $result ) { |
|
| 106 | - return false; |
|
| 107 | - } |
|
| 108 | - if ( ! array_key_exists( 'scheme', $result ) |
|
| 109 | - || ! array_key_exists( 'host', $result ) |
|
| 110 | - || ! array_key_exists( 'path', $result ) ) { |
|
| 111 | - return false; |
|
| 112 | - } |
|
| 96 | + /** |
|
| 97 | + * @param $entity_url string |
|
| 98 | + * Formats the entity url from https://foo.com/some/path to |
|
| 99 | + * https/foo.com/some/path |
|
| 100 | + * |
|
| 101 | + * @return bool|string |
|
| 102 | + */ |
|
| 103 | + public static function format_entity_url( $entity_url ) { |
|
| 104 | + $result = parse_url( $entity_url ); |
|
| 105 | + if ( ! $result ) { |
|
| 106 | + return false; |
|
| 107 | + } |
|
| 108 | + if ( ! array_key_exists( 'scheme', $result ) |
|
| 109 | + || ! array_key_exists( 'host', $result ) |
|
| 110 | + || ! array_key_exists( 'path', $result ) ) { |
|
| 111 | + return false; |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - return $result['scheme'] . "/" . $result['host'] . $result['path']; |
|
| 115 | - } |
|
| 114 | + return $result['scheme'] . "/" . $result['host'] . $result['path']; |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - private function get_meta( $entity_url ) { |
|
| 117 | + private function get_meta( $entity_url ) { |
|
| 118 | 118 | |
| 119 | 119 | |
| 120 | - $cache_results = $this->cache_service->get( $entity_url ); |
|
| 120 | + $cache_results = $this->cache_service->get( $entity_url ); |
|
| 121 | 121 | |
| 122 | - if ( $cache_results !== false ) { |
|
| 123 | - return $cache_results; |
|
| 124 | - } |
|
| 122 | + if ( $cache_results !== false ) { |
|
| 123 | + return $cache_results; |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - $formatted_url = self::format_entity_url( $entity_url ); |
|
| 126 | + $formatted_url = self::format_entity_url( $entity_url ); |
|
| 127 | 127 | |
| 128 | - if ( ! $formatted_url ) { |
|
| 129 | - return array(); |
|
| 130 | - } |
|
| 128 | + if ( ! $formatted_url ) { |
|
| 129 | + return array(); |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - $meta_url = 'https://api.wordlift.io/id/' . $formatted_url; |
|
| 132 | + $meta_url = 'https://api.wordlift.io/id/' . $formatted_url; |
|
| 133 | 133 | |
| 134 | - $response = wp_remote_get( $meta_url ); |
|
| 134 | + $response = wp_remote_get( $meta_url ); |
|
| 135 | 135 | |
| 136 | - $this->log->debug( "Requesting entity data for url :" . $meta_url ); |
|
| 137 | - $this->log->debug( "Got entity meta data as : " ); |
|
| 138 | - $this->log->debug( var_export( $response, true ) ); |
|
| 139 | - if ( ! is_wp_error( $response ) ) { |
|
| 140 | - $meta = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 141 | - $this->log->debug( "Saved entity data to meta :" ); |
|
| 142 | - $this->log->debug( var_export( $meta, true ) ); |
|
| 143 | - $this->cache_service->put( $entity_url, $meta ); |
|
| 136 | + $this->log->debug( "Requesting entity data for url :" . $meta_url ); |
|
| 137 | + $this->log->debug( "Got entity meta data as : " ); |
|
| 138 | + $this->log->debug( var_export( $response, true ) ); |
|
| 139 | + if ( ! is_wp_error( $response ) ) { |
|
| 140 | + $meta = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 141 | + $this->log->debug( "Saved entity data to meta :" ); |
|
| 142 | + $this->log->debug( var_export( $meta, true ) ); |
|
| 143 | + $this->cache_service->put( $entity_url, $meta ); |
|
| 144 | 144 | |
| 145 | - return $meta; |
|
| 146 | - } |
|
| 145 | + return $meta; |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | 148 | |
| 149 | - return array(); |
|
| 149 | + return array(); |
|
| 150 | 150 | |
| 151 | - } |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - private function get_meta_for_entities( $entities ) { |
|
| 153 | + private function get_meta_for_entities( $entities ) { |
|
| 154 | 154 | |
| 155 | - $filtered_entities = array(); |
|
| 156 | - foreach ( $entities as $entity ) { |
|
| 157 | - $entity['meta'] = array(); |
|
| 158 | - $meta = $this->get_meta( $entity['entityId'] ); |
|
| 159 | - $meta = Default_Entity_List::compact_jsonld($meta); |
|
| 160 | - if ( $meta ) { |
|
| 161 | - $entity['meta'] = $meta; |
|
| 162 | - } |
|
| 163 | - $filtered_entities[] = $entity; |
|
| 164 | - } |
|
| 165 | - $this->log->debug("Returning filtered entities as"); |
|
| 166 | - $this->log->debug(var_export($filtered_entities, true)); |
|
| 155 | + $filtered_entities = array(); |
|
| 156 | + foreach ( $entities as $entity ) { |
|
| 157 | + $entity['meta'] = array(); |
|
| 158 | + $meta = $this->get_meta( $entity['entityId'] ); |
|
| 159 | + $meta = Default_Entity_List::compact_jsonld($meta); |
|
| 160 | + if ( $meta ) { |
|
| 161 | + $entity['meta'] = $meta; |
|
| 162 | + } |
|
| 163 | + $filtered_entities[] = $entity; |
|
| 164 | + } |
|
| 165 | + $this->log->debug("Returning filtered entities as"); |
|
| 166 | + $this->log->debug(var_export($filtered_entities, true)); |
|
| 167 | 167 | |
| 168 | - return $filtered_entities; |
|
| 168 | + return $filtered_entities; |
|
| 169 | 169 | |
| 170 | - } |
|
| 170 | + } |
|
| 171 | 171 | |
| 172 | 172 | |
| 173 | 173 | } |
@@ -33,13 +33,13 @@ discard block |
||
| 33 | 33 | * @param Default_Api_Service $api_service |
| 34 | 34 | * @param Cache $cache_service |
| 35 | 35 | */ |
| 36 | - public function __construct( $api_service, $cache_service ) { |
|
| 36 | + public function __construct($api_service, $cache_service) { |
|
| 37 | 37 | |
| 38 | 38 | $this->api_service = $api_service; |
| 39 | 39 | |
| 40 | 40 | $this->cache_service = $cache_service; |
| 41 | 41 | |
| 42 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 42 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
| 43 | 43 | |
| 44 | 44 | } |
| 45 | 45 | |
@@ -50,11 +50,11 @@ discard block |
||
| 50 | 50 | * |
| 51 | 51 | * @param $tag \WP_Term |
| 52 | 52 | */ |
| 53 | - public function get_entities( $tag ) { |
|
| 53 | + public function get_entities($tag) { |
|
| 54 | 54 | |
| 55 | 55 | $cache_key = $tag->term_id; |
| 56 | - $cache_result = $this->cache_service->get( $cache_key ); |
|
| 57 | - if ( $cache_result !== false ) { |
|
| 56 | + $cache_result = $this->cache_service->get($cache_key); |
|
| 57 | + if ($cache_result !== false) { |
|
| 58 | 58 | return $cache_result; |
| 59 | 59 | } |
| 60 | 60 | |
@@ -62,31 +62,31 @@ discard block |
||
| 62 | 62 | $response = $this->api_service->request( |
| 63 | 63 | 'POST', |
| 64 | 64 | "/analysis/single", |
| 65 | - array( 'Content-Type' => 'application/json' ), |
|
| 66 | - wp_json_encode( array( |
|
| 65 | + array('Content-Type' => 'application/json'), |
|
| 66 | + wp_json_encode(array( |
|
| 67 | 67 | "content" => $tag->name, |
| 68 | 68 | "contentType" => "text/plain", |
| 69 | 69 | "version" => "1.0.0", |
| 70 | 70 | "contentLanguage" => "en", |
| 71 | 71 | "scope" => "all", |
| 72 | - ) ) |
|
| 72 | + )) |
|
| 73 | 73 | ); |
| 74 | 74 | |
| 75 | 75 | |
| 76 | - if ( ! $response->is_success() ) { |
|
| 76 | + if ( ! $response->is_success()) { |
|
| 77 | 77 | return false; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - $response = json_decode( $response->get_body(), true ); |
|
| 80 | + $response = json_decode($response->get_body(), true); |
|
| 81 | 81 | |
| 82 | - if ( ! array_key_exists( 'entities', $response ) ) { |
|
| 82 | + if ( ! array_key_exists('entities', $response)) { |
|
| 83 | 83 | return false; |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | |
| 87 | - $entities = $this->get_meta_for_entities( $response['entities'] ); |
|
| 87 | + $entities = $this->get_meta_for_entities($response['entities']); |
|
| 88 | 88 | |
| 89 | - $this->cache_service->put( $cache_key, $entities ); |
|
| 89 | + $this->cache_service->put($cache_key, $entities); |
|
| 90 | 90 | |
| 91 | 91 | return $entities; |
| 92 | 92 | |
@@ -100,47 +100,47 @@ discard block |
||
| 100 | 100 | * |
| 101 | 101 | * @return bool|string |
| 102 | 102 | */ |
| 103 | - public static function format_entity_url( $entity_url ) { |
|
| 104 | - $result = parse_url( $entity_url ); |
|
| 105 | - if ( ! $result ) { |
|
| 103 | + public static function format_entity_url($entity_url) { |
|
| 104 | + $result = parse_url($entity_url); |
|
| 105 | + if ( ! $result) { |
|
| 106 | 106 | return false; |
| 107 | 107 | } |
| 108 | - if ( ! array_key_exists( 'scheme', $result ) |
|
| 109 | - || ! array_key_exists( 'host', $result ) |
|
| 110 | - || ! array_key_exists( 'path', $result ) ) { |
|
| 108 | + if ( ! array_key_exists('scheme', $result) |
|
| 109 | + || ! array_key_exists('host', $result) |
|
| 110 | + || ! array_key_exists('path', $result)) { |
|
| 111 | 111 | return false; |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | - return $result['scheme'] . "/" . $result['host'] . $result['path']; |
|
| 114 | + return $result['scheme']."/".$result['host'].$result['path']; |
|
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - private function get_meta( $entity_url ) { |
|
| 117 | + private function get_meta($entity_url) { |
|
| 118 | 118 | |
| 119 | 119 | |
| 120 | - $cache_results = $this->cache_service->get( $entity_url ); |
|
| 120 | + $cache_results = $this->cache_service->get($entity_url); |
|
| 121 | 121 | |
| 122 | - if ( $cache_results !== false ) { |
|
| 122 | + if ($cache_results !== false) { |
|
| 123 | 123 | return $cache_results; |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | - $formatted_url = self::format_entity_url( $entity_url ); |
|
| 126 | + $formatted_url = self::format_entity_url($entity_url); |
|
| 127 | 127 | |
| 128 | - if ( ! $formatted_url ) { |
|
| 128 | + if ( ! $formatted_url) { |
|
| 129 | 129 | return array(); |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | - $meta_url = 'https://api.wordlift.io/id/' . $formatted_url; |
|
| 132 | + $meta_url = 'https://api.wordlift.io/id/'.$formatted_url; |
|
| 133 | 133 | |
| 134 | - $response = wp_remote_get( $meta_url ); |
|
| 134 | + $response = wp_remote_get($meta_url); |
|
| 135 | 135 | |
| 136 | - $this->log->debug( "Requesting entity data for url :" . $meta_url ); |
|
| 137 | - $this->log->debug( "Got entity meta data as : " ); |
|
| 138 | - $this->log->debug( var_export( $response, true ) ); |
|
| 139 | - if ( ! is_wp_error( $response ) ) { |
|
| 140 | - $meta = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 141 | - $this->log->debug( "Saved entity data to meta :" ); |
|
| 142 | - $this->log->debug( var_export( $meta, true ) ); |
|
| 143 | - $this->cache_service->put( $entity_url, $meta ); |
|
| 136 | + $this->log->debug("Requesting entity data for url :".$meta_url); |
|
| 137 | + $this->log->debug("Got entity meta data as : "); |
|
| 138 | + $this->log->debug(var_export($response, true)); |
|
| 139 | + if ( ! is_wp_error($response)) { |
|
| 140 | + $meta = json_decode(wp_remote_retrieve_body($response), true); |
|
| 141 | + $this->log->debug("Saved entity data to meta :"); |
|
| 142 | + $this->log->debug(var_export($meta, true)); |
|
| 143 | + $this->cache_service->put($entity_url, $meta); |
|
| 144 | 144 | |
| 145 | 145 | return $meta; |
| 146 | 146 | } |
@@ -150,14 +150,14 @@ discard block |
||
| 150 | 150 | |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | - private function get_meta_for_entities( $entities ) { |
|
| 153 | + private function get_meta_for_entities($entities) { |
|
| 154 | 154 | |
| 155 | 155 | $filtered_entities = array(); |
| 156 | - foreach ( $entities as $entity ) { |
|
| 156 | + foreach ($entities as $entity) { |
|
| 157 | 157 | $entity['meta'] = array(); |
| 158 | - $meta = $this->get_meta( $entity['entityId'] ); |
|
| 158 | + $meta = $this->get_meta($entity['entityId']); |
|
| 159 | 159 | $meta = Default_Entity_List::compact_jsonld($meta); |
| 160 | - if ( $meta ) { |
|
| 160 | + if ($meta) { |
|
| 161 | 161 | $entity['meta'] = $meta; |
| 162 | 162 | } |
| 163 | 163 | $filtered_entities[] = $entity; |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | * Default_Entity constructor. |
| 21 | 21 | * |
| 22 | 22 | * @param $term_id int |
| 23 | - * @param $legacy_entity Legacy_Entity_List |
|
| 23 | + * @param Legacy_Entity_List $legacy_entity Legacy_Entity_List |
|
| 24 | 24 | */ |
| 25 | 25 | public function __construct( $term_id, $legacy_entity ) { |
| 26 | 26 | parent::__construct( $term_id ); |
@@ -46,6 +46,7 @@ discard block |
||
| 46 | 46 | * Check if the key exists and value is array. |
| 47 | 47 | * |
| 48 | 48 | * @param $entity_data |
| 49 | + * @param string $key |
|
| 49 | 50 | * |
| 50 | 51 | * @return bool |
| 51 | 52 | */ |
@@ -9,162 +9,162 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Default_Entity_List extends Entity_List { |
| 11 | 11 | |
| 12 | - const META_KEY = '_wl_vocabulary_entity_match_for_term'; |
|
| 13 | - |
|
| 14 | - /** |
|
| 15 | - * @var Legacy_Entity_List |
|
| 16 | - */ |
|
| 17 | - private $legacy_entity; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Default_Entity constructor. |
|
| 21 | - * |
|
| 22 | - * @param $term_id int |
|
| 23 | - * @param $legacy_entity Legacy_Entity_List |
|
| 24 | - */ |
|
| 25 | - public function __construct( $term_id, $legacy_entity ) { |
|
| 26 | - parent::__construct( $term_id ); |
|
| 27 | - $this->legacy_entity = $legacy_entity; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @param $values |
|
| 32 | - * |
|
| 33 | - * @return array |
|
| 34 | - */ |
|
| 35 | - private static function extract_jsonld_values( $values ) { |
|
| 36 | - return array_map( function ( $value ) { |
|
| 37 | - if ( ! is_array($value) || ! array_key_exists( '@value', $value ) ) { |
|
| 38 | - return $value; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - return $value['@value']; |
|
| 42 | - }, $values ); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Check if the key exists and value is array. |
|
| 47 | - * |
|
| 48 | - * @param $entity_data |
|
| 49 | - * |
|
| 50 | - * @return bool |
|
| 51 | - */ |
|
| 52 | - private static function is_value_array( $key, $entity_data ) { |
|
| 53 | - return array_key_exists( $key, $entity_data ) && is_array( $entity_data[ $key ] ); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - public function get_jsonld_data() { |
|
| 57 | - $default_data = get_term_meta( $this->term_id, self::META_KEY ); |
|
| 58 | - if ( is_array( $default_data ) && $default_data ) { |
|
| 59 | - return $default_data; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - // Use legacy entity if the data doesnt exist on that key. |
|
| 63 | - return $this->legacy_entity->get_jsonld_data(); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - public function save_jsonld_data( $entity_data ) { |
|
| 67 | - |
|
| 68 | - $entity_id = $entity_data['@id']; |
|
| 69 | - |
|
| 70 | - if ( $entity_id ) { |
|
| 71 | - $entity_data['sameAs'] = array_merge( $entity_data['sameAs'], array( $entity_id ) ); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - $entity_list = get_term_meta( $this->term_id, self::META_KEY ); |
|
| 75 | - |
|
| 76 | - $entity_list[] = $this->compact_jsonld( $this->filter_entity_data( $entity_data ) ); |
|
| 77 | - |
|
| 78 | - $this->clear_and_save_list( $entity_list ); |
|
| 79 | - |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - public function clear_data() { |
|
| 83 | - delete_term_meta( $this->term_id, self::META_KEY ); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - |
|
| 87 | - public function remove_entity_by_id( $entity_id ) { |
|
| 88 | - $entity_list = get_term_meta( $this->term_id, self::META_KEY ); |
|
| 89 | - foreach ( $entity_list as $key => $entity ) { |
|
| 90 | - $same_as = $entity['sameAs']; |
|
| 91 | - if ( in_array( $entity_id, $same_as ) ) { |
|
| 92 | - // since entity ids are unique, we break after finding the first instance. |
|
| 93 | - unset( $entity_list[ $key ] ); |
|
| 94 | - break; |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - $this->clear_and_save_list( $entity_list ); |
|
| 98 | - |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @param $entity_list |
|
| 103 | - */ |
|
| 104 | - private function save_entity_list( $entity_list ) { |
|
| 105 | - foreach ( $entity_list as $single_entity ) { |
|
| 106 | - add_term_meta( $this->term_id, self::META_KEY, $single_entity ); |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @param $entity_list |
|
| 112 | - */ |
|
| 113 | - private function clear_and_save_list( $entity_list ) { |
|
| 114 | - // Clear all data and add the new one. |
|
| 115 | - $this->clear_data(); |
|
| 116 | - $this->save_entity_list( $entity_list ); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * For now support only these properties. |
|
| 122 | - * |
|
| 123 | - * @param $entity_data |
|
| 124 | - * |
|
| 125 | - * @return array |
|
| 126 | - */ |
|
| 127 | - private function filter_entity_data( $entity_data ) { |
|
| 128 | - $allowed_keys = array( '@id', 'description', 'sameAs', '@type', 'name' ); |
|
| 129 | - $data = array(); |
|
| 130 | - foreach ( $entity_data as $key => $value ) { |
|
| 131 | - if ( in_array( $key, $allowed_keys ) ) { |
|
| 132 | - $data[ $key ] = $value; |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - return $data; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - |
|
| 140 | - public static function compact_jsonld( $entity_data ) { |
|
| 141 | - |
|
| 142 | - if ( self::is_value_array( '@type', $entity_data ) ) { |
|
| 143 | - $entity_data['@type'] = array_map( function ( $type ) { |
|
| 144 | - return str_replace( "http://schema.org/", "", $type ); |
|
| 145 | - }, $entity_data['@type'] ); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - if ( self::is_value_array( 'description', $entity_data ) ) { |
|
| 149 | - $entity_data['description'] = self::extract_jsonld_values( $entity_data['description'] ); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - |
|
| 153 | - if ( self::is_value_array( 'name', $entity_data ) ) { |
|
| 154 | - $entity_data['name'] = self::extract_jsonld_values( $entity_data['name'] ); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - |
|
| 158 | - if ( self::is_value_array( 'sameAs', $entity_data ) ) { |
|
| 159 | - $entity_data['sameAs'] = array_map( function ( $sameas ) { |
|
| 160 | - if ( ! is_array($sameas) || ! array_key_exists( '@id', $sameas ) ) { |
|
| 161 | - return $sameas; |
|
| 162 | - } |
|
| 163 | - return $sameas['@id']; |
|
| 164 | - }, $entity_data['sameAs'] ); |
|
| 165 | - } |
|
| 166 | - |
|
| 12 | + const META_KEY = '_wl_vocabulary_entity_match_for_term'; |
|
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * @var Legacy_Entity_List |
|
| 16 | + */ |
|
| 17 | + private $legacy_entity; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Default_Entity constructor. |
|
| 21 | + * |
|
| 22 | + * @param $term_id int |
|
| 23 | + * @param $legacy_entity Legacy_Entity_List |
|
| 24 | + */ |
|
| 25 | + public function __construct( $term_id, $legacy_entity ) { |
|
| 26 | + parent::__construct( $term_id ); |
|
| 27 | + $this->legacy_entity = $legacy_entity; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @param $values |
|
| 32 | + * |
|
| 33 | + * @return array |
|
| 34 | + */ |
|
| 35 | + private static function extract_jsonld_values( $values ) { |
|
| 36 | + return array_map( function ( $value ) { |
|
| 37 | + if ( ! is_array($value) || ! array_key_exists( '@value', $value ) ) { |
|
| 38 | + return $value; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + return $value['@value']; |
|
| 42 | + }, $values ); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Check if the key exists and value is array. |
|
| 47 | + * |
|
| 48 | + * @param $entity_data |
|
| 49 | + * |
|
| 50 | + * @return bool |
|
| 51 | + */ |
|
| 52 | + private static function is_value_array( $key, $entity_data ) { |
|
| 53 | + return array_key_exists( $key, $entity_data ) && is_array( $entity_data[ $key ] ); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + public function get_jsonld_data() { |
|
| 57 | + $default_data = get_term_meta( $this->term_id, self::META_KEY ); |
|
| 58 | + if ( is_array( $default_data ) && $default_data ) { |
|
| 59 | + return $default_data; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + // Use legacy entity if the data doesnt exist on that key. |
|
| 63 | + return $this->legacy_entity->get_jsonld_data(); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + public function save_jsonld_data( $entity_data ) { |
|
| 67 | + |
|
| 68 | + $entity_id = $entity_data['@id']; |
|
| 69 | + |
|
| 70 | + if ( $entity_id ) { |
|
| 71 | + $entity_data['sameAs'] = array_merge( $entity_data['sameAs'], array( $entity_id ) ); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + $entity_list = get_term_meta( $this->term_id, self::META_KEY ); |
|
| 75 | + |
|
| 76 | + $entity_list[] = $this->compact_jsonld( $this->filter_entity_data( $entity_data ) ); |
|
| 77 | + |
|
| 78 | + $this->clear_and_save_list( $entity_list ); |
|
| 79 | + |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + public function clear_data() { |
|
| 83 | + delete_term_meta( $this->term_id, self::META_KEY ); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + |
|
| 87 | + public function remove_entity_by_id( $entity_id ) { |
|
| 88 | + $entity_list = get_term_meta( $this->term_id, self::META_KEY ); |
|
| 89 | + foreach ( $entity_list as $key => $entity ) { |
|
| 90 | + $same_as = $entity['sameAs']; |
|
| 91 | + if ( in_array( $entity_id, $same_as ) ) { |
|
| 92 | + // since entity ids are unique, we break after finding the first instance. |
|
| 93 | + unset( $entity_list[ $key ] ); |
|
| 94 | + break; |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + $this->clear_and_save_list( $entity_list ); |
|
| 98 | + |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @param $entity_list |
|
| 103 | + */ |
|
| 104 | + private function save_entity_list( $entity_list ) { |
|
| 105 | + foreach ( $entity_list as $single_entity ) { |
|
| 106 | + add_term_meta( $this->term_id, self::META_KEY, $single_entity ); |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @param $entity_list |
|
| 112 | + */ |
|
| 113 | + private function clear_and_save_list( $entity_list ) { |
|
| 114 | + // Clear all data and add the new one. |
|
| 115 | + $this->clear_data(); |
|
| 116 | + $this->save_entity_list( $entity_list ); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * For now support only these properties. |
|
| 122 | + * |
|
| 123 | + * @param $entity_data |
|
| 124 | + * |
|
| 125 | + * @return array |
|
| 126 | + */ |
|
| 127 | + private function filter_entity_data( $entity_data ) { |
|
| 128 | + $allowed_keys = array( '@id', 'description', 'sameAs', '@type', 'name' ); |
|
| 129 | + $data = array(); |
|
| 130 | + foreach ( $entity_data as $key => $value ) { |
|
| 131 | + if ( in_array( $key, $allowed_keys ) ) { |
|
| 132 | + $data[ $key ] = $value; |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + return $data; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + |
|
| 140 | + public static function compact_jsonld( $entity_data ) { |
|
| 141 | + |
|
| 142 | + if ( self::is_value_array( '@type', $entity_data ) ) { |
|
| 143 | + $entity_data['@type'] = array_map( function ( $type ) { |
|
| 144 | + return str_replace( "http://schema.org/", "", $type ); |
|
| 145 | + }, $entity_data['@type'] ); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + if ( self::is_value_array( 'description', $entity_data ) ) { |
|
| 149 | + $entity_data['description'] = self::extract_jsonld_values( $entity_data['description'] ); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + |
|
| 153 | + if ( self::is_value_array( 'name', $entity_data ) ) { |
|
| 154 | + $entity_data['name'] = self::extract_jsonld_values( $entity_data['name'] ); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + |
|
| 158 | + if ( self::is_value_array( 'sameAs', $entity_data ) ) { |
|
| 159 | + $entity_data['sameAs'] = array_map( function ( $sameas ) { |
|
| 160 | + if ( ! is_array($sameas) || ! array_key_exists( '@id', $sameas ) ) { |
|
| 161 | + return $sameas; |
|
| 162 | + } |
|
| 163 | + return $sameas['@id']; |
|
| 164 | + }, $entity_data['sameAs'] ); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | 167 | |
| 168 | - return $entity_data; |
|
| 169 | - } |
|
| 168 | + return $entity_data; |
|
| 169 | + } |
|
| 170 | 170 | } |
| 171 | 171 | \ No newline at end of file |
@@ -22,8 +22,8 @@ discard block |
||
| 22 | 22 | * @param $term_id int |
| 23 | 23 | * @param $legacy_entity Legacy_Entity_List |
| 24 | 24 | */ |
| 25 | - public function __construct( $term_id, $legacy_entity ) { |
|
| 26 | - parent::__construct( $term_id ); |
|
| 25 | + public function __construct($term_id, $legacy_entity) { |
|
| 26 | + parent::__construct($term_id); |
|
| 27 | 27 | $this->legacy_entity = $legacy_entity; |
| 28 | 28 | } |
| 29 | 29 | |
@@ -32,14 +32,14 @@ discard block |
||
| 32 | 32 | * |
| 33 | 33 | * @return array |
| 34 | 34 | */ |
| 35 | - private static function extract_jsonld_values( $values ) { |
|
| 36 | - return array_map( function ( $value ) { |
|
| 37 | - if ( ! is_array($value) || ! array_key_exists( '@value', $value ) ) { |
|
| 35 | + private static function extract_jsonld_values($values) { |
|
| 36 | + return array_map(function($value) { |
|
| 37 | + if ( ! is_array($value) || ! array_key_exists('@value', $value)) { |
|
| 38 | 38 | return $value; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | return $value['@value']; |
| 42 | - }, $values ); |
|
| 42 | + }, $values); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | /** |
@@ -49,13 +49,13 @@ discard block |
||
| 49 | 49 | * |
| 50 | 50 | * @return bool |
| 51 | 51 | */ |
| 52 | - private static function is_value_array( $key, $entity_data ) { |
|
| 53 | - return array_key_exists( $key, $entity_data ) && is_array( $entity_data[ $key ] ); |
|
| 52 | + private static function is_value_array($key, $entity_data) { |
|
| 53 | + return array_key_exists($key, $entity_data) && is_array($entity_data[$key]); |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | public function get_jsonld_data() { |
| 57 | - $default_data = get_term_meta( $this->term_id, self::META_KEY ); |
|
| 58 | - if ( is_array( $default_data ) && $default_data ) { |
|
| 57 | + $default_data = get_term_meta($this->term_id, self::META_KEY); |
|
| 58 | + if (is_array($default_data) && $default_data) { |
|
| 59 | 59 | return $default_data; |
| 60 | 60 | } |
| 61 | 61 | |
@@ -63,57 +63,57 @@ discard block |
||
| 63 | 63 | return $this->legacy_entity->get_jsonld_data(); |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - public function save_jsonld_data( $entity_data ) { |
|
| 66 | + public function save_jsonld_data($entity_data) { |
|
| 67 | 67 | |
| 68 | 68 | $entity_id = $entity_data['@id']; |
| 69 | 69 | |
| 70 | - if ( $entity_id ) { |
|
| 71 | - $entity_data['sameAs'] = array_merge( $entity_data['sameAs'], array( $entity_id ) ); |
|
| 70 | + if ($entity_id) { |
|
| 71 | + $entity_data['sameAs'] = array_merge($entity_data['sameAs'], array($entity_id)); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - $entity_list = get_term_meta( $this->term_id, self::META_KEY ); |
|
| 74 | + $entity_list = get_term_meta($this->term_id, self::META_KEY); |
|
| 75 | 75 | |
| 76 | - $entity_list[] = $this->compact_jsonld( $this->filter_entity_data( $entity_data ) ); |
|
| 76 | + $entity_list[] = $this->compact_jsonld($this->filter_entity_data($entity_data)); |
|
| 77 | 77 | |
| 78 | - $this->clear_and_save_list( $entity_list ); |
|
| 78 | + $this->clear_and_save_list($entity_list); |
|
| 79 | 79 | |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | public function clear_data() { |
| 83 | - delete_term_meta( $this->term_id, self::META_KEY ); |
|
| 83 | + delete_term_meta($this->term_id, self::META_KEY); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | |
| 87 | - public function remove_entity_by_id( $entity_id ) { |
|
| 88 | - $entity_list = get_term_meta( $this->term_id, self::META_KEY ); |
|
| 89 | - foreach ( $entity_list as $key => $entity ) { |
|
| 87 | + public function remove_entity_by_id($entity_id) { |
|
| 88 | + $entity_list = get_term_meta($this->term_id, self::META_KEY); |
|
| 89 | + foreach ($entity_list as $key => $entity) { |
|
| 90 | 90 | $same_as = $entity['sameAs']; |
| 91 | - if ( in_array( $entity_id, $same_as ) ) { |
|
| 91 | + if (in_array($entity_id, $same_as)) { |
|
| 92 | 92 | // since entity ids are unique, we break after finding the first instance. |
| 93 | - unset( $entity_list[ $key ] ); |
|
| 93 | + unset($entity_list[$key]); |
|
| 94 | 94 | break; |
| 95 | 95 | } |
| 96 | 96 | } |
| 97 | - $this->clear_and_save_list( $entity_list ); |
|
| 97 | + $this->clear_and_save_list($entity_list); |
|
| 98 | 98 | |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | /** |
| 102 | 102 | * @param $entity_list |
| 103 | 103 | */ |
| 104 | - private function save_entity_list( $entity_list ) { |
|
| 105 | - foreach ( $entity_list as $single_entity ) { |
|
| 106 | - add_term_meta( $this->term_id, self::META_KEY, $single_entity ); |
|
| 104 | + private function save_entity_list($entity_list) { |
|
| 105 | + foreach ($entity_list as $single_entity) { |
|
| 106 | + add_term_meta($this->term_id, self::META_KEY, $single_entity); |
|
| 107 | 107 | } |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | /** |
| 111 | 111 | * @param $entity_list |
| 112 | 112 | */ |
| 113 | - private function clear_and_save_list( $entity_list ) { |
|
| 113 | + private function clear_and_save_list($entity_list) { |
|
| 114 | 114 | // Clear all data and add the new one. |
| 115 | 115 | $this->clear_data(); |
| 116 | - $this->save_entity_list( $entity_list ); |
|
| 116 | + $this->save_entity_list($entity_list); |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | |
@@ -124,12 +124,12 @@ discard block |
||
| 124 | 124 | * |
| 125 | 125 | * @return array |
| 126 | 126 | */ |
| 127 | - private function filter_entity_data( $entity_data ) { |
|
| 128 | - $allowed_keys = array( '@id', 'description', 'sameAs', '@type', 'name' ); |
|
| 127 | + private function filter_entity_data($entity_data) { |
|
| 128 | + $allowed_keys = array('@id', 'description', 'sameAs', '@type', 'name'); |
|
| 129 | 129 | $data = array(); |
| 130 | - foreach ( $entity_data as $key => $value ) { |
|
| 131 | - if ( in_array( $key, $allowed_keys ) ) { |
|
| 132 | - $data[ $key ] = $value; |
|
| 130 | + foreach ($entity_data as $key => $value) { |
|
| 131 | + if (in_array($key, $allowed_keys)) { |
|
| 132 | + $data[$key] = $value; |
|
| 133 | 133 | } |
| 134 | 134 | } |
| 135 | 135 | |
@@ -137,31 +137,31 @@ discard block |
||
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | |
| 140 | - public static function compact_jsonld( $entity_data ) { |
|
| 140 | + public static function compact_jsonld($entity_data) { |
|
| 141 | 141 | |
| 142 | - if ( self::is_value_array( '@type', $entity_data ) ) { |
|
| 143 | - $entity_data['@type'] = array_map( function ( $type ) { |
|
| 144 | - return str_replace( "http://schema.org/", "", $type ); |
|
| 145 | - }, $entity_data['@type'] ); |
|
| 142 | + if (self::is_value_array('@type', $entity_data)) { |
|
| 143 | + $entity_data['@type'] = array_map(function($type) { |
|
| 144 | + return str_replace("http://schema.org/", "", $type); |
|
| 145 | + }, $entity_data['@type']); |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | - if ( self::is_value_array( 'description', $entity_data ) ) { |
|
| 149 | - $entity_data['description'] = self::extract_jsonld_values( $entity_data['description'] ); |
|
| 148 | + if (self::is_value_array('description', $entity_data)) { |
|
| 149 | + $entity_data['description'] = self::extract_jsonld_values($entity_data['description']); |
|
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | |
| 153 | - if ( self::is_value_array( 'name', $entity_data ) ) { |
|
| 154 | - $entity_data['name'] = self::extract_jsonld_values( $entity_data['name'] ); |
|
| 153 | + if (self::is_value_array('name', $entity_data)) { |
|
| 154 | + $entity_data['name'] = self::extract_jsonld_values($entity_data['name']); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | |
| 158 | - if ( self::is_value_array( 'sameAs', $entity_data ) ) { |
|
| 159 | - $entity_data['sameAs'] = array_map( function ( $sameas ) { |
|
| 160 | - if ( ! is_array($sameas) || ! array_key_exists( '@id', $sameas ) ) { |
|
| 158 | + if (self::is_value_array('sameAs', $entity_data)) { |
|
| 159 | + $entity_data['sameAs'] = array_map(function($sameas) { |
|
| 160 | + if ( ! is_array($sameas) || ! array_key_exists('@id', $sameas)) { |
|
| 161 | 161 | return $sameas; |
| 162 | 162 | } |
| 163 | 163 | return $sameas['@id']; |
| 164 | - }, $entity_data['sameAs'] ); |
|
| 164 | + }, $entity_data['sameAs']); |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | |
@@ -6,135 +6,135 @@ |
||
| 6 | 6 | class Analysis_Background_Service { |
| 7 | 7 | |
| 8 | 8 | |
| 9 | - const ANALYSIS_DONE_FLAG = '_wl_cmkg_analysis_complete_for_term_options_cache'; |
|
| 10 | - const TERMS_COUNT_TRANSIENT = '_wl_cmkg_analysis_background_service_terms_count'; |
|
| 11 | - const ENTITIES_PRESENT_FOR_TERM = '_wl_cmkg_analysis_entities_present_for_term_options_cache'; |
|
| 12 | - |
|
| 13 | - /** |
|
| 14 | - * @var Analysis_Service |
|
| 15 | - */ |
|
| 16 | - private $analysis_service; |
|
| 17 | - /** |
|
| 18 | - * @var Analysis_Background_Process |
|
| 19 | - */ |
|
| 20 | - private $analysis_background_process; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @var \Wordlift_Log_Service |
|
| 24 | - */ |
|
| 25 | - private $log; |
|
| 26 | - |
|
| 27 | - |
|
| 28 | - public function __construct( $analysis_service ) { |
|
| 29 | - |
|
| 30 | - $this->analysis_service = $analysis_service; |
|
| 31 | - |
|
| 32 | - $this->analysis_background_process = new Analysis_Background_Process( $this ); |
|
| 33 | - |
|
| 34 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - public function start() { |
|
| 38 | - $this->analysis_background_process->start(); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - public function cancel() { |
|
| 42 | - $this->analysis_background_process->cancel(); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - public function stop() { |
|
| 46 | - $this->analysis_background_process->cancel(); |
|
| 47 | - $this->analysis_background_process->request_cancel(); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * A list of term ids. |
|
| 52 | - * @return int|\WP_Error|\WP_Term[] |
|
| 53 | - */ |
|
| 54 | - public function next() { |
|
| 55 | - |
|
| 56 | - $state = $this->info(); |
|
| 57 | - |
|
| 58 | - return Terms_Compat::get_terms( Terms_Compat::get_public_taxonomies(), array( |
|
| 59 | - 'fields' => 'ids', |
|
| 60 | - 'hide_empty' => false, |
|
| 61 | - 'number' => $this->get_batch_size(), |
|
| 62 | - 'offset' => $state->index, |
|
| 63 | - 'meta_query' => array( |
|
| 64 | - array( |
|
| 65 | - 'key' => self::ANALYSIS_DONE_FLAG, |
|
| 66 | - 'compare' => 'NOT EXISTS' |
|
| 67 | - ) |
|
| 68 | - ), |
|
| 69 | - ) ); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - public function count() { |
|
| 73 | - |
|
| 74 | - $count = count( Terms_Compat::get_terms( |
|
| 75 | - Terms_Compat::get_public_taxonomies(), |
|
| 76 | - array( |
|
| 77 | - 'fields' => 'ids', |
|
| 78 | - 'hide_empty' => false, |
|
| 79 | - // return all terms, we cant pass -1 here. |
|
| 80 | - 'number' => 0, |
|
| 81 | - 'meta_query' => array( |
|
| 82 | - array( |
|
| 83 | - 'key' => self::ANALYSIS_DONE_FLAG, |
|
| 84 | - 'compare' => 'NOT EXISTS' |
|
| 85 | - ) |
|
| 86 | - ), |
|
| 87 | - ) ) ); |
|
| 88 | - |
|
| 89 | - $this->log->debug( "Count returned as $count" ); |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - return $count; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - public function get_batch_size() { |
|
| 96 | - return 10; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - public function info() { |
|
| 100 | - return Analysis_Background_Process::get_state(); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @param $term_ids |
|
| 105 | - * |
|
| 106 | - * @return bool |
|
| 107 | - */ |
|
| 108 | - public function perform_analysis_for_terms( $term_ids ) { |
|
| 109 | - |
|
| 110 | - foreach ( $term_ids as $term_id ) { |
|
| 111 | - |
|
| 112 | - $tag = get_term( $term_id ); |
|
| 113 | - |
|
| 114 | - // This adds the entities to ttl cache |
|
| 115 | - $result = $this->analysis_service->get_entities( $tag ); |
|
| 9 | + const ANALYSIS_DONE_FLAG = '_wl_cmkg_analysis_complete_for_term_options_cache'; |
|
| 10 | + const TERMS_COUNT_TRANSIENT = '_wl_cmkg_analysis_background_service_terms_count'; |
|
| 11 | + const ENTITIES_PRESENT_FOR_TERM = '_wl_cmkg_analysis_entities_present_for_term_options_cache'; |
|
| 12 | + |
|
| 13 | + /** |
|
| 14 | + * @var Analysis_Service |
|
| 15 | + */ |
|
| 16 | + private $analysis_service; |
|
| 17 | + /** |
|
| 18 | + * @var Analysis_Background_Process |
|
| 19 | + */ |
|
| 20 | + private $analysis_background_process; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @var \Wordlift_Log_Service |
|
| 24 | + */ |
|
| 25 | + private $log; |
|
| 26 | + |
|
| 27 | + |
|
| 28 | + public function __construct( $analysis_service ) { |
|
| 29 | + |
|
| 30 | + $this->analysis_service = $analysis_service; |
|
| 31 | + |
|
| 32 | + $this->analysis_background_process = new Analysis_Background_Process( $this ); |
|
| 33 | + |
|
| 34 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + public function start() { |
|
| 38 | + $this->analysis_background_process->start(); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + public function cancel() { |
|
| 42 | + $this->analysis_background_process->cancel(); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + public function stop() { |
|
| 46 | + $this->analysis_background_process->cancel(); |
|
| 47 | + $this->analysis_background_process->request_cancel(); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * A list of term ids. |
|
| 52 | + * @return int|\WP_Error|\WP_Term[] |
|
| 53 | + */ |
|
| 54 | + public function next() { |
|
| 55 | + |
|
| 56 | + $state = $this->info(); |
|
| 57 | + |
|
| 58 | + return Terms_Compat::get_terms( Terms_Compat::get_public_taxonomies(), array( |
|
| 59 | + 'fields' => 'ids', |
|
| 60 | + 'hide_empty' => false, |
|
| 61 | + 'number' => $this->get_batch_size(), |
|
| 62 | + 'offset' => $state->index, |
|
| 63 | + 'meta_query' => array( |
|
| 64 | + array( |
|
| 65 | + 'key' => self::ANALYSIS_DONE_FLAG, |
|
| 66 | + 'compare' => 'NOT EXISTS' |
|
| 67 | + ) |
|
| 68 | + ), |
|
| 69 | + ) ); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + public function count() { |
|
| 73 | + |
|
| 74 | + $count = count( Terms_Compat::get_terms( |
|
| 75 | + Terms_Compat::get_public_taxonomies(), |
|
| 76 | + array( |
|
| 77 | + 'fields' => 'ids', |
|
| 78 | + 'hide_empty' => false, |
|
| 79 | + // return all terms, we cant pass -1 here. |
|
| 80 | + 'number' => 0, |
|
| 81 | + 'meta_query' => array( |
|
| 82 | + array( |
|
| 83 | + 'key' => self::ANALYSIS_DONE_FLAG, |
|
| 84 | + 'compare' => 'NOT EXISTS' |
|
| 85 | + ) |
|
| 86 | + ), |
|
| 87 | + ) ) ); |
|
| 88 | + |
|
| 89 | + $this->log->debug( "Count returned as $count" ); |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + return $count; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + public function get_batch_size() { |
|
| 96 | + return 10; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + public function info() { |
|
| 100 | + return Analysis_Background_Process::get_state(); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @param $term_ids |
|
| 105 | + * |
|
| 106 | + * @return bool |
|
| 107 | + */ |
|
| 108 | + public function perform_analysis_for_terms( $term_ids ) { |
|
| 109 | + |
|
| 110 | + foreach ( $term_ids as $term_id ) { |
|
| 111 | + |
|
| 112 | + $tag = get_term( $term_id ); |
|
| 113 | + |
|
| 114 | + // This adds the entities to ttl cache |
|
| 115 | + $result = $this->analysis_service->get_entities( $tag ); |
|
| 116 | 116 | |
| 117 | - $this->log->debug( "Received result " . var_export( $result ) . " for ${term_id}" ); |
|
| 117 | + $this->log->debug( "Received result " . var_export( $result ) . " for ${term_id}" ); |
|
| 118 | 118 | |
| 119 | - if ( $result !== false ) { |
|
| 120 | - // then set the analysis complete flag. |
|
| 121 | - update_term_meta( $term_id, self::ANALYSIS_DONE_FLAG, 1 ); |
|
| 122 | - if ( count( $result ) > 0 ) { |
|
| 123 | - update_term_meta( $term_id, self::ENTITIES_PRESENT_FOR_TERM, 1 ); |
|
| 124 | - } |
|
| 125 | - } |
|
| 119 | + if ( $result !== false ) { |
|
| 120 | + // then set the analysis complete flag. |
|
| 121 | + update_term_meta( $term_id, self::ANALYSIS_DONE_FLAG, 1 ); |
|
| 122 | + if ( count( $result ) > 0 ) { |
|
| 123 | + update_term_meta( $term_id, self::ENTITIES_PRESENT_FOR_TERM, 1 ); |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - } |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - /** |
|
| 130 | - * This action fires when the analysis is complete for the current batch |
|
| 131 | - * @since 3.30.0 |
|
| 132 | - */ |
|
| 133 | - do_action( 'wordlift_vocabulary_analysis_complete_for_terms_batch' ); |
|
| 134 | - |
|
| 135 | - return true; |
|
| 136 | - |
|
| 137 | - |
|
| 138 | - } |
|
| 129 | + /** |
|
| 130 | + * This action fires when the analysis is complete for the current batch |
|
| 131 | + * @since 3.30.0 |
|
| 132 | + */ |
|
| 133 | + do_action( 'wordlift_vocabulary_analysis_complete_for_terms_batch' ); |
|
| 134 | + |
|
| 135 | + return true; |
|
| 136 | + |
|
| 137 | + |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | 140 | } |
| 141 | 141 | \ No newline at end of file |
@@ -25,13 +25,13 @@ discard block |
||
| 25 | 25 | private $log; |
| 26 | 26 | |
| 27 | 27 | |
| 28 | - public function __construct( $analysis_service ) { |
|
| 28 | + public function __construct($analysis_service) { |
|
| 29 | 29 | |
| 30 | 30 | $this->analysis_service = $analysis_service; |
| 31 | 31 | |
| 32 | - $this->analysis_background_process = new Analysis_Background_Process( $this ); |
|
| 32 | + $this->analysis_background_process = new Analysis_Background_Process($this); |
|
| 33 | 33 | |
| 34 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 34 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | public function start() { |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | |
| 56 | 56 | $state = $this->info(); |
| 57 | 57 | |
| 58 | - return Terms_Compat::get_terms( Terms_Compat::get_public_taxonomies(), array( |
|
| 58 | + return Terms_Compat::get_terms(Terms_Compat::get_public_taxonomies(), array( |
|
| 59 | 59 | 'fields' => 'ids', |
| 60 | 60 | 'hide_empty' => false, |
| 61 | 61 | 'number' => $this->get_batch_size(), |
@@ -66,12 +66,12 @@ discard block |
||
| 66 | 66 | 'compare' => 'NOT EXISTS' |
| 67 | 67 | ) |
| 68 | 68 | ), |
| 69 | - ) ); |
|
| 69 | + )); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | public function count() { |
| 73 | 73 | |
| 74 | - $count = count( Terms_Compat::get_terms( |
|
| 74 | + $count = count(Terms_Compat::get_terms( |
|
| 75 | 75 | Terms_Compat::get_public_taxonomies(), |
| 76 | 76 | array( |
| 77 | 77 | 'fields' => 'ids', |
@@ -84,9 +84,9 @@ discard block |
||
| 84 | 84 | 'compare' => 'NOT EXISTS' |
| 85 | 85 | ) |
| 86 | 86 | ), |
| 87 | - ) ) ); |
|
| 87 | + ) )); |
|
| 88 | 88 | |
| 89 | - $this->log->debug( "Count returned as $count" ); |
|
| 89 | + $this->log->debug("Count returned as $count"); |
|
| 90 | 90 | |
| 91 | 91 | |
| 92 | 92 | return $count; |
@@ -105,22 +105,22 @@ discard block |
||
| 105 | 105 | * |
| 106 | 106 | * @return bool |
| 107 | 107 | */ |
| 108 | - public function perform_analysis_for_terms( $term_ids ) { |
|
| 108 | + public function perform_analysis_for_terms($term_ids) { |
|
| 109 | 109 | |
| 110 | - foreach ( $term_ids as $term_id ) { |
|
| 110 | + foreach ($term_ids as $term_id) { |
|
| 111 | 111 | |
| 112 | - $tag = get_term( $term_id ); |
|
| 112 | + $tag = get_term($term_id); |
|
| 113 | 113 | |
| 114 | 114 | // This adds the entities to ttl cache |
| 115 | - $result = $this->analysis_service->get_entities( $tag ); |
|
| 115 | + $result = $this->analysis_service->get_entities($tag); |
|
| 116 | 116 | |
| 117 | - $this->log->debug( "Received result " . var_export( $result ) . " for ${term_id}" ); |
|
| 117 | + $this->log->debug("Received result ".var_export($result)." for ${term_id}"); |
|
| 118 | 118 | |
| 119 | - if ( $result !== false ) { |
|
| 119 | + if ($result !== false) { |
|
| 120 | 120 | // then set the analysis complete flag. |
| 121 | - update_term_meta( $term_id, self::ANALYSIS_DONE_FLAG, 1 ); |
|
| 122 | - if ( count( $result ) > 0 ) { |
|
| 123 | - update_term_meta( $term_id, self::ENTITIES_PRESENT_FOR_TERM, 1 ); |
|
| 121 | + update_term_meta($term_id, self::ANALYSIS_DONE_FLAG, 1); |
|
| 122 | + if (count($result) > 0) { |
|
| 123 | + update_term_meta($term_id, self::ENTITIES_PRESENT_FOR_TERM, 1); |
|
| 124 | 124 | } |
| 125 | 125 | } |
| 126 | 126 | |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | * This action fires when the analysis is complete for the current batch |
| 131 | 131 | * @since 3.30.0 |
| 132 | 132 | */ |
| 133 | - do_action( 'wordlift_vocabulary_analysis_complete_for_terms_batch' ); |
|
| 133 | + do_action('wordlift_vocabulary_analysis_complete_for_terms_batch'); |
|
| 134 | 134 | |
| 135 | 135 | return true; |
| 136 | 136 | |
@@ -12,108 +12,108 @@ |
||
| 12 | 12 | |
| 13 | 13 | class Post_Jsonld { |
| 14 | 14 | |
| 15 | - public function enhance_post_jsonld() { |
|
| 16 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 17 | - add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 11, 2 ); |
|
| 18 | - } |
|
| 15 | + public function enhance_post_jsonld() { |
|
| 16 | + add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 17 | + add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 11, 2 ); |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - public function wl_post_jsonld_array( $arr, $post_id ) { |
|
| 20 | + public function wl_post_jsonld_array( $arr, $post_id ) { |
|
| 21 | 21 | |
| 22 | - $jsonld = $arr['jsonld']; |
|
| 23 | - $references = $arr['references']; |
|
| 22 | + $jsonld = $arr['jsonld']; |
|
| 23 | + $references = $arr['references']; |
|
| 24 | 24 | |
| 25 | - $this->add_mentions( $post_id, $jsonld, $references ); |
|
| 25 | + $this->add_mentions( $post_id, $jsonld, $references ); |
|
| 26 | 26 | |
| 27 | - return array( |
|
| 28 | - 'jsonld' => $jsonld, |
|
| 29 | - 'references' => $references |
|
| 30 | - ); |
|
| 27 | + return array( |
|
| 28 | + 'jsonld' => $jsonld, |
|
| 29 | + 'references' => $references |
|
| 30 | + ); |
|
| 31 | 31 | |
| 32 | - } |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - public function add_mentions( $post_id, &$jsonld, &$references ) { |
|
| 34 | + public function add_mentions( $post_id, &$jsonld, &$references ) { |
|
| 35 | 35 | |
| 36 | - $taxonomies = Terms_Compat::get_public_taxonomies(); |
|
| 37 | - $terms = array(); |
|
| 36 | + $taxonomies = Terms_Compat::get_public_taxonomies(); |
|
| 37 | + $terms = array(); |
|
| 38 | 38 | |
| 39 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 40 | - $taxonomy_terms = get_the_terms( $post_id, $taxonomy ); |
|
| 41 | - if ( ! $taxonomy_terms ) { |
|
| 42 | - continue; |
|
| 43 | - } |
|
| 44 | - $terms = array_merge( $taxonomy_terms, $terms ); |
|
| 45 | - } |
|
| 39 | + foreach ( $taxonomies as $taxonomy ) { |
|
| 40 | + $taxonomy_terms = get_the_terms( $post_id, $taxonomy ); |
|
| 41 | + if ( ! $taxonomy_terms ) { |
|
| 42 | + continue; |
|
| 43 | + } |
|
| 44 | + $terms = array_merge( $taxonomy_terms, $terms ); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - if ( ! $terms ) { |
|
| 48 | - return; |
|
| 49 | - } |
|
| 47 | + if ( ! $terms ) { |
|
| 48 | + return; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - if ( ! array_key_exists( 'mentions', $jsonld ) ) { |
|
| 52 | - $jsonld['mentions'] = array(); |
|
| 53 | - } |
|
| 51 | + if ( ! array_key_exists( 'mentions', $jsonld ) ) { |
|
| 52 | + $jsonld['mentions'] = array(); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - foreach ( $terms as $term ) { |
|
| 55 | + foreach ( $terms as $term ) { |
|
| 56 | 56 | |
| 57 | - $is_matched = intval( get_term_meta( $term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) ) === 1; |
|
| 57 | + $is_matched = intval( get_term_meta( $term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) ) === 1; |
|
| 58 | 58 | |
| 59 | - if ( ! $is_matched ) { |
|
| 60 | - continue; |
|
| 61 | - } |
|
| 59 | + if ( ! $is_matched ) { |
|
| 60 | + continue; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - $entity = Entity_List_Factory::get_instance( $term->term_id ); |
|
| 63 | + $entity = Entity_List_Factory::get_instance( $term->term_id ); |
|
| 64 | 64 | |
| 65 | - $entities = $entity->get_jsonld_data(); |
|
| 65 | + $entities = $entity->get_jsonld_data(); |
|
| 66 | 66 | |
| 67 | - if ( count( $entities ) === 0 ) { |
|
| 68 | - continue; |
|
| 69 | - } |
|
| 67 | + if ( count( $entities ) === 0 ) { |
|
| 68 | + continue; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - $jsonld['mentions'] = array_merge( $jsonld['mentions'], self::add_additional_attrs( $term, $entities ) ); |
|
| 72 | - } |
|
| 71 | + $jsonld['mentions'] = array_merge( $jsonld['mentions'], self::add_additional_attrs( $term, $entities ) ); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - } |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * @param $term \WP_Term |
|
| 78 | - * @param $entities |
|
| 79 | - * |
|
| 80 | - * @return array |
|
| 81 | - */ |
|
| 82 | - public static function add_additional_attrs( $term, $entities ) { |
|
| 76 | + /** |
|
| 77 | + * @param $term \WP_Term |
|
| 78 | + * @param $entities |
|
| 79 | + * |
|
| 80 | + * @return array |
|
| 81 | + */ |
|
| 82 | + public static function add_additional_attrs( $term, $entities ) { |
|
| 83 | 83 | |
| 84 | - return array_map( function ( $entity ) use ( $term ) { |
|
| 85 | - $entity['@id'] = get_term_link( $term->term_id ) . '#id'; |
|
| 86 | - if ( ! empty( $term->description ) ) { |
|
| 87 | - $entity['description'] = $term->description; |
|
| 88 | - } |
|
| 84 | + return array_map( function ( $entity ) use ( $term ) { |
|
| 85 | + $entity['@id'] = get_term_link( $term->term_id ) . '#id'; |
|
| 86 | + if ( ! empty( $term->description ) ) { |
|
| 87 | + $entity['description'] = $term->description; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - return $entity; |
|
| 90 | + return $entity; |
|
| 91 | 91 | |
| 92 | - }, $entities ); |
|
| 92 | + }, $entities ); |
|
| 93 | 93 | |
| 94 | - } |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - public function wl_after_get_jsonld( $jsonld, $post_id ) { |
|
| 96 | + public function wl_after_get_jsonld( $jsonld, $post_id ) { |
|
| 97 | 97 | |
| 98 | - if ( ! is_array( $jsonld ) || count( $jsonld ) === 0 ) { |
|
| 99 | - return $jsonld; |
|
| 100 | - } |
|
| 98 | + if ( ! is_array( $jsonld ) || count( $jsonld ) === 0 ) { |
|
| 99 | + return $jsonld; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - foreach ( $jsonld as $key => $value ) { |
|
| 103 | - if ( $value['@type'] === 'Article' && isset( $value['image'] ) ) { |
|
| 104 | - $image = $value['image']; |
|
| 105 | - } |
|
| 106 | - if ( $value['@type'] === 'Recipe' && ! isset( $value['image'] ) ) { |
|
| 107 | - $index = $key; |
|
| 108 | - } |
|
| 109 | - } |
|
| 102 | + foreach ( $jsonld as $key => $value ) { |
|
| 103 | + if ( $value['@type'] === 'Article' && isset( $value['image'] ) ) { |
|
| 104 | + $image = $value['image']; |
|
| 105 | + } |
|
| 106 | + if ( $value['@type'] === 'Recipe' && ! isset( $value['image'] ) ) { |
|
| 107 | + $index = $key; |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - if ( isset( $index ) && ! empty( $image ) ) { |
|
| 112 | - $jsonld[ $index ]['image'] = $image; |
|
| 113 | - } |
|
| 111 | + if ( isset( $index ) && ! empty( $image ) ) { |
|
| 112 | + $jsonld[ $index ]['image'] = $image; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - return $jsonld; |
|
| 115 | + return $jsonld; |
|
| 116 | 116 | |
| 117 | - } |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | 119 | } |
@@ -13,16 +13,16 @@ discard block |
||
| 13 | 13 | class Post_Jsonld { |
| 14 | 14 | |
| 15 | 15 | public function enhance_post_jsonld() { |
| 16 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 17 | - add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 11, 2 ); |
|
| 16 | + add_filter('wl_post_jsonld_array', array($this, 'wl_post_jsonld_array'), 11, 2); |
|
| 17 | + add_filter('wl_after_get_jsonld', array($this, 'wl_after_get_jsonld'), 11, 2); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | - public function wl_post_jsonld_array( $arr, $post_id ) { |
|
| 20 | + public function wl_post_jsonld_array($arr, $post_id) { |
|
| 21 | 21 | |
| 22 | 22 | $jsonld = $arr['jsonld']; |
| 23 | 23 | $references = $arr['references']; |
| 24 | 24 | |
| 25 | - $this->add_mentions( $post_id, $jsonld, $references ); |
|
| 25 | + $this->add_mentions($post_id, $jsonld, $references); |
|
| 26 | 26 | |
| 27 | 27 | return array( |
| 28 | 28 | 'jsonld' => $jsonld, |
@@ -31,44 +31,44 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - public function add_mentions( $post_id, &$jsonld, &$references ) { |
|
| 34 | + public function add_mentions($post_id, &$jsonld, &$references) { |
|
| 35 | 35 | |
| 36 | 36 | $taxonomies = Terms_Compat::get_public_taxonomies(); |
| 37 | 37 | $terms = array(); |
| 38 | 38 | |
| 39 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 40 | - $taxonomy_terms = get_the_terms( $post_id, $taxonomy ); |
|
| 41 | - if ( ! $taxonomy_terms ) { |
|
| 39 | + foreach ($taxonomies as $taxonomy) { |
|
| 40 | + $taxonomy_terms = get_the_terms($post_id, $taxonomy); |
|
| 41 | + if ( ! $taxonomy_terms) { |
|
| 42 | 42 | continue; |
| 43 | 43 | } |
| 44 | - $terms = array_merge( $taxonomy_terms, $terms ); |
|
| 44 | + $terms = array_merge($taxonomy_terms, $terms); |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | - if ( ! $terms ) { |
|
| 47 | + if ( ! $terms) { |
|
| 48 | 48 | return; |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | - if ( ! array_key_exists( 'mentions', $jsonld ) ) { |
|
| 51 | + if ( ! array_key_exists('mentions', $jsonld)) { |
|
| 52 | 52 | $jsonld['mentions'] = array(); |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | - foreach ( $terms as $term ) { |
|
| 55 | + foreach ($terms as $term) { |
|
| 56 | 56 | |
| 57 | - $is_matched = intval( get_term_meta( $term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) ) === 1; |
|
| 57 | + $is_matched = intval(get_term_meta($term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true)) === 1; |
|
| 58 | 58 | |
| 59 | - if ( ! $is_matched ) { |
|
| 59 | + if ( ! $is_matched) { |
|
| 60 | 60 | continue; |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - $entity = Entity_List_Factory::get_instance( $term->term_id ); |
|
| 63 | + $entity = Entity_List_Factory::get_instance($term->term_id); |
|
| 64 | 64 | |
| 65 | 65 | $entities = $entity->get_jsonld_data(); |
| 66 | 66 | |
| 67 | - if ( count( $entities ) === 0 ) { |
|
| 67 | + if (count($entities) === 0) { |
|
| 68 | 68 | continue; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - $jsonld['mentions'] = array_merge( $jsonld['mentions'], self::add_additional_attrs( $term, $entities ) ); |
|
| 71 | + $jsonld['mentions'] = array_merge($jsonld['mentions'], self::add_additional_attrs($term, $entities)); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | } |
@@ -79,37 +79,37 @@ discard block |
||
| 79 | 79 | * |
| 80 | 80 | * @return array |
| 81 | 81 | */ |
| 82 | - public static function add_additional_attrs( $term, $entities ) { |
|
| 82 | + public static function add_additional_attrs($term, $entities) { |
|
| 83 | 83 | |
| 84 | - return array_map( function ( $entity ) use ( $term ) { |
|
| 85 | - $entity['@id'] = get_term_link( $term->term_id ) . '#id'; |
|
| 86 | - if ( ! empty( $term->description ) ) { |
|
| 84 | + return array_map(function($entity) use ($term) { |
|
| 85 | + $entity['@id'] = get_term_link($term->term_id).'#id'; |
|
| 86 | + if ( ! empty($term->description)) { |
|
| 87 | 87 | $entity['description'] = $term->description; |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | return $entity; |
| 91 | 91 | |
| 92 | - }, $entities ); |
|
| 92 | + }, $entities); |
|
| 93 | 93 | |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | - public function wl_after_get_jsonld( $jsonld, $post_id ) { |
|
| 96 | + public function wl_after_get_jsonld($jsonld, $post_id) { |
|
| 97 | 97 | |
| 98 | - if ( ! is_array( $jsonld ) || count( $jsonld ) === 0 ) { |
|
| 98 | + if ( ! is_array($jsonld) || count($jsonld) === 0) { |
|
| 99 | 99 | return $jsonld; |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | - foreach ( $jsonld as $key => $value ) { |
|
| 103 | - if ( $value['@type'] === 'Article' && isset( $value['image'] ) ) { |
|
| 102 | + foreach ($jsonld as $key => $value) { |
|
| 103 | + if ($value['@type'] === 'Article' && isset($value['image'])) { |
|
| 104 | 104 | $image = $value['image']; |
| 105 | 105 | } |
| 106 | - if ( $value['@type'] === 'Recipe' && ! isset( $value['image'] ) ) { |
|
| 106 | + if ($value['@type'] === 'Recipe' && ! isset($value['image'])) { |
|
| 107 | 107 | $index = $key; |
| 108 | 108 | } |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - if ( isset( $index ) && ! empty( $image ) ) { |
|
| 112 | - $jsonld[ $index ]['image'] = $image; |
|
| 111 | + if (isset($index) && ! empty($image)) { |
|
| 112 | + $jsonld[$index]['image'] = $image; |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | return $jsonld; |
@@ -3,20 +3,20 @@ |
||
| 3 | 3 | |
| 4 | 4 | class Terms_Compat { |
| 5 | 5 | |
| 6 | - public static function get_terms( $taxonomy, $args_with_taxonomy_key ) { |
|
| 7 | - global $wp_version; |
|
| 6 | + public static function get_terms( $taxonomy, $args_with_taxonomy_key ) { |
|
| 7 | + global $wp_version; |
|
| 8 | 8 | |
| 9 | - if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
| 10 | - return get_terms( $taxonomy, $args_with_taxonomy_key ); |
|
| 11 | - } else { |
|
| 12 | - $args_with_taxonomy_key['taxonomy'] = $taxonomy; |
|
| 13 | - return get_terms( $args_with_taxonomy_key ); |
|
| 14 | - } |
|
| 15 | - } |
|
| 9 | + if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
| 10 | + return get_terms( $taxonomy, $args_with_taxonomy_key ); |
|
| 11 | + } else { |
|
| 12 | + $args_with_taxonomy_key['taxonomy'] = $taxonomy; |
|
| 13 | + return get_terms( $args_with_taxonomy_key ); |
|
| 14 | + } |
|
| 15 | + } |
|
| 16 | 16 | |
| 17 | - public static function get_public_taxonomies() { |
|
| 18 | - return get_taxonomies( array( 'public' => true ) ); |
|
| 19 | - } |
|
| 17 | + public static function get_public_taxonomies() { |
|
| 18 | + return get_taxonomies( array( 'public' => true ) ); |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | 21 | |
| 22 | 22 | } |
| 23 | 23 | \ No newline at end of file |
@@ -3,19 +3,19 @@ |
||
| 3 | 3 | |
| 4 | 4 | class Terms_Compat { |
| 5 | 5 | |
| 6 | - public static function get_terms( $taxonomy, $args_with_taxonomy_key ) { |
|
| 6 | + public static function get_terms($taxonomy, $args_with_taxonomy_key) { |
|
| 7 | 7 | global $wp_version; |
| 8 | 8 | |
| 9 | - if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
| 10 | - return get_terms( $taxonomy, $args_with_taxonomy_key ); |
|
| 9 | + if (version_compare($wp_version, '4.5', '<')) { |
|
| 10 | + return get_terms($taxonomy, $args_with_taxonomy_key); |
|
| 11 | 11 | } else { |
| 12 | 12 | $args_with_taxonomy_key['taxonomy'] = $taxonomy; |
| 13 | - return get_terms( $args_with_taxonomy_key ); |
|
| 13 | + return get_terms($args_with_taxonomy_key); |
|
| 14 | 14 | } |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | public static function get_public_taxonomies() { |
| 18 | - return get_taxonomies( array( 'public' => true ) ); |
|
| 18 | + return get_taxonomies(array('public' => true)); |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | |
@@ -9,31 +9,31 @@ |
||
| 9 | 9 | |
| 10 | 10 | class Default_Term_Data implements Term_Data { |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * @var array An array of entities bound to the term. |
|
| 14 | - */ |
|
| 15 | - private $entities; |
|
| 16 | - /** |
|
| 17 | - * @var \WP_Term |
|
| 18 | - */ |
|
| 19 | - private $term; |
|
| 12 | + /** |
|
| 13 | + * @var array An array of entities bound to the term. |
|
| 14 | + */ |
|
| 15 | + private $entities; |
|
| 16 | + /** |
|
| 17 | + * @var \WP_Term |
|
| 18 | + */ |
|
| 19 | + private $term; |
|
| 20 | 20 | |
| 21 | - public function __construct( $term, $entities ) { |
|
| 22 | - $this->term = $term; |
|
| 23 | - $this->entities = $entities; |
|
| 24 | - } |
|
| 21 | + public function __construct( $term, $entities ) { |
|
| 22 | + $this->term = $term; |
|
| 23 | + $this->entities = $entities; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - public function get_data() { |
|
| 26 | + public function get_data() { |
|
| 27 | 27 | |
| 28 | - return array( |
|
| 29 | - 'tagId' => $this->term->term_id, |
|
| 30 | - 'tagName' => $this->term->name, |
|
| 31 | - 'tagDescription' => $this->term->description, |
|
| 32 | - // Before 4.5.0 taxonomy parameter **must** be passed to this function. |
|
| 33 | - 'tagLink' => get_edit_term_link( $this->term->term_id, $this->term->taxonomy ), |
|
| 34 | - 'tagPostCount' => $this->term->count, |
|
| 35 | - 'tagTaxonomy' => get_taxonomy( $this->term->taxonomy )->label, |
|
| 36 | - 'entities' => $this->entities, |
|
| 37 | - ); |
|
| 38 | - } |
|
| 28 | + return array( |
|
| 29 | + 'tagId' => $this->term->term_id, |
|
| 30 | + 'tagName' => $this->term->name, |
|
| 31 | + 'tagDescription' => $this->term->description, |
|
| 32 | + // Before 4.5.0 taxonomy parameter **must** be passed to this function. |
|
| 33 | + 'tagLink' => get_edit_term_link( $this->term->term_id, $this->term->taxonomy ), |
|
| 34 | + 'tagPostCount' => $this->term->count, |
|
| 35 | + 'tagTaxonomy' => get_taxonomy( $this->term->taxonomy )->label, |
|
| 36 | + 'entities' => $this->entities, |
|
| 37 | + ); |
|
| 38 | + } |
|
| 39 | 39 | } |
| 40 | 40 | \ No newline at end of file |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | private $term; |
| 20 | 20 | |
| 21 | - public function __construct( $term, $entities ) { |
|
| 21 | + public function __construct($term, $entities) { |
|
| 22 | 22 | $this->term = $term; |
| 23 | 23 | $this->entities = $entities; |
| 24 | 24 | } |
@@ -30,9 +30,9 @@ discard block |
||
| 30 | 30 | 'tagName' => $this->term->name, |
| 31 | 31 | 'tagDescription' => $this->term->description, |
| 32 | 32 | // Before 4.5.0 taxonomy parameter **must** be passed to this function. |
| 33 | - 'tagLink' => get_edit_term_link( $this->term->term_id, $this->term->taxonomy ), |
|
| 33 | + 'tagLink' => get_edit_term_link($this->term->term_id, $this->term->taxonomy), |
|
| 34 | 34 | 'tagPostCount' => $this->term->count, |
| 35 | - 'tagTaxonomy' => get_taxonomy( $this->term->taxonomy )->label, |
|
| 35 | + 'tagTaxonomy' => get_taxonomy($this->term->taxonomy)->label, |
|
| 36 | 36 | 'entities' => $this->entities, |
| 37 | 37 | ); |
| 38 | 38 | } |
@@ -9,19 +9,19 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Cached_Term_count_Manager { |
| 11 | 11 | |
| 12 | - public function connect_hook() { |
|
| 12 | + public function connect_hook() { |
|
| 13 | 13 | |
| 14 | - add_action( 'wordlift_vocabulary_analysis_complete_for_terms_batch', function () { |
|
| 15 | - delete_transient( Cached_Term_Count::TRANSIENT_KEY ); |
|
| 16 | - } ); |
|
| 14 | + add_action( 'wordlift_vocabulary_analysis_complete_for_terms_batch', function () { |
|
| 15 | + delete_transient( Cached_Term_Count::TRANSIENT_KEY ); |
|
| 16 | + } ); |
|
| 17 | 17 | |
| 18 | - $taxonomies = Terms_Compat::get_public_taxonomies(); |
|
| 19 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 20 | - add_action( "created_${taxonomy}", function () { |
|
| 21 | - delete_transient( Cached_Term_Count::TRANSIENT_KEY ); |
|
| 22 | - } ); |
|
| 23 | - } |
|
| 18 | + $taxonomies = Terms_Compat::get_public_taxonomies(); |
|
| 19 | + foreach ( $taxonomies as $taxonomy ) { |
|
| 20 | + add_action( "created_${taxonomy}", function () { |
|
| 21 | + delete_transient( Cached_Term_Count::TRANSIENT_KEY ); |
|
| 22 | + } ); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - } |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | 27 | } |
| 28 | 28 | \ No newline at end of file |
@@ -11,14 +11,14 @@ |
||
| 11 | 11 | |
| 12 | 12 | public function connect_hook() { |
| 13 | 13 | |
| 14 | - add_action( 'wordlift_vocabulary_analysis_complete_for_terms_batch', function () { |
|
| 15 | - delete_transient( Cached_Term_Count::TRANSIENT_KEY ); |
|
| 14 | + add_action('wordlift_vocabulary_analysis_complete_for_terms_batch', function() { |
|
| 15 | + delete_transient(Cached_Term_Count::TRANSIENT_KEY); |
|
| 16 | 16 | } ); |
| 17 | 17 | |
| 18 | 18 | $taxonomies = Terms_Compat::get_public_taxonomies(); |
| 19 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 20 | - add_action( "created_${taxonomy}", function () { |
|
| 21 | - delete_transient( Cached_Term_Count::TRANSIENT_KEY ); |
|
| 19 | + foreach ($taxonomies as $taxonomy) { |
|
| 20 | + add_action("created_${taxonomy}", function() { |
|
| 21 | + delete_transient(Cached_Term_Count::TRANSIENT_KEY); |
|
| 22 | 22 | } ); |
| 23 | 23 | } |
| 24 | 24 | |
@@ -13,28 +13,28 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Default_Term_Count implements Term_Count { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Return count of terms which have entities and also not matched by editor, currently |
|
| 18 | - * it is used for the menu icon badge. |
|
| 19 | - * @return int |
|
| 20 | - */ |
|
| 21 | - public function get_term_count() { |
|
| 22 | - return count( Terms_Compat::get_terms( Terms_Compat::get_public_taxonomies(), array( |
|
| 23 | - 'hide_empty' => false, |
|
| 24 | - 'fields' => 'ids', |
|
| 25 | - 'meta_query' => array( |
|
| 26 | - array( |
|
| 27 | - 'key' => Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, |
|
| 28 | - 'compare' => 'NOT EXISTS', |
|
| 29 | - ), |
|
| 30 | - array( |
|
| 31 | - 'key' => Analysis_Background_Service::ENTITIES_PRESENT_FOR_TERM, |
|
| 32 | - 'compare' => 'EXISTS' |
|
| 33 | - ) |
|
| 34 | - ), |
|
| 35 | - ) ) ); |
|
| 36 | - |
|
| 37 | - } |
|
| 16 | + /** |
|
| 17 | + * Return count of terms which have entities and also not matched by editor, currently |
|
| 18 | + * it is used for the menu icon badge. |
|
| 19 | + * @return int |
|
| 20 | + */ |
|
| 21 | + public function get_term_count() { |
|
| 22 | + return count( Terms_Compat::get_terms( Terms_Compat::get_public_taxonomies(), array( |
|
| 23 | + 'hide_empty' => false, |
|
| 24 | + 'fields' => 'ids', |
|
| 25 | + 'meta_query' => array( |
|
| 26 | + array( |
|
| 27 | + 'key' => Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, |
|
| 28 | + 'compare' => 'NOT EXISTS', |
|
| 29 | + ), |
|
| 30 | + array( |
|
| 31 | + 'key' => Analysis_Background_Service::ENTITIES_PRESENT_FOR_TERM, |
|
| 32 | + 'compare' => 'EXISTS' |
|
| 33 | + ) |
|
| 34 | + ), |
|
| 35 | + ) ) ); |
|
| 36 | + |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | 39 | |
| 40 | 40 | |
@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | * @return int |
| 20 | 20 | */ |
| 21 | 21 | public function get_term_count() { |
| 22 | - return count( Terms_Compat::get_terms( Terms_Compat::get_public_taxonomies(), array( |
|
| 22 | + return count(Terms_Compat::get_terms(Terms_Compat::get_public_taxonomies(), array( |
|
| 23 | 23 | 'hide_empty' => false, |
| 24 | 24 | 'fields' => 'ids', |
| 25 | 25 | 'meta_query' => array( |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | 'compare' => 'EXISTS' |
| 33 | 33 | ) |
| 34 | 34 | ), |
| 35 | - ) ) ); |
|
| 35 | + ))); |
|
| 36 | 36 | |
| 37 | 37 | } |
| 38 | 38 | |