@@ -7,7 +7,6 @@ |
||
| 7 | 7 | namespace Wordlift\Vocabulary\Jsonld; |
| 8 | 8 | |
| 9 | 9 | use Wordlift\Vocabulary\Api\Entity_Rest_Endpoint; |
| 10 | -use Wordlift\Vocabulary\Data\Entity_List\Entity_List_Factory; |
|
| 11 | 10 | use Wordlift\Vocabulary\Terms_Compat; |
| 12 | 11 | |
| 13 | 12 | class Post_Jsonld { |
@@ -14,106 +14,106 @@ |
||
| 14 | 14 | |
| 15 | 15 | |
| 16 | 16 | |
| 17 | - public function enhance_post_jsonld() { |
|
| 18 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 19 | - add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 11, 2 ); |
|
| 20 | - } |
|
| 17 | + public function enhance_post_jsonld() { |
|
| 18 | + add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 19 | + add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 11, 2 ); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - public function wl_post_jsonld_array( $arr, $post_id ) { |
|
| 22 | + public function wl_post_jsonld_array( $arr, $post_id ) { |
|
| 23 | 23 | |
| 24 | - $jsonld = $arr['jsonld']; |
|
| 25 | - $references = $arr['references']; |
|
| 24 | + $jsonld = $arr['jsonld']; |
|
| 25 | + $references = $arr['references']; |
|
| 26 | 26 | |
| 27 | - $this->add_mentions( $post_id, $jsonld, $references ); |
|
| 27 | + $this->add_mentions( $post_id, $jsonld, $references ); |
|
| 28 | 28 | |
| 29 | - return array( |
|
| 30 | - 'jsonld' => $jsonld, |
|
| 31 | - 'references' => $references |
|
| 32 | - ); |
|
| 29 | + return array( |
|
| 30 | + 'jsonld' => $jsonld, |
|
| 31 | + 'references' => $references |
|
| 32 | + ); |
|
| 33 | 33 | |
| 34 | - } |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - public function add_mentions( $post_id, &$jsonld, &$references ) { |
|
| 36 | + public function add_mentions( $post_id, &$jsonld, &$references ) { |
|
| 37 | 37 | |
| 38 | - $taxonomies = Terms_Compat::get_public_taxonomies(); |
|
| 39 | - $terms = array(); |
|
| 38 | + $taxonomies = Terms_Compat::get_public_taxonomies(); |
|
| 39 | + $terms = array(); |
|
| 40 | 40 | |
| 41 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 42 | - $taxonomy_terms = get_the_terms( $post_id, $taxonomy ); |
|
| 43 | - if ( ! $taxonomy_terms ) { |
|
| 44 | - continue; |
|
| 45 | - } |
|
| 46 | - $terms = array_merge( $taxonomy_terms, $terms ); |
|
| 47 | - } |
|
| 41 | + foreach ( $taxonomies as $taxonomy ) { |
|
| 42 | + $taxonomy_terms = get_the_terms( $post_id, $taxonomy ); |
|
| 43 | + if ( ! $taxonomy_terms ) { |
|
| 44 | + continue; |
|
| 45 | + } |
|
| 46 | + $terms = array_merge( $taxonomy_terms, $terms ); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - if ( ! $terms ) { |
|
| 50 | - return; |
|
| 51 | - } |
|
| 49 | + if ( ! $terms ) { |
|
| 50 | + return; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - if ( ! array_key_exists( 'mentions', $jsonld ) ) { |
|
| 54 | - $jsonld['mentions'] = array(); |
|
| 55 | - } |
|
| 53 | + if ( ! array_key_exists( 'mentions', $jsonld ) ) { |
|
| 54 | + $jsonld['mentions'] = array(); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - foreach ( $terms as $term ) { |
|
| 57 | + foreach ( $terms as $term ) { |
|
| 58 | 58 | |
| 59 | - $is_matched = intval( get_term_meta( $term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) ) === 1; |
|
| 59 | + $is_matched = intval( get_term_meta( $term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) ) === 1; |
|
| 60 | 60 | |
| 61 | - if ( ! $is_matched ) { |
|
| 62 | - continue; |
|
| 63 | - } |
|
| 61 | + if ( ! $is_matched ) { |
|
| 62 | + continue; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - $entities = Jsonld_Utils::get_matched_entities_for_term( $term->term_id ); |
|
| 65 | + $entities = Jsonld_Utils::get_matched_entities_for_term( $term->term_id ); |
|
| 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 | } |
@@ -15,16 +15,16 @@ discard block |
||
| 15 | 15 | |
| 16 | 16 | |
| 17 | 17 | public function enhance_post_jsonld() { |
| 18 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 19 | - add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 11, 2 ); |
|
| 18 | + add_filter('wl_post_jsonld_array', array($this, 'wl_post_jsonld_array'), 11, 2); |
|
| 19 | + add_filter('wl_after_get_jsonld', array($this, 'wl_after_get_jsonld'), 11, 2); |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | - public function wl_post_jsonld_array( $arr, $post_id ) { |
|
| 22 | + public function wl_post_jsonld_array($arr, $post_id) { |
|
| 23 | 23 | |
| 24 | 24 | $jsonld = $arr['jsonld']; |
| 25 | 25 | $references = $arr['references']; |
| 26 | 26 | |
| 27 | - $this->add_mentions( $post_id, $jsonld, $references ); |
|
| 27 | + $this->add_mentions($post_id, $jsonld, $references); |
|
| 28 | 28 | |
| 29 | 29 | return array( |
| 30 | 30 | 'jsonld' => $jsonld, |
@@ -33,42 +33,42 @@ discard block |
||
| 33 | 33 | |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | - public function add_mentions( $post_id, &$jsonld, &$references ) { |
|
| 36 | + public function add_mentions($post_id, &$jsonld, &$references) { |
|
| 37 | 37 | |
| 38 | 38 | $taxonomies = Terms_Compat::get_public_taxonomies(); |
| 39 | 39 | $terms = array(); |
| 40 | 40 | |
| 41 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 42 | - $taxonomy_terms = get_the_terms( $post_id, $taxonomy ); |
|
| 43 | - if ( ! $taxonomy_terms ) { |
|
| 41 | + foreach ($taxonomies as $taxonomy) { |
|
| 42 | + $taxonomy_terms = get_the_terms($post_id, $taxonomy); |
|
| 43 | + if ( ! $taxonomy_terms) { |
|
| 44 | 44 | continue; |
| 45 | 45 | } |
| 46 | - $terms = array_merge( $taxonomy_terms, $terms ); |
|
| 46 | + $terms = array_merge($taxonomy_terms, $terms); |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | - if ( ! $terms ) { |
|
| 49 | + if ( ! $terms) { |
|
| 50 | 50 | return; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - if ( ! array_key_exists( 'mentions', $jsonld ) ) { |
|
| 53 | + if ( ! array_key_exists('mentions', $jsonld)) { |
|
| 54 | 54 | $jsonld['mentions'] = array(); |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - foreach ( $terms as $term ) { |
|
| 57 | + foreach ($terms as $term) { |
|
| 58 | 58 | |
| 59 | - $is_matched = intval( get_term_meta( $term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) ) === 1; |
|
| 59 | + $is_matched = intval(get_term_meta($term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true)) === 1; |
|
| 60 | 60 | |
| 61 | - if ( ! $is_matched ) { |
|
| 61 | + if ( ! $is_matched) { |
|
| 62 | 62 | continue; |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - $entities = Jsonld_Utils::get_matched_entities_for_term( $term->term_id ); |
|
| 65 | + $entities = Jsonld_Utils::get_matched_entities_for_term($term->term_id); |
|
| 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; |
@@ -22,63 +22,63 @@ |
||
| 22 | 22 | |
| 23 | 23 | class Vocabulary_Loader { |
| 24 | 24 | |
| 25 | - public function init_vocabulary() { |
|
| 25 | + public function init_vocabulary() { |
|
| 26 | 26 | |
| 27 | - $configuration_service = \Wordlift_Configuration_Service::get_instance(); |
|
| 27 | + $configuration_service = \Wordlift_Configuration_Service::get_instance(); |
|
| 28 | 28 | |
| 29 | - $api_service = new Default_Api_Service( |
|
| 30 | - apply_filters( 'wl_api_base_url', 'https://api.wordlift.io' ), |
|
| 31 | - 60, |
|
| 32 | - User_Agent::get_user_agent(), |
|
| 33 | - $configuration_service->get_key() |
|
| 34 | - ); |
|
| 29 | + $api_service = new Default_Api_Service( |
|
| 30 | + apply_filters( 'wl_api_base_url', 'https://api.wordlift.io' ), |
|
| 31 | + 60, |
|
| 32 | + User_Agent::get_user_agent(), |
|
| 33 | + $configuration_service->get_key() |
|
| 34 | + ); |
|
| 35 | 35 | |
| 36 | - $cache_service = Cache_Service_Factory::get_cache_service(); |
|
| 36 | + $cache_service = Cache_Service_Factory::get_cache_service(); |
|
| 37 | 37 | |
| 38 | 38 | |
| 39 | - $analysis_service = new Analysis_Service( $api_service, $cache_service ); |
|
| 39 | + $analysis_service = new Analysis_Service( $api_service, $cache_service ); |
|
| 40 | 40 | |
| 41 | - $term_data_factory = new Term_Data_Factory( $analysis_service ); |
|
| 41 | + $term_data_factory = new Term_Data_Factory( $analysis_service ); |
|
| 42 | 42 | |
| 43 | - $tag_rest_endpoint = new Tag_Rest_Endpoint( $term_data_factory ); |
|
| 44 | - $tag_rest_endpoint->register_routes(); |
|
| 43 | + $tag_rest_endpoint = new Tag_Rest_Endpoint( $term_data_factory ); |
|
| 44 | + $tag_rest_endpoint->register_routes(); |
|
| 45 | 45 | |
| 46 | - $entity_rest_endpoint = new Entity_Rest_Endpoint(); |
|
| 47 | - $entity_rest_endpoint->register_routes(); |
|
| 46 | + $entity_rest_endpoint = new Entity_Rest_Endpoint(); |
|
| 47 | + $entity_rest_endpoint->register_routes(); |
|
| 48 | 48 | |
| 49 | - $post_jsonld = new Post_Jsonld(); |
|
| 50 | - $post_jsonld->enhance_post_jsonld(); |
|
| 49 | + $post_jsonld = new Post_Jsonld(); |
|
| 50 | + $post_jsonld->enhance_post_jsonld(); |
|
| 51 | 51 | |
| 52 | - $term_jsonld = new Term_Jsonld(); |
|
| 53 | - $term_jsonld->init(); |
|
| 52 | + $term_jsonld = new Term_Jsonld(); |
|
| 53 | + $term_jsonld->init(); |
|
| 54 | 54 | |
| 55 | - $term_count = Term_Count_Factory::get_instance( Term_Count_Factory::CACHED_TERM_COUNT ); |
|
| 56 | - new Match_Terms( $term_count ); |
|
| 55 | + $term_count = Term_Count_Factory::get_instance( Term_Count_Factory::CACHED_TERM_COUNT ); |
|
| 56 | + new Match_Terms( $term_count ); |
|
| 57 | 57 | |
| 58 | - $analysis_background_service = new Analysis_Background_Service( $analysis_service ); |
|
| 58 | + $analysis_background_service = new Analysis_Background_Service( $analysis_service ); |
|
| 59 | 59 | |
| 60 | 60 | |
| 61 | - new Tag_Created_Hook( $analysis_background_service ); |
|
| 61 | + new Tag_Created_Hook( $analysis_background_service ); |
|
| 62 | 62 | |
| 63 | - new Background_Analysis_Endpoint( $analysis_background_service, $cache_service ); |
|
| 63 | + new Background_Analysis_Endpoint( $analysis_background_service, $cache_service ); |
|
| 64 | 64 | |
| 65 | - $reconcile_progress_endpoint = new Reconcile_Progress_Endpoint(); |
|
| 66 | - $reconcile_progress_endpoint->register_routes(); |
|
| 65 | + $reconcile_progress_endpoint = new Reconcile_Progress_Endpoint(); |
|
| 66 | + $reconcile_progress_endpoint->register_routes(); |
|
| 67 | 67 | |
| 68 | 68 | |
| 69 | - $term_page_hook = new Term_Page_Hook( $term_data_factory ); |
|
| 70 | - $term_page_hook->connect_hook(); |
|
| 69 | + $term_page_hook = new Term_Page_Hook( $term_data_factory ); |
|
| 70 | + $term_page_hook->connect_hook(); |
|
| 71 | 71 | |
| 72 | - $dashboard_widget = new Term_Matches_Widget( $term_count ); |
|
| 73 | - $dashboard_widget->connect_hook(); |
|
| 72 | + $dashboard_widget = new Term_Matches_Widget( $term_count ); |
|
| 73 | + $dashboard_widget->connect_hook(); |
|
| 74 | 74 | |
| 75 | - $cached_term_count_manager = new Cached_Term_count_Manager(); |
|
| 76 | - $cached_term_count_manager->connect_hook(); |
|
| 75 | + $cached_term_count_manager = new Cached_Term_count_Manager(); |
|
| 76 | + $cached_term_count_manager->connect_hook(); |
|
| 77 | 77 | |
| 78 | - $settings_tab = new Settings_Tab(); |
|
| 79 | - $settings_tab->connect_hook(); |
|
| 78 | + $settings_tab = new Settings_Tab(); |
|
| 79 | + $settings_tab->connect_hook(); |
|
| 80 | 80 | |
| 81 | - } |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | 83 | } |
| 84 | 84 | |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | $configuration_service = \Wordlift_Configuration_Service::get_instance(); |
| 28 | 28 | |
| 29 | 29 | $api_service = new Default_Api_Service( |
| 30 | - apply_filters( 'wl_api_base_url', 'https://api.wordlift.io' ), |
|
| 30 | + apply_filters('wl_api_base_url', 'https://api.wordlift.io'), |
|
| 31 | 31 | 60, |
| 32 | 32 | User_Agent::get_user_agent(), |
| 33 | 33 | $configuration_service->get_key() |
@@ -36,11 +36,11 @@ discard block |
||
| 36 | 36 | $cache_service = Cache_Service_Factory::get_cache_service(); |
| 37 | 37 | |
| 38 | 38 | |
| 39 | - $analysis_service = new Analysis_Service( $api_service, $cache_service ); |
|
| 39 | + $analysis_service = new Analysis_Service($api_service, $cache_service); |
|
| 40 | 40 | |
| 41 | - $term_data_factory = new Term_Data_Factory( $analysis_service ); |
|
| 41 | + $term_data_factory = new Term_Data_Factory($analysis_service); |
|
| 42 | 42 | |
| 43 | - $tag_rest_endpoint = new Tag_Rest_Endpoint( $term_data_factory ); |
|
| 43 | + $tag_rest_endpoint = new Tag_Rest_Endpoint($term_data_factory); |
|
| 44 | 44 | $tag_rest_endpoint->register_routes(); |
| 45 | 45 | |
| 46 | 46 | $entity_rest_endpoint = new Entity_Rest_Endpoint(); |
@@ -52,24 +52,24 @@ discard block |
||
| 52 | 52 | $term_jsonld = new Term_Jsonld(); |
| 53 | 53 | $term_jsonld->init(); |
| 54 | 54 | |
| 55 | - $term_count = Term_Count_Factory::get_instance( Term_Count_Factory::CACHED_TERM_COUNT ); |
|
| 56 | - new Match_Terms( $term_count ); |
|
| 55 | + $term_count = Term_Count_Factory::get_instance(Term_Count_Factory::CACHED_TERM_COUNT); |
|
| 56 | + new Match_Terms($term_count); |
|
| 57 | 57 | |
| 58 | - $analysis_background_service = new Analysis_Background_Service( $analysis_service ); |
|
| 58 | + $analysis_background_service = new Analysis_Background_Service($analysis_service); |
|
| 59 | 59 | |
| 60 | 60 | |
| 61 | - new Tag_Created_Hook( $analysis_background_service ); |
|
| 61 | + new Tag_Created_Hook($analysis_background_service); |
|
| 62 | 62 | |
| 63 | - new Background_Analysis_Endpoint( $analysis_background_service, $cache_service ); |
|
| 63 | + new Background_Analysis_Endpoint($analysis_background_service, $cache_service); |
|
| 64 | 64 | |
| 65 | 65 | $reconcile_progress_endpoint = new Reconcile_Progress_Endpoint(); |
| 66 | 66 | $reconcile_progress_endpoint->register_routes(); |
| 67 | 67 | |
| 68 | 68 | |
| 69 | - $term_page_hook = new Term_Page_Hook( $term_data_factory ); |
|
| 69 | + $term_page_hook = new Term_Page_Hook($term_data_factory); |
|
| 70 | 70 | $term_page_hook->connect_hook(); |
| 71 | 71 | |
| 72 | - $dashboard_widget = new Term_Matches_Widget( $term_count ); |
|
| 72 | + $dashboard_widget = new Term_Matches_Widget($term_count); |
|
| 73 | 73 | $dashboard_widget->connect_hook(); |
| 74 | 74 | |
| 75 | 75 | $cached_term_count_manager = new Cached_Term_count_Manager(); |
@@ -12,16 +12,16 @@ |
||
| 12 | 12 | |
| 13 | 13 | class Jsonld_Utils { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @param $term_id |
|
| 17 | - * |
|
| 18 | - * @return array|array[]|mixed |
|
| 19 | - */ |
|
| 20 | - public static function get_matched_entities_for_term( $term_id ) { |
|
| 15 | + /** |
|
| 16 | + * @param $term_id |
|
| 17 | + * |
|
| 18 | + * @return array|array[]|mixed |
|
| 19 | + */ |
|
| 20 | + public static function get_matched_entities_for_term( $term_id ) { |
|
| 21 | 21 | |
| 22 | - $entity = Entity_List_Factory::get_instance( $term_id ); |
|
| 22 | + $entity = Entity_List_Factory::get_instance( $term_id ); |
|
| 23 | 23 | |
| 24 | - return $entity->get_jsonld_data(); |
|
| 25 | - } |
|
| 24 | + return $entity->get_jsonld_data(); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | 27 | } |
| 28 | 28 | \ No newline at end of file |
@@ -17,9 +17,9 @@ |
||
| 17 | 17 | * |
| 18 | 18 | * @return array|array[]|mixed |
| 19 | 19 | */ |
| 20 | - public static function get_matched_entities_for_term( $term_id ) { |
|
| 20 | + public static function get_matched_entities_for_term($term_id) { |
|
| 21 | 21 | |
| 22 | - $entity = Entity_List_Factory::get_instance( $term_id ); |
|
| 22 | + $entity = Entity_List_Factory::get_instance($term_id); |
|
| 23 | 23 | |
| 24 | 24 | return $entity->get_jsonld_data(); |
| 25 | 25 | } |
@@ -45,7 +45,7 @@ |
||
| 45 | 45 | * Jsonld_Article_Wrapper constructor. |
| 46 | 46 | * |
| 47 | 47 | * @param Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter |
| 48 | - * @param $cached_postid_to_jsonld_converter |
|
| 48 | + * @param \Wordlift_Cached_Post_Converter $cached_postid_to_jsonld_converter |
|
| 49 | 49 | */ |
| 50 | 50 | public function __construct( $post_to_jsonld_converter, $cached_postid_to_jsonld_converter ) { |
| 51 | 51 | |
@@ -6,143 +6,143 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Jsonld_Article_Wrapper { |
| 8 | 8 | |
| 9 | - private static $article_types = array( |
|
| 10 | - 'Article', |
|
| 11 | - 'AdvertiserContentArticle', |
|
| 12 | - 'NewsArticle', |
|
| 13 | - 'AnalysisNewsArticle', |
|
| 14 | - 'AskPublicNewsArticle', |
|
| 15 | - 'BackgroundNewsArticle', |
|
| 16 | - 'OpinionNewsArticle', |
|
| 17 | - 'ReportageNewsArticle', |
|
| 18 | - 'ReviewNewsArticle', |
|
| 19 | - 'Report', |
|
| 20 | - 'SatiricalArticle', |
|
| 21 | - 'ScholarlyArticle', |
|
| 22 | - 'MedicalScholarlyArticle', |
|
| 23 | - 'SocialMediaPosting', |
|
| 24 | - 'BlogPosting', |
|
| 25 | - 'LiveBlogPosting', |
|
| 26 | - 'DiscussionForumPosting', |
|
| 27 | - 'TechArticle', |
|
| 28 | - 'APIReference' |
|
| 29 | - ); |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var Wordlift_Post_To_Jsonld_Converter |
|
| 33 | - */ |
|
| 34 | - private $post_to_jsonld_converter; |
|
| 35 | - /** |
|
| 36 | - * @var \Wordlift_Cached_Post_Converter |
|
| 37 | - */ |
|
| 38 | - private $cached_postid_to_jsonld_converter; |
|
| 39 | - /** |
|
| 40 | - * @var \Wordlift_Entity_Uri_Service |
|
| 41 | - */ |
|
| 42 | - private $entity_uri_service; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Jsonld_Article_Wrapper constructor. |
|
| 46 | - * |
|
| 47 | - * @param Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter |
|
| 48 | - * @param $cached_postid_to_jsonld_converter |
|
| 49 | - */ |
|
| 50 | - public function __construct( $post_to_jsonld_converter, $cached_postid_to_jsonld_converter ) { |
|
| 51 | - |
|
| 52 | - $this->post_to_jsonld_converter = $post_to_jsonld_converter->new_instance_with_filters_disabled(); |
|
| 53 | - |
|
| 54 | - add_filter( 'wl_after_get_jsonld', array( $this, 'after_get_jsonld' ), PHP_INT_MAX - 100, 3 ); |
|
| 55 | - |
|
| 56 | - $this->cached_postid_to_jsonld_converter = $cached_postid_to_jsonld_converter; |
|
| 57 | - |
|
| 58 | - $this->entity_uri_service = \Wordlift_Entity_Uri_Service::get_instance(); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - public function after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 62 | - |
|
| 63 | - if ( Jsonld_Context_Enum::PAGE !== $context || ! is_array( $jsonld ) || ! isset( $jsonld[0] ) |
|
| 64 | - || ! is_array( $jsonld[0] ) ) { |
|
| 65 | - return $jsonld; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - // Copy the 1st array element |
|
| 69 | - $post_jsonld = $jsonld[0]; |
|
| 70 | - |
|
| 71 | - // Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be |
|
| 72 | - // in the schema.org context, i.e. `Article`, not `http://schema.org/Article`. |
|
| 73 | - if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) { |
|
| 74 | - return $jsonld; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - // Convert the post as Article. |
|
| 78 | - $article_jsonld = $this->post_to_jsonld_converter->convert( $post_id ); |
|
| 79 | - |
|
| 80 | - $article_jsonld['@id'] = $post_jsonld['@id'] . '#article'; |
|
| 81 | - // Reset the type, since by default the type assigned via the Entity Type taxonomy is used. |
|
| 82 | - $article_jsonld['@type'] = 'Article'; |
|
| 83 | - $article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] ); |
|
| 84 | - |
|
| 85 | - |
|
| 86 | - // Copy over the URLs. |
|
| 87 | - if ( isset( $post_jsonld['url'] ) ) { |
|
| 88 | - $article_jsonld['url'] = $post_jsonld['url']; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - array_unshift( $jsonld, $article_jsonld ); |
|
| 92 | - |
|
| 93 | - $author_jsonld = $this->get_author_linked_entity( $article_jsonld ); |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * The author entities can be present in graph for some entity types |
|
| 97 | - * for Person and Organization, so check before we add it to graph. |
|
| 98 | - * reference : https://schema.org/author |
|
| 99 | - */ |
|
| 100 | - if ( $author_jsonld && ! $this->is_author_entity_present_in_graph( $jsonld, $article_jsonld['author']['@id'] ) ) { |
|
| 101 | - $jsonld[] = $author_jsonld; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - return $jsonld; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - private function is_article( $schema_types ) { |
|
| 108 | - |
|
| 109 | - $array_intersect = array_intersect( self::$article_types, ( array ) $schema_types ); |
|
| 110 | - |
|
| 111 | - return ! empty( $array_intersect ); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - private function get_author_linked_entity( $article_jsonld ) { |
|
| 115 | - if ( ! array_key_exists( 'author', $article_jsonld ) ) { |
|
| 116 | - return false; |
|
| 117 | - } |
|
| 9 | + private static $article_types = array( |
|
| 10 | + 'Article', |
|
| 11 | + 'AdvertiserContentArticle', |
|
| 12 | + 'NewsArticle', |
|
| 13 | + 'AnalysisNewsArticle', |
|
| 14 | + 'AskPublicNewsArticle', |
|
| 15 | + 'BackgroundNewsArticle', |
|
| 16 | + 'OpinionNewsArticle', |
|
| 17 | + 'ReportageNewsArticle', |
|
| 18 | + 'ReviewNewsArticle', |
|
| 19 | + 'Report', |
|
| 20 | + 'SatiricalArticle', |
|
| 21 | + 'ScholarlyArticle', |
|
| 22 | + 'MedicalScholarlyArticle', |
|
| 23 | + 'SocialMediaPosting', |
|
| 24 | + 'BlogPosting', |
|
| 25 | + 'LiveBlogPosting', |
|
| 26 | + 'DiscussionForumPosting', |
|
| 27 | + 'TechArticle', |
|
| 28 | + 'APIReference' |
|
| 29 | + ); |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var Wordlift_Post_To_Jsonld_Converter |
|
| 33 | + */ |
|
| 34 | + private $post_to_jsonld_converter; |
|
| 35 | + /** |
|
| 36 | + * @var \Wordlift_Cached_Post_Converter |
|
| 37 | + */ |
|
| 38 | + private $cached_postid_to_jsonld_converter; |
|
| 39 | + /** |
|
| 40 | + * @var \Wordlift_Entity_Uri_Service |
|
| 41 | + */ |
|
| 42 | + private $entity_uri_service; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Jsonld_Article_Wrapper constructor. |
|
| 46 | + * |
|
| 47 | + * @param Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter |
|
| 48 | + * @param $cached_postid_to_jsonld_converter |
|
| 49 | + */ |
|
| 50 | + public function __construct( $post_to_jsonld_converter, $cached_postid_to_jsonld_converter ) { |
|
| 51 | + |
|
| 52 | + $this->post_to_jsonld_converter = $post_to_jsonld_converter->new_instance_with_filters_disabled(); |
|
| 53 | + |
|
| 54 | + add_filter( 'wl_after_get_jsonld', array( $this, 'after_get_jsonld' ), PHP_INT_MAX - 100, 3 ); |
|
| 55 | + |
|
| 56 | + $this->cached_postid_to_jsonld_converter = $cached_postid_to_jsonld_converter; |
|
| 57 | + |
|
| 58 | + $this->entity_uri_service = \Wordlift_Entity_Uri_Service::get_instance(); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + public function after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 62 | + |
|
| 63 | + if ( Jsonld_Context_Enum::PAGE !== $context || ! is_array( $jsonld ) || ! isset( $jsonld[0] ) |
|
| 64 | + || ! is_array( $jsonld[0] ) ) { |
|
| 65 | + return $jsonld; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + // Copy the 1st array element |
|
| 69 | + $post_jsonld = $jsonld[0]; |
|
| 70 | + |
|
| 71 | + // Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be |
|
| 72 | + // in the schema.org context, i.e. `Article`, not `http://schema.org/Article`. |
|
| 73 | + if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) { |
|
| 74 | + return $jsonld; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + // Convert the post as Article. |
|
| 78 | + $article_jsonld = $this->post_to_jsonld_converter->convert( $post_id ); |
|
| 79 | + |
|
| 80 | + $article_jsonld['@id'] = $post_jsonld['@id'] . '#article'; |
|
| 81 | + // Reset the type, since by default the type assigned via the Entity Type taxonomy is used. |
|
| 82 | + $article_jsonld['@type'] = 'Article'; |
|
| 83 | + $article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] ); |
|
| 84 | + |
|
| 85 | + |
|
| 86 | + // Copy over the URLs. |
|
| 87 | + if ( isset( $post_jsonld['url'] ) ) { |
|
| 88 | + $article_jsonld['url'] = $post_jsonld['url']; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + array_unshift( $jsonld, $article_jsonld ); |
|
| 92 | + |
|
| 93 | + $author_jsonld = $this->get_author_linked_entity( $article_jsonld ); |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * The author entities can be present in graph for some entity types |
|
| 97 | + * for Person and Organization, so check before we add it to graph. |
|
| 98 | + * reference : https://schema.org/author |
|
| 99 | + */ |
|
| 100 | + if ( $author_jsonld && ! $this->is_author_entity_present_in_graph( $jsonld, $article_jsonld['author']['@id'] ) ) { |
|
| 101 | + $jsonld[] = $author_jsonld; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + return $jsonld; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + private function is_article( $schema_types ) { |
|
| 108 | + |
|
| 109 | + $array_intersect = array_intersect( self::$article_types, ( array ) $schema_types ); |
|
| 110 | + |
|
| 111 | + return ! empty( $array_intersect ); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + private function get_author_linked_entity( $article_jsonld ) { |
|
| 115 | + if ( ! array_key_exists( 'author', $article_jsonld ) ) { |
|
| 116 | + return false; |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - $author = $article_jsonld['author']; |
|
| 119 | + $author = $article_jsonld['author']; |
|
| 120 | 120 | |
| 121 | - if ( count( array_keys( $author ) ) !== 1 || ! array_key_exists( '@id', $author ) ) { |
|
| 122 | - return false; |
|
| 123 | - } |
|
| 121 | + if ( count( array_keys( $author ) ) !== 1 || ! array_key_exists( '@id', $author ) ) { |
|
| 122 | + return false; |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - $author_linked_entity_id = $author['@id']; |
|
| 125 | + $author_linked_entity_id = $author['@id']; |
|
| 126 | 126 | |
| 127 | - $author_entity_post = $this->entity_uri_service->get_entity( $author_linked_entity_id ); |
|
| 127 | + $author_entity_post = $this->entity_uri_service->get_entity( $author_linked_entity_id ); |
|
| 128 | 128 | |
| 129 | - if ( ! $author_entity_post instanceof \WP_Post ) { |
|
| 130 | - return false; |
|
| 131 | - } |
|
| 129 | + if ( ! $author_entity_post instanceof \WP_Post ) { |
|
| 130 | + return false; |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - return $this->cached_postid_to_jsonld_converter->convert( $author_entity_post->ID ); |
|
| 133 | + return $this->cached_postid_to_jsonld_converter->convert( $author_entity_post->ID ); |
|
| 134 | 134 | |
| 135 | - } |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - private function is_author_entity_present_in_graph( $jsonld, $author_entity_id ) { |
|
| 137 | + private function is_author_entity_present_in_graph( $jsonld, $author_entity_id ) { |
|
| 138 | 138 | |
| 139 | - foreach ( $jsonld as $item ) { |
|
| 140 | - if ( $item && array_key_exists( '@id', $item ) && $item['@id'] === $author_entity_id ) { |
|
| 141 | - return true; |
|
| 142 | - } |
|
| 143 | - } |
|
| 139 | + foreach ( $jsonld as $item ) { |
|
| 140 | + if ( $item && array_key_exists( '@id', $item ) && $item['@id'] === $author_entity_id ) { |
|
| 141 | + return true; |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - return false; |
|
| 146 | - } |
|
| 145 | + return false; |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | 148 | } |
| 149 | 149 | \ No newline at end of file |
@@ -47,21 +47,21 @@ discard block |
||
| 47 | 47 | * @param Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter |
| 48 | 48 | * @param $cached_postid_to_jsonld_converter |
| 49 | 49 | */ |
| 50 | - public function __construct( $post_to_jsonld_converter, $cached_postid_to_jsonld_converter ) { |
|
| 50 | + public function __construct($post_to_jsonld_converter, $cached_postid_to_jsonld_converter) { |
|
| 51 | 51 | |
| 52 | 52 | $this->post_to_jsonld_converter = $post_to_jsonld_converter->new_instance_with_filters_disabled(); |
| 53 | 53 | |
| 54 | - add_filter( 'wl_after_get_jsonld', array( $this, 'after_get_jsonld' ), PHP_INT_MAX - 100, 3 ); |
|
| 54 | + add_filter('wl_after_get_jsonld', array($this, 'after_get_jsonld'), PHP_INT_MAX - 100, 3); |
|
| 55 | 55 | |
| 56 | 56 | $this->cached_postid_to_jsonld_converter = $cached_postid_to_jsonld_converter; |
| 57 | 57 | |
| 58 | 58 | $this->entity_uri_service = \Wordlift_Entity_Uri_Service::get_instance(); |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | - public function after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 61 | + public function after_get_jsonld($jsonld, $post_id, $context) { |
|
| 62 | 62 | |
| 63 | - if ( Jsonld_Context_Enum::PAGE !== $context || ! is_array( $jsonld ) || ! isset( $jsonld[0] ) |
|
| 64 | - || ! is_array( $jsonld[0] ) ) { |
|
| 63 | + if (Jsonld_Context_Enum::PAGE !== $context || ! is_array($jsonld) || ! isset($jsonld[0]) |
|
| 64 | + || ! is_array($jsonld[0])) { |
|
| 65 | 65 | return $jsonld; |
| 66 | 66 | } |
| 67 | 67 | |
@@ -70,74 +70,74 @@ discard block |
||
| 70 | 70 | |
| 71 | 71 | // Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be |
| 72 | 72 | // in the schema.org context, i.e. `Article`, not `http://schema.org/Article`. |
| 73 | - if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) { |
|
| 73 | + if ( ! isset($post_jsonld['@id']) || ! isset($post_jsonld['@type']) || $this->is_article($post_jsonld['@type'])) { |
|
| 74 | 74 | return $jsonld; |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | // Convert the post as Article. |
| 78 | - $article_jsonld = $this->post_to_jsonld_converter->convert( $post_id ); |
|
| 78 | + $article_jsonld = $this->post_to_jsonld_converter->convert($post_id); |
|
| 79 | 79 | |
| 80 | - $article_jsonld['@id'] = $post_jsonld['@id'] . '#article'; |
|
| 80 | + $article_jsonld['@id'] = $post_jsonld['@id'].'#article'; |
|
| 81 | 81 | // Reset the type, since by default the type assigned via the Entity Type taxonomy is used. |
| 82 | 82 | $article_jsonld['@type'] = 'Article'; |
| 83 | - $article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] ); |
|
| 83 | + $article_jsonld['about'] = array('@id' => $post_jsonld['@id']); |
|
| 84 | 84 | |
| 85 | 85 | |
| 86 | 86 | // Copy over the URLs. |
| 87 | - if ( isset( $post_jsonld['url'] ) ) { |
|
| 87 | + if (isset($post_jsonld['url'])) { |
|
| 88 | 88 | $article_jsonld['url'] = $post_jsonld['url']; |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | - array_unshift( $jsonld, $article_jsonld ); |
|
| 91 | + array_unshift($jsonld, $article_jsonld); |
|
| 92 | 92 | |
| 93 | - $author_jsonld = $this->get_author_linked_entity( $article_jsonld ); |
|
| 93 | + $author_jsonld = $this->get_author_linked_entity($article_jsonld); |
|
| 94 | 94 | |
| 95 | 95 | /** |
| 96 | 96 | * The author entities can be present in graph for some entity types |
| 97 | 97 | * for Person and Organization, so check before we add it to graph. |
| 98 | 98 | * reference : https://schema.org/author |
| 99 | 99 | */ |
| 100 | - if ( $author_jsonld && ! $this->is_author_entity_present_in_graph( $jsonld, $article_jsonld['author']['@id'] ) ) { |
|
| 100 | + if ($author_jsonld && ! $this->is_author_entity_present_in_graph($jsonld, $article_jsonld['author']['@id'])) { |
|
| 101 | 101 | $jsonld[] = $author_jsonld; |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | return $jsonld; |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | - private function is_article( $schema_types ) { |
|
| 107 | + private function is_article($schema_types) { |
|
| 108 | 108 | |
| 109 | - $array_intersect = array_intersect( self::$article_types, ( array ) $schema_types ); |
|
| 109 | + $array_intersect = array_intersect(self::$article_types, (array) $schema_types); |
|
| 110 | 110 | |
| 111 | - return ! empty( $array_intersect ); |
|
| 111 | + return ! empty($array_intersect); |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | - private function get_author_linked_entity( $article_jsonld ) { |
|
| 115 | - if ( ! array_key_exists( 'author', $article_jsonld ) ) { |
|
| 114 | + private function get_author_linked_entity($article_jsonld) { |
|
| 115 | + if ( ! array_key_exists('author', $article_jsonld)) { |
|
| 116 | 116 | return false; |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | $author = $article_jsonld['author']; |
| 120 | 120 | |
| 121 | - if ( count( array_keys( $author ) ) !== 1 || ! array_key_exists( '@id', $author ) ) { |
|
| 121 | + if (count(array_keys($author)) !== 1 || ! array_key_exists('@id', $author)) { |
|
| 122 | 122 | return false; |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | $author_linked_entity_id = $author['@id']; |
| 126 | 126 | |
| 127 | - $author_entity_post = $this->entity_uri_service->get_entity( $author_linked_entity_id ); |
|
| 127 | + $author_entity_post = $this->entity_uri_service->get_entity($author_linked_entity_id); |
|
| 128 | 128 | |
| 129 | - if ( ! $author_entity_post instanceof \WP_Post ) { |
|
| 129 | + if ( ! $author_entity_post instanceof \WP_Post) { |
|
| 130 | 130 | return false; |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | - return $this->cached_postid_to_jsonld_converter->convert( $author_entity_post->ID ); |
|
| 133 | + return $this->cached_postid_to_jsonld_converter->convert($author_entity_post->ID); |
|
| 134 | 134 | |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | - private function is_author_entity_present_in_graph( $jsonld, $author_entity_id ) { |
|
| 137 | + private function is_author_entity_present_in_graph($jsonld, $author_entity_id) { |
|
| 138 | 138 | |
| 139 | - foreach ( $jsonld as $item ) { |
|
| 140 | - if ( $item && array_key_exists( '@id', $item ) && $item['@id'] === $author_entity_id ) { |
|
| 139 | + foreach ($jsonld as $item) { |
|
| 140 | + if ($item && array_key_exists('@id', $item) && $item['@id'] === $author_entity_id) { |
|
| 141 | 141 | return true; |
| 142 | 142 | } |
| 143 | 143 | } |
@@ -18,17 +18,17 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | interface Wordlift_Post_Converter { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Convert the specified post id. |
|
| 23 | - * |
|
| 24 | - * @param int $post_id The post id. |
|
| 25 | - * @param array $references An array of posts referenced by the specified post. |
|
| 26 | - * @param array $references_infos |
|
| 27 | - * |
|
| 28 | - * @return mixed The conversion result. |
|
| 29 | - * @since 3.16.0 $references argument added. |
|
| 30 | - * @since 3.10.0 |
|
| 31 | - */ |
|
| 32 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ); |
|
| 21 | + /** |
|
| 22 | + * Convert the specified post id. |
|
| 23 | + * |
|
| 24 | + * @param int $post_id The post id. |
|
| 25 | + * @param array $references An array of posts referenced by the specified post. |
|
| 26 | + * @param array $references_infos |
|
| 27 | + * |
|
| 28 | + * @return mixed The conversion result. |
|
| 29 | + * @since 3.16.0 $references argument added. |
|
| 30 | + * @since 3.10.0 |
|
| 31 | + */ |
|
| 32 | + public function convert( $post_id, &$references = array(), &$references_infos = array() ); |
|
| 33 | 33 | |
| 34 | 34 | } |
@@ -29,6 +29,6 @@ |
||
| 29 | 29 | * @since 3.16.0 $references argument added. |
| 30 | 30 | * @since 3.10.0 |
| 31 | 31 | */ |
| 32 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ); |
|
| 32 | + public function convert($post_id, &$references = array(), &$references_infos = array()); |
|
| 33 | 33 | |
| 34 | 34 | } |
@@ -15,468 +15,468 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Wordlift_Post_To_Jsonld_Converter extends Wordlift_Abstract_Post_To_Jsonld_Converter { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var Wordlift_Post_To_Jsonld_Converter |
|
| 20 | - */ |
|
| 21 | - private static $instance; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * A {@link Wordlift_Configuration_Service} instance. |
|
| 25 | - * |
|
| 26 | - * @since 3.10.0 |
|
| 27 | - * @access private |
|
| 28 | - * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 29 | - */ |
|
| 30 | - private $configuration_service; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * A {@link Wordlift_Log_Service} instance. |
|
| 34 | - * |
|
| 35 | - * @since 3.10.0 |
|
| 36 | - * @access private |
|
| 37 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 38 | - */ |
|
| 39 | - private $log; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var false |
|
| 43 | - */ |
|
| 44 | - private $disable_convert_filters; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 48 | - * |
|
| 49 | - * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 50 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 51 | - * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 52 | - * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 53 | - * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 54 | - * |
|
| 55 | - * @since 3.10.0 |
|
| 56 | - * |
|
| 57 | - */ |
|
| 58 | - public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service, $disable_convert_filters = false ) { |
|
| 59 | - parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 60 | - |
|
| 61 | - $this->configuration_service = $configuration_service; |
|
| 62 | - $this->disable_convert_filters = $disable_convert_filters; |
|
| 63 | - |
|
| 64 | - // Set a reference to the logger. |
|
| 65 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 66 | - |
|
| 67 | - self::$instance = $this; |
|
| 68 | - |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - public static function get_instance() { |
|
| 72 | - |
|
| 73 | - return self::$instance; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - public function new_instance_with_filters_disabled() { |
|
| 77 | - return new static( $this->entity_type_service, $this->entity_service, $this->user_service, $this->attachment_service, $this->configuration_service, true ); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 82 | - * found while processing the post is set in the $references array. |
|
| 83 | - * |
|
| 84 | - * @param int $post_id The post id. |
|
| 85 | - * @param array $references An array of entity references. |
|
| 86 | - * @param array $references_infos |
|
| 87 | - * |
|
| 88 | - * @return array A JSON-LD array. |
|
| 89 | - * @since 3.10.0 |
|
| 90 | - */ |
|
| 91 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 92 | - |
|
| 93 | - // Get the post instance. |
|
| 94 | - if ( null === $post = get_post( $post_id ) ) { |
|
| 95 | - // Post not found. |
|
| 96 | - return null; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - // Get the base JSON-LD and the list of entities referenced by this entity. |
|
| 100 | - $jsonld = parent::convert( $post_id, $references, $references_infos ); |
|
| 101 | - |
|
| 102 | - // Set WebPage by default. |
|
| 103 | - if ( empty( $jsonld['@type'] ) ) { |
|
| 104 | - $jsonld['@type'] = 'WebPage'; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - // Get the entity name. |
|
| 108 | - $jsonld['headline'] = $post->post_title; |
|
| 109 | - |
|
| 110 | - // Set the published and modified dates. |
|
| 111 | - /* |
|
| 18 | + /** |
|
| 19 | + * @var Wordlift_Post_To_Jsonld_Converter |
|
| 20 | + */ |
|
| 21 | + private static $instance; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * A {@link Wordlift_Configuration_Service} instance. |
|
| 25 | + * |
|
| 26 | + * @since 3.10.0 |
|
| 27 | + * @access private |
|
| 28 | + * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 29 | + */ |
|
| 30 | + private $configuration_service; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * A {@link Wordlift_Log_Service} instance. |
|
| 34 | + * |
|
| 35 | + * @since 3.10.0 |
|
| 36 | + * @access private |
|
| 37 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 38 | + */ |
|
| 39 | + private $log; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var false |
|
| 43 | + */ |
|
| 44 | + private $disable_convert_filters; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 48 | + * |
|
| 49 | + * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 50 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 51 | + * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 52 | + * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 53 | + * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 54 | + * |
|
| 55 | + * @since 3.10.0 |
|
| 56 | + * |
|
| 57 | + */ |
|
| 58 | + public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service, $disable_convert_filters = false ) { |
|
| 59 | + parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 60 | + |
|
| 61 | + $this->configuration_service = $configuration_service; |
|
| 62 | + $this->disable_convert_filters = $disable_convert_filters; |
|
| 63 | + |
|
| 64 | + // Set a reference to the logger. |
|
| 65 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 66 | + |
|
| 67 | + self::$instance = $this; |
|
| 68 | + |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + public static function get_instance() { |
|
| 72 | + |
|
| 73 | + return self::$instance; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + public function new_instance_with_filters_disabled() { |
|
| 77 | + return new static( $this->entity_type_service, $this->entity_service, $this->user_service, $this->attachment_service, $this->configuration_service, true ); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 82 | + * found while processing the post is set in the $references array. |
|
| 83 | + * |
|
| 84 | + * @param int $post_id The post id. |
|
| 85 | + * @param array $references An array of entity references. |
|
| 86 | + * @param array $references_infos |
|
| 87 | + * |
|
| 88 | + * @return array A JSON-LD array. |
|
| 89 | + * @since 3.10.0 |
|
| 90 | + */ |
|
| 91 | + public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 92 | + |
|
| 93 | + // Get the post instance. |
|
| 94 | + if ( null === $post = get_post( $post_id ) ) { |
|
| 95 | + // Post not found. |
|
| 96 | + return null; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + // Get the base JSON-LD and the list of entities referenced by this entity. |
|
| 100 | + $jsonld = parent::convert( $post_id, $references, $references_infos ); |
|
| 101 | + |
|
| 102 | + // Set WebPage by default. |
|
| 103 | + if ( empty( $jsonld['@type'] ) ) { |
|
| 104 | + $jsonld['@type'] = 'WebPage'; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + // Get the entity name. |
|
| 108 | + $jsonld['headline'] = $post->post_title; |
|
| 109 | + |
|
| 110 | + // Set the published and modified dates. |
|
| 111 | + /* |
|
| 112 | 112 | * Set the `datePublished` and `dateModified` using the local timezone. |
| 113 | 113 | * |
| 114 | 114 | * @see https://github.com/insideout10/wordlift-plugin/issues/887 |
| 115 | 115 | * |
| 116 | 116 | * @since 3.20.0 |
| 117 | 117 | */ |
| 118 | - try { |
|
| 119 | - $default_timezone = date_default_timezone_get(); |
|
| 120 | - $timezone = get_option( 'timezone_string' ); |
|
| 121 | - if ( ! empty( $timezone ) ) { |
|
| 122 | - date_default_timezone_set( $timezone ); |
|
| 123 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 124 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 125 | - date_default_timezone_set( $default_timezone ); |
|
| 126 | - } else { |
|
| 127 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 128 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 129 | - } |
|
| 130 | - } catch ( Exception $e ) { |
|
| 131 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 132 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - // Get the word count for the post. |
|
| 136 | - /* |
|
| 118 | + try { |
|
| 119 | + $default_timezone = date_default_timezone_get(); |
|
| 120 | + $timezone = get_option( 'timezone_string' ); |
|
| 121 | + if ( ! empty( $timezone ) ) { |
|
| 122 | + date_default_timezone_set( $timezone ); |
|
| 123 | + $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 124 | + $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 125 | + date_default_timezone_set( $default_timezone ); |
|
| 126 | + } else { |
|
| 127 | + $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 128 | + $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 129 | + } |
|
| 130 | + } catch ( Exception $e ) { |
|
| 131 | + $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 132 | + $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + // Get the word count for the post. |
|
| 136 | + /* |
|
| 137 | 137 | * Do not display the `wordCount` on a `WebPage`. |
| 138 | 138 | * |
| 139 | 139 | * @see https://github.com/insideout10/wordlift-plugin/issues/888 |
| 140 | 140 | * |
| 141 | 141 | * @since 3.20.0 |
| 142 | 142 | */ |
| 143 | - if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 144 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 145 | - $jsonld['wordCount'] = $post_adapter->word_count(); |
|
| 146 | - } |
|
| 143 | + if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 144 | + $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 145 | + $jsonld['wordCount'] = $post_adapter->word_count(); |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - /* |
|
| 148 | + /* |
|
| 149 | 149 | * Add keywords, articleSection, commentCount and inLanguage properties to `Article` JSON-LD |
| 150 | 150 | * |
| 151 | 151 | * @see https://github.com/insideout10/wordlift-plugin/issues/1140 |
| 152 | 152 | * |
| 153 | 153 | * @since 3.27.2 |
| 154 | 154 | */ |
| 155 | - if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 156 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 157 | - $keywords = $post_adapter->keywords(); |
|
| 158 | - $article_section = $post_adapter->article_section(); |
|
| 159 | - $comment_count = $post_adapter->comment_count(); |
|
| 160 | - $locale = $post_adapter->locale(); |
|
| 161 | - |
|
| 162 | - if ( isset( $keywords ) ) { |
|
| 163 | - $jsonld['keywords'] = $keywords; |
|
| 164 | - } |
|
| 165 | - if ( isset( $article_section ) ) { |
|
| 166 | - $jsonld['articleSection'] = $article_section; |
|
| 167 | - } |
|
| 168 | - $jsonld['commentCount'] = $comment_count; |
|
| 169 | - $jsonld['inLanguage'] = $locale; |
|
| 170 | - $post_adapter->add_mentions( $post_id, $references ); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - // Set the publisher. |
|
| 174 | - $this->set_publisher( $jsonld ); |
|
| 175 | - |
|
| 176 | - // Process the references if any. |
|
| 177 | - if ( 0 < count( $references ) ) { |
|
| 178 | - |
|
| 179 | - // Prepare the `about` and `mentions` array. |
|
| 180 | - $about = $mentions = array(); |
|
| 181 | - |
|
| 182 | - // If the entity is in the title, then it should be an `about`. |
|
| 183 | - foreach ( $references as $reference ) { |
|
| 184 | - |
|
| 185 | - // Get the entity labels. |
|
| 186 | - $labels = $this->entity_service->get_labels( $reference ); |
|
| 187 | - |
|
| 188 | - // Get the entity URI. |
|
| 189 | - $item = array( |
|
| 190 | - '@id' => $this->entity_service->get_uri( $reference ), |
|
| 191 | - ); |
|
| 192 | - |
|
| 193 | - $escaped_labels = array_map( function ( $value ) { |
|
| 194 | - return preg_quote( $value, '/' ); |
|
| 195 | - }, $labels ); |
|
| 196 | - |
|
| 197 | - // Check if the labels match any part of the title. |
|
| 198 | - $matches = 1 === preg_match( '/' . implode( '|', $escaped_labels ) . '/', $post->post_title ); |
|
| 199 | - |
|
| 200 | - // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
| 201 | - if ( $matches ) { |
|
| 202 | - $about[] = $item; |
|
| 203 | - } else { |
|
| 204 | - $mentions[] = $item; |
|
| 205 | - } |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - // If we have abouts, assign them to the JSON-LD. |
|
| 209 | - if ( 0 < count( $about ) ) { |
|
| 210 | - $jsonld['about'] = $about; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - // If we have mentions, assign them to the JSON-LD. |
|
| 214 | - if ( 0 < count( $mentions ) ) { |
|
| 215 | - $jsonld['mentions'] = $mentions; |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - // Finally set the author. |
|
| 220 | - $jsonld['author'] = $this->get_author( $post->post_author, $references ); |
|
| 221 | - |
|
| 222 | - // Return the JSON-LD if filters are disabled by the client. |
|
| 223 | - if ( $this->disable_convert_filters ) { |
|
| 224 | - return $jsonld; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references. |
|
| 229 | - * |
|
| 230 | - * @param array $value { |
|
| 231 | - * |
|
| 232 | - * @type array $jsonld The JSON-LD structure. |
|
| 233 | - * @type int[] $references An array of post IDs. |
|
| 234 | - * } |
|
| 235 | - * @since 3.25.0 |
|
| 236 | - * |
|
| 237 | - * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ |
|
| 238 | - * |
|
| 239 | - * @api |
|
| 240 | - */ |
|
| 241 | - $ret_val = apply_filters( 'wl_post_jsonld_array', array( |
|
| 242 | - 'jsonld' => $jsonld, |
|
| 243 | - 'references' => $references, |
|
| 244 | - ), $post_id ); |
|
| 245 | - |
|
| 246 | - $jsonld = $ret_val['jsonld']; |
|
| 247 | - $references = $ret_val['references']; |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Call the `wl_post_jsonld` filter. |
|
| 251 | - * |
|
| 252 | - * @param array $jsonld The JSON-LD structure. |
|
| 253 | - * @param int $post_id The {@link WP_Post} `id`. |
|
| 254 | - * @param array $references The array of referenced entities. |
|
| 255 | - * |
|
| 256 | - * @since 3.14.0 |
|
| 257 | - * |
|
| 258 | - * @api |
|
| 259 | - */ |
|
| 260 | - return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references ); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Get the author's JSON-LD fragment. |
|
| 265 | - * |
|
| 266 | - * The JSON-LD fragment is generated using the {@link WP_User}'s data or |
|
| 267 | - * the referenced entity if configured for the {@link WP_User}. |
|
| 268 | - * |
|
| 269 | - * @param int $author_id The author {@link WP_User}'s `id`. |
|
| 270 | - * @param array $references An array of referenced entities. |
|
| 271 | - * |
|
| 272 | - * @return string|array A JSON-LD structure. |
|
| 273 | - * @since 3.14.0 |
|
| 274 | - * |
|
| 275 | - */ |
|
| 276 | - private function get_author( $author_id, &$references ) { |
|
| 277 | - |
|
| 278 | - // Get the entity bound to this user. |
|
| 279 | - $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 280 | - |
|
| 281 | - // If there's no entity bound return a simple author structure. |
|
| 282 | - if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) { |
|
| 283 | - |
|
| 284 | - $author = get_the_author_meta( 'display_name', $author_id ); |
|
| 285 | - $author_first_name = get_the_author_meta( 'first_name', $author_id ); |
|
| 286 | - $author_last_name = get_the_author_meta( 'last_name', $author_id ); |
|
| 287 | - $author_uri = $this->user_service->get_uri( $author_id ); |
|
| 288 | - |
|
| 289 | - return array( |
|
| 290 | - '@type' => 'Person', |
|
| 291 | - '@id' => $author_uri, |
|
| 292 | - 'name' => $author, |
|
| 293 | - 'givenName' => $author_first_name, |
|
| 294 | - 'familyName' => $author_last_name |
|
| 295 | - ); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - // Add the author to the references. |
|
| 299 | - $author_uri = $this->entity_service->get_uri( $entity_id ); |
|
| 300 | - $references[] = $entity_id; |
|
| 301 | - |
|
| 302 | - // Return the JSON-LD for the referenced entity. |
|
| 303 | - return array( |
|
| 304 | - '@id' => $author_uri, |
|
| 305 | - ); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Enrich the provided params array with publisher data, if available. |
|
| 310 | - * |
|
| 311 | - * @param array $params The parameters array. |
|
| 312 | - * |
|
| 313 | - * @since 3.10.0 |
|
| 314 | - * |
|
| 315 | - */ |
|
| 316 | - protected function set_publisher( &$params ) { |
|
| 317 | - |
|
| 318 | - // If the publisher id isn't set don't do anything. |
|
| 319 | - if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) { |
|
| 320 | - return; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - // Get the post instance. |
|
| 324 | - if ( null === $post = get_post( $publisher_id ) ) { |
|
| 325 | - // Publisher not found. |
|
| 326 | - return; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - // Get the item id. |
|
| 330 | - $id = $this->entity_service->get_uri( $publisher_id ); |
|
| 331 | - |
|
| 332 | - // Get the type. |
|
| 333 | - $type = $this->entity_type_service->get( $publisher_id ); |
|
| 334 | - |
|
| 335 | - // Get the name. |
|
| 336 | - $name = $post->post_title; |
|
| 337 | - |
|
| 338 | - // Set the publisher data. |
|
| 339 | - $params['publisher'] = array( |
|
| 340 | - '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 341 | - '@id' => $id, |
|
| 342 | - 'name' => $name, |
|
| 343 | - ); |
|
| 344 | - |
|
| 345 | - // Add the sameAs values associated with the publisher. |
|
| 346 | - $storage_factory = Wordlift_Storage_Factory::get_instance(); |
|
| 347 | - $sameas = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id ); |
|
| 348 | - if ( ! empty( $sameas ) ) { |
|
| 349 | - $params['publisher']['sameAs'] = $sameas; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - // Set the logo, only for http://schema.org/Organization as Person doesn't |
|
| 353 | - // support the logo property. |
|
| 354 | - // |
|
| 355 | - // See http://schema.org/logo. |
|
| 356 | - if ( 1 !== preg_match( '~Organization$~', $type['uri'] ) ) { |
|
| 357 | - return; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - // Get the publisher logo. |
|
| 361 | - $publisher_logo = $this->get_publisher_logo( $post->ID ); |
|
| 362 | - |
|
| 363 | - // Bail out if the publisher logo isn't set. |
|
| 364 | - if ( false === $publisher_logo ) { |
|
| 365 | - return; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - // Copy over some useful properties. |
|
| 369 | - // |
|
| 370 | - // See https://developers.google.com/search/docs/data-types/articles. |
|
| 371 | - $params['publisher']['logo']['@type'] = 'ImageObject'; |
|
| 372 | - $params['publisher']['logo']['url'] = $publisher_logo['url']; |
|
| 373 | - |
|
| 374 | - // If you specify a "width" or "height" value you should leave out |
|
| 375 | - // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 376 | - // |
|
| 377 | - // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 378 | - $params['publisher']['logo']['width'] = $publisher_logo['width']; |
|
| 379 | - $params['publisher']['logo']['height'] = $publisher_logo['height']; |
|
| 380 | - |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * Get the publisher logo structure. |
|
| 385 | - * |
|
| 386 | - * The function returns false when the publisher logo cannot be determined, i.e.: |
|
| 387 | - * - the post has no featured image. |
|
| 388 | - * - the featured image has no file. |
|
| 389 | - * - a wp_image_editor instance cannot be instantiated on the original file or on the publisher logo file. |
|
| 390 | - * |
|
| 391 | - * @param int $post_id The post id. |
|
| 392 | - * |
|
| 393 | - * @return array|false Returns an array with the `url`, `width` and `height` for the publisher logo or false in case |
|
| 394 | - * of errors. |
|
| 395 | - * @since 3.19.2 |
|
| 396 | - * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue. |
|
| 397 | - * |
|
| 398 | - */ |
|
| 399 | - private function get_publisher_logo( $post_id ) { |
|
| 400 | - |
|
| 401 | - // Get the featured image for the post. |
|
| 402 | - $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 403 | - |
|
| 404 | - // Bail out if thumbnail not available. |
|
| 405 | - if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) { |
|
| 406 | - $this->log->info( "Featured image not set for post $post_id." ); |
|
| 407 | - |
|
| 408 | - return false; |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - // Get the uploads base URL. |
|
| 412 | - $uploads_dir = wp_upload_dir(); |
|
| 413 | - |
|
| 414 | - // Get the attachment metadata. |
|
| 415 | - $metadata = wp_get_attachment_metadata( $thumbnail_id ); |
|
| 416 | - |
|
| 417 | - // Bail out if the file isn't set. |
|
| 418 | - if ( ! isset( $metadata['file'] ) ) { |
|
| 419 | - $this->log->warn( "Featured image file not found for post $post_id." ); |
|
| 420 | - |
|
| 421 | - return false; |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - // Retrieve the relative filename, e.g. "2018/05/logo_publisher.png" |
|
| 425 | - $path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file']; |
|
| 426 | - |
|
| 427 | - // Use image src, if local file does not exist. @see https://github.com/insideout10/wordlift-plugin/issues/1149 |
|
| 428 | - if ( ! file_exists( $path ) ) { |
|
| 429 | - $this->log->warn( "Featured image file $path doesn't exist for post $post_id." ); |
|
| 430 | - |
|
| 431 | - $attachment_image_src = wp_get_attachment_image_src( $thumbnail_id, '' ); |
|
| 432 | - if ( $attachment_image_src ) { |
|
| 433 | - return array( |
|
| 434 | - 'url' => $attachment_image_src[0], |
|
| 435 | - 'width' => $attachment_image_src[1], |
|
| 436 | - 'height' => $attachment_image_src[2], |
|
| 437 | - ); |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - // Bail out if we cant fetch wp_get_attachment_image_src |
|
| 441 | - return false; |
|
| 442 | - |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 446 | - $original_file_editor = wp_get_image_editor( $path ); |
|
| 447 | - if ( is_wp_error( $original_file_editor ) ) { |
|
| 448 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." ); |
|
| 449 | - |
|
| 450 | - return false; |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - // Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling |
|
| 454 | - // and we don't actually know the end values. |
|
| 455 | - $publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' ); |
|
| 456 | - |
|
| 457 | - // If the file doesn't exist yet, create it. |
|
| 458 | - if ( ! file_exists( $publisher_logo_path ) ) { |
|
| 459 | - $original_file_editor->resize( 600, 60 ); |
|
| 460 | - $original_file_editor->save( $publisher_logo_path ); |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 464 | - $publisher_logo_editor = wp_get_image_editor( $publisher_logo_path ); |
|
| 465 | - if ( is_wp_error( $publisher_logo_editor ) ) { |
|
| 466 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." ); |
|
| 467 | - |
|
| 468 | - return false; |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - // Get the actual size. |
|
| 472 | - $size = $publisher_logo_editor->get_size(); |
|
| 473 | - |
|
| 474 | - // Finally return the array with data. |
|
| 475 | - return array( |
|
| 476 | - 'url' => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ), |
|
| 477 | - 'width' => $size['width'], |
|
| 478 | - 'height' => $size['height'], |
|
| 479 | - ); |
|
| 480 | - } |
|
| 155 | + if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 156 | + $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 157 | + $keywords = $post_adapter->keywords(); |
|
| 158 | + $article_section = $post_adapter->article_section(); |
|
| 159 | + $comment_count = $post_adapter->comment_count(); |
|
| 160 | + $locale = $post_adapter->locale(); |
|
| 161 | + |
|
| 162 | + if ( isset( $keywords ) ) { |
|
| 163 | + $jsonld['keywords'] = $keywords; |
|
| 164 | + } |
|
| 165 | + if ( isset( $article_section ) ) { |
|
| 166 | + $jsonld['articleSection'] = $article_section; |
|
| 167 | + } |
|
| 168 | + $jsonld['commentCount'] = $comment_count; |
|
| 169 | + $jsonld['inLanguage'] = $locale; |
|
| 170 | + $post_adapter->add_mentions( $post_id, $references ); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + // Set the publisher. |
|
| 174 | + $this->set_publisher( $jsonld ); |
|
| 175 | + |
|
| 176 | + // Process the references if any. |
|
| 177 | + if ( 0 < count( $references ) ) { |
|
| 178 | + |
|
| 179 | + // Prepare the `about` and `mentions` array. |
|
| 180 | + $about = $mentions = array(); |
|
| 181 | + |
|
| 182 | + // If the entity is in the title, then it should be an `about`. |
|
| 183 | + foreach ( $references as $reference ) { |
|
| 184 | + |
|
| 185 | + // Get the entity labels. |
|
| 186 | + $labels = $this->entity_service->get_labels( $reference ); |
|
| 187 | + |
|
| 188 | + // Get the entity URI. |
|
| 189 | + $item = array( |
|
| 190 | + '@id' => $this->entity_service->get_uri( $reference ), |
|
| 191 | + ); |
|
| 192 | + |
|
| 193 | + $escaped_labels = array_map( function ( $value ) { |
|
| 194 | + return preg_quote( $value, '/' ); |
|
| 195 | + }, $labels ); |
|
| 196 | + |
|
| 197 | + // Check if the labels match any part of the title. |
|
| 198 | + $matches = 1 === preg_match( '/' . implode( '|', $escaped_labels ) . '/', $post->post_title ); |
|
| 199 | + |
|
| 200 | + // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
| 201 | + if ( $matches ) { |
|
| 202 | + $about[] = $item; |
|
| 203 | + } else { |
|
| 204 | + $mentions[] = $item; |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + // If we have abouts, assign them to the JSON-LD. |
|
| 209 | + if ( 0 < count( $about ) ) { |
|
| 210 | + $jsonld['about'] = $about; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + // If we have mentions, assign them to the JSON-LD. |
|
| 214 | + if ( 0 < count( $mentions ) ) { |
|
| 215 | + $jsonld['mentions'] = $mentions; |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + // Finally set the author. |
|
| 220 | + $jsonld['author'] = $this->get_author( $post->post_author, $references ); |
|
| 221 | + |
|
| 222 | + // Return the JSON-LD if filters are disabled by the client. |
|
| 223 | + if ( $this->disable_convert_filters ) { |
|
| 224 | + return $jsonld; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references. |
|
| 229 | + * |
|
| 230 | + * @param array $value { |
|
| 231 | + * |
|
| 232 | + * @type array $jsonld The JSON-LD structure. |
|
| 233 | + * @type int[] $references An array of post IDs. |
|
| 234 | + * } |
|
| 235 | + * @since 3.25.0 |
|
| 236 | + * |
|
| 237 | + * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ |
|
| 238 | + * |
|
| 239 | + * @api |
|
| 240 | + */ |
|
| 241 | + $ret_val = apply_filters( 'wl_post_jsonld_array', array( |
|
| 242 | + 'jsonld' => $jsonld, |
|
| 243 | + 'references' => $references, |
|
| 244 | + ), $post_id ); |
|
| 245 | + |
|
| 246 | + $jsonld = $ret_val['jsonld']; |
|
| 247 | + $references = $ret_val['references']; |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Call the `wl_post_jsonld` filter. |
|
| 251 | + * |
|
| 252 | + * @param array $jsonld The JSON-LD structure. |
|
| 253 | + * @param int $post_id The {@link WP_Post} `id`. |
|
| 254 | + * @param array $references The array of referenced entities. |
|
| 255 | + * |
|
| 256 | + * @since 3.14.0 |
|
| 257 | + * |
|
| 258 | + * @api |
|
| 259 | + */ |
|
| 260 | + return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references ); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Get the author's JSON-LD fragment. |
|
| 265 | + * |
|
| 266 | + * The JSON-LD fragment is generated using the {@link WP_User}'s data or |
|
| 267 | + * the referenced entity if configured for the {@link WP_User}. |
|
| 268 | + * |
|
| 269 | + * @param int $author_id The author {@link WP_User}'s `id`. |
|
| 270 | + * @param array $references An array of referenced entities. |
|
| 271 | + * |
|
| 272 | + * @return string|array A JSON-LD structure. |
|
| 273 | + * @since 3.14.0 |
|
| 274 | + * |
|
| 275 | + */ |
|
| 276 | + private function get_author( $author_id, &$references ) { |
|
| 277 | + |
|
| 278 | + // Get the entity bound to this user. |
|
| 279 | + $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 280 | + |
|
| 281 | + // If there's no entity bound return a simple author structure. |
|
| 282 | + if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) { |
|
| 283 | + |
|
| 284 | + $author = get_the_author_meta( 'display_name', $author_id ); |
|
| 285 | + $author_first_name = get_the_author_meta( 'first_name', $author_id ); |
|
| 286 | + $author_last_name = get_the_author_meta( 'last_name', $author_id ); |
|
| 287 | + $author_uri = $this->user_service->get_uri( $author_id ); |
|
| 288 | + |
|
| 289 | + return array( |
|
| 290 | + '@type' => 'Person', |
|
| 291 | + '@id' => $author_uri, |
|
| 292 | + 'name' => $author, |
|
| 293 | + 'givenName' => $author_first_name, |
|
| 294 | + 'familyName' => $author_last_name |
|
| 295 | + ); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + // Add the author to the references. |
|
| 299 | + $author_uri = $this->entity_service->get_uri( $entity_id ); |
|
| 300 | + $references[] = $entity_id; |
|
| 301 | + |
|
| 302 | + // Return the JSON-LD for the referenced entity. |
|
| 303 | + return array( |
|
| 304 | + '@id' => $author_uri, |
|
| 305 | + ); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Enrich the provided params array with publisher data, if available. |
|
| 310 | + * |
|
| 311 | + * @param array $params The parameters array. |
|
| 312 | + * |
|
| 313 | + * @since 3.10.0 |
|
| 314 | + * |
|
| 315 | + */ |
|
| 316 | + protected function set_publisher( &$params ) { |
|
| 317 | + |
|
| 318 | + // If the publisher id isn't set don't do anything. |
|
| 319 | + if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) { |
|
| 320 | + return; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + // Get the post instance. |
|
| 324 | + if ( null === $post = get_post( $publisher_id ) ) { |
|
| 325 | + // Publisher not found. |
|
| 326 | + return; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + // Get the item id. |
|
| 330 | + $id = $this->entity_service->get_uri( $publisher_id ); |
|
| 331 | + |
|
| 332 | + // Get the type. |
|
| 333 | + $type = $this->entity_type_service->get( $publisher_id ); |
|
| 334 | + |
|
| 335 | + // Get the name. |
|
| 336 | + $name = $post->post_title; |
|
| 337 | + |
|
| 338 | + // Set the publisher data. |
|
| 339 | + $params['publisher'] = array( |
|
| 340 | + '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 341 | + '@id' => $id, |
|
| 342 | + 'name' => $name, |
|
| 343 | + ); |
|
| 344 | + |
|
| 345 | + // Add the sameAs values associated with the publisher. |
|
| 346 | + $storage_factory = Wordlift_Storage_Factory::get_instance(); |
|
| 347 | + $sameas = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id ); |
|
| 348 | + if ( ! empty( $sameas ) ) { |
|
| 349 | + $params['publisher']['sameAs'] = $sameas; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + // Set the logo, only for http://schema.org/Organization as Person doesn't |
|
| 353 | + // support the logo property. |
|
| 354 | + // |
|
| 355 | + // See http://schema.org/logo. |
|
| 356 | + if ( 1 !== preg_match( '~Organization$~', $type['uri'] ) ) { |
|
| 357 | + return; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + // Get the publisher logo. |
|
| 361 | + $publisher_logo = $this->get_publisher_logo( $post->ID ); |
|
| 362 | + |
|
| 363 | + // Bail out if the publisher logo isn't set. |
|
| 364 | + if ( false === $publisher_logo ) { |
|
| 365 | + return; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + // Copy over some useful properties. |
|
| 369 | + // |
|
| 370 | + // See https://developers.google.com/search/docs/data-types/articles. |
|
| 371 | + $params['publisher']['logo']['@type'] = 'ImageObject'; |
|
| 372 | + $params['publisher']['logo']['url'] = $publisher_logo['url']; |
|
| 373 | + |
|
| 374 | + // If you specify a "width" or "height" value you should leave out |
|
| 375 | + // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 376 | + // |
|
| 377 | + // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 378 | + $params['publisher']['logo']['width'] = $publisher_logo['width']; |
|
| 379 | + $params['publisher']['logo']['height'] = $publisher_logo['height']; |
|
| 380 | + |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * Get the publisher logo structure. |
|
| 385 | + * |
|
| 386 | + * The function returns false when the publisher logo cannot be determined, i.e.: |
|
| 387 | + * - the post has no featured image. |
|
| 388 | + * - the featured image has no file. |
|
| 389 | + * - a wp_image_editor instance cannot be instantiated on the original file or on the publisher logo file. |
|
| 390 | + * |
|
| 391 | + * @param int $post_id The post id. |
|
| 392 | + * |
|
| 393 | + * @return array|false Returns an array with the `url`, `width` and `height` for the publisher logo or false in case |
|
| 394 | + * of errors. |
|
| 395 | + * @since 3.19.2 |
|
| 396 | + * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue. |
|
| 397 | + * |
|
| 398 | + */ |
|
| 399 | + private function get_publisher_logo( $post_id ) { |
|
| 400 | + |
|
| 401 | + // Get the featured image for the post. |
|
| 402 | + $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 403 | + |
|
| 404 | + // Bail out if thumbnail not available. |
|
| 405 | + if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) { |
|
| 406 | + $this->log->info( "Featured image not set for post $post_id." ); |
|
| 407 | + |
|
| 408 | + return false; |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + // Get the uploads base URL. |
|
| 412 | + $uploads_dir = wp_upload_dir(); |
|
| 413 | + |
|
| 414 | + // Get the attachment metadata. |
|
| 415 | + $metadata = wp_get_attachment_metadata( $thumbnail_id ); |
|
| 416 | + |
|
| 417 | + // Bail out if the file isn't set. |
|
| 418 | + if ( ! isset( $metadata['file'] ) ) { |
|
| 419 | + $this->log->warn( "Featured image file not found for post $post_id." ); |
|
| 420 | + |
|
| 421 | + return false; |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + // Retrieve the relative filename, e.g. "2018/05/logo_publisher.png" |
|
| 425 | + $path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file']; |
|
| 426 | + |
|
| 427 | + // Use image src, if local file does not exist. @see https://github.com/insideout10/wordlift-plugin/issues/1149 |
|
| 428 | + if ( ! file_exists( $path ) ) { |
|
| 429 | + $this->log->warn( "Featured image file $path doesn't exist for post $post_id." ); |
|
| 430 | + |
|
| 431 | + $attachment_image_src = wp_get_attachment_image_src( $thumbnail_id, '' ); |
|
| 432 | + if ( $attachment_image_src ) { |
|
| 433 | + return array( |
|
| 434 | + 'url' => $attachment_image_src[0], |
|
| 435 | + 'width' => $attachment_image_src[1], |
|
| 436 | + 'height' => $attachment_image_src[2], |
|
| 437 | + ); |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + // Bail out if we cant fetch wp_get_attachment_image_src |
|
| 441 | + return false; |
|
| 442 | + |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 446 | + $original_file_editor = wp_get_image_editor( $path ); |
|
| 447 | + if ( is_wp_error( $original_file_editor ) ) { |
|
| 448 | + $this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." ); |
|
| 449 | + |
|
| 450 | + return false; |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + // Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling |
|
| 454 | + // and we don't actually know the end values. |
|
| 455 | + $publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' ); |
|
| 456 | + |
|
| 457 | + // If the file doesn't exist yet, create it. |
|
| 458 | + if ( ! file_exists( $publisher_logo_path ) ) { |
|
| 459 | + $original_file_editor->resize( 600, 60 ); |
|
| 460 | + $original_file_editor->save( $publisher_logo_path ); |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 464 | + $publisher_logo_editor = wp_get_image_editor( $publisher_logo_path ); |
|
| 465 | + if ( is_wp_error( $publisher_logo_editor ) ) { |
|
| 466 | + $this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." ); |
|
| 467 | + |
|
| 468 | + return false; |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + // Get the actual size. |
|
| 472 | + $size = $publisher_logo_editor->get_size(); |
|
| 473 | + |
|
| 474 | + // Finally return the array with data. |
|
| 475 | + return array( |
|
| 476 | + 'url' => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ), |
|
| 477 | + 'width' => $size['width'], |
|
| 478 | + 'height' => $size['height'], |
|
| 479 | + ); |
|
| 480 | + } |
|
| 481 | 481 | |
| 482 | 482 | } |
@@ -55,14 +55,14 @@ discard block |
||
| 55 | 55 | * @since 3.10.0 |
| 56 | 56 | * |
| 57 | 57 | */ |
| 58 | - public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service, $disable_convert_filters = false ) { |
|
| 59 | - parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 58 | + public function __construct($entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service, $disable_convert_filters = false) { |
|
| 59 | + parent::__construct($entity_type_service, $entity_service, $user_service, $attachment_service); |
|
| 60 | 60 | |
| 61 | 61 | $this->configuration_service = $configuration_service; |
| 62 | 62 | $this->disable_convert_filters = $disable_convert_filters; |
| 63 | 63 | |
| 64 | 64 | // Set a reference to the logger. |
| 65 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 65 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Post_To_Jsonld_Converter'); |
|
| 66 | 66 | |
| 67 | 67 | self::$instance = $this; |
| 68 | 68 | |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | public function new_instance_with_filters_disabled() { |
| 77 | - return new static( $this->entity_type_service, $this->entity_service, $this->user_service, $this->attachment_service, $this->configuration_service, true ); |
|
| 77 | + return new static($this->entity_type_service, $this->entity_service, $this->user_service, $this->attachment_service, $this->configuration_service, true); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | /** |
@@ -88,19 +88,19 @@ discard block |
||
| 88 | 88 | * @return array A JSON-LD array. |
| 89 | 89 | * @since 3.10.0 |
| 90 | 90 | */ |
| 91 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 91 | + public function convert($post_id, &$references = array(), &$references_infos = array()) { |
|
| 92 | 92 | |
| 93 | 93 | // Get the post instance. |
| 94 | - if ( null === $post = get_post( $post_id ) ) { |
|
| 94 | + if (null === $post = get_post($post_id)) { |
|
| 95 | 95 | // Post not found. |
| 96 | 96 | return null; |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | // Get the base JSON-LD and the list of entities referenced by this entity. |
| 100 | - $jsonld = parent::convert( $post_id, $references, $references_infos ); |
|
| 100 | + $jsonld = parent::convert($post_id, $references, $references_infos); |
|
| 101 | 101 | |
| 102 | 102 | // Set WebPage by default. |
| 103 | - if ( empty( $jsonld['@type'] ) ) { |
|
| 103 | + if (empty($jsonld['@type'])) { |
|
| 104 | 104 | $jsonld['@type'] = 'WebPage'; |
| 105 | 105 | } |
| 106 | 106 | |
@@ -117,19 +117,19 @@ discard block |
||
| 117 | 117 | */ |
| 118 | 118 | try { |
| 119 | 119 | $default_timezone = date_default_timezone_get(); |
| 120 | - $timezone = get_option( 'timezone_string' ); |
|
| 121 | - if ( ! empty( $timezone ) ) { |
|
| 122 | - date_default_timezone_set( $timezone ); |
|
| 123 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 124 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 125 | - date_default_timezone_set( $default_timezone ); |
|
| 120 | + $timezone = get_option('timezone_string'); |
|
| 121 | + if ( ! empty($timezone)) { |
|
| 122 | + date_default_timezone_set($timezone); |
|
| 123 | + $jsonld['datePublished'] = get_post_time('Y-m-d\TH:i:sP', false, $post); |
|
| 124 | + $jsonld['dateModified'] = get_post_modified_time('Y-m-d\TH:i:sP', false, $post); |
|
| 125 | + date_default_timezone_set($default_timezone); |
|
| 126 | 126 | } else { |
| 127 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 128 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 127 | + $jsonld['datePublished'] = get_post_time('Y-m-d\TH:i', true, $post, false); |
|
| 128 | + $jsonld['dateModified'] = get_post_modified_time('Y-m-d\TH:i', true, $post, false); |
|
| 129 | 129 | } |
| 130 | - } catch ( Exception $e ) { |
|
| 131 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 132 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 130 | + } catch (Exception $e) { |
|
| 131 | + $jsonld['datePublished'] = get_post_time('Y-m-d\TH:i', true, $post, false); |
|
| 132 | + $jsonld['dateModified'] = get_post_modified_time('Y-m-d\TH:i', true, $post, false); |
|
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | // Get the word count for the post. |
@@ -140,8 +140,8 @@ discard block |
||
| 140 | 140 | * |
| 141 | 141 | * @since 3.20.0 |
| 142 | 142 | */ |
| 143 | - if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 144 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 143 | + if ( ! empty($jsonld['@type']) && 'WebPage' !== $jsonld['@type']) { |
|
| 144 | + $post_adapter = new Wordlift_Post_Adapter($post_id); |
|
| 145 | 145 | $jsonld['wordCount'] = $post_adapter->word_count(); |
| 146 | 146 | } |
| 147 | 147 | |
@@ -152,53 +152,53 @@ discard block |
||
| 152 | 152 | * |
| 153 | 153 | * @since 3.27.2 |
| 154 | 154 | */ |
| 155 | - if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 156 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 155 | + if ( ! empty($jsonld['@type']) && 'WebPage' !== $jsonld['@type']) { |
|
| 156 | + $post_adapter = new Wordlift_Post_Adapter($post_id); |
|
| 157 | 157 | $keywords = $post_adapter->keywords(); |
| 158 | 158 | $article_section = $post_adapter->article_section(); |
| 159 | 159 | $comment_count = $post_adapter->comment_count(); |
| 160 | 160 | $locale = $post_adapter->locale(); |
| 161 | 161 | |
| 162 | - if ( isset( $keywords ) ) { |
|
| 162 | + if (isset($keywords)) { |
|
| 163 | 163 | $jsonld['keywords'] = $keywords; |
| 164 | 164 | } |
| 165 | - if ( isset( $article_section ) ) { |
|
| 165 | + if (isset($article_section)) { |
|
| 166 | 166 | $jsonld['articleSection'] = $article_section; |
| 167 | 167 | } |
| 168 | 168 | $jsonld['commentCount'] = $comment_count; |
| 169 | 169 | $jsonld['inLanguage'] = $locale; |
| 170 | - $post_adapter->add_mentions( $post_id, $references ); |
|
| 170 | + $post_adapter->add_mentions($post_id, $references); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | // Set the publisher. |
| 174 | - $this->set_publisher( $jsonld ); |
|
| 174 | + $this->set_publisher($jsonld); |
|
| 175 | 175 | |
| 176 | 176 | // Process the references if any. |
| 177 | - if ( 0 < count( $references ) ) { |
|
| 177 | + if (0 < count($references)) { |
|
| 178 | 178 | |
| 179 | 179 | // Prepare the `about` and `mentions` array. |
| 180 | 180 | $about = $mentions = array(); |
| 181 | 181 | |
| 182 | 182 | // If the entity is in the title, then it should be an `about`. |
| 183 | - foreach ( $references as $reference ) { |
|
| 183 | + foreach ($references as $reference) { |
|
| 184 | 184 | |
| 185 | 185 | // Get the entity labels. |
| 186 | - $labels = $this->entity_service->get_labels( $reference ); |
|
| 186 | + $labels = $this->entity_service->get_labels($reference); |
|
| 187 | 187 | |
| 188 | 188 | // Get the entity URI. |
| 189 | 189 | $item = array( |
| 190 | - '@id' => $this->entity_service->get_uri( $reference ), |
|
| 190 | + '@id' => $this->entity_service->get_uri($reference), |
|
| 191 | 191 | ); |
| 192 | 192 | |
| 193 | - $escaped_labels = array_map( function ( $value ) { |
|
| 194 | - return preg_quote( $value, '/' ); |
|
| 195 | - }, $labels ); |
|
| 193 | + $escaped_labels = array_map(function($value) { |
|
| 194 | + return preg_quote($value, '/'); |
|
| 195 | + }, $labels); |
|
| 196 | 196 | |
| 197 | 197 | // Check if the labels match any part of the title. |
| 198 | - $matches = 1 === preg_match( '/' . implode( '|', $escaped_labels ) . '/', $post->post_title ); |
|
| 198 | + $matches = 1 === preg_match('/'.implode('|', $escaped_labels).'/', $post->post_title); |
|
| 199 | 199 | |
| 200 | 200 | // If the title matches, assign the entity to the about, otherwise to the mentions. |
| 201 | - if ( $matches ) { |
|
| 201 | + if ($matches) { |
|
| 202 | 202 | $about[] = $item; |
| 203 | 203 | } else { |
| 204 | 204 | $mentions[] = $item; |
@@ -206,21 +206,21 @@ discard block |
||
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | // If we have abouts, assign them to the JSON-LD. |
| 209 | - if ( 0 < count( $about ) ) { |
|
| 209 | + if (0 < count($about)) { |
|
| 210 | 210 | $jsonld['about'] = $about; |
| 211 | 211 | } |
| 212 | 212 | |
| 213 | 213 | // If we have mentions, assign them to the JSON-LD. |
| 214 | - if ( 0 < count( $mentions ) ) { |
|
| 214 | + if (0 < count($mentions)) { |
|
| 215 | 215 | $jsonld['mentions'] = $mentions; |
| 216 | 216 | } |
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | // Finally set the author. |
| 220 | - $jsonld['author'] = $this->get_author( $post->post_author, $references ); |
|
| 220 | + $jsonld['author'] = $this->get_author($post->post_author, $references); |
|
| 221 | 221 | |
| 222 | 222 | // Return the JSON-LD if filters are disabled by the client. |
| 223 | - if ( $this->disable_convert_filters ) { |
|
| 223 | + if ($this->disable_convert_filters) { |
|
| 224 | 224 | return $jsonld; |
| 225 | 225 | } |
| 226 | 226 | |
@@ -238,10 +238,10 @@ discard block |
||
| 238 | 238 | * |
| 239 | 239 | * @api |
| 240 | 240 | */ |
| 241 | - $ret_val = apply_filters( 'wl_post_jsonld_array', array( |
|
| 241 | + $ret_val = apply_filters('wl_post_jsonld_array', array( |
|
| 242 | 242 | 'jsonld' => $jsonld, |
| 243 | 243 | 'references' => $references, |
| 244 | - ), $post_id ); |
|
| 244 | + ), $post_id); |
|
| 245 | 245 | |
| 246 | 246 | $jsonld = $ret_val['jsonld']; |
| 247 | 247 | $references = $ret_val['references']; |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | * |
| 258 | 258 | * @api |
| 259 | 259 | */ |
| 260 | - return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references ); |
|
| 260 | + return apply_filters('wl_post_jsonld', $jsonld, $post_id, $references); |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | /** |
@@ -273,18 +273,18 @@ discard block |
||
| 273 | 273 | * @since 3.14.0 |
| 274 | 274 | * |
| 275 | 275 | */ |
| 276 | - private function get_author( $author_id, &$references ) { |
|
| 276 | + private function get_author($author_id, &$references) { |
|
| 277 | 277 | |
| 278 | 278 | // Get the entity bound to this user. |
| 279 | - $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 279 | + $entity_id = $this->user_service->get_entity($author_id); |
|
| 280 | 280 | |
| 281 | 281 | // If there's no entity bound return a simple author structure. |
| 282 | - if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) { |
|
| 282 | + if (empty($entity_id) || 'publish' !== get_post_status($entity_id)) { |
|
| 283 | 283 | |
| 284 | - $author = get_the_author_meta( 'display_name', $author_id ); |
|
| 285 | - $author_first_name = get_the_author_meta( 'first_name', $author_id ); |
|
| 286 | - $author_last_name = get_the_author_meta( 'last_name', $author_id ); |
|
| 287 | - $author_uri = $this->user_service->get_uri( $author_id ); |
|
| 284 | + $author = get_the_author_meta('display_name', $author_id); |
|
| 285 | + $author_first_name = get_the_author_meta('first_name', $author_id); |
|
| 286 | + $author_last_name = get_the_author_meta('last_name', $author_id); |
|
| 287 | + $author_uri = $this->user_service->get_uri($author_id); |
|
| 288 | 288 | |
| 289 | 289 | return array( |
| 290 | 290 | '@type' => 'Person', |
@@ -296,7 +296,7 @@ discard block |
||
| 296 | 296 | } |
| 297 | 297 | |
| 298 | 298 | // Add the author to the references. |
| 299 | - $author_uri = $this->entity_service->get_uri( $entity_id ); |
|
| 299 | + $author_uri = $this->entity_service->get_uri($entity_id); |
|
| 300 | 300 | $references[] = $entity_id; |
| 301 | 301 | |
| 302 | 302 | // Return the JSON-LD for the referenced entity. |
@@ -313,39 +313,39 @@ discard block |
||
| 313 | 313 | * @since 3.10.0 |
| 314 | 314 | * |
| 315 | 315 | */ |
| 316 | - protected function set_publisher( &$params ) { |
|
| 316 | + protected function set_publisher(&$params) { |
|
| 317 | 317 | |
| 318 | 318 | // If the publisher id isn't set don't do anything. |
| 319 | - if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) { |
|
| 319 | + if (null === $publisher_id = $this->configuration_service->get_publisher_id()) { |
|
| 320 | 320 | return; |
| 321 | 321 | } |
| 322 | 322 | |
| 323 | 323 | // Get the post instance. |
| 324 | - if ( null === $post = get_post( $publisher_id ) ) { |
|
| 324 | + if (null === $post = get_post($publisher_id)) { |
|
| 325 | 325 | // Publisher not found. |
| 326 | 326 | return; |
| 327 | 327 | } |
| 328 | 328 | |
| 329 | 329 | // Get the item id. |
| 330 | - $id = $this->entity_service->get_uri( $publisher_id ); |
|
| 330 | + $id = $this->entity_service->get_uri($publisher_id); |
|
| 331 | 331 | |
| 332 | 332 | // Get the type. |
| 333 | - $type = $this->entity_type_service->get( $publisher_id ); |
|
| 333 | + $type = $this->entity_type_service->get($publisher_id); |
|
| 334 | 334 | |
| 335 | 335 | // Get the name. |
| 336 | 336 | $name = $post->post_title; |
| 337 | 337 | |
| 338 | 338 | // Set the publisher data. |
| 339 | 339 | $params['publisher'] = array( |
| 340 | - '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 340 | + '@type' => $this->relative_to_context($type['uri']), |
|
| 341 | 341 | '@id' => $id, |
| 342 | 342 | 'name' => $name, |
| 343 | 343 | ); |
| 344 | 344 | |
| 345 | 345 | // Add the sameAs values associated with the publisher. |
| 346 | 346 | $storage_factory = Wordlift_Storage_Factory::get_instance(); |
| 347 | - $sameas = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id ); |
|
| 348 | - if ( ! empty( $sameas ) ) { |
|
| 347 | + $sameas = $storage_factory->post_meta(Wordlift_Schema_Service::FIELD_SAME_AS)->get($publisher_id); |
|
| 348 | + if ( ! empty($sameas)) { |
|
| 349 | 349 | $params['publisher']['sameAs'] = $sameas; |
| 350 | 350 | } |
| 351 | 351 | |
@@ -353,15 +353,15 @@ discard block |
||
| 353 | 353 | // support the logo property. |
| 354 | 354 | // |
| 355 | 355 | // See http://schema.org/logo. |
| 356 | - if ( 1 !== preg_match( '~Organization$~', $type['uri'] ) ) { |
|
| 356 | + if (1 !== preg_match('~Organization$~', $type['uri'])) { |
|
| 357 | 357 | return; |
| 358 | 358 | } |
| 359 | 359 | |
| 360 | 360 | // Get the publisher logo. |
| 361 | - $publisher_logo = $this->get_publisher_logo( $post->ID ); |
|
| 361 | + $publisher_logo = $this->get_publisher_logo($post->ID); |
|
| 362 | 362 | |
| 363 | 363 | // Bail out if the publisher logo isn't set. |
| 364 | - if ( false === $publisher_logo ) { |
|
| 364 | + if (false === $publisher_logo) { |
|
| 365 | 365 | return; |
| 366 | 366 | } |
| 367 | 367 | |
@@ -396,14 +396,14 @@ discard block |
||
| 396 | 396 | * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue. |
| 397 | 397 | * |
| 398 | 398 | */ |
| 399 | - private function get_publisher_logo( $post_id ) { |
|
| 399 | + private function get_publisher_logo($post_id) { |
|
| 400 | 400 | |
| 401 | 401 | // Get the featured image for the post. |
| 402 | - $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 402 | + $thumbnail_id = get_post_thumbnail_id($post_id); |
|
| 403 | 403 | |
| 404 | 404 | // Bail out if thumbnail not available. |
| 405 | - if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) { |
|
| 406 | - $this->log->info( "Featured image not set for post $post_id." ); |
|
| 405 | + if (empty($thumbnail_id) || 0 === $thumbnail_id) { |
|
| 406 | + $this->log->info("Featured image not set for post $post_id."); |
|
| 407 | 407 | |
| 408 | 408 | return false; |
| 409 | 409 | } |
@@ -412,24 +412,24 @@ discard block |
||
| 412 | 412 | $uploads_dir = wp_upload_dir(); |
| 413 | 413 | |
| 414 | 414 | // Get the attachment metadata. |
| 415 | - $metadata = wp_get_attachment_metadata( $thumbnail_id ); |
|
| 415 | + $metadata = wp_get_attachment_metadata($thumbnail_id); |
|
| 416 | 416 | |
| 417 | 417 | // Bail out if the file isn't set. |
| 418 | - if ( ! isset( $metadata['file'] ) ) { |
|
| 419 | - $this->log->warn( "Featured image file not found for post $post_id." ); |
|
| 418 | + if ( ! isset($metadata['file'])) { |
|
| 419 | + $this->log->warn("Featured image file not found for post $post_id."); |
|
| 420 | 420 | |
| 421 | 421 | return false; |
| 422 | 422 | } |
| 423 | 423 | |
| 424 | 424 | // Retrieve the relative filename, e.g. "2018/05/logo_publisher.png" |
| 425 | - $path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file']; |
|
| 425 | + $path = $uploads_dir['basedir'].DIRECTORY_SEPARATOR.$metadata['file']; |
|
| 426 | 426 | |
| 427 | 427 | // Use image src, if local file does not exist. @see https://github.com/insideout10/wordlift-plugin/issues/1149 |
| 428 | - if ( ! file_exists( $path ) ) { |
|
| 429 | - $this->log->warn( "Featured image file $path doesn't exist for post $post_id." ); |
|
| 428 | + if ( ! file_exists($path)) { |
|
| 429 | + $this->log->warn("Featured image file $path doesn't exist for post $post_id."); |
|
| 430 | 430 | |
| 431 | - $attachment_image_src = wp_get_attachment_image_src( $thumbnail_id, '' ); |
|
| 432 | - if ( $attachment_image_src ) { |
|
| 431 | + $attachment_image_src = wp_get_attachment_image_src($thumbnail_id, ''); |
|
| 432 | + if ($attachment_image_src) { |
|
| 433 | 433 | return array( |
| 434 | 434 | 'url' => $attachment_image_src[0], |
| 435 | 435 | 'width' => $attachment_image_src[1], |
@@ -443,27 +443,27 @@ discard block |
||
| 443 | 443 | } |
| 444 | 444 | |
| 445 | 445 | // Try to get the image editor and bail out if the editor cannot be instantiated. |
| 446 | - $original_file_editor = wp_get_image_editor( $path ); |
|
| 447 | - if ( is_wp_error( $original_file_editor ) ) { |
|
| 448 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." ); |
|
| 446 | + $original_file_editor = wp_get_image_editor($path); |
|
| 447 | + if (is_wp_error($original_file_editor)) { |
|
| 448 | + $this->log->warn("Cannot instantiate WP Image Editor on file $path for post $post_id."); |
|
| 449 | 449 | |
| 450 | 450 | return false; |
| 451 | 451 | } |
| 452 | 452 | |
| 453 | 453 | // Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling |
| 454 | 454 | // and we don't actually know the end values. |
| 455 | - $publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' ); |
|
| 455 | + $publisher_logo_path = $original_file_editor->generate_filename('-publisher-logo'); |
|
| 456 | 456 | |
| 457 | 457 | // If the file doesn't exist yet, create it. |
| 458 | - if ( ! file_exists( $publisher_logo_path ) ) { |
|
| 459 | - $original_file_editor->resize( 600, 60 ); |
|
| 460 | - $original_file_editor->save( $publisher_logo_path ); |
|
| 458 | + if ( ! file_exists($publisher_logo_path)) { |
|
| 459 | + $original_file_editor->resize(600, 60); |
|
| 460 | + $original_file_editor->save($publisher_logo_path); |
|
| 461 | 461 | } |
| 462 | 462 | |
| 463 | 463 | // Try to get the image editor and bail out if the editor cannot be instantiated. |
| 464 | - $publisher_logo_editor = wp_get_image_editor( $publisher_logo_path ); |
|
| 465 | - if ( is_wp_error( $publisher_logo_editor ) ) { |
|
| 466 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." ); |
|
| 464 | + $publisher_logo_editor = wp_get_image_editor($publisher_logo_path); |
|
| 465 | + if (is_wp_error($publisher_logo_editor)) { |
|
| 466 | + $this->log->warn("Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id."); |
|
| 467 | 467 | |
| 468 | 468 | return false; |
| 469 | 469 | } |
@@ -473,7 +473,7 @@ discard block |
||
| 473 | 473 | |
| 474 | 474 | // Finally return the array with data. |
| 475 | 475 | return array( |
| 476 | - 'url' => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ), |
|
| 476 | + 'url' => $uploads_dir['baseurl'].substr($publisher_logo_path, strlen($uploads_dir['basedir'])), |
|
| 477 | 477 | 'width' => $size['width'], |
| 478 | 478 | 'height' => $size['height'], |
| 479 | 479 | ); |
@@ -8,24 +8,24 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Term_Jsonld { |
| 10 | 10 | |
| 11 | - public function init() { |
|
| 12 | - add_action( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 13 | - } |
|
| 11 | + public function init() { |
|
| 12 | + add_action( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 13 | + } |
|
| 14 | 14 | |
| 15 | - public function wl_term_jsonld_array( $jsonld_array, $term_id ) { |
|
| 15 | + public function wl_term_jsonld_array( $jsonld_array, $term_id ) { |
|
| 16 | 16 | |
| 17 | - $entities = Jsonld_Utils::get_matched_entities_for_term( $term_id ); |
|
| 17 | + $entities = Jsonld_Utils::get_matched_entities_for_term( $term_id ); |
|
| 18 | 18 | |
| 19 | - if ( count( $entities ) > 0 ) { |
|
| 20 | - $entity = array_shift( $entities ); |
|
| 21 | - $entity['@context'] = 'http://schema.org'; |
|
| 22 | - $entity['@id'] = get_term_link( $term_id ) . "/#id"; |
|
| 23 | - $entity['url'] = get_term_link( $term_id ); |
|
| 24 | - $entity['mainEntityOfPage'] = get_term_link( $term_id ); |
|
| 25 | - $jsonld_array['jsonld'][] = $entity; |
|
| 26 | - } |
|
| 19 | + if ( count( $entities ) > 0 ) { |
|
| 20 | + $entity = array_shift( $entities ); |
|
| 21 | + $entity['@context'] = 'http://schema.org'; |
|
| 22 | + $entity['@id'] = get_term_link( $term_id ) . "/#id"; |
|
| 23 | + $entity['url'] = get_term_link( $term_id ); |
|
| 24 | + $entity['mainEntityOfPage'] = get_term_link( $term_id ); |
|
| 25 | + $jsonld_array['jsonld'][] = $entity; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - return $jsonld_array; |
|
| 29 | - } |
|
| 28 | + return $jsonld_array; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | 31 | } |
| 32 | 32 | \ No newline at end of file |
@@ -9,19 +9,19 @@ |
||
| 9 | 9 | class Term_Jsonld { |
| 10 | 10 | |
| 11 | 11 | public function init() { |
| 12 | - add_action( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 12 | + add_action('wl_term_jsonld_array', array($this, 'wl_term_jsonld_array'), 10, 2); |
|
| 13 | 13 | } |
| 14 | 14 | |
| 15 | - public function wl_term_jsonld_array( $jsonld_array, $term_id ) { |
|
| 15 | + public function wl_term_jsonld_array($jsonld_array, $term_id) { |
|
| 16 | 16 | |
| 17 | - $entities = Jsonld_Utils::get_matched_entities_for_term( $term_id ); |
|
| 17 | + $entities = Jsonld_Utils::get_matched_entities_for_term($term_id); |
|
| 18 | 18 | |
| 19 | - if ( count( $entities ) > 0 ) { |
|
| 20 | - $entity = array_shift( $entities ); |
|
| 19 | + if (count($entities) > 0) { |
|
| 20 | + $entity = array_shift($entities); |
|
| 21 | 21 | $entity['@context'] = 'http://schema.org'; |
| 22 | - $entity['@id'] = get_term_link( $term_id ) . "/#id"; |
|
| 23 | - $entity['url'] = get_term_link( $term_id ); |
|
| 24 | - $entity['mainEntityOfPage'] = get_term_link( $term_id ); |
|
| 22 | + $entity['@id'] = get_term_link($term_id)."/#id"; |
|
| 23 | + $entity['url'] = get_term_link($term_id); |
|
| 24 | + $entity['mainEntityOfPage'] = get_term_link($term_id); |
|
| 25 | 25 | $jsonld_array['jsonld'][] = $entity; |
| 26 | 26 | } |
| 27 | 27 | |