@@ -15,121 +15,121 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Tag_Rest_Endpoint { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var Term_Data_Factory |
|
| 20 | - */ |
|
| 21 | - private $term_data_factory; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Tag_Rest_Endpoint constructor. |
|
| 25 | - * |
|
| 26 | - * @param Term_Data_Factory $term_data_factory |
|
| 27 | - */ |
|
| 28 | - public function __construct( $term_data_factory ) { |
|
| 29 | - |
|
| 30 | - $this->term_data_factory = $term_data_factory; |
|
| 31 | - |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - |
|
| 35 | - public function register_routes() { |
|
| 36 | - $that = $this; |
|
| 37 | - add_action( 'rest_api_init', |
|
| 38 | - function () use ( $that ) { |
|
| 39 | - register_rest_route( |
|
| 40 | - Api_Config::REST_NAMESPACE, |
|
| 41 | - '/tags', |
|
| 42 | - array( |
|
| 43 | - 'methods' => WP_REST_Server::CREATABLE, |
|
| 44 | - 'callback' => array( $that, 'get_tags' ), |
|
| 45 | - //@todo : review the permission level |
|
| 46 | - 'permission_callback' => function () { |
|
| 47 | - return current_user_can( 'manage_options' ); |
|
| 48 | - }, |
|
| 49 | - 'args' => array( |
|
| 50 | - 'limit' => array( |
|
| 51 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 52 | - return is_numeric( $param ) && $param; |
|
| 53 | - }, |
|
| 54 | - 'required' => true, |
|
| 55 | - ), |
|
| 56 | - 'offset' => array( |
|
| 57 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 58 | - return is_numeric( $param ); |
|
| 59 | - }, |
|
| 60 | - 'required' => true, |
|
| 61 | - ), |
|
| 62 | - ), |
|
| 63 | - ) |
|
| 64 | - ); |
|
| 65 | - } ); |
|
| 66 | - |
|
| 67 | - |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - |
|
| 71 | - public function get_tags( $request ) { |
|
| 72 | - |
|
| 73 | - $data = $request->get_params(); |
|
| 74 | - $offset = (int) $data['offset']; |
|
| 75 | - $limit = (int) $data['limit']; |
|
| 76 | - $tags = $this->get_terms_from_db( $limit, $offset ); |
|
| 77 | - $term_data_list = array(); |
|
| 78 | - |
|
| 79 | - foreach ( $tags as $tag ) { |
|
| 80 | - |
|
| 81 | - if ( $this->is_tag_excluded_from_ui( $tag ) ) { |
|
| 82 | - continue; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param $tag \WP_Term |
|
| 87 | - */ |
|
| 88 | - $term_data_instance = $this->term_data_factory->get_term_data($tag); |
|
| 89 | - $term_data = $term_data_instance->get_data(); |
|
| 90 | - if ( $term_data['entities'] ) { |
|
| 91 | - $term_data_list[] = $term_data; |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - |
|
| 96 | - return $term_data_list; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * @param \WP_Term $tag |
|
| 101 | - * |
|
| 102 | - * @return bool |
|
| 103 | - */ |
|
| 104 | - private function is_tag_excluded_from_ui( $tag ) { |
|
| 105 | - return (int) get_term_meta( $tag->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) === 1; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @param $limit |
|
| 110 | - * @param $offset |
|
| 111 | - * |
|
| 112 | - * @return int|\WP_Error|\WP_Term[] |
|
| 113 | - */ |
|
| 114 | - public function get_terms_from_db( $limit, $offset ) { |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - return Terms_Compat::get_terms(Terms_Compat::get_public_taxonomies(), array( |
|
| 118 | - 'hide_empty' => false, |
|
| 119 | - 'number' => $limit, |
|
| 120 | - 'offset' => $offset, |
|
| 121 | - 'meta_query' => array( |
|
| 122 | - array( |
|
| 123 | - 'key' => Analysis_Background_Service::ENTITIES_PRESENT_FOR_TERM, |
|
| 124 | - 'compare' => 'EXISTS' |
|
| 125 | - ) |
|
| 126 | - ), |
|
| 127 | - 'orderby' => 'count', |
|
| 128 | - 'order' => 'DESC', |
|
| 129 | - )); |
|
| 130 | - |
|
| 131 | - |
|
| 132 | - } |
|
| 18 | + /** |
|
| 19 | + * @var Term_Data_Factory |
|
| 20 | + */ |
|
| 21 | + private $term_data_factory; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Tag_Rest_Endpoint constructor. |
|
| 25 | + * |
|
| 26 | + * @param Term_Data_Factory $term_data_factory |
|
| 27 | + */ |
|
| 28 | + public function __construct( $term_data_factory ) { |
|
| 29 | + |
|
| 30 | + $this->term_data_factory = $term_data_factory; |
|
| 31 | + |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + |
|
| 35 | + public function register_routes() { |
|
| 36 | + $that = $this; |
|
| 37 | + add_action( 'rest_api_init', |
|
| 38 | + function () use ( $that ) { |
|
| 39 | + register_rest_route( |
|
| 40 | + Api_Config::REST_NAMESPACE, |
|
| 41 | + '/tags', |
|
| 42 | + array( |
|
| 43 | + 'methods' => WP_REST_Server::CREATABLE, |
|
| 44 | + 'callback' => array( $that, 'get_tags' ), |
|
| 45 | + //@todo : review the permission level |
|
| 46 | + 'permission_callback' => function () { |
|
| 47 | + return current_user_can( 'manage_options' ); |
|
| 48 | + }, |
|
| 49 | + 'args' => array( |
|
| 50 | + 'limit' => array( |
|
| 51 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
| 52 | + return is_numeric( $param ) && $param; |
|
| 53 | + }, |
|
| 54 | + 'required' => true, |
|
| 55 | + ), |
|
| 56 | + 'offset' => array( |
|
| 57 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
| 58 | + return is_numeric( $param ); |
|
| 59 | + }, |
|
| 60 | + 'required' => true, |
|
| 61 | + ), |
|
| 62 | + ), |
|
| 63 | + ) |
|
| 64 | + ); |
|
| 65 | + } ); |
|
| 66 | + |
|
| 67 | + |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + |
|
| 71 | + public function get_tags( $request ) { |
|
| 72 | + |
|
| 73 | + $data = $request->get_params(); |
|
| 74 | + $offset = (int) $data['offset']; |
|
| 75 | + $limit = (int) $data['limit']; |
|
| 76 | + $tags = $this->get_terms_from_db( $limit, $offset ); |
|
| 77 | + $term_data_list = array(); |
|
| 78 | + |
|
| 79 | + foreach ( $tags as $tag ) { |
|
| 80 | + |
|
| 81 | + if ( $this->is_tag_excluded_from_ui( $tag ) ) { |
|
| 82 | + continue; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param $tag \WP_Term |
|
| 87 | + */ |
|
| 88 | + $term_data_instance = $this->term_data_factory->get_term_data($tag); |
|
| 89 | + $term_data = $term_data_instance->get_data(); |
|
| 90 | + if ( $term_data['entities'] ) { |
|
| 91 | + $term_data_list[] = $term_data; |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + |
|
| 96 | + return $term_data_list; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * @param \WP_Term $tag |
|
| 101 | + * |
|
| 102 | + * @return bool |
|
| 103 | + */ |
|
| 104 | + private function is_tag_excluded_from_ui( $tag ) { |
|
| 105 | + return (int) get_term_meta( $tag->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) === 1; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @param $limit |
|
| 110 | + * @param $offset |
|
| 111 | + * |
|
| 112 | + * @return int|\WP_Error|\WP_Term[] |
|
| 113 | + */ |
|
| 114 | + public function get_terms_from_db( $limit, $offset ) { |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + return Terms_Compat::get_terms(Terms_Compat::get_public_taxonomies(), array( |
|
| 118 | + 'hide_empty' => false, |
|
| 119 | + 'number' => $limit, |
|
| 120 | + 'offset' => $offset, |
|
| 121 | + 'meta_query' => array( |
|
| 122 | + array( |
|
| 123 | + 'key' => Analysis_Background_Service::ENTITIES_PRESENT_FOR_TERM, |
|
| 124 | + 'compare' => 'EXISTS' |
|
| 125 | + ) |
|
| 126 | + ), |
|
| 127 | + 'orderby' => 'count', |
|
| 128 | + 'order' => 'DESC', |
|
| 129 | + )); |
|
| 130 | + |
|
| 131 | + |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | 134 | |
| 135 | 135 | } |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | * |
| 26 | 26 | * @param Term_Data_Factory $term_data_factory |
| 27 | 27 | */ |
| 28 | - public function __construct( $term_data_factory ) { |
|
| 28 | + public function __construct($term_data_factory) { |
|
| 29 | 29 | |
| 30 | 30 | $this->term_data_factory = $term_data_factory; |
| 31 | 31 | |
@@ -34,28 +34,28 @@ discard block |
||
| 34 | 34 | |
| 35 | 35 | public function register_routes() { |
| 36 | 36 | $that = $this; |
| 37 | - add_action( 'rest_api_init', |
|
| 38 | - function () use ( $that ) { |
|
| 37 | + add_action('rest_api_init', |
|
| 38 | + function() use ($that) { |
|
| 39 | 39 | register_rest_route( |
| 40 | 40 | Api_Config::REST_NAMESPACE, |
| 41 | 41 | '/tags', |
| 42 | 42 | array( |
| 43 | 43 | 'methods' => WP_REST_Server::CREATABLE, |
| 44 | - 'callback' => array( $that, 'get_tags' ), |
|
| 44 | + 'callback' => array($that, 'get_tags'), |
|
| 45 | 45 | //@todo : review the permission level |
| 46 | - 'permission_callback' => function () { |
|
| 47 | - return current_user_can( 'manage_options' ); |
|
| 46 | + 'permission_callback' => function() { |
|
| 47 | + return current_user_can('manage_options'); |
|
| 48 | 48 | }, |
| 49 | 49 | 'args' => array( |
| 50 | 50 | 'limit' => array( |
| 51 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 52 | - return is_numeric( $param ) && $param; |
|
| 51 | + 'validate_callback' => function($param, $request, $key) { |
|
| 52 | + return is_numeric($param) && $param; |
|
| 53 | 53 | }, |
| 54 | 54 | 'required' => true, |
| 55 | 55 | ), |
| 56 | 56 | 'offset' => array( |
| 57 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 58 | - return is_numeric( $param ); |
|
| 57 | + 'validate_callback' => function($param, $request, $key) { |
|
| 58 | + return is_numeric($param); |
|
| 59 | 59 | }, |
| 60 | 60 | 'required' => true, |
| 61 | 61 | ), |
@@ -68,17 +68,17 @@ discard block |
||
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | |
| 71 | - public function get_tags( $request ) { |
|
| 71 | + public function get_tags($request) { |
|
| 72 | 72 | |
| 73 | 73 | $data = $request->get_params(); |
| 74 | 74 | $offset = (int) $data['offset']; |
| 75 | 75 | $limit = (int) $data['limit']; |
| 76 | - $tags = $this->get_terms_from_db( $limit, $offset ); |
|
| 76 | + $tags = $this->get_terms_from_db($limit, $offset); |
|
| 77 | 77 | $term_data_list = array(); |
| 78 | 78 | |
| 79 | - foreach ( $tags as $tag ) { |
|
| 79 | + foreach ($tags as $tag) { |
|
| 80 | 80 | |
| 81 | - if ( $this->is_tag_excluded_from_ui( $tag ) ) { |
|
| 81 | + if ($this->is_tag_excluded_from_ui($tag)) { |
|
| 82 | 82 | continue; |
| 83 | 83 | } |
| 84 | 84 | |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | */ |
| 88 | 88 | $term_data_instance = $this->term_data_factory->get_term_data($tag); |
| 89 | 89 | $term_data = $term_data_instance->get_data(); |
| 90 | - if ( $term_data['entities'] ) { |
|
| 90 | + if ($term_data['entities']) { |
|
| 91 | 91 | $term_data_list[] = $term_data; |
| 92 | 92 | } |
| 93 | 93 | } |
@@ -101,8 +101,8 @@ discard block |
||
| 101 | 101 | * |
| 102 | 102 | * @return bool |
| 103 | 103 | */ |
| 104 | - private function is_tag_excluded_from_ui( $tag ) { |
|
| 105 | - return (int) get_term_meta( $tag->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) === 1; |
|
| 104 | + private function is_tag_excluded_from_ui($tag) { |
|
| 105 | + return (int) get_term_meta($tag->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true) === 1; |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | /** |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | * |
| 112 | 112 | * @return int|\WP_Error|\WP_Term[] |
| 113 | 113 | */ |
| 114 | - public function get_terms_from_db( $limit, $offset ) { |
|
| 114 | + public function get_terms_from_db($limit, $offset) { |
|
| 115 | 115 | |
| 116 | 116 | |
| 117 | 117 | return Terms_Compat::get_terms(Terms_Compat::get_public_taxonomies(), array( |
@@ -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 | } |
@@ -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; |
@@ -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 | } |
@@ -6,143 +6,143 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Jsonld_Article_Wrapper { |
| 8 | 8 | |
| 9 | - public 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 | + public 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 |
@@ -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 | } |
@@ -10,76 +10,76 @@ |
||
| 10 | 10 | |
| 11 | 11 | class Video_Sitemap { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @var Ttl_Cache |
|
| 15 | - */ |
|
| 16 | - private $sitemap_cache; |
|
| 17 | - |
|
| 18 | - const XML_CACHE_KEY = 'video_sitemap'; |
|
| 19 | - |
|
| 20 | - public function __construct( $sitemap_cache ) { |
|
| 21 | - $this->sitemap_cache = $sitemap_cache; |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - public function init() { |
|
| 25 | - if ( self::is_video_sitemap_enabled() ) { |
|
| 26 | - add_action( 'template_redirect', array( $this, 'print_video_sitemap' ), 1 ); |
|
| 27 | - } |
|
| 28 | - add_action( 'wordlift_videoobject_video_storage_updated', array( $this, 'flush_cache' ) ); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - public function flush_cache() { |
|
| 32 | - $this->sitemap_cache->flush(); |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Print video sitemap. |
|
| 37 | - */ |
|
| 38 | - public function print_video_sitemap() { |
|
| 39 | - global $wp; |
|
| 40 | - |
|
| 41 | - $url = home_url( $wp->request ); |
|
| 42 | - |
|
| 43 | - $pattern = '/wl-video-sitemap\.xml$/m'; |
|
| 44 | - |
|
| 45 | - if ( preg_match( $pattern, $url ) !== 1 ) { |
|
| 46 | - return; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - header( "Content-type: text/xml" ); |
|
| 50 | - // set 200 status code. |
|
| 51 | - status_header(200); |
|
| 52 | - |
|
| 53 | - $xml = $this->sitemap_cache->get( self::XML_CACHE_KEY ); |
|
| 54 | - |
|
| 55 | - if ( ! $xml ) { |
|
| 56 | - $xml = $this->get_sitemap_xml(); |
|
| 57 | - $this->sitemap_cache->put( self::XML_CACHE_KEY, $xml ); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - echo $xml; |
|
| 61 | - die(); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - public static function is_video_sitemap_enabled() { |
|
| 65 | - return intval( get_option( '_wl_video_sitemap_generation', false ) ) === 1; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @return string |
|
| 70 | - */ |
|
| 71 | - private function get_sitemap_xml() { |
|
| 72 | - $sitemap_start_tag = <<<EOF |
|
| 13 | + /** |
|
| 14 | + * @var Ttl_Cache |
|
| 15 | + */ |
|
| 16 | + private $sitemap_cache; |
|
| 17 | + |
|
| 18 | + const XML_CACHE_KEY = 'video_sitemap'; |
|
| 19 | + |
|
| 20 | + public function __construct( $sitemap_cache ) { |
|
| 21 | + $this->sitemap_cache = $sitemap_cache; |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + public function init() { |
|
| 25 | + if ( self::is_video_sitemap_enabled() ) { |
|
| 26 | + add_action( 'template_redirect', array( $this, 'print_video_sitemap' ), 1 ); |
|
| 27 | + } |
|
| 28 | + add_action( 'wordlift_videoobject_video_storage_updated', array( $this, 'flush_cache' ) ); |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + public function flush_cache() { |
|
| 32 | + $this->sitemap_cache->flush(); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Print video sitemap. |
|
| 37 | + */ |
|
| 38 | + public function print_video_sitemap() { |
|
| 39 | + global $wp; |
|
| 40 | + |
|
| 41 | + $url = home_url( $wp->request ); |
|
| 42 | + |
|
| 43 | + $pattern = '/wl-video-sitemap\.xml$/m'; |
|
| 44 | + |
|
| 45 | + if ( preg_match( $pattern, $url ) !== 1 ) { |
|
| 46 | + return; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + header( "Content-type: text/xml" ); |
|
| 50 | + // set 200 status code. |
|
| 51 | + status_header(200); |
|
| 52 | + |
|
| 53 | + $xml = $this->sitemap_cache->get( self::XML_CACHE_KEY ); |
|
| 54 | + |
|
| 55 | + if ( ! $xml ) { |
|
| 56 | + $xml = $this->get_sitemap_xml(); |
|
| 57 | + $this->sitemap_cache->put( self::XML_CACHE_KEY, $xml ); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + echo $xml; |
|
| 61 | + die(); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + public static function is_video_sitemap_enabled() { |
|
| 65 | + return intval( get_option( '_wl_video_sitemap_generation', false ) ) === 1; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @return string |
|
| 70 | + */ |
|
| 71 | + private function get_sitemap_xml() { |
|
| 72 | + $sitemap_start_tag = <<<EOF |
|
| 73 | 73 | <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" |
| 74 | 74 | xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"> |
| 75 | 75 | EOF; |
| 76 | - $sitemap_body = Xml_Generator::get_xml_for_all_posts_with_videos(); |
|
| 76 | + $sitemap_body = Xml_Generator::get_xml_for_all_posts_with_videos(); |
|
| 77 | 77 | |
| 78 | - $sitemap_end_tag = "</urlset>"; |
|
| 78 | + $sitemap_end_tag = "</urlset>"; |
|
| 79 | 79 | |
| 80 | - $xml = $sitemap_start_tag . $sitemap_body . $sitemap_end_tag; |
|
| 80 | + $xml = $sitemap_start_tag . $sitemap_body . $sitemap_end_tag; |
|
| 81 | 81 | |
| 82 | - return $xml; |
|
| 83 | - } |
|
| 82 | + return $xml; |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | 85 | } |
| 86 | 86 | \ No newline at end of file |
@@ -17,15 +17,15 @@ discard block |
||
| 17 | 17 | |
| 18 | 18 | const XML_CACHE_KEY = 'video_sitemap'; |
| 19 | 19 | |
| 20 | - public function __construct( $sitemap_cache ) { |
|
| 20 | + public function __construct($sitemap_cache) { |
|
| 21 | 21 | $this->sitemap_cache = $sitemap_cache; |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | public function init() { |
| 25 | - if ( self::is_video_sitemap_enabled() ) { |
|
| 26 | - add_action( 'template_redirect', array( $this, 'print_video_sitemap' ), 1 ); |
|
| 25 | + if (self::is_video_sitemap_enabled()) { |
|
| 26 | + add_action('template_redirect', array($this, 'print_video_sitemap'), 1); |
|
| 27 | 27 | } |
| 28 | - add_action( 'wordlift_videoobject_video_storage_updated', array( $this, 'flush_cache' ) ); |
|
| 28 | + add_action('wordlift_videoobject_video_storage_updated', array($this, 'flush_cache')); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | public function flush_cache() { |
@@ -38,23 +38,23 @@ discard block |
||
| 38 | 38 | public function print_video_sitemap() { |
| 39 | 39 | global $wp; |
| 40 | 40 | |
| 41 | - $url = home_url( $wp->request ); |
|
| 41 | + $url = home_url($wp->request); |
|
| 42 | 42 | |
| 43 | 43 | $pattern = '/wl-video-sitemap\.xml$/m'; |
| 44 | 44 | |
| 45 | - if ( preg_match( $pattern, $url ) !== 1 ) { |
|
| 45 | + if (preg_match($pattern, $url) !== 1) { |
|
| 46 | 46 | return; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | - header( "Content-type: text/xml" ); |
|
| 49 | + header("Content-type: text/xml"); |
|
| 50 | 50 | // set 200 status code. |
| 51 | 51 | status_header(200); |
| 52 | 52 | |
| 53 | - $xml = $this->sitemap_cache->get( self::XML_CACHE_KEY ); |
|
| 53 | + $xml = $this->sitemap_cache->get(self::XML_CACHE_KEY); |
|
| 54 | 54 | |
| 55 | - if ( ! $xml ) { |
|
| 55 | + if ( ! $xml) { |
|
| 56 | 56 | $xml = $this->get_sitemap_xml(); |
| 57 | - $this->sitemap_cache->put( self::XML_CACHE_KEY, $xml ); |
|
| 57 | + $this->sitemap_cache->put(self::XML_CACHE_KEY, $xml); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | echo $xml; |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | public static function is_video_sitemap_enabled() { |
| 65 | - return intval( get_option( '_wl_video_sitemap_generation', false ) ) === 1; |
|
| 65 | + return intval(get_option('_wl_video_sitemap_generation', false)) === 1; |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
@@ -73,11 +73,11 @@ discard block |
||
| 73 | 73 | <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" |
| 74 | 74 | xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"> |
| 75 | 75 | EOF; |
| 76 | - $sitemap_body = Xml_Generator::get_xml_for_all_posts_with_videos(); |
|
| 76 | + $sitemap_body = Xml_Generator::get_xml_for_all_posts_with_videos(); |
|
| 77 | 77 | |
| 78 | 78 | $sitemap_end_tag = "</urlset>"; |
| 79 | 79 | |
| 80 | - $xml = $sitemap_start_tag . $sitemap_body . $sitemap_end_tag; |
|
| 80 | + $xml = $sitemap_start_tag.$sitemap_body.$sitemap_end_tag; |
|
| 81 | 81 | |
| 82 | 82 | return $xml; |
| 83 | 83 | } |
@@ -4,40 +4,40 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | class Import_Videos_Page { |
| 6 | 6 | |
| 7 | - /** |
|
| 8 | - * Sync_Page constructor. |
|
| 9 | - */ |
|
| 10 | - public function __construct() { |
|
| 7 | + /** |
|
| 8 | + * Sync_Page constructor. |
|
| 9 | + */ |
|
| 10 | + public function __construct() { |
|
| 11 | 11 | |
| 12 | - add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
| 12 | + add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
| 13 | 13 | |
| 14 | - } |
|
| 14 | + } |
|
| 15 | 15 | |
| 16 | - public function admin_menu() { |
|
| 16 | + public function admin_menu() { |
|
| 17 | 17 | |
| 18 | - add_submenu_page( 'wl_admin_menu', |
|
| 19 | - __( 'Import all videos', 'wordlift' ), |
|
| 20 | - __( 'Import all videos', 'wordlift' ), |
|
| 21 | - 'manage_options', 'wl_videos_import', array( |
|
| 22 | - $this, |
|
| 23 | - 'render' |
|
| 24 | - ) ); |
|
| 18 | + add_submenu_page( 'wl_admin_menu', |
|
| 19 | + __( 'Import all videos', 'wordlift' ), |
|
| 20 | + __( 'Import all videos', 'wordlift' ), |
|
| 21 | + 'manage_options', 'wl_videos_import', array( |
|
| 22 | + $this, |
|
| 23 | + 'render' |
|
| 24 | + ) ); |
|
| 25 | 25 | |
| 26 | - } |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - public function render() { |
|
| 28 | + public function render() { |
|
| 29 | 29 | |
| 30 | - wp_enqueue_script( |
|
| 31 | - 'wl-videos-sync-page', |
|
| 32 | - plugin_dir_url( __FILE__ ) . 'assets/videoobject-import-page.js', |
|
| 33 | - array( 'wp-api' ), |
|
| 34 | - \Wordlift::get_instance()->get_version() ); |
|
| 35 | - wp_localize_script( 'wl-videos-sync-page', '_wlVideoObjectImportSettings', array( |
|
| 36 | - 'restUrl' => get_rest_url(), |
|
| 37 | - 'nonce' => wp_create_nonce( 'wp_rest' ) |
|
| 38 | - ) ) |
|
| 30 | + wp_enqueue_script( |
|
| 31 | + 'wl-videos-sync-page', |
|
| 32 | + plugin_dir_url( __FILE__ ) . 'assets/videoobject-import-page.js', |
|
| 33 | + array( 'wp-api' ), |
|
| 34 | + \Wordlift::get_instance()->get_version() ); |
|
| 35 | + wp_localize_script( 'wl-videos-sync-page', '_wlVideoObjectImportSettings', array( |
|
| 36 | + 'restUrl' => get_rest_url(), |
|
| 37 | + 'nonce' => wp_create_nonce( 'wp_rest' ) |
|
| 38 | + ) ) |
|
| 39 | 39 | |
| 40 | - ?> |
|
| 40 | + ?> |
|
| 41 | 41 | <div class="wrap"> |
| 42 | 42 | <h2><?php esc_html_e( 'Import all videos', 'wordlift' ); ?></h2> |
| 43 | 43 | |
@@ -47,12 +47,12 @@ discard block |
||
| 47 | 47 | </div> |
| 48 | 48 | |
| 49 | 49 | <button id="wl-start-btn" type="button" class="button button-large button-primary"><?php |
| 50 | - esc_html_e( 'Start', 'wordlift' ); ?></button> |
|
| 50 | + esc_html_e( 'Start', 'wordlift' ); ?></button> |
|
| 51 | 51 | <button id="wl-stop-btn" type="button" class="button button-large button-primary hidden"><?php |
| 52 | - esc_html_e( 'Stop', 'wordlift' ); ?></button> |
|
| 52 | + esc_html_e( 'Stop', 'wordlift' ); ?></button> |
|
| 53 | 53 | |
| 54 | 54 | </div> |
| 55 | 55 | <?php |
| 56 | - } |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | 58 | } |
| 59 | 59 | \ No newline at end of file |
@@ -9,19 +9,19 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | public function __construct() { |
| 11 | 11 | |
| 12 | - add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
| 12 | + add_action('admin_menu', array($this, 'admin_menu')); |
|
| 13 | 13 | |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | public function admin_menu() { |
| 17 | 17 | |
| 18 | - add_submenu_page( 'wl_admin_menu', |
|
| 19 | - __( 'Import all videos', 'wordlift' ), |
|
| 20 | - __( 'Import all videos', 'wordlift' ), |
|
| 18 | + add_submenu_page('wl_admin_menu', |
|
| 19 | + __('Import all videos', 'wordlift'), |
|
| 20 | + __('Import all videos', 'wordlift'), |
|
| 21 | 21 | 'manage_options', 'wl_videos_import', array( |
| 22 | 22 | $this, |
| 23 | 23 | 'render' |
| 24 | - ) ); |
|
| 24 | + )); |
|
| 25 | 25 | |
| 26 | 26 | } |
| 27 | 27 | |
@@ -29,17 +29,17 @@ discard block |
||
| 29 | 29 | |
| 30 | 30 | wp_enqueue_script( |
| 31 | 31 | 'wl-videos-sync-page', |
| 32 | - plugin_dir_url( __FILE__ ) . 'assets/videoobject-import-page.js', |
|
| 33 | - array( 'wp-api' ), |
|
| 32 | + plugin_dir_url(__FILE__).'assets/videoobject-import-page.js', |
|
| 33 | + array('wp-api'), |
|
| 34 | 34 | \Wordlift::get_instance()->get_version() ); |
| 35 | - wp_localize_script( 'wl-videos-sync-page', '_wlVideoObjectImportSettings', array( |
|
| 35 | + wp_localize_script('wl-videos-sync-page', '_wlVideoObjectImportSettings', array( |
|
| 36 | 36 | 'restUrl' => get_rest_url(), |
| 37 | - 'nonce' => wp_create_nonce( 'wp_rest' ) |
|
| 38 | - ) ) |
|
| 37 | + 'nonce' => wp_create_nonce('wp_rest') |
|
| 38 | + )) |
|
| 39 | 39 | |
| 40 | 40 | ?> |
| 41 | 41 | <div class="wrap"> |
| 42 | - <h2><?php esc_html_e( 'Import all videos', 'wordlift' ); ?></h2> |
|
| 42 | + <h2><?php esc_html_e('Import all videos', 'wordlift'); ?></h2> |
|
| 43 | 43 | |
| 44 | 44 | <div class="wl-task__progress" style="border: 1px solid #23282D; height: 20px; margin: 8px 0;"> |
| 45 | 45 | <div class="wl-task__progress__bar" |
@@ -47,9 +47,9 @@ discard block |
||
| 47 | 47 | </div> |
| 48 | 48 | |
| 49 | 49 | <button id="wl-start-btn" type="button" class="button button-large button-primary"><?php |
| 50 | - esc_html_e( 'Start', 'wordlift' ); ?></button> |
|
| 50 | + esc_html_e('Start', 'wordlift'); ?></button> |
|
| 51 | 51 | <button id="wl-stop-btn" type="button" class="button button-large button-primary hidden"><?php |
| 52 | - esc_html_e( 'Stop', 'wordlift' ); ?></button> |
|
| 52 | + esc_html_e('Stop', 'wordlift'); ?></button> |
|
| 53 | 53 | |
| 54 | 54 | </div> |
| 55 | 55 | <?php |
@@ -16,164 +16,164 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Related_Entities_Cloud_Shortcode extends Wordlift_Shortcode { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * {@inheritdoc} |
|
| 21 | - */ |
|
| 22 | - const SHORTCODE = 'wl_cloud'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * The {@link Wordlift_Relation_Service} instance. |
|
| 26 | - * |
|
| 27 | - * @since 3.15.0 |
|
| 28 | - * @access private |
|
| 29 | - * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 30 | - */ |
|
| 31 | - private $relation_service; |
|
| 32 | - /** |
|
| 33 | - * @var Wordlift_Entity_Service |
|
| 34 | - */ |
|
| 35 | - private $entity_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Create a {@link Wordlift_Related_Entities_Cloud_Shortcode} instance. |
|
| 39 | - * |
|
| 40 | - * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 41 | - * @param \Wordlift_Entity_Service $entity_service |
|
| 42 | - * |
|
| 43 | - * @since 3.15.0 |
|
| 44 | - * |
|
| 45 | - */ |
|
| 46 | - public function __construct( $relation_service, $entity_service ) { |
|
| 47 | - parent::__construct(); |
|
| 48 | - |
|
| 49 | - $this->relation_service = $relation_service; |
|
| 50 | - |
|
| 51 | - $this->entity_service = $entity_service; |
|
| 52 | - |
|
| 53 | - $this->register_block_type(); |
|
| 54 | - |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * {@inheritdoc} |
|
| 59 | - */ |
|
| 60 | - public function render( $atts ) { |
|
| 61 | - |
|
| 62 | - $tags = $this->get_related_entities_tags(); |
|
| 63 | - |
|
| 64 | - // Bail out if there are no associated entities. |
|
| 65 | - if ( empty( $tags ) ) { |
|
| 66 | - return ''; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /* |
|
| 19 | + /** |
|
| 20 | + * {@inheritdoc} |
|
| 21 | + */ |
|
| 22 | + const SHORTCODE = 'wl_cloud'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * The {@link Wordlift_Relation_Service} instance. |
|
| 26 | + * |
|
| 27 | + * @since 3.15.0 |
|
| 28 | + * @access private |
|
| 29 | + * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 30 | + */ |
|
| 31 | + private $relation_service; |
|
| 32 | + /** |
|
| 33 | + * @var Wordlift_Entity_Service |
|
| 34 | + */ |
|
| 35 | + private $entity_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Create a {@link Wordlift_Related_Entities_Cloud_Shortcode} instance. |
|
| 39 | + * |
|
| 40 | + * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 41 | + * @param \Wordlift_Entity_Service $entity_service |
|
| 42 | + * |
|
| 43 | + * @since 3.15.0 |
|
| 44 | + * |
|
| 45 | + */ |
|
| 46 | + public function __construct( $relation_service, $entity_service ) { |
|
| 47 | + parent::__construct(); |
|
| 48 | + |
|
| 49 | + $this->relation_service = $relation_service; |
|
| 50 | + |
|
| 51 | + $this->entity_service = $entity_service; |
|
| 52 | + |
|
| 53 | + $this->register_block_type(); |
|
| 54 | + |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * {@inheritdoc} |
|
| 59 | + */ |
|
| 60 | + public function render( $atts ) { |
|
| 61 | + |
|
| 62 | + $tags = $this->get_related_entities_tags(); |
|
| 63 | + |
|
| 64 | + // Bail out if there are no associated entities. |
|
| 65 | + if ( empty( $tags ) ) { |
|
| 66 | + return ''; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /* |
|
| 70 | 70 | * Since the output is use in the widget as well, we need to have the |
| 71 | 71 | * same class as the core tagcloud widget, to easily inherit its styling. |
| 72 | 72 | */ |
| 73 | 73 | |
| 74 | - return '<div class="tagcloud wl-related-entities-cloud">' . |
|
| 75 | - wp_generate_tag_cloud( $tags ) . |
|
| 76 | - '</div>'; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - private function register_block_type() { |
|
| 80 | - |
|
| 81 | - $scope = $this; |
|
| 82 | - |
|
| 83 | - add_action( 'init', function () use ( $scope ) { |
|
| 84 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 85 | - // Gutenberg is not active. |
|
| 86 | - return; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - register_block_type( 'wordlift/cloud', array( |
|
| 90 | - 'editor_script' => 'wl-block-editor', |
|
| 91 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 92 | - $attr_code = ''; |
|
| 93 | - foreach ( $attributes as $key => $value ) { |
|
| 94 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 98 | - }, |
|
| 99 | - 'attributes' => array( |
|
| 100 | - 'preview' => array( |
|
| 101 | - 'type' => 'boolean', |
|
| 102 | - 'default' => false, |
|
| 103 | - ), |
|
| 104 | - 'preview_src' => array( |
|
| 105 | - 'type' => 'string', |
|
| 106 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/cloud.png', |
|
| 107 | - ), |
|
| 108 | - ), |
|
| 109 | - ) ); |
|
| 110 | - } ); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Find the related entities to the currently displayed post and |
|
| 115 | - * calculate the "tags" for them as wp_generate_tag_cloud expects to get. |
|
| 116 | - * |
|
| 117 | - * @return array Array of tags. Empty array in case we re not in a context |
|
| 118 | - * of a post, or it has no related entities. |
|
| 119 | - * @since 3.11.0 |
|
| 120 | - * |
|
| 121 | - */ |
|
| 122 | - public function get_related_entities_tags() { |
|
| 123 | - |
|
| 124 | - // Define the supported types list. |
|
| 125 | - $supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 126 | - |
|
| 127 | - // Show nothing if not on a post or entity page. |
|
| 128 | - if ( ! is_singular( $supported_types ) ) { |
|
| 129 | - return array(); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - // Get the IDs of entities related to current post. |
|
| 133 | - $related_entities = wl_core_get_related_entity_ids( get_the_ID(), array( 'status' => 'publish' ) ); |
|
| 134 | - |
|
| 135 | - // Bail out if there are no associated entities. |
|
| 136 | - if ( empty( $related_entities ) ) { |
|
| 137 | - return array(); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /* |
|
| 74 | + return '<div class="tagcloud wl-related-entities-cloud">' . |
|
| 75 | + wp_generate_tag_cloud( $tags ) . |
|
| 76 | + '</div>'; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + private function register_block_type() { |
|
| 80 | + |
|
| 81 | + $scope = $this; |
|
| 82 | + |
|
| 83 | + add_action( 'init', function () use ( $scope ) { |
|
| 84 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 85 | + // Gutenberg is not active. |
|
| 86 | + return; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + register_block_type( 'wordlift/cloud', array( |
|
| 90 | + 'editor_script' => 'wl-block-editor', |
|
| 91 | + 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 92 | + $attr_code = ''; |
|
| 93 | + foreach ( $attributes as $key => $value ) { |
|
| 94 | + $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 98 | + }, |
|
| 99 | + 'attributes' => array( |
|
| 100 | + 'preview' => array( |
|
| 101 | + 'type' => 'boolean', |
|
| 102 | + 'default' => false, |
|
| 103 | + ), |
|
| 104 | + 'preview_src' => array( |
|
| 105 | + 'type' => 'string', |
|
| 106 | + 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/cloud.png', |
|
| 107 | + ), |
|
| 108 | + ), |
|
| 109 | + ) ); |
|
| 110 | + } ); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Find the related entities to the currently displayed post and |
|
| 115 | + * calculate the "tags" for them as wp_generate_tag_cloud expects to get. |
|
| 116 | + * |
|
| 117 | + * @return array Array of tags. Empty array in case we re not in a context |
|
| 118 | + * of a post, or it has no related entities. |
|
| 119 | + * @since 3.11.0 |
|
| 120 | + * |
|
| 121 | + */ |
|
| 122 | + public function get_related_entities_tags() { |
|
| 123 | + |
|
| 124 | + // Define the supported types list. |
|
| 125 | + $supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 126 | + |
|
| 127 | + // Show nothing if not on a post or entity page. |
|
| 128 | + if ( ! is_singular( $supported_types ) ) { |
|
| 129 | + return array(); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + // Get the IDs of entities related to current post. |
|
| 133 | + $related_entities = wl_core_get_related_entity_ids( get_the_ID(), array( 'status' => 'publish' ) ); |
|
| 134 | + |
|
| 135 | + // Bail out if there are no associated entities. |
|
| 136 | + if ( empty( $related_entities ) ) { |
|
| 137 | + return array(); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /* |
|
| 141 | 141 | * Create an array of "tags" to feed to wp_generate_tag_cloud. |
| 142 | 142 | * Use the number of posts and entities connected to the entity as a weight. |
| 143 | 143 | */ |
| 144 | - $tags = array(); |
|
| 145 | - |
|
| 146 | - foreach ( array_unique( $related_entities ) as $entity_id ) { |
|
| 147 | - |
|
| 148 | - $connected_entities = count( wl_core_get_related_entity_ids( $entity_id, array( 'status' => 'publish' ) ) ); |
|
| 149 | - $connected_posts = count( $this->relation_service->get_article_subjects( $entity_id, '*', null, 'publish' ) ); |
|
| 150 | - /** |
|
| 151 | - * @since 3.31.5 |
|
| 152 | - * if synonym exists, use it instead of entity name. |
|
| 153 | - */ |
|
| 154 | - $synonyms = $this->get_synonyms( $entity_id ); |
|
| 155 | - $entity_name = count( $synonyms ) > 0 ? $synonyms[0] : get_the_title( $entity_id ); |
|
| 156 | - |
|
| 157 | - $tags[] = (object) array( |
|
| 158 | - 'id' => $entity_id, |
|
| 159 | - // Used to give a unique class on the tag. |
|
| 160 | - 'name' => $entity_name, |
|
| 161 | - // The text of the tag. |
|
| 162 | - 'slug' => get_the_title( $entity_id ), |
|
| 163 | - // Required but not seem to be relevant |
|
| 164 | - 'link' => get_permalink( $entity_id ), |
|
| 165 | - // the url the tag links to. |
|
| 166 | - 'count' => $connected_entities + $connected_posts, |
|
| 167 | - // The weight. |
|
| 168 | - ); |
|
| 169 | - |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - return $tags; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - private function get_synonyms( $entity_id ) { |
|
| 176 | - return $this->entity_service->get_alternative_labels( $entity_id ); |
|
| 177 | - } |
|
| 144 | + $tags = array(); |
|
| 145 | + |
|
| 146 | + foreach ( array_unique( $related_entities ) as $entity_id ) { |
|
| 147 | + |
|
| 148 | + $connected_entities = count( wl_core_get_related_entity_ids( $entity_id, array( 'status' => 'publish' ) ) ); |
|
| 149 | + $connected_posts = count( $this->relation_service->get_article_subjects( $entity_id, '*', null, 'publish' ) ); |
|
| 150 | + /** |
|
| 151 | + * @since 3.31.5 |
|
| 152 | + * if synonym exists, use it instead of entity name. |
|
| 153 | + */ |
|
| 154 | + $synonyms = $this->get_synonyms( $entity_id ); |
|
| 155 | + $entity_name = count( $synonyms ) > 0 ? $synonyms[0] : get_the_title( $entity_id ); |
|
| 156 | + |
|
| 157 | + $tags[] = (object) array( |
|
| 158 | + 'id' => $entity_id, |
|
| 159 | + // Used to give a unique class on the tag. |
|
| 160 | + 'name' => $entity_name, |
|
| 161 | + // The text of the tag. |
|
| 162 | + 'slug' => get_the_title( $entity_id ), |
|
| 163 | + // Required but not seem to be relevant |
|
| 164 | + 'link' => get_permalink( $entity_id ), |
|
| 165 | + // the url the tag links to. |
|
| 166 | + 'count' => $connected_entities + $connected_posts, |
|
| 167 | + // The weight. |
|
| 168 | + ); |
|
| 169 | + |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + return $tags; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + private function get_synonyms( $entity_id ) { |
|
| 176 | + return $this->entity_service->get_alternative_labels( $entity_id ); |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | 179 | } |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | * @since 3.15.0 |
| 44 | 44 | * |
| 45 | 45 | */ |
| 46 | - public function __construct( $relation_service, $entity_service ) { |
|
| 46 | + public function __construct($relation_service, $entity_service) { |
|
| 47 | 47 | parent::__construct(); |
| 48 | 48 | |
| 49 | 49 | $this->relation_service = $relation_service; |
@@ -57,12 +57,12 @@ discard block |
||
| 57 | 57 | /** |
| 58 | 58 | * {@inheritdoc} |
| 59 | 59 | */ |
| 60 | - public function render( $atts ) { |
|
| 60 | + public function render($atts) { |
|
| 61 | 61 | |
| 62 | 62 | $tags = $this->get_related_entities_tags(); |
| 63 | 63 | |
| 64 | 64 | // Bail out if there are no associated entities. |
| 65 | - if ( empty( $tags ) ) { |
|
| 65 | + if (empty($tags)) { |
|
| 66 | 66 | return ''; |
| 67 | 67 | } |
| 68 | 68 | |
@@ -71,8 +71,8 @@ discard block |
||
| 71 | 71 | * same class as the core tagcloud widget, to easily inherit its styling. |
| 72 | 72 | */ |
| 73 | 73 | |
| 74 | - return '<div class="tagcloud wl-related-entities-cloud">' . |
|
| 75 | - wp_generate_tag_cloud( $tags ) . |
|
| 74 | + return '<div class="tagcloud wl-related-entities-cloud">'. |
|
| 75 | + wp_generate_tag_cloud($tags). |
|
| 76 | 76 | '</div>'; |
| 77 | 77 | } |
| 78 | 78 | |
@@ -80,21 +80,21 @@ discard block |
||
| 80 | 80 | |
| 81 | 81 | $scope = $this; |
| 82 | 82 | |
| 83 | - add_action( 'init', function () use ( $scope ) { |
|
| 84 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 83 | + add_action('init', function() use ($scope) { |
|
| 84 | + if ( ! function_exists('register_block_type')) { |
|
| 85 | 85 | // Gutenberg is not active. |
| 86 | 86 | return; |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | - register_block_type( 'wordlift/cloud', array( |
|
| 89 | + register_block_type('wordlift/cloud', array( |
|
| 90 | 90 | 'editor_script' => 'wl-block-editor', |
| 91 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 91 | + 'render_callback' => function($attributes) use ($scope) { |
|
| 92 | 92 | $attr_code = ''; |
| 93 | - foreach ( $attributes as $key => $value ) { |
|
| 94 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 93 | + foreach ($attributes as $key => $value) { |
|
| 94 | + $attr_code .= $key.'="'.htmlentities($value).'" '; |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 97 | + return '['.$scope::SHORTCODE.' '.$attr_code.']'; |
|
| 98 | 98 | }, |
| 99 | 99 | 'attributes' => array( |
| 100 | 100 | 'preview' => array( |
@@ -103,10 +103,10 @@ discard block |
||
| 103 | 103 | ), |
| 104 | 104 | 'preview_src' => array( |
| 105 | 105 | 'type' => 'string', |
| 106 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/cloud.png', |
|
| 106 | + 'default' => WP_CONTENT_URL.'/plugins/wordlift/images/block-previews/cloud.png', |
|
| 107 | 107 | ), |
| 108 | 108 | ), |
| 109 | - ) ); |
|
| 109 | + )); |
|
| 110 | 110 | } ); |
| 111 | 111 | } |
| 112 | 112 | |
@@ -125,15 +125,15 @@ discard block |
||
| 125 | 125 | $supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
| 126 | 126 | |
| 127 | 127 | // Show nothing if not on a post or entity page. |
| 128 | - if ( ! is_singular( $supported_types ) ) { |
|
| 128 | + if ( ! is_singular($supported_types)) { |
|
| 129 | 129 | return array(); |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | // Get the IDs of entities related to current post. |
| 133 | - $related_entities = wl_core_get_related_entity_ids( get_the_ID(), array( 'status' => 'publish' ) ); |
|
| 133 | + $related_entities = wl_core_get_related_entity_ids(get_the_ID(), array('status' => 'publish')); |
|
| 134 | 134 | |
| 135 | 135 | // Bail out if there are no associated entities. |
| 136 | - if ( empty( $related_entities ) ) { |
|
| 136 | + if (empty($related_entities)) { |
|
| 137 | 137 | return array(); |
| 138 | 138 | } |
| 139 | 139 | |
@@ -143,25 +143,25 @@ discard block |
||
| 143 | 143 | */ |
| 144 | 144 | $tags = array(); |
| 145 | 145 | |
| 146 | - foreach ( array_unique( $related_entities ) as $entity_id ) { |
|
| 146 | + foreach (array_unique($related_entities) as $entity_id) { |
|
| 147 | 147 | |
| 148 | - $connected_entities = count( wl_core_get_related_entity_ids( $entity_id, array( 'status' => 'publish' ) ) ); |
|
| 149 | - $connected_posts = count( $this->relation_service->get_article_subjects( $entity_id, '*', null, 'publish' ) ); |
|
| 148 | + $connected_entities = count(wl_core_get_related_entity_ids($entity_id, array('status' => 'publish'))); |
|
| 149 | + $connected_posts = count($this->relation_service->get_article_subjects($entity_id, '*', null, 'publish')); |
|
| 150 | 150 | /** |
| 151 | 151 | * @since 3.31.5 |
| 152 | 152 | * if synonym exists, use it instead of entity name. |
| 153 | 153 | */ |
| 154 | - $synonyms = $this->get_synonyms( $entity_id ); |
|
| 155 | - $entity_name = count( $synonyms ) > 0 ? $synonyms[0] : get_the_title( $entity_id ); |
|
| 154 | + $synonyms = $this->get_synonyms($entity_id); |
|
| 155 | + $entity_name = count($synonyms) > 0 ? $synonyms[0] : get_the_title($entity_id); |
|
| 156 | 156 | |
| 157 | 157 | $tags[] = (object) array( |
| 158 | 158 | 'id' => $entity_id, |
| 159 | 159 | // Used to give a unique class on the tag. |
| 160 | 160 | 'name' => $entity_name, |
| 161 | 161 | // The text of the tag. |
| 162 | - 'slug' => get_the_title( $entity_id ), |
|
| 162 | + 'slug' => get_the_title($entity_id), |
|
| 163 | 163 | // Required but not seem to be relevant |
| 164 | - 'link' => get_permalink( $entity_id ), |
|
| 164 | + 'link' => get_permalink($entity_id), |
|
| 165 | 165 | // the url the tag links to. |
| 166 | 166 | 'count' => $connected_entities + $connected_posts, |
| 167 | 167 | // The weight. |
@@ -172,8 +172,8 @@ discard block |
||
| 172 | 172 | return $tags; |
| 173 | 173 | } |
| 174 | 174 | |
| 175 | - private function get_synonyms( $entity_id ) { |
|
| 176 | - return $this->entity_service->get_alternative_labels( $entity_id ); |
|
| 175 | + private function get_synonyms($entity_id) { |
|
| 176 | + return $this->entity_service->get_alternative_labels($entity_id); |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | } |
@@ -12,110 +12,110 @@ discard block |
||
| 12 | 12 | |
| 13 | 13 | class Xml_Generator { |
| 14 | 14 | |
| 15 | - private static function iso8601_to_seconds( $iso8601_interval_string ) { |
|
| 16 | - try { |
|
| 17 | - $interval = new \DateInterval( $iso8601_interval_string ); |
|
| 18 | - } catch ( \Exception $e ) { |
|
| 19 | - return 0; |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - $days_to_seconds = $interval->d * 60 * 60 * 24; |
|
| 23 | - $hours_to_seconds = $interval->h * 60 * 60; |
|
| 24 | - $minutes_to_seconds = $interval->i * 60; |
|
| 25 | - $seconds = $interval->s; |
|
| 26 | - |
|
| 27 | - return $days_to_seconds + $hours_to_seconds + $minutes_to_seconds + $seconds; |
|
| 28 | - |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - public static function get_xml_for_all_posts_with_videos() { |
|
| 32 | - /** |
|
| 33 | - * @since 3.31.6 |
|
| 34 | - * Filter the query args, add support for all custom public post types. |
|
| 35 | - */ |
|
| 36 | - $video_sitemap_query_args = apply_filters( 'wl_videoobject_sitemap_query_args', array( |
|
| 37 | - 'fields' => 'ids', |
|
| 38 | - 'numberposts' => - 1, |
|
| 39 | - 'post_type' => get_post_types( array( 'public' => true ) ), |
|
| 40 | - 'meta_query' => array( |
|
| 41 | - array( |
|
| 42 | - 'key' => Meta_Storage::META_KEY, |
|
| 43 | - 'compare' => 'EXISTS' |
|
| 44 | - ) |
|
| 45 | - ) |
|
| 46 | - ) ); |
|
| 47 | - |
|
| 48 | - $posts = get_posts( $video_sitemap_query_args ); |
|
| 49 | - |
|
| 50 | - $all_posts_xml = ""; |
|
| 51 | - |
|
| 52 | - if ( ! $posts ) { |
|
| 53 | - return $all_posts_xml; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - foreach ( $posts as $post_id ) { |
|
| 57 | - $all_posts_xml .= self::get_xml_for_single_post( $post_id ); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - return $all_posts_xml; |
|
| 61 | - |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @param $post_id |
|
| 67 | - * |
|
| 68 | - * @return string XML string for single post. |
|
| 69 | - */ |
|
| 70 | - public static function get_xml_for_single_post( $post_id ) { |
|
| 71 | - $videos = Video_Storage_Factory::get_storage()->get_all_videos( $post_id ); |
|
| 72 | - if ( ! $videos ) { |
|
| 73 | - return ""; |
|
| 74 | - } |
|
| 75 | - $single_post_xml = ""; |
|
| 76 | - foreach ( $videos as $video ) { |
|
| 77 | - $single_post_xml .= self::get_xml_for_single_video( $video, $post_id ); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - return $single_post_xml; |
|
| 81 | - |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param $video Video |
|
| 87 | - * @param $post_id int |
|
| 88 | - * |
|
| 89 | - * @return string |
|
| 90 | - */ |
|
| 91 | - public static function get_xml_for_single_video( $video, $post_id ) { |
|
| 92 | - |
|
| 93 | - $permalink = get_permalink( $post_id ); |
|
| 94 | - $title = esc_html( $video->name ); |
|
| 95 | - $description = esc_html( $video->description ); |
|
| 96 | - $thumbnail_url = $video->thumbnail_urls[0]; |
|
| 97 | - |
|
| 98 | - // If description is empty use title. |
|
| 99 | - if ( ! $description ) { |
|
| 100 | - $description = $title; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $optional_fields = array( |
|
| 104 | - 'content_loc' => $video->content_url, |
|
| 105 | - 'player_loc' => $video->embed_url, |
|
| 106 | - 'duration' => self::iso8601_to_seconds( $video->duration ), |
|
| 107 | - 'view_count' => $video->views, |
|
| 108 | - 'live' => $video->is_live_video ? 'yes' : 'no' |
|
| 109 | - ); |
|
| 110 | - |
|
| 111 | - $optional_data = ""; |
|
| 112 | - foreach ( $optional_fields as $xml_key => $xml_value ) { |
|
| 113 | - if ( $xml_value ) { |
|
| 114 | - $optional_data .= "<video:${xml_key}>${xml_value}</video:${xml_key}>"; |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - return <<<EOF |
|
| 15 | + private static function iso8601_to_seconds( $iso8601_interval_string ) { |
|
| 16 | + try { |
|
| 17 | + $interval = new \DateInterval( $iso8601_interval_string ); |
|
| 18 | + } catch ( \Exception $e ) { |
|
| 19 | + return 0; |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + $days_to_seconds = $interval->d * 60 * 60 * 24; |
|
| 23 | + $hours_to_seconds = $interval->h * 60 * 60; |
|
| 24 | + $minutes_to_seconds = $interval->i * 60; |
|
| 25 | + $seconds = $interval->s; |
|
| 26 | + |
|
| 27 | + return $days_to_seconds + $hours_to_seconds + $minutes_to_seconds + $seconds; |
|
| 28 | + |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + public static function get_xml_for_all_posts_with_videos() { |
|
| 32 | + /** |
|
| 33 | + * @since 3.31.6 |
|
| 34 | + * Filter the query args, add support for all custom public post types. |
|
| 35 | + */ |
|
| 36 | + $video_sitemap_query_args = apply_filters( 'wl_videoobject_sitemap_query_args', array( |
|
| 37 | + 'fields' => 'ids', |
|
| 38 | + 'numberposts' => - 1, |
|
| 39 | + 'post_type' => get_post_types( array( 'public' => true ) ), |
|
| 40 | + 'meta_query' => array( |
|
| 41 | + array( |
|
| 42 | + 'key' => Meta_Storage::META_KEY, |
|
| 43 | + 'compare' => 'EXISTS' |
|
| 44 | + ) |
|
| 45 | + ) |
|
| 46 | + ) ); |
|
| 47 | + |
|
| 48 | + $posts = get_posts( $video_sitemap_query_args ); |
|
| 49 | + |
|
| 50 | + $all_posts_xml = ""; |
|
| 51 | + |
|
| 52 | + if ( ! $posts ) { |
|
| 53 | + return $all_posts_xml; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + foreach ( $posts as $post_id ) { |
|
| 57 | + $all_posts_xml .= self::get_xml_for_single_post( $post_id ); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + return $all_posts_xml; |
|
| 61 | + |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @param $post_id |
|
| 67 | + * |
|
| 68 | + * @return string XML string for single post. |
|
| 69 | + */ |
|
| 70 | + public static function get_xml_for_single_post( $post_id ) { |
|
| 71 | + $videos = Video_Storage_Factory::get_storage()->get_all_videos( $post_id ); |
|
| 72 | + if ( ! $videos ) { |
|
| 73 | + return ""; |
|
| 74 | + } |
|
| 75 | + $single_post_xml = ""; |
|
| 76 | + foreach ( $videos as $video ) { |
|
| 77 | + $single_post_xml .= self::get_xml_for_single_video( $video, $post_id ); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + return $single_post_xml; |
|
| 81 | + |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param $video Video |
|
| 87 | + * @param $post_id int |
|
| 88 | + * |
|
| 89 | + * @return string |
|
| 90 | + */ |
|
| 91 | + public static function get_xml_for_single_video( $video, $post_id ) { |
|
| 92 | + |
|
| 93 | + $permalink = get_permalink( $post_id ); |
|
| 94 | + $title = esc_html( $video->name ); |
|
| 95 | + $description = esc_html( $video->description ); |
|
| 96 | + $thumbnail_url = $video->thumbnail_urls[0]; |
|
| 97 | + |
|
| 98 | + // If description is empty use title. |
|
| 99 | + if ( ! $description ) { |
|
| 100 | + $description = $title; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $optional_fields = array( |
|
| 104 | + 'content_loc' => $video->content_url, |
|
| 105 | + 'player_loc' => $video->embed_url, |
|
| 106 | + 'duration' => self::iso8601_to_seconds( $video->duration ), |
|
| 107 | + 'view_count' => $video->views, |
|
| 108 | + 'live' => $video->is_live_video ? 'yes' : 'no' |
|
| 109 | + ); |
|
| 110 | + |
|
| 111 | + $optional_data = ""; |
|
| 112 | + foreach ( $optional_fields as $xml_key => $xml_value ) { |
|
| 113 | + if ( $xml_value ) { |
|
| 114 | + $optional_data .= "<video:${xml_key}>${xml_value}</video:${xml_key}>"; |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + return <<<EOF |
|
| 119 | 119 | <url> |
| 120 | 120 | <loc>${permalink}</loc> |
| 121 | 121 | <video:video> |
@@ -127,6 +127,6 @@ discard block |
||
| 127 | 127 | </url> |
| 128 | 128 | EOF; |
| 129 | 129 | |
| 130 | - } |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | 132 | } |
| 133 | 133 | \ No newline at end of file |
@@ -12,10 +12,10 @@ discard block |
||
| 12 | 12 | |
| 13 | 13 | class Xml_Generator { |
| 14 | 14 | |
| 15 | - private static function iso8601_to_seconds( $iso8601_interval_string ) { |
|
| 15 | + private static function iso8601_to_seconds($iso8601_interval_string) { |
|
| 16 | 16 | try { |
| 17 | - $interval = new \DateInterval( $iso8601_interval_string ); |
|
| 18 | - } catch ( \Exception $e ) { |
|
| 17 | + $interval = new \DateInterval($iso8601_interval_string); |
|
| 18 | + } catch (\Exception $e) { |
|
| 19 | 19 | return 0; |
| 20 | 20 | } |
| 21 | 21 | |
@@ -33,28 +33,28 @@ discard block |
||
| 33 | 33 | * @since 3.31.6 |
| 34 | 34 | * Filter the query args, add support for all custom public post types. |
| 35 | 35 | */ |
| 36 | - $video_sitemap_query_args = apply_filters( 'wl_videoobject_sitemap_query_args', array( |
|
| 36 | + $video_sitemap_query_args = apply_filters('wl_videoobject_sitemap_query_args', array( |
|
| 37 | 37 | 'fields' => 'ids', |
| 38 | - 'numberposts' => - 1, |
|
| 39 | - 'post_type' => get_post_types( array( 'public' => true ) ), |
|
| 38 | + 'numberposts' => -1, |
|
| 39 | + 'post_type' => get_post_types(array('public' => true)), |
|
| 40 | 40 | 'meta_query' => array( |
| 41 | 41 | array( |
| 42 | 42 | 'key' => Meta_Storage::META_KEY, |
| 43 | 43 | 'compare' => 'EXISTS' |
| 44 | 44 | ) |
| 45 | 45 | ) |
| 46 | - ) ); |
|
| 46 | + )); |
|
| 47 | 47 | |
| 48 | - $posts = get_posts( $video_sitemap_query_args ); |
|
| 48 | + $posts = get_posts($video_sitemap_query_args); |
|
| 49 | 49 | |
| 50 | 50 | $all_posts_xml = ""; |
| 51 | 51 | |
| 52 | - if ( ! $posts ) { |
|
| 52 | + if ( ! $posts) { |
|
| 53 | 53 | return $all_posts_xml; |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | - foreach ( $posts as $post_id ) { |
|
| 57 | - $all_posts_xml .= self::get_xml_for_single_post( $post_id ); |
|
| 56 | + foreach ($posts as $post_id) { |
|
| 57 | + $all_posts_xml .= self::get_xml_for_single_post($post_id); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | return $all_posts_xml; |
@@ -67,14 +67,14 @@ discard block |
||
| 67 | 67 | * |
| 68 | 68 | * @return string XML string for single post. |
| 69 | 69 | */ |
| 70 | - public static function get_xml_for_single_post( $post_id ) { |
|
| 71 | - $videos = Video_Storage_Factory::get_storage()->get_all_videos( $post_id ); |
|
| 72 | - if ( ! $videos ) { |
|
| 70 | + public static function get_xml_for_single_post($post_id) { |
|
| 71 | + $videos = Video_Storage_Factory::get_storage()->get_all_videos($post_id); |
|
| 72 | + if ( ! $videos) { |
|
| 73 | 73 | return ""; |
| 74 | 74 | } |
| 75 | 75 | $single_post_xml = ""; |
| 76 | - foreach ( $videos as $video ) { |
|
| 77 | - $single_post_xml .= self::get_xml_for_single_video( $video, $post_id ); |
|
| 76 | + foreach ($videos as $video) { |
|
| 77 | + $single_post_xml .= self::get_xml_for_single_video($video, $post_id); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | return $single_post_xml; |
@@ -88,29 +88,29 @@ discard block |
||
| 88 | 88 | * |
| 89 | 89 | * @return string |
| 90 | 90 | */ |
| 91 | - public static function get_xml_for_single_video( $video, $post_id ) { |
|
| 91 | + public static function get_xml_for_single_video($video, $post_id) { |
|
| 92 | 92 | |
| 93 | - $permalink = get_permalink( $post_id ); |
|
| 94 | - $title = esc_html( $video->name ); |
|
| 95 | - $description = esc_html( $video->description ); |
|
| 93 | + $permalink = get_permalink($post_id); |
|
| 94 | + $title = esc_html($video->name); |
|
| 95 | + $description = esc_html($video->description); |
|
| 96 | 96 | $thumbnail_url = $video->thumbnail_urls[0]; |
| 97 | 97 | |
| 98 | 98 | // If description is empty use title. |
| 99 | - if ( ! $description ) { |
|
| 99 | + if ( ! $description) { |
|
| 100 | 100 | $description = $title; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | $optional_fields = array( |
| 104 | 104 | 'content_loc' => $video->content_url, |
| 105 | 105 | 'player_loc' => $video->embed_url, |
| 106 | - 'duration' => self::iso8601_to_seconds( $video->duration ), |
|
| 106 | + 'duration' => self::iso8601_to_seconds($video->duration), |
|
| 107 | 107 | 'view_count' => $video->views, |
| 108 | 108 | 'live' => $video->is_live_video ? 'yes' : 'no' |
| 109 | 109 | ); |
| 110 | 110 | |
| 111 | 111 | $optional_data = ""; |
| 112 | - foreach ( $optional_fields as $xml_key => $xml_value ) { |
|
| 113 | - if ( $xml_value ) { |
|
| 112 | + foreach ($optional_fields as $xml_key => $xml_value) { |
|
| 113 | + if ($xml_value) { |
|
| 114 | 114 | $optional_data .= "<video:${xml_key}>${xml_value}</video:${xml_key}>"; |
| 115 | 115 | } |
| 116 | 116 | } |