@@ -14,11 +14,11 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | function wl_ajax_analyze_action() { |
| 16 | 16 | |
| 17 | - check_admin_referer( 'wl_analyze' ); |
|
| 17 | + check_admin_referer( 'wl_analyze' ); |
|
| 18 | 18 | |
| 19 | - $data = filter_input( INPUT_POST, 'data' ); |
|
| 19 | + $data = filter_input( INPUT_POST, 'data' ); |
|
| 20 | 20 | |
| 21 | - wp_send_json_success( wl_analyze_content( $data, 'application/json; charset=' . strtolower( get_bloginfo( 'charset' ) ) ) ); |
|
| 21 | + wp_send_json_success( wl_analyze_content( $data, 'application/json; charset=' . strtolower( get_bloginfo( 'charset' ) ) ) ); |
|
| 22 | 22 | |
| 23 | 23 | } |
| 24 | 24 | |
@@ -45,47 +45,47 @@ discard block |
||
| 45 | 45 | // // Set the content type to the request content type or to text/plain by default. |
| 46 | 46 | // $content_type = isset( $_SERVER['CONTENT_TYPE'] ) ? $_SERVER['CONTENT_TYPE'] : 'text/plain'; |
| 47 | 47 | |
| 48 | - add_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
| 49 | - $json = Wordlift_Api_Service::get_instance() |
|
| 50 | - ->post_custom_content_type( 'analysis/single', $data, $content_type ); |
|
| 51 | - remove_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
| 48 | + add_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
| 49 | + $json = Wordlift_Api_Service::get_instance() |
|
| 50 | + ->post_custom_content_type( 'analysis/single', $data, $content_type ); |
|
| 51 | + remove_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
| 52 | 52 | |
| 53 | - // If it's an error log it. |
|
| 54 | - if ( is_wp_error( $json ) ) { |
|
| 55 | - $request_body = json_decode( $data, true ); |
|
| 53 | + // If it's an error log it. |
|
| 54 | + if ( is_wp_error( $json ) ) { |
|
| 55 | + $request_body = json_decode( $data, true ); |
|
| 56 | 56 | |
| 57 | - return Analysis_Response_Ops_Factory::get_instance() |
|
| 58 | - ->create( json_decode( '{ "entities": {}, "annotations": {}, "topics": {} }' ) ) |
|
| 59 | - ->make_entities_local() |
|
| 60 | - ->add_occurrences( $request_body['content'] ) |
|
| 61 | - ->get_json(); |
|
| 62 | - } |
|
| 57 | + return Analysis_Response_Ops_Factory::get_instance() |
|
| 58 | + ->create( json_decode( '{ "entities": {}, "annotations": {}, "topics": {} }' ) ) |
|
| 59 | + ->make_entities_local() |
|
| 60 | + ->add_occurrences( $request_body['content'] ) |
|
| 61 | + ->get_json(); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /* |
|
| 64 | + /* |
|
| 65 | 65 | * We pass the response to the Analysis_Response_Ops to ensure that we make remote entities local. |
| 66 | 66 | * |
| 67 | 67 | * @see https://github.com/insideout10/wordlift-plugin/issues/944 |
| 68 | 68 | * @since 3.21.5 |
| 69 | 69 | */ |
| 70 | 70 | |
| 71 | - // Get the actual content sent to the analysis, so that we can pass it to the Analysis_Response_Ops to populate |
|
| 72 | - // the occurrences for the local entities. |
|
| 73 | - if ( 0 === strpos( $content_type, 'application/json' ) ) { |
|
| 74 | - $request_json = json_decode( $data ); |
|
| 75 | - $request_content = $request_json->content; |
|
| 76 | - } else { |
|
| 77 | - $request_content = $data; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - return Analysis_Response_Ops_Factory::get_instance() |
|
| 81 | - ->create( $json ) |
|
| 82 | - ->make_entities_local() |
|
| 83 | - ->add_occurrences( $request_content ) |
|
| 84 | - ->get_json(); |
|
| 71 | + // Get the actual content sent to the analysis, so that we can pass it to the Analysis_Response_Ops to populate |
|
| 72 | + // the occurrences for the local entities. |
|
| 73 | + if ( 0 === strpos( $content_type, 'application/json' ) ) { |
|
| 74 | + $request_json = json_decode( $data ); |
|
| 75 | + $request_content = $request_json->content; |
|
| 76 | + } else { |
|
| 77 | + $request_content = $data; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + return Analysis_Response_Ops_Factory::get_instance() |
|
| 81 | + ->create( $json ) |
|
| 82 | + ->make_entities_local() |
|
| 83 | + ->add_occurrences( $request_content ) |
|
| 84 | + ->get_json(); |
|
| 85 | 85 | |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | function wl_use_analysis_on_api_wordlift_io( $value ) { |
| 89 | 89 | |
| 90 | - return preg_replace( '|https://api\.wordlift\.it/|', 'https://api.wordlift.io/', $value ); |
|
| 90 | + return preg_replace( '|https://api\.wordlift\.it/|', 'https://api.wordlift.io/', $value ); |
|
| 91 | 91 | } |
@@ -14,15 +14,15 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | function wl_ajax_analyze_action() { |
| 16 | 16 | |
| 17 | - check_admin_referer( 'wl_analyze' ); |
|
| 17 | + check_admin_referer('wl_analyze'); |
|
| 18 | 18 | |
| 19 | - $data = filter_input( INPUT_POST, 'data' ); |
|
| 19 | + $data = filter_input(INPUT_POST, 'data'); |
|
| 20 | 20 | |
| 21 | - wp_send_json_success( wl_analyze_content( $data, 'application/json; charset=' . strtolower( get_bloginfo( 'charset' ) ) ) ); |
|
| 21 | + wp_send_json_success(wl_analyze_content($data, 'application/json; charset='.strtolower(get_bloginfo('charset')))); |
|
| 22 | 22 | |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | -add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_action' ); |
|
| 25 | +add_action('wp_ajax_wl_analyze', 'wl_ajax_analyze_action'); |
|
| 26 | 26 | |
| 27 | 27 | /** |
| 28 | 28 | * Analyze the provided content. The analysis will make use of the method *wl_ajax_analyze_action* |
@@ -40,24 +40,24 @@ discard block |
||
| 40 | 40 | * @since 3.24.2 We don't return an error anymore, but an empty analysis response. This is required to allow the editor |
| 41 | 41 | * to manage entities or to manually add them even when analysis isn't available. |
| 42 | 42 | */ |
| 43 | -function wl_analyze_content( $data, $content_type ) { |
|
| 43 | +function wl_analyze_content($data, $content_type) { |
|
| 44 | 44 | |
| 45 | 45 | // // Set the content type to the request content type or to text/plain by default. |
| 46 | 46 | // $content_type = isset( $_SERVER['CONTENT_TYPE'] ) ? $_SERVER['CONTENT_TYPE'] : 'text/plain'; |
| 47 | 47 | |
| 48 | - add_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
| 48 | + add_filter('wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io'); |
|
| 49 | 49 | $json = Wordlift_Api_Service::get_instance() |
| 50 | - ->post_custom_content_type( 'analysis/single', $data, $content_type ); |
|
| 51 | - remove_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
| 50 | + ->post_custom_content_type('analysis/single', $data, $content_type); |
|
| 51 | + remove_filter('wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io'); |
|
| 52 | 52 | |
| 53 | 53 | // If it's an error log it. |
| 54 | - if ( is_wp_error( $json ) ) { |
|
| 55 | - $request_body = json_decode( $data, true ); |
|
| 54 | + if (is_wp_error($json)) { |
|
| 55 | + $request_body = json_decode($data, true); |
|
| 56 | 56 | |
| 57 | 57 | return Analysis_Response_Ops_Factory::get_instance() |
| 58 | - ->create( json_decode( '{ "entities": {}, "annotations": {}, "topics": {} }' ) ) |
|
| 58 | + ->create(json_decode('{ "entities": {}, "annotations": {}, "topics": {} }')) |
|
| 59 | 59 | ->make_entities_local() |
| 60 | - ->add_occurrences( $request_body['content'] ) |
|
| 60 | + ->add_occurrences($request_body['content']) |
|
| 61 | 61 | ->get_json(); |
| 62 | 62 | } |
| 63 | 63 | |
@@ -70,22 +70,22 @@ discard block |
||
| 70 | 70 | |
| 71 | 71 | // Get the actual content sent to the analysis, so that we can pass it to the Analysis_Response_Ops to populate |
| 72 | 72 | // the occurrences for the local entities. |
| 73 | - if ( 0 === strpos( $content_type, 'application/json' ) ) { |
|
| 74 | - $request_json = json_decode( $data ); |
|
| 73 | + if (0 === strpos($content_type, 'application/json')) { |
|
| 74 | + $request_json = json_decode($data); |
|
| 75 | 75 | $request_content = $request_json->content; |
| 76 | 76 | } else { |
| 77 | 77 | $request_content = $data; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | return Analysis_Response_Ops_Factory::get_instance() |
| 81 | - ->create( $json ) |
|
| 81 | + ->create($json) |
|
| 82 | 82 | ->make_entities_local() |
| 83 | - ->add_occurrences( $request_content ) |
|
| 83 | + ->add_occurrences($request_content) |
|
| 84 | 84 | ->get_json(); |
| 85 | 85 | |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | -function wl_use_analysis_on_api_wordlift_io( $value ) { |
|
| 88 | +function wl_use_analysis_on_api_wordlift_io($value) { |
|
| 89 | 89 | |
| 90 | - return preg_replace( '|https://api\.wordlift\.it/|', 'https://api.wordlift.io/', $value ); |
|
| 90 | + return preg_replace('|https://api\.wordlift\.it/|', 'https://api.wordlift.io/', $value); |
|
| 91 | 91 | } |
@@ -18,45 +18,45 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Post_Image_Storage extends Wordlift_Storage { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Get the property value. |
|
| 23 | - * |
|
| 24 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 25 | - * |
|
| 26 | - * @return array|string|null A single string, or an array of values or null |
|
| 27 | - * if the property isn't recognized. |
|
| 28 | - * @since 3.15.0 |
|
| 29 | - * |
|
| 30 | - */ |
|
| 31 | - public function get( $post_id ) { |
|
| 32 | - |
|
| 33 | - // Prepare the return array. |
|
| 34 | - $image_urls = array(); |
|
| 35 | - |
|
| 36 | - // If there is a featured image it has the priority. |
|
| 37 | - $featured_image_id = get_post_thumbnail_id( $post_id ); |
|
| 38 | - if ( is_numeric( $featured_image_id ) && 0 < $featured_image_id ) { |
|
| 39 | - $image_url = wp_get_attachment_url( $featured_image_id ); |
|
| 40 | - |
|
| 41 | - $image_urls[] = $image_url; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - $images = get_children( array( |
|
| 45 | - 'post_parent' => $post_id, |
|
| 46 | - 'post_type' => 'attachment', |
|
| 47 | - 'post_mime_type' => 'image', |
|
| 48 | - ) ); |
|
| 49 | - |
|
| 50 | - // Collect the URLs. |
|
| 51 | - foreach ( $images as $attachment_id => $attachment ) { |
|
| 52 | - $image_url = wp_get_attachment_url( $attachment_id ); |
|
| 53 | - // Ensure the URL isn't collected already. |
|
| 54 | - if ( ! in_array( $image_url, $image_urls ) ) { |
|
| 55 | - array_push( $image_urls, $image_url ); |
|
| 56 | - } |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - return $image_urls; |
|
| 60 | - } |
|
| 21 | + /** |
|
| 22 | + * Get the property value. |
|
| 23 | + * |
|
| 24 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 25 | + * |
|
| 26 | + * @return array|string|null A single string, or an array of values or null |
|
| 27 | + * if the property isn't recognized. |
|
| 28 | + * @since 3.15.0 |
|
| 29 | + * |
|
| 30 | + */ |
|
| 31 | + public function get( $post_id ) { |
|
| 32 | + |
|
| 33 | + // Prepare the return array. |
|
| 34 | + $image_urls = array(); |
|
| 35 | + |
|
| 36 | + // If there is a featured image it has the priority. |
|
| 37 | + $featured_image_id = get_post_thumbnail_id( $post_id ); |
|
| 38 | + if ( is_numeric( $featured_image_id ) && 0 < $featured_image_id ) { |
|
| 39 | + $image_url = wp_get_attachment_url( $featured_image_id ); |
|
| 40 | + |
|
| 41 | + $image_urls[] = $image_url; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + $images = get_children( array( |
|
| 45 | + 'post_parent' => $post_id, |
|
| 46 | + 'post_type' => 'attachment', |
|
| 47 | + 'post_mime_type' => 'image', |
|
| 48 | + ) ); |
|
| 49 | + |
|
| 50 | + // Collect the URLs. |
|
| 51 | + foreach ( $images as $attachment_id => $attachment ) { |
|
| 52 | + $image_url = wp_get_attachment_url( $attachment_id ); |
|
| 53 | + // Ensure the URL isn't collected already. |
|
| 54 | + if ( ! in_array( $image_url, $image_urls ) ) { |
|
| 55 | + array_push( $image_urls, $image_url ); |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + return $image_urls; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | 62 | } |
@@ -28,31 +28,31 @@ |
||
| 28 | 28 | * @since 3.15.0 |
| 29 | 29 | * |
| 30 | 30 | */ |
| 31 | - public function get( $post_id ) { |
|
| 31 | + public function get($post_id) { |
|
| 32 | 32 | |
| 33 | 33 | // Prepare the return array. |
| 34 | 34 | $image_urls = array(); |
| 35 | 35 | |
| 36 | 36 | // If there is a featured image it has the priority. |
| 37 | - $featured_image_id = get_post_thumbnail_id( $post_id ); |
|
| 38 | - if ( is_numeric( $featured_image_id ) && 0 < $featured_image_id ) { |
|
| 39 | - $image_url = wp_get_attachment_url( $featured_image_id ); |
|
| 37 | + $featured_image_id = get_post_thumbnail_id($post_id); |
|
| 38 | + if (is_numeric($featured_image_id) && 0 < $featured_image_id) { |
|
| 39 | + $image_url = wp_get_attachment_url($featured_image_id); |
|
| 40 | 40 | |
| 41 | 41 | $image_urls[] = $image_url; |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - $images = get_children( array( |
|
| 44 | + $images = get_children(array( |
|
| 45 | 45 | 'post_parent' => $post_id, |
| 46 | 46 | 'post_type' => 'attachment', |
| 47 | 47 | 'post_mime_type' => 'image', |
| 48 | - ) ); |
|
| 48 | + )); |
|
| 49 | 49 | |
| 50 | 50 | // Collect the URLs. |
| 51 | - foreach ( $images as $attachment_id => $attachment ) { |
|
| 52 | - $image_url = wp_get_attachment_url( $attachment_id ); |
|
| 51 | + foreach ($images as $attachment_id => $attachment) { |
|
| 52 | + $image_url = wp_get_attachment_url($attachment_id); |
|
| 53 | 53 | // Ensure the URL isn't collected already. |
| 54 | - if ( ! in_array( $image_url, $image_urls ) ) { |
|
| 55 | - array_push( $image_urls, $image_url ); |
|
| 54 | + if ( ! in_array($image_url, $image_urls)) { |
|
| 55 | + array_push($image_urls, $image_url); |
|
| 56 | 56 | } |
| 57 | 57 | } |
| 58 | 58 | |
@@ -15,393 +15,393 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Wordlift_Post_To_Jsonld_Converter extends Wordlift_Abstract_Post_To_Jsonld_Converter { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * A {@link Wordlift_Configuration_Service} instance. |
|
| 20 | - * |
|
| 21 | - * @since 3.10.0 |
|
| 22 | - * @access private |
|
| 23 | - * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 24 | - */ |
|
| 25 | - private $configuration_service; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * A {@link Wordlift_Log_Service} instance. |
|
| 29 | - * |
|
| 30 | - * @since 3.10.0 |
|
| 31 | - * @access private |
|
| 32 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 33 | - */ |
|
| 34 | - private $log; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 38 | - * |
|
| 39 | - * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 40 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 41 | - * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 42 | - * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 43 | - * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 44 | - * |
|
| 45 | - * @since 3.10.0 |
|
| 46 | - * |
|
| 47 | - */ |
|
| 48 | - public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) { |
|
| 49 | - parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 50 | - |
|
| 51 | - $this->configuration_service = $configuration_service; |
|
| 52 | - |
|
| 53 | - // Set a reference to the logger. |
|
| 54 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 59 | - * found while processing the post is set in the $references array. |
|
| 60 | - * |
|
| 61 | - * @param int $post_id The post id. |
|
| 62 | - * @param array $references An array of entity references. |
|
| 63 | - * |
|
| 64 | - * @return array A JSON-LD array. |
|
| 65 | - * @since 3.10.0 |
|
| 66 | - * |
|
| 67 | - */ |
|
| 68 | - public function convert( $post_id, &$references = array() ) { |
|
| 69 | - |
|
| 70 | - // Get the post instance. |
|
| 71 | - if ( null === $post = get_post( $post_id ) ) { |
|
| 72 | - // Post not found. |
|
| 73 | - return null; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - // Get the base JSON-LD and the list of entities referenced by this entity. |
|
| 77 | - $jsonld = parent::convert( $post_id, $references ); |
|
| 78 | - |
|
| 79 | - // Set WebPage by default. |
|
| 80 | - if ( empty( $jsonld['@type'] ) ) { |
|
| 81 | - $jsonld['@type'] = 'WebPage'; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - // Get the entity name. |
|
| 85 | - $jsonld['headline'] = $post->post_title; |
|
| 86 | - |
|
| 87 | - // Set the published and modified dates. |
|
| 88 | - /* |
|
| 18 | + /** |
|
| 19 | + * A {@link Wordlift_Configuration_Service} instance. |
|
| 20 | + * |
|
| 21 | + * @since 3.10.0 |
|
| 22 | + * @access private |
|
| 23 | + * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 24 | + */ |
|
| 25 | + private $configuration_service; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * A {@link Wordlift_Log_Service} instance. |
|
| 29 | + * |
|
| 30 | + * @since 3.10.0 |
|
| 31 | + * @access private |
|
| 32 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 33 | + */ |
|
| 34 | + private $log; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 38 | + * |
|
| 39 | + * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 40 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 41 | + * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 42 | + * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 43 | + * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 44 | + * |
|
| 45 | + * @since 3.10.0 |
|
| 46 | + * |
|
| 47 | + */ |
|
| 48 | + public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) { |
|
| 49 | + parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 50 | + |
|
| 51 | + $this->configuration_service = $configuration_service; |
|
| 52 | + |
|
| 53 | + // Set a reference to the logger. |
|
| 54 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 59 | + * found while processing the post is set in the $references array. |
|
| 60 | + * |
|
| 61 | + * @param int $post_id The post id. |
|
| 62 | + * @param array $references An array of entity references. |
|
| 63 | + * |
|
| 64 | + * @return array A JSON-LD array. |
|
| 65 | + * @since 3.10.0 |
|
| 66 | + * |
|
| 67 | + */ |
|
| 68 | + public function convert( $post_id, &$references = array() ) { |
|
| 69 | + |
|
| 70 | + // Get the post instance. |
|
| 71 | + if ( null === $post = get_post( $post_id ) ) { |
|
| 72 | + // Post not found. |
|
| 73 | + return null; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + // Get the base JSON-LD and the list of entities referenced by this entity. |
|
| 77 | + $jsonld = parent::convert( $post_id, $references ); |
|
| 78 | + |
|
| 79 | + // Set WebPage by default. |
|
| 80 | + if ( empty( $jsonld['@type'] ) ) { |
|
| 81 | + $jsonld['@type'] = 'WebPage'; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + // Get the entity name. |
|
| 85 | + $jsonld['headline'] = $post->post_title; |
|
| 86 | + |
|
| 87 | + // Set the published and modified dates. |
|
| 88 | + /* |
|
| 89 | 89 | * Set the `datePublished` and `dateModified` using the local timezone. |
| 90 | 90 | * |
| 91 | 91 | * @see https://github.com/insideout10/wordlift-plugin/issues/887 |
| 92 | 92 | * |
| 93 | 93 | * @since 3.20.0 |
| 94 | 94 | */ |
| 95 | - try { |
|
| 96 | - $default_timezone = date_default_timezone_get(); |
|
| 97 | - date_default_timezone_set( get_option( 'timezone_string' ) ); |
|
| 98 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 99 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 100 | - date_default_timezone_set( $default_timezone ); |
|
| 101 | - } catch ( Exception $e ) { |
|
| 102 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 103 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - // Get the word count for the post. |
|
| 107 | - /* |
|
| 95 | + try { |
|
| 96 | + $default_timezone = date_default_timezone_get(); |
|
| 97 | + date_default_timezone_set( get_option( 'timezone_string' ) ); |
|
| 98 | + $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 99 | + $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 100 | + date_default_timezone_set( $default_timezone ); |
|
| 101 | + } catch ( Exception $e ) { |
|
| 102 | + $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 103 | + $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + // Get the word count for the post. |
|
| 107 | + /* |
|
| 108 | 108 | * Do not display the `wordCount` on a `WebPage`. |
| 109 | 109 | * |
| 110 | 110 | * @see https://github.com/insideout10/wordlift-plugin/issues/888 |
| 111 | 111 | * |
| 112 | 112 | * @since 3.20.0 |
| 113 | 113 | */ |
| 114 | - if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 115 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 116 | - $jsonld['wordCount'] = $post_adapter->word_count(); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - // Set the publisher. |
|
| 120 | - $this->set_publisher( $jsonld ); |
|
| 121 | - |
|
| 122 | - // Process the references if any. |
|
| 123 | - if ( 0 < count( $references ) ) { |
|
| 124 | - |
|
| 125 | - // Prepare the `about` and `mentions` array. |
|
| 126 | - $about = $mentions = array(); |
|
| 127 | - |
|
| 128 | - // If the entity is in the title, then it should be an `about`. |
|
| 129 | - foreach ( $references as $reference ) { |
|
| 130 | - |
|
| 131 | - // Get the entity labels. |
|
| 132 | - $labels = $this->entity_service->get_labels( $reference ); |
|
| 133 | - |
|
| 134 | - // Get the entity URI. |
|
| 135 | - $item = array( |
|
| 136 | - '@id' => $this->entity_service->get_uri( $reference ), |
|
| 137 | - ); |
|
| 138 | - |
|
| 139 | - $escaped_labels = array_map( function ( $value ) { |
|
| 140 | - return preg_quote( $value, '/' ); |
|
| 141 | - }, $labels ); |
|
| 142 | - |
|
| 143 | - // Check if the labels match any part of the title. |
|
| 144 | - $matches = 1 === preg_match( '/' . implode( '|', $escaped_labels ) . '/', $post->post_title ); |
|
| 145 | - |
|
| 146 | - // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
| 147 | - if ( $matches ) { |
|
| 148 | - $about[] = $item; |
|
| 149 | - } else { |
|
| 150 | - $mentions[] = $item; |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - // If we have abouts, assign them to the JSON-LD. |
|
| 155 | - if ( 0 < count( $about ) ) { |
|
| 156 | - $jsonld['about'] = $about; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - // If we have mentions, assign them to the JSON-LD. |
|
| 160 | - if ( 0 < count( $mentions ) ) { |
|
| 161 | - $jsonld['mentions'] = $mentions; |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - // Finally set the author. |
|
| 166 | - $jsonld['author'] = $this->get_author( $post->post_author, $references ); |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references. |
|
| 170 | - * |
|
| 171 | - * @param array $value { |
|
| 172 | - * |
|
| 173 | - * @type array $jsonld The JSON-LD structure. |
|
| 174 | - * @type int[] $references An array of post IDs. |
|
| 175 | - * } |
|
| 176 | - * @since 3.25.0 |
|
| 177 | - * |
|
| 178 | - * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ |
|
| 179 | - * |
|
| 180 | - * @api |
|
| 181 | - */ |
|
| 182 | - $ret_val = apply_filters( 'wl_post_jsonld_array', array( |
|
| 183 | - 'jsonld' => $jsonld, |
|
| 184 | - 'references' => $references, |
|
| 185 | - ), $post_id ); |
|
| 186 | - $jsonld = $ret_val['jsonld']; |
|
| 187 | - $references = $ret_val['references']; |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Call the `wl_post_jsonld` filter. |
|
| 191 | - * |
|
| 192 | - * @param array $jsonld The JSON-LD structure. |
|
| 193 | - * @param int $post_id The {@link WP_Post} `id`. |
|
| 194 | - * @param array $references The array of referenced entities. |
|
| 195 | - * |
|
| 196 | - * @since 3.14.0 |
|
| 197 | - * |
|
| 198 | - * @api |
|
| 199 | - */ |
|
| 200 | - return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references ); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * Get the author's JSON-LD fragment. |
|
| 205 | - * |
|
| 206 | - * The JSON-LD fragment is generated using the {@link WP_User}'s data or |
|
| 207 | - * the referenced entity if configured for the {@link WP_User}. |
|
| 208 | - * |
|
| 209 | - * @param int $author_id The author {@link WP_User}'s `id`. |
|
| 210 | - * @param array $references An array of referenced entities. |
|
| 211 | - * |
|
| 212 | - * @return string|array A JSON-LD structure. |
|
| 213 | - * @since 3.14.0 |
|
| 214 | - * |
|
| 215 | - */ |
|
| 216 | - private function get_author( $author_id, &$references ) { |
|
| 217 | - |
|
| 218 | - // Get the entity bound to this user. |
|
| 219 | - $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 220 | - |
|
| 221 | - // If there's no entity bound return a simple author structure. |
|
| 222 | - if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) { |
|
| 223 | - |
|
| 224 | - $author = get_the_author_meta( 'display_name', $author_id ); |
|
| 225 | - $author_uri = $this->user_service->get_uri( $author_id ); |
|
| 226 | - |
|
| 227 | - return array( |
|
| 228 | - '@type' => 'Person', |
|
| 229 | - '@id' => $author_uri, |
|
| 230 | - 'name' => $author, |
|
| 231 | - ); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - // Add the author to the references. |
|
| 235 | - $author_uri = $this->entity_service->get_uri( $entity_id ); |
|
| 236 | - $references[] = $entity_id; |
|
| 237 | - |
|
| 238 | - // Return the JSON-LD for the referenced entity. |
|
| 239 | - return array( |
|
| 240 | - '@id' => $author_uri, |
|
| 241 | - ); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Enrich the provided params array with publisher data, if available. |
|
| 246 | - * |
|
| 247 | - * @param array $params The parameters array. |
|
| 248 | - * |
|
| 249 | - * @since 3.10.0 |
|
| 250 | - * |
|
| 251 | - */ |
|
| 252 | - protected function set_publisher( &$params ) { |
|
| 253 | - |
|
| 254 | - // If the publisher id isn't set don't do anything. |
|
| 255 | - if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) { |
|
| 256 | - return; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - // Get the post instance. |
|
| 260 | - if ( null === $post = get_post( $publisher_id ) ) { |
|
| 261 | - // Publisher not found. |
|
| 262 | - return; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - // Get the item id. |
|
| 266 | - $id = $this->entity_service->get_uri( $publisher_id ); |
|
| 267 | - |
|
| 268 | - // Get the type. |
|
| 269 | - $type = $this->entity_type_service->get( $publisher_id ); |
|
| 270 | - |
|
| 271 | - // Get the name. |
|
| 272 | - $name = $post->post_title; |
|
| 273 | - |
|
| 274 | - // Set the publisher data. |
|
| 275 | - $params['publisher'] = array( |
|
| 276 | - '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 277 | - '@id' => $id, |
|
| 278 | - 'name' => $name, |
|
| 279 | - ); |
|
| 280 | - |
|
| 281 | - // Add the sameAs values associated with the publisher. |
|
| 282 | - $storage_factory = Wordlift_Storage_Factory::get_instance(); |
|
| 283 | - $sameas = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id ); |
|
| 284 | - if ( ! empty( $sameas ) ) { |
|
| 285 | - $params['publisher']['sameAs'] = $sameas; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - // Set the logo, only for http://schema.org/Organization as Person doesn't |
|
| 289 | - // support the logo property. |
|
| 290 | - // |
|
| 291 | - // See http://schema.org/logo. |
|
| 292 | - if ( 'http://schema.org/Organization' !== $type['uri'] ) { |
|
| 293 | - return; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - // Get the publisher logo. |
|
| 297 | - $publisher_logo = $this->get_publisher_logo( $post->ID ); |
|
| 298 | - |
|
| 299 | - // Bail out if the publisher logo isn't set. |
|
| 300 | - if ( false === $publisher_logo ) { |
|
| 301 | - return; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - // Copy over some useful properties. |
|
| 305 | - // |
|
| 306 | - // See https://developers.google.com/search/docs/data-types/articles. |
|
| 307 | - $params['publisher']['logo']['@type'] = 'ImageObject'; |
|
| 308 | - $params['publisher']['logo']['url'] = $publisher_logo['url']; |
|
| 309 | - |
|
| 310 | - // If you specify a "width" or "height" value you should leave out |
|
| 311 | - // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 312 | - // |
|
| 313 | - // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 314 | - $params['publisher']['logo']['width'] = $publisher_logo['width']; |
|
| 315 | - $params['publisher']['logo']['height'] = $publisher_logo['height']; |
|
| 316 | - |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Get the publisher logo structure. |
|
| 321 | - * |
|
| 322 | - * The function returns false when the publisher logo cannot be determined, i.e.: |
|
| 323 | - * - the post has no featured image. |
|
| 324 | - * - the featured image has no file. |
|
| 325 | - * - a wp_image_editor instance cannot be instantiated on the original file or on the publisher logo file. |
|
| 326 | - * |
|
| 327 | - * @param int $post_id The post id. |
|
| 328 | - * |
|
| 329 | - * @return array|false Returns an array with the `url`, `width` and `height` for the publisher logo or false in case |
|
| 330 | - * of errors. |
|
| 331 | - * @since 3.19.2 |
|
| 332 | - * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue. |
|
| 333 | - * |
|
| 334 | - */ |
|
| 335 | - private function get_publisher_logo( $post_id ) { |
|
| 336 | - |
|
| 337 | - // Get the featured image for the post. |
|
| 338 | - $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 339 | - |
|
| 340 | - // Bail out if thumbnail not available. |
|
| 341 | - if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) { |
|
| 342 | - $this->log->info( "Featured image not set for post $post_id." ); |
|
| 343 | - |
|
| 344 | - return false; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - // Get the uploads base URL. |
|
| 348 | - $uploads_dir = wp_upload_dir(); |
|
| 349 | - |
|
| 350 | - // Get the attachment metadata. |
|
| 351 | - $metadata = wp_get_attachment_metadata( $thumbnail_id ); |
|
| 352 | - |
|
| 353 | - // Bail out if the file isn't set. |
|
| 354 | - if ( ! isset( $metadata['file'] ) ) { |
|
| 355 | - $this->log->warn( "Featured image file not found for post $post_id." ); |
|
| 356 | - |
|
| 357 | - return false; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - // Retrieve the relative filename, e.g. "2018/05/logo_publisher.png" |
|
| 361 | - $path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file']; |
|
| 362 | - |
|
| 363 | - // Bail out if the file isn't found. |
|
| 364 | - if ( ! file_exists( $path ) ) { |
|
| 365 | - $this->log->warn( "Featured image file $path doesn't exist for post $post_id." ); |
|
| 366 | - |
|
| 367 | - return false; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 371 | - $original_file_editor = wp_get_image_editor( $path ); |
|
| 372 | - if ( is_wp_error( $original_file_editor ) ) { |
|
| 373 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." ); |
|
| 374 | - |
|
| 375 | - return false; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - // Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling |
|
| 379 | - // and we don't actually know the end values. |
|
| 380 | - $publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' ); |
|
| 381 | - |
|
| 382 | - // If the file doesn't exist yet, create it. |
|
| 383 | - if ( ! file_exists( $publisher_logo_path ) ) { |
|
| 384 | - $original_file_editor->resize( 600, 60 ); |
|
| 385 | - $original_file_editor->save( $publisher_logo_path ); |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 389 | - $publisher_logo_editor = wp_get_image_editor( $publisher_logo_path ); |
|
| 390 | - if ( is_wp_error( $publisher_logo_editor ) ) { |
|
| 391 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." ); |
|
| 392 | - |
|
| 393 | - return false; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - // Get the actual size. |
|
| 397 | - $size = $publisher_logo_editor->get_size(); |
|
| 398 | - |
|
| 399 | - // Finally return the array with data. |
|
| 400 | - return array( |
|
| 401 | - 'url' => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ), |
|
| 402 | - 'width' => $size['width'], |
|
| 403 | - 'height' => $size['height'], |
|
| 404 | - ); |
|
| 405 | - } |
|
| 114 | + if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 115 | + $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 116 | + $jsonld['wordCount'] = $post_adapter->word_count(); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + // Set the publisher. |
|
| 120 | + $this->set_publisher( $jsonld ); |
|
| 121 | + |
|
| 122 | + // Process the references if any. |
|
| 123 | + if ( 0 < count( $references ) ) { |
|
| 124 | + |
|
| 125 | + // Prepare the `about` and `mentions` array. |
|
| 126 | + $about = $mentions = array(); |
|
| 127 | + |
|
| 128 | + // If the entity is in the title, then it should be an `about`. |
|
| 129 | + foreach ( $references as $reference ) { |
|
| 130 | + |
|
| 131 | + // Get the entity labels. |
|
| 132 | + $labels = $this->entity_service->get_labels( $reference ); |
|
| 133 | + |
|
| 134 | + // Get the entity URI. |
|
| 135 | + $item = array( |
|
| 136 | + '@id' => $this->entity_service->get_uri( $reference ), |
|
| 137 | + ); |
|
| 138 | + |
|
| 139 | + $escaped_labels = array_map( function ( $value ) { |
|
| 140 | + return preg_quote( $value, '/' ); |
|
| 141 | + }, $labels ); |
|
| 142 | + |
|
| 143 | + // Check if the labels match any part of the title. |
|
| 144 | + $matches = 1 === preg_match( '/' . implode( '|', $escaped_labels ) . '/', $post->post_title ); |
|
| 145 | + |
|
| 146 | + // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
| 147 | + if ( $matches ) { |
|
| 148 | + $about[] = $item; |
|
| 149 | + } else { |
|
| 150 | + $mentions[] = $item; |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + // If we have abouts, assign them to the JSON-LD. |
|
| 155 | + if ( 0 < count( $about ) ) { |
|
| 156 | + $jsonld['about'] = $about; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + // If we have mentions, assign them to the JSON-LD. |
|
| 160 | + if ( 0 < count( $mentions ) ) { |
|
| 161 | + $jsonld['mentions'] = $mentions; |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + // Finally set the author. |
|
| 166 | + $jsonld['author'] = $this->get_author( $post->post_author, $references ); |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references. |
|
| 170 | + * |
|
| 171 | + * @param array $value { |
|
| 172 | + * |
|
| 173 | + * @type array $jsonld The JSON-LD structure. |
|
| 174 | + * @type int[] $references An array of post IDs. |
|
| 175 | + * } |
|
| 176 | + * @since 3.25.0 |
|
| 177 | + * |
|
| 178 | + * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ |
|
| 179 | + * |
|
| 180 | + * @api |
|
| 181 | + */ |
|
| 182 | + $ret_val = apply_filters( 'wl_post_jsonld_array', array( |
|
| 183 | + 'jsonld' => $jsonld, |
|
| 184 | + 'references' => $references, |
|
| 185 | + ), $post_id ); |
|
| 186 | + $jsonld = $ret_val['jsonld']; |
|
| 187 | + $references = $ret_val['references']; |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Call the `wl_post_jsonld` filter. |
|
| 191 | + * |
|
| 192 | + * @param array $jsonld The JSON-LD structure. |
|
| 193 | + * @param int $post_id The {@link WP_Post} `id`. |
|
| 194 | + * @param array $references The array of referenced entities. |
|
| 195 | + * |
|
| 196 | + * @since 3.14.0 |
|
| 197 | + * |
|
| 198 | + * @api |
|
| 199 | + */ |
|
| 200 | + return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references ); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * Get the author's JSON-LD fragment. |
|
| 205 | + * |
|
| 206 | + * The JSON-LD fragment is generated using the {@link WP_User}'s data or |
|
| 207 | + * the referenced entity if configured for the {@link WP_User}. |
|
| 208 | + * |
|
| 209 | + * @param int $author_id The author {@link WP_User}'s `id`. |
|
| 210 | + * @param array $references An array of referenced entities. |
|
| 211 | + * |
|
| 212 | + * @return string|array A JSON-LD structure. |
|
| 213 | + * @since 3.14.0 |
|
| 214 | + * |
|
| 215 | + */ |
|
| 216 | + private function get_author( $author_id, &$references ) { |
|
| 217 | + |
|
| 218 | + // Get the entity bound to this user. |
|
| 219 | + $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 220 | + |
|
| 221 | + // If there's no entity bound return a simple author structure. |
|
| 222 | + if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) { |
|
| 223 | + |
|
| 224 | + $author = get_the_author_meta( 'display_name', $author_id ); |
|
| 225 | + $author_uri = $this->user_service->get_uri( $author_id ); |
|
| 226 | + |
|
| 227 | + return array( |
|
| 228 | + '@type' => 'Person', |
|
| 229 | + '@id' => $author_uri, |
|
| 230 | + 'name' => $author, |
|
| 231 | + ); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + // Add the author to the references. |
|
| 235 | + $author_uri = $this->entity_service->get_uri( $entity_id ); |
|
| 236 | + $references[] = $entity_id; |
|
| 237 | + |
|
| 238 | + // Return the JSON-LD for the referenced entity. |
|
| 239 | + return array( |
|
| 240 | + '@id' => $author_uri, |
|
| 241 | + ); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Enrich the provided params array with publisher data, if available. |
|
| 246 | + * |
|
| 247 | + * @param array $params The parameters array. |
|
| 248 | + * |
|
| 249 | + * @since 3.10.0 |
|
| 250 | + * |
|
| 251 | + */ |
|
| 252 | + protected function set_publisher( &$params ) { |
|
| 253 | + |
|
| 254 | + // If the publisher id isn't set don't do anything. |
|
| 255 | + if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) { |
|
| 256 | + return; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + // Get the post instance. |
|
| 260 | + if ( null === $post = get_post( $publisher_id ) ) { |
|
| 261 | + // Publisher not found. |
|
| 262 | + return; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + // Get the item id. |
|
| 266 | + $id = $this->entity_service->get_uri( $publisher_id ); |
|
| 267 | + |
|
| 268 | + // Get the type. |
|
| 269 | + $type = $this->entity_type_service->get( $publisher_id ); |
|
| 270 | + |
|
| 271 | + // Get the name. |
|
| 272 | + $name = $post->post_title; |
|
| 273 | + |
|
| 274 | + // Set the publisher data. |
|
| 275 | + $params['publisher'] = array( |
|
| 276 | + '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 277 | + '@id' => $id, |
|
| 278 | + 'name' => $name, |
|
| 279 | + ); |
|
| 280 | + |
|
| 281 | + // Add the sameAs values associated with the publisher. |
|
| 282 | + $storage_factory = Wordlift_Storage_Factory::get_instance(); |
|
| 283 | + $sameas = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id ); |
|
| 284 | + if ( ! empty( $sameas ) ) { |
|
| 285 | + $params['publisher']['sameAs'] = $sameas; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + // Set the logo, only for http://schema.org/Organization as Person doesn't |
|
| 289 | + // support the logo property. |
|
| 290 | + // |
|
| 291 | + // See http://schema.org/logo. |
|
| 292 | + if ( 'http://schema.org/Organization' !== $type['uri'] ) { |
|
| 293 | + return; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + // Get the publisher logo. |
|
| 297 | + $publisher_logo = $this->get_publisher_logo( $post->ID ); |
|
| 298 | + |
|
| 299 | + // Bail out if the publisher logo isn't set. |
|
| 300 | + if ( false === $publisher_logo ) { |
|
| 301 | + return; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + // Copy over some useful properties. |
|
| 305 | + // |
|
| 306 | + // See https://developers.google.com/search/docs/data-types/articles. |
|
| 307 | + $params['publisher']['logo']['@type'] = 'ImageObject'; |
|
| 308 | + $params['publisher']['logo']['url'] = $publisher_logo['url']; |
|
| 309 | + |
|
| 310 | + // If you specify a "width" or "height" value you should leave out |
|
| 311 | + // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 312 | + // |
|
| 313 | + // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 314 | + $params['publisher']['logo']['width'] = $publisher_logo['width']; |
|
| 315 | + $params['publisher']['logo']['height'] = $publisher_logo['height']; |
|
| 316 | + |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Get the publisher logo structure. |
|
| 321 | + * |
|
| 322 | + * The function returns false when the publisher logo cannot be determined, i.e.: |
|
| 323 | + * - the post has no featured image. |
|
| 324 | + * - the featured image has no file. |
|
| 325 | + * - a wp_image_editor instance cannot be instantiated on the original file or on the publisher logo file. |
|
| 326 | + * |
|
| 327 | + * @param int $post_id The post id. |
|
| 328 | + * |
|
| 329 | + * @return array|false Returns an array with the `url`, `width` and `height` for the publisher logo or false in case |
|
| 330 | + * of errors. |
|
| 331 | + * @since 3.19.2 |
|
| 332 | + * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue. |
|
| 333 | + * |
|
| 334 | + */ |
|
| 335 | + private function get_publisher_logo( $post_id ) { |
|
| 336 | + |
|
| 337 | + // Get the featured image for the post. |
|
| 338 | + $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 339 | + |
|
| 340 | + // Bail out if thumbnail not available. |
|
| 341 | + if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) { |
|
| 342 | + $this->log->info( "Featured image not set for post $post_id." ); |
|
| 343 | + |
|
| 344 | + return false; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + // Get the uploads base URL. |
|
| 348 | + $uploads_dir = wp_upload_dir(); |
|
| 349 | + |
|
| 350 | + // Get the attachment metadata. |
|
| 351 | + $metadata = wp_get_attachment_metadata( $thumbnail_id ); |
|
| 352 | + |
|
| 353 | + // Bail out if the file isn't set. |
|
| 354 | + if ( ! isset( $metadata['file'] ) ) { |
|
| 355 | + $this->log->warn( "Featured image file not found for post $post_id." ); |
|
| 356 | + |
|
| 357 | + return false; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + // Retrieve the relative filename, e.g. "2018/05/logo_publisher.png" |
|
| 361 | + $path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file']; |
|
| 362 | + |
|
| 363 | + // Bail out if the file isn't found. |
|
| 364 | + if ( ! file_exists( $path ) ) { |
|
| 365 | + $this->log->warn( "Featured image file $path doesn't exist for post $post_id." ); |
|
| 366 | + |
|
| 367 | + return false; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 371 | + $original_file_editor = wp_get_image_editor( $path ); |
|
| 372 | + if ( is_wp_error( $original_file_editor ) ) { |
|
| 373 | + $this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." ); |
|
| 374 | + |
|
| 375 | + return false; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + // Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling |
|
| 379 | + // and we don't actually know the end values. |
|
| 380 | + $publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' ); |
|
| 381 | + |
|
| 382 | + // If the file doesn't exist yet, create it. |
|
| 383 | + if ( ! file_exists( $publisher_logo_path ) ) { |
|
| 384 | + $original_file_editor->resize( 600, 60 ); |
|
| 385 | + $original_file_editor->save( $publisher_logo_path ); |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 389 | + $publisher_logo_editor = wp_get_image_editor( $publisher_logo_path ); |
|
| 390 | + if ( is_wp_error( $publisher_logo_editor ) ) { |
|
| 391 | + $this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." ); |
|
| 392 | + |
|
| 393 | + return false; |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + // Get the actual size. |
|
| 397 | + $size = $publisher_logo_editor->get_size(); |
|
| 398 | + |
|
| 399 | + // Finally return the array with data. |
|
| 400 | + return array( |
|
| 401 | + 'url' => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ), |
|
| 402 | + 'width' => $size['width'], |
|
| 403 | + 'height' => $size['height'], |
|
| 404 | + ); |
|
| 405 | + } |
|
| 406 | 406 | |
| 407 | 407 | } |
@@ -45,13 +45,13 @@ discard block |
||
| 45 | 45 | * @since 3.10.0 |
| 46 | 46 | * |
| 47 | 47 | */ |
| 48 | - public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) { |
|
| 49 | - parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 48 | + public function __construct($entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service) { |
|
| 49 | + parent::__construct($entity_type_service, $entity_service, $user_service, $attachment_service); |
|
| 50 | 50 | |
| 51 | 51 | $this->configuration_service = $configuration_service; |
| 52 | 52 | |
| 53 | 53 | // Set a reference to the logger. |
| 54 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 54 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Post_To_Jsonld_Converter'); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
@@ -65,19 +65,19 @@ discard block |
||
| 65 | 65 | * @since 3.10.0 |
| 66 | 66 | * |
| 67 | 67 | */ |
| 68 | - public function convert( $post_id, &$references = array() ) { |
|
| 68 | + public function convert($post_id, &$references = array()) { |
|
| 69 | 69 | |
| 70 | 70 | // Get the post instance. |
| 71 | - if ( null === $post = get_post( $post_id ) ) { |
|
| 71 | + if (null === $post = get_post($post_id)) { |
|
| 72 | 72 | // Post not found. |
| 73 | 73 | return null; |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | // Get the base JSON-LD and the list of entities referenced by this entity. |
| 77 | - $jsonld = parent::convert( $post_id, $references ); |
|
| 77 | + $jsonld = parent::convert($post_id, $references); |
|
| 78 | 78 | |
| 79 | 79 | // Set WebPage by default. |
| 80 | - if ( empty( $jsonld['@type'] ) ) { |
|
| 80 | + if (empty($jsonld['@type'])) { |
|
| 81 | 81 | $jsonld['@type'] = 'WebPage'; |
| 82 | 82 | } |
| 83 | 83 | |
@@ -94,13 +94,13 @@ discard block |
||
| 94 | 94 | */ |
| 95 | 95 | try { |
| 96 | 96 | $default_timezone = date_default_timezone_get(); |
| 97 | - date_default_timezone_set( get_option( 'timezone_string' ) ); |
|
| 98 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 99 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 100 | - date_default_timezone_set( $default_timezone ); |
|
| 101 | - } catch ( Exception $e ) { |
|
| 102 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 103 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 97 | + date_default_timezone_set(get_option('timezone_string')); |
|
| 98 | + $jsonld['datePublished'] = get_post_time('Y-m-d\TH:i:sP', false, $post); |
|
| 99 | + $jsonld['dateModified'] = get_post_modified_time('Y-m-d\TH:i:sP', false, $post); |
|
| 100 | + date_default_timezone_set($default_timezone); |
|
| 101 | + } catch (Exception $e) { |
|
| 102 | + $jsonld['datePublished'] = get_post_time('Y-m-d\TH:i', true, $post, false); |
|
| 103 | + $jsonld['dateModified'] = get_post_modified_time('Y-m-d\TH:i', true, $post, false); |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | // Get the word count for the post. |
@@ -111,40 +111,40 @@ discard block |
||
| 111 | 111 | * |
| 112 | 112 | * @since 3.20.0 |
| 113 | 113 | */ |
| 114 | - if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 115 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 114 | + if ( ! empty($jsonld['@type']) && 'WebPage' !== $jsonld['@type']) { |
|
| 115 | + $post_adapter = new Wordlift_Post_Adapter($post_id); |
|
| 116 | 116 | $jsonld['wordCount'] = $post_adapter->word_count(); |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | // Set the publisher. |
| 120 | - $this->set_publisher( $jsonld ); |
|
| 120 | + $this->set_publisher($jsonld); |
|
| 121 | 121 | |
| 122 | 122 | // Process the references if any. |
| 123 | - if ( 0 < count( $references ) ) { |
|
| 123 | + if (0 < count($references)) { |
|
| 124 | 124 | |
| 125 | 125 | // Prepare the `about` and `mentions` array. |
| 126 | 126 | $about = $mentions = array(); |
| 127 | 127 | |
| 128 | 128 | // If the entity is in the title, then it should be an `about`. |
| 129 | - foreach ( $references as $reference ) { |
|
| 129 | + foreach ($references as $reference) { |
|
| 130 | 130 | |
| 131 | 131 | // Get the entity labels. |
| 132 | - $labels = $this->entity_service->get_labels( $reference ); |
|
| 132 | + $labels = $this->entity_service->get_labels($reference); |
|
| 133 | 133 | |
| 134 | 134 | // Get the entity URI. |
| 135 | 135 | $item = array( |
| 136 | - '@id' => $this->entity_service->get_uri( $reference ), |
|
| 136 | + '@id' => $this->entity_service->get_uri($reference), |
|
| 137 | 137 | ); |
| 138 | 138 | |
| 139 | - $escaped_labels = array_map( function ( $value ) { |
|
| 140 | - return preg_quote( $value, '/' ); |
|
| 141 | - }, $labels ); |
|
| 139 | + $escaped_labels = array_map(function($value) { |
|
| 140 | + return preg_quote($value, '/'); |
|
| 141 | + }, $labels); |
|
| 142 | 142 | |
| 143 | 143 | // Check if the labels match any part of the title. |
| 144 | - $matches = 1 === preg_match( '/' . implode( '|', $escaped_labels ) . '/', $post->post_title ); |
|
| 144 | + $matches = 1 === preg_match('/'.implode('|', $escaped_labels).'/', $post->post_title); |
|
| 145 | 145 | |
| 146 | 146 | // If the title matches, assign the entity to the about, otherwise to the mentions. |
| 147 | - if ( $matches ) { |
|
| 147 | + if ($matches) { |
|
| 148 | 148 | $about[] = $item; |
| 149 | 149 | } else { |
| 150 | 150 | $mentions[] = $item; |
@@ -152,18 +152,18 @@ discard block |
||
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | // If we have abouts, assign them to the JSON-LD. |
| 155 | - if ( 0 < count( $about ) ) { |
|
| 155 | + if (0 < count($about)) { |
|
| 156 | 156 | $jsonld['about'] = $about; |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | // If we have mentions, assign them to the JSON-LD. |
| 160 | - if ( 0 < count( $mentions ) ) { |
|
| 160 | + if (0 < count($mentions)) { |
|
| 161 | 161 | $jsonld['mentions'] = $mentions; |
| 162 | 162 | } |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | // Finally set the author. |
| 166 | - $jsonld['author'] = $this->get_author( $post->post_author, $references ); |
|
| 166 | + $jsonld['author'] = $this->get_author($post->post_author, $references); |
|
| 167 | 167 | |
| 168 | 168 | /** |
| 169 | 169 | * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references. |
@@ -179,10 +179,10 @@ discard block |
||
| 179 | 179 | * |
| 180 | 180 | * @api |
| 181 | 181 | */ |
| 182 | - $ret_val = apply_filters( 'wl_post_jsonld_array', array( |
|
| 182 | + $ret_val = apply_filters('wl_post_jsonld_array', array( |
|
| 183 | 183 | 'jsonld' => $jsonld, |
| 184 | 184 | 'references' => $references, |
| 185 | - ), $post_id ); |
|
| 185 | + ), $post_id); |
|
| 186 | 186 | $jsonld = $ret_val['jsonld']; |
| 187 | 187 | $references = $ret_val['references']; |
| 188 | 188 | |
@@ -197,7 +197,7 @@ discard block |
||
| 197 | 197 | * |
| 198 | 198 | * @api |
| 199 | 199 | */ |
| 200 | - return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references ); |
|
| 200 | + return apply_filters('wl_post_jsonld', $jsonld, $post_id, $references); |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | /** |
@@ -213,16 +213,16 @@ discard block |
||
| 213 | 213 | * @since 3.14.0 |
| 214 | 214 | * |
| 215 | 215 | */ |
| 216 | - private function get_author( $author_id, &$references ) { |
|
| 216 | + private function get_author($author_id, &$references) { |
|
| 217 | 217 | |
| 218 | 218 | // Get the entity bound to this user. |
| 219 | - $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 219 | + $entity_id = $this->user_service->get_entity($author_id); |
|
| 220 | 220 | |
| 221 | 221 | // If there's no entity bound return a simple author structure. |
| 222 | - if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) { |
|
| 222 | + if (empty($entity_id) || 'publish' !== get_post_status($entity_id)) { |
|
| 223 | 223 | |
| 224 | - $author = get_the_author_meta( 'display_name', $author_id ); |
|
| 225 | - $author_uri = $this->user_service->get_uri( $author_id ); |
|
| 224 | + $author = get_the_author_meta('display_name', $author_id); |
|
| 225 | + $author_uri = $this->user_service->get_uri($author_id); |
|
| 226 | 226 | |
| 227 | 227 | return array( |
| 228 | 228 | '@type' => 'Person', |
@@ -232,7 +232,7 @@ discard block |
||
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | // Add the author to the references. |
| 235 | - $author_uri = $this->entity_service->get_uri( $entity_id ); |
|
| 235 | + $author_uri = $this->entity_service->get_uri($entity_id); |
|
| 236 | 236 | $references[] = $entity_id; |
| 237 | 237 | |
| 238 | 238 | // Return the JSON-LD for the referenced entity. |
@@ -249,39 +249,39 @@ discard block |
||
| 249 | 249 | * @since 3.10.0 |
| 250 | 250 | * |
| 251 | 251 | */ |
| 252 | - protected function set_publisher( &$params ) { |
|
| 252 | + protected function set_publisher(&$params) { |
|
| 253 | 253 | |
| 254 | 254 | // If the publisher id isn't set don't do anything. |
| 255 | - if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) { |
|
| 255 | + if (null === $publisher_id = $this->configuration_service->get_publisher_id()) { |
|
| 256 | 256 | return; |
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | // Get the post instance. |
| 260 | - if ( null === $post = get_post( $publisher_id ) ) { |
|
| 260 | + if (null === $post = get_post($publisher_id)) { |
|
| 261 | 261 | // Publisher not found. |
| 262 | 262 | return; |
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | // Get the item id. |
| 266 | - $id = $this->entity_service->get_uri( $publisher_id ); |
|
| 266 | + $id = $this->entity_service->get_uri($publisher_id); |
|
| 267 | 267 | |
| 268 | 268 | // Get the type. |
| 269 | - $type = $this->entity_type_service->get( $publisher_id ); |
|
| 269 | + $type = $this->entity_type_service->get($publisher_id); |
|
| 270 | 270 | |
| 271 | 271 | // Get the name. |
| 272 | 272 | $name = $post->post_title; |
| 273 | 273 | |
| 274 | 274 | // Set the publisher data. |
| 275 | 275 | $params['publisher'] = array( |
| 276 | - '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 276 | + '@type' => $this->relative_to_context($type['uri']), |
|
| 277 | 277 | '@id' => $id, |
| 278 | 278 | 'name' => $name, |
| 279 | 279 | ); |
| 280 | 280 | |
| 281 | 281 | // Add the sameAs values associated with the publisher. |
| 282 | 282 | $storage_factory = Wordlift_Storage_Factory::get_instance(); |
| 283 | - $sameas = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id ); |
|
| 284 | - if ( ! empty( $sameas ) ) { |
|
| 283 | + $sameas = $storage_factory->post_meta(Wordlift_Schema_Service::FIELD_SAME_AS)->get($publisher_id); |
|
| 284 | + if ( ! empty($sameas)) { |
|
| 285 | 285 | $params['publisher']['sameAs'] = $sameas; |
| 286 | 286 | } |
| 287 | 287 | |
@@ -289,15 +289,15 @@ discard block |
||
| 289 | 289 | // support the logo property. |
| 290 | 290 | // |
| 291 | 291 | // See http://schema.org/logo. |
| 292 | - if ( 'http://schema.org/Organization' !== $type['uri'] ) { |
|
| 292 | + if ('http://schema.org/Organization' !== $type['uri']) { |
|
| 293 | 293 | return; |
| 294 | 294 | } |
| 295 | 295 | |
| 296 | 296 | // Get the publisher logo. |
| 297 | - $publisher_logo = $this->get_publisher_logo( $post->ID ); |
|
| 297 | + $publisher_logo = $this->get_publisher_logo($post->ID); |
|
| 298 | 298 | |
| 299 | 299 | // Bail out if the publisher logo isn't set. |
| 300 | - if ( false === $publisher_logo ) { |
|
| 300 | + if (false === $publisher_logo) { |
|
| 301 | 301 | return; |
| 302 | 302 | } |
| 303 | 303 | |
@@ -332,14 +332,14 @@ discard block |
||
| 332 | 332 | * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue. |
| 333 | 333 | * |
| 334 | 334 | */ |
| 335 | - private function get_publisher_logo( $post_id ) { |
|
| 335 | + private function get_publisher_logo($post_id) { |
|
| 336 | 336 | |
| 337 | 337 | // Get the featured image for the post. |
| 338 | - $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 338 | + $thumbnail_id = get_post_thumbnail_id($post_id); |
|
| 339 | 339 | |
| 340 | 340 | // Bail out if thumbnail not available. |
| 341 | - if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) { |
|
| 342 | - $this->log->info( "Featured image not set for post $post_id." ); |
|
| 341 | + if (empty($thumbnail_id) || 0 === $thumbnail_id) { |
|
| 342 | + $this->log->info("Featured image not set for post $post_id."); |
|
| 343 | 343 | |
| 344 | 344 | return false; |
| 345 | 345 | } |
@@ -348,47 +348,47 @@ discard block |
||
| 348 | 348 | $uploads_dir = wp_upload_dir(); |
| 349 | 349 | |
| 350 | 350 | // Get the attachment metadata. |
| 351 | - $metadata = wp_get_attachment_metadata( $thumbnail_id ); |
|
| 351 | + $metadata = wp_get_attachment_metadata($thumbnail_id); |
|
| 352 | 352 | |
| 353 | 353 | // Bail out if the file isn't set. |
| 354 | - if ( ! isset( $metadata['file'] ) ) { |
|
| 355 | - $this->log->warn( "Featured image file not found for post $post_id." ); |
|
| 354 | + if ( ! isset($metadata['file'])) { |
|
| 355 | + $this->log->warn("Featured image file not found for post $post_id."); |
|
| 356 | 356 | |
| 357 | 357 | return false; |
| 358 | 358 | } |
| 359 | 359 | |
| 360 | 360 | // Retrieve the relative filename, e.g. "2018/05/logo_publisher.png" |
| 361 | - $path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file']; |
|
| 361 | + $path = $uploads_dir['basedir'].DIRECTORY_SEPARATOR.$metadata['file']; |
|
| 362 | 362 | |
| 363 | 363 | // Bail out if the file isn't found. |
| 364 | - if ( ! file_exists( $path ) ) { |
|
| 365 | - $this->log->warn( "Featured image file $path doesn't exist for post $post_id." ); |
|
| 364 | + if ( ! file_exists($path)) { |
|
| 365 | + $this->log->warn("Featured image file $path doesn't exist for post $post_id."); |
|
| 366 | 366 | |
| 367 | 367 | return false; |
| 368 | 368 | } |
| 369 | 369 | |
| 370 | 370 | // Try to get the image editor and bail out if the editor cannot be instantiated. |
| 371 | - $original_file_editor = wp_get_image_editor( $path ); |
|
| 372 | - if ( is_wp_error( $original_file_editor ) ) { |
|
| 373 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." ); |
|
| 371 | + $original_file_editor = wp_get_image_editor($path); |
|
| 372 | + if (is_wp_error($original_file_editor)) { |
|
| 373 | + $this->log->warn("Cannot instantiate WP Image Editor on file $path for post $post_id."); |
|
| 374 | 374 | |
| 375 | 375 | return false; |
| 376 | 376 | } |
| 377 | 377 | |
| 378 | 378 | // Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling |
| 379 | 379 | // and we don't actually know the end values. |
| 380 | - $publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' ); |
|
| 380 | + $publisher_logo_path = $original_file_editor->generate_filename('-publisher-logo'); |
|
| 381 | 381 | |
| 382 | 382 | // If the file doesn't exist yet, create it. |
| 383 | - if ( ! file_exists( $publisher_logo_path ) ) { |
|
| 384 | - $original_file_editor->resize( 600, 60 ); |
|
| 385 | - $original_file_editor->save( $publisher_logo_path ); |
|
| 383 | + if ( ! file_exists($publisher_logo_path)) { |
|
| 384 | + $original_file_editor->resize(600, 60); |
|
| 385 | + $original_file_editor->save($publisher_logo_path); |
|
| 386 | 386 | } |
| 387 | 387 | |
| 388 | 388 | // Try to get the image editor and bail out if the editor cannot be instantiated. |
| 389 | - $publisher_logo_editor = wp_get_image_editor( $publisher_logo_path ); |
|
| 390 | - if ( is_wp_error( $publisher_logo_editor ) ) { |
|
| 391 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." ); |
|
| 389 | + $publisher_logo_editor = wp_get_image_editor($publisher_logo_path); |
|
| 390 | + if (is_wp_error($publisher_logo_editor)) { |
|
| 391 | + $this->log->warn("Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id."); |
|
| 392 | 392 | |
| 393 | 393 | return false; |
| 394 | 394 | } |
@@ -398,7 +398,7 @@ discard block |
||
| 398 | 398 | |
| 399 | 399 | // Finally return the array with data. |
| 400 | 400 | return array( |
| 401 | - 'url' => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ), |
|
| 401 | + 'url' => $uploads_dir['baseurl'].substr($publisher_logo_path, strlen($uploads_dir['basedir'])), |
|
| 402 | 402 | 'width' => $size['width'], |
| 403 | 403 | 'height' => $size['height'], |
| 404 | 404 | ); |
@@ -15,45 +15,45 @@ discard block |
||
| 15 | 15 | * @return mixed |
| 16 | 16 | */ |
| 17 | 17 | function wl_shortcode_chord_most_referenced_entity_id() { |
| 18 | - // Get the last 20 articles by post date. |
|
| 19 | - // For each article get the entities they reference. |
|
| 20 | - $post_ids = get_posts( array( |
|
| 21 | - 'numberposts' => 20, |
|
| 22 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 23 | - 'fields' => 'ids', // Only get post IDs. |
|
| 24 | - 'post_status' => 'publish', |
|
| 25 | - 'tax_query' => array( |
|
| 26 | - 'relation' => 'OR', |
|
| 27 | - array( |
|
| 28 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 29 | - 'operator' => 'NOT EXISTS', |
|
| 30 | - ), |
|
| 31 | - array( |
|
| 32 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 33 | - 'field' => 'slug', |
|
| 34 | - 'terms' => 'article', |
|
| 35 | - ), |
|
| 36 | - ), |
|
| 37 | - 'orderby' => 'post_date', |
|
| 38 | - 'order' => 'DESC', |
|
| 39 | - ) ); |
|
| 40 | - |
|
| 41 | - if ( empty( $post_ids ) ) { |
|
| 42 | - return null; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - $entities = array(); |
|
| 46 | - foreach ( $post_ids as $id ) { |
|
| 47 | - $entities = array_merge( $entities, wl_core_get_related_entity_ids( $id ) ); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - $famous_entities = array_count_values( $entities ); |
|
| 51 | - arsort( $famous_entities ); |
|
| 52 | - if ( sizeof( $famous_entities ) >= 1 ) { |
|
| 53 | - return key( $famous_entities ); |
|
| 54 | - } else { |
|
| 55 | - return $post_ids[0]; |
|
| 56 | - } |
|
| 18 | + // Get the last 20 articles by post date. |
|
| 19 | + // For each article get the entities they reference. |
|
| 20 | + $post_ids = get_posts( array( |
|
| 21 | + 'numberposts' => 20, |
|
| 22 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 23 | + 'fields' => 'ids', // Only get post IDs. |
|
| 24 | + 'post_status' => 'publish', |
|
| 25 | + 'tax_query' => array( |
|
| 26 | + 'relation' => 'OR', |
|
| 27 | + array( |
|
| 28 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 29 | + 'operator' => 'NOT EXISTS', |
|
| 30 | + ), |
|
| 31 | + array( |
|
| 32 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 33 | + 'field' => 'slug', |
|
| 34 | + 'terms' => 'article', |
|
| 35 | + ), |
|
| 36 | + ), |
|
| 37 | + 'orderby' => 'post_date', |
|
| 38 | + 'order' => 'DESC', |
|
| 39 | + ) ); |
|
| 40 | + |
|
| 41 | + if ( empty( $post_ids ) ) { |
|
| 42 | + return null; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + $entities = array(); |
|
| 46 | + foreach ( $post_ids as $id ) { |
|
| 47 | + $entities = array_merge( $entities, wl_core_get_related_entity_ids( $id ) ); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + $famous_entities = array_count_values( $entities ); |
|
| 51 | + arsort( $famous_entities ); |
|
| 52 | + if ( sizeof( $famous_entities ) >= 1 ) { |
|
| 53 | + return key( $famous_entities ); |
|
| 54 | + } else { |
|
| 55 | + return $post_ids[0]; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | 58 | } |
| 59 | 59 | |
@@ -71,60 +71,60 @@ discard block |
||
| 71 | 71 | */ |
| 72 | 72 | function wl_shortcode_chord_get_relations( $entity_id, $depth = 2, $related = null, $max_size = 9 ) { |
| 73 | 73 | |
| 74 | - if ( ! is_null( $related ) ) { |
|
| 75 | - if ( 0 === $depth ) { |
|
| 76 | - return $related; |
|
| 77 | - } |
|
| 78 | - } |
|
| 74 | + if ( ! is_null( $related ) ) { |
|
| 75 | + if ( 0 === $depth ) { |
|
| 76 | + return $related; |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - wl_write_log( "wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: " . ( is_null( $related ) ? 'yes' : 'no' ) . " ]" ); |
|
| 80 | + wl_write_log( "wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: " . ( is_null( $related ) ? 'yes' : 'no' ) . " ]" ); |
|
| 81 | 81 | |
| 82 | - // Create a related array which will hold entities and relations. |
|
| 83 | - if ( is_null( $related ) ) { |
|
| 84 | - $related = array( |
|
| 85 | - 'entities' => array( $entity_id ), |
|
| 86 | - 'relations' => array(), |
|
| 87 | - ); |
|
| 88 | - } |
|
| 82 | + // Create a related array which will hold entities and relations. |
|
| 83 | + if ( is_null( $related ) ) { |
|
| 84 | + $related = array( |
|
| 85 | + 'entities' => array( $entity_id ), |
|
| 86 | + 'relations' => array(), |
|
| 87 | + ); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - // Get related entities |
|
| 91 | - $related_entity_ids = wl_core_get_related_entity_ids( $entity_id, array( |
|
| 92 | - 'status' => 'publish', |
|
| 93 | - ) ); |
|
| 90 | + // Get related entities |
|
| 91 | + $related_entity_ids = wl_core_get_related_entity_ids( $entity_id, array( |
|
| 92 | + 'status' => 'publish', |
|
| 93 | + ) ); |
|
| 94 | 94 | |
| 95 | - // If the current node is an entity, add related posts too |
|
| 96 | - $related_post_ids = ( Wordlift_Entity_Service::get_instance() |
|
| 97 | - ->is_entity( $entity_id ) ) ? |
|
| 98 | - wl_core_get_related_post_ids( $entity_id, array( |
|
| 99 | - 'status' => 'publish', |
|
| 100 | - ) ) : |
|
| 101 | - array(); |
|
| 95 | + // If the current node is an entity, add related posts too |
|
| 96 | + $related_post_ids = ( Wordlift_Entity_Service::get_instance() |
|
| 97 | + ->is_entity( $entity_id ) ) ? |
|
| 98 | + wl_core_get_related_post_ids( $entity_id, array( |
|
| 99 | + 'status' => 'publish', |
|
| 100 | + ) ) : |
|
| 101 | + array(); |
|
| 102 | 102 | |
| 103 | - // Merge results and remove duplicated entries |
|
| 104 | - $related_ids = array_unique( array_merge( $related_post_ids, $related_entity_ids ) ); |
|
| 103 | + // Merge results and remove duplicated entries |
|
| 104 | + $related_ids = array_unique( array_merge( $related_post_ids, $related_entity_ids ) ); |
|
| 105 | 105 | |
| 106 | - // TODO: List of entities ($rel) should be ordered by interest factors. |
|
| 107 | - shuffle( $related_ids ); |
|
| 106 | + // TODO: List of entities ($rel) should be ordered by interest factors. |
|
| 107 | + shuffle( $related_ids ); |
|
| 108 | 108 | |
| 109 | - // Now we have all the related IDs. |
|
| 110 | - foreach ( $related_ids as $related_id ) { |
|
| 109 | + // Now we have all the related IDs. |
|
| 110 | + foreach ( $related_ids as $related_id ) { |
|
| 111 | 111 | |
| 112 | - if ( count( $related['entities'] ) >= $max_size ) { |
|
| 113 | - return $related; |
|
| 114 | - } |
|
| 112 | + if ( count( $related['entities'] ) >= $max_size ) { |
|
| 113 | + return $related; |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - $related['relations'][] = array( $entity_id, $related_id ); |
|
| 116 | + $related['relations'][] = array( $entity_id, $related_id ); |
|
| 117 | 117 | |
| 118 | - if ( ! in_array( $related_id, $related['entities'] ) ) { |
|
| 119 | - // Found new related entity! |
|
| 120 | - $related['entities'][] = $related_id; |
|
| 118 | + if ( ! in_array( $related_id, $related['entities'] ) ) { |
|
| 119 | + // Found new related entity! |
|
| 120 | + $related['entities'][] = $related_id; |
|
| 121 | 121 | |
| 122 | - $related = wl_shortcode_chord_get_relations( $related_id, ( $depth - 1 ), $related, $max_size ); |
|
| 123 | - } |
|
| 124 | - } |
|
| 122 | + $related = wl_shortcode_chord_get_relations( $related_id, ( $depth - 1 ), $related, $max_size ); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - // End condition 2: no more entities to search for. |
|
| 127 | - return $related; |
|
| 126 | + // End condition 2: no more entities to search for. |
|
| 127 | + return $related; |
|
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | /** |
@@ -138,57 +138,57 @@ discard block |
||
| 138 | 138 | */ |
| 139 | 139 | function wl_shortcode_chord_get_graph( $data ) { |
| 140 | 140 | |
| 141 | - // Refactor the entities array in order to provide entities relevant data (uri, url, label, type, css_class). |
|
| 142 | - array_walk( $data['entities'], function ( &$item ) { |
|
| 143 | - $post = get_post( $item ); |
|
| 144 | - |
|
| 145 | - // Skip non-existing posts. |
|
| 146 | - if ( is_null( $post ) ) { |
|
| 147 | - wl_write_log( "wl_shortcode_chord_get_graph : post not found [ post id :: $item ]" ); |
|
| 148 | - |
|
| 149 | - return $item; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - // Get the entity taxonomy bound to this post (if there's no taxonomy, no stylesheet will be set). |
|
| 153 | - $term = Wordlift_Entity_Type_Service::get_instance()->get( $item ); |
|
| 154 | - |
|
| 155 | - // The following log may create a circular loop. |
|
| 156 | - // wl_write_log( "wl_shortcode_chord_get_graph [ post id :: $post->ID ][ term :: " . var_export( $term, true ) . " ]" ); |
|
| 157 | - |
|
| 158 | - // TODO: get all images |
|
| 159 | - $thumbnail = null; |
|
| 160 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 161 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 162 | - $attachment = wp_get_attachment_image_src( $thumbnail_id ); |
|
| 163 | - if ( false !== $attachment ) { |
|
| 164 | - $thumbnail = esc_attr( $attachment[0] ); |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - $entity = array( |
|
| 169 | - 'uri' => wl_get_entity_uri( $item ), |
|
| 170 | - 'url' => get_permalink( $item ), |
|
| 171 | - 'label' => $post->post_title, |
|
| 172 | - 'type' => $post->post_type, |
|
| 173 | - 'thumbnails' => array( $thumbnail ), |
|
| 174 | - 'css_class' => ( isset( $term['css_class'] ) ? $term['css_class'] : '' ), |
|
| 175 | - ); |
|
| 176 | - |
|
| 177 | - $item = $entity; |
|
| 178 | - } ); |
|
| 179 | - |
|
| 180 | - // Refactor the relations. |
|
| 181 | - array_walk( $data['relations'], function ( &$item ) { |
|
| 182 | - $relation = array( |
|
| 183 | - 's' => wl_get_entity_uri( $item[0] ), |
|
| 184 | - 'o' => wl_get_entity_uri( $item[1] ), |
|
| 185 | - ); |
|
| 186 | - |
|
| 187 | - $item = $relation; |
|
| 188 | - } ); |
|
| 189 | - |
|
| 190 | - // Return the JSON representation. |
|
| 191 | - return $data; |
|
| 141 | + // Refactor the entities array in order to provide entities relevant data (uri, url, label, type, css_class). |
|
| 142 | + array_walk( $data['entities'], function ( &$item ) { |
|
| 143 | + $post = get_post( $item ); |
|
| 144 | + |
|
| 145 | + // Skip non-existing posts. |
|
| 146 | + if ( is_null( $post ) ) { |
|
| 147 | + wl_write_log( "wl_shortcode_chord_get_graph : post not found [ post id :: $item ]" ); |
|
| 148 | + |
|
| 149 | + return $item; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + // Get the entity taxonomy bound to this post (if there's no taxonomy, no stylesheet will be set). |
|
| 153 | + $term = Wordlift_Entity_Type_Service::get_instance()->get( $item ); |
|
| 154 | + |
|
| 155 | + // The following log may create a circular loop. |
|
| 156 | + // wl_write_log( "wl_shortcode_chord_get_graph [ post id :: $post->ID ][ term :: " . var_export( $term, true ) . " ]" ); |
|
| 157 | + |
|
| 158 | + // TODO: get all images |
|
| 159 | + $thumbnail = null; |
|
| 160 | + $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 161 | + if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 162 | + $attachment = wp_get_attachment_image_src( $thumbnail_id ); |
|
| 163 | + if ( false !== $attachment ) { |
|
| 164 | + $thumbnail = esc_attr( $attachment[0] ); |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + $entity = array( |
|
| 169 | + 'uri' => wl_get_entity_uri( $item ), |
|
| 170 | + 'url' => get_permalink( $item ), |
|
| 171 | + 'label' => $post->post_title, |
|
| 172 | + 'type' => $post->post_type, |
|
| 173 | + 'thumbnails' => array( $thumbnail ), |
|
| 174 | + 'css_class' => ( isset( $term['css_class'] ) ? $term['css_class'] : '' ), |
|
| 175 | + ); |
|
| 176 | + |
|
| 177 | + $item = $entity; |
|
| 178 | + } ); |
|
| 179 | + |
|
| 180 | + // Refactor the relations. |
|
| 181 | + array_walk( $data['relations'], function ( &$item ) { |
|
| 182 | + $relation = array( |
|
| 183 | + 's' => wl_get_entity_uri( $item[0] ), |
|
| 184 | + 'o' => wl_get_entity_uri( $item[1] ), |
|
| 185 | + ); |
|
| 186 | + |
|
| 187 | + $item = $relation; |
|
| 188 | + } ); |
|
| 189 | + |
|
| 190 | + // Return the JSON representation. |
|
| 191 | + return $data; |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | /** |
@@ -199,13 +199,13 @@ discard block |
||
| 199 | 199 | */ |
| 200 | 200 | function wl_shortcode_chord_ajax() { |
| 201 | 201 | |
| 202 | - $post_id = $_REQUEST['post_id']; |
|
| 203 | - $depth = $_REQUEST['depth']; |
|
| 202 | + $post_id = $_REQUEST['post_id']; |
|
| 203 | + $depth = $_REQUEST['depth']; |
|
| 204 | 204 | |
| 205 | - $relations = wl_shortcode_chord_get_relations( $post_id, $depth ); |
|
| 206 | - $graph = wl_shortcode_chord_get_graph( $relations ); |
|
| 205 | + $relations = wl_shortcode_chord_get_relations( $post_id, $depth ); |
|
| 206 | + $graph = wl_shortcode_chord_get_graph( $relations ); |
|
| 207 | 207 | |
| 208 | - wl_core_send_json( $graph ); |
|
| 208 | + wl_core_send_json( $graph ); |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | add_action( 'wp_ajax_wl_chord', 'wl_shortcode_chord_ajax' ); |
@@ -216,42 +216,42 @@ discard block |
||
| 216 | 216 | */ |
| 217 | 217 | add_action( 'init', function () { |
| 218 | 218 | |
| 219 | - // Bail out if the `register_block_type` function isn't available. |
|
| 220 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 221 | - return; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - register_block_type( 'wordlift/chord', array( |
|
| 225 | - 'editor_script' => 'wl-block-editor', |
|
| 226 | - 'render_callback' => function ( $attributes ) { |
|
| 227 | - $attr_code = ''; |
|
| 228 | - foreach ( $attributes as $key => $value ) { |
|
| 229 | - $attr_code .= $key . '="' . $value . '" '; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - return '[wl_chord ' . $attr_code . ']'; |
|
| 233 | - }, |
|
| 234 | - 'attributes' => array( |
|
| 235 | - 'width' => array( |
|
| 236 | - 'type' => 'string', |
|
| 237 | - 'default' => '100%', |
|
| 238 | - ), |
|
| 239 | - 'height' => array( |
|
| 240 | - 'type' => 'string', |
|
| 241 | - 'default' => '500px', |
|
| 242 | - ), |
|
| 243 | - 'main_color' => array( |
|
| 244 | - 'type' => 'string', |
|
| 245 | - 'default' => '000', |
|
| 246 | - ), |
|
| 247 | - 'depth' => array( |
|
| 248 | - 'type' => 'number', |
|
| 249 | - 'default' => 2, |
|
| 250 | - ), |
|
| 251 | - 'global' => array( |
|
| 252 | - 'type' => 'bool', |
|
| 253 | - 'default' => false, |
|
| 254 | - ), |
|
| 255 | - ), |
|
| 256 | - ) ); |
|
| 219 | + // Bail out if the `register_block_type` function isn't available. |
|
| 220 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 221 | + return; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + register_block_type( 'wordlift/chord', array( |
|
| 225 | + 'editor_script' => 'wl-block-editor', |
|
| 226 | + 'render_callback' => function ( $attributes ) { |
|
| 227 | + $attr_code = ''; |
|
| 228 | + foreach ( $attributes as $key => $value ) { |
|
| 229 | + $attr_code .= $key . '="' . $value . '" '; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + return '[wl_chord ' . $attr_code . ']'; |
|
| 233 | + }, |
|
| 234 | + 'attributes' => array( |
|
| 235 | + 'width' => array( |
|
| 236 | + 'type' => 'string', |
|
| 237 | + 'default' => '100%', |
|
| 238 | + ), |
|
| 239 | + 'height' => array( |
|
| 240 | + 'type' => 'string', |
|
| 241 | + 'default' => '500px', |
|
| 242 | + ), |
|
| 243 | + 'main_color' => array( |
|
| 244 | + 'type' => 'string', |
|
| 245 | + 'default' => '000', |
|
| 246 | + ), |
|
| 247 | + 'depth' => array( |
|
| 248 | + 'type' => 'number', |
|
| 249 | + 'default' => 2, |
|
| 250 | + ), |
|
| 251 | + 'global' => array( |
|
| 252 | + 'type' => 'bool', |
|
| 253 | + 'default' => false, |
|
| 254 | + ), |
|
| 255 | + ), |
|
| 256 | + ) ); |
|
| 257 | 257 | } ); |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | function wl_shortcode_chord_most_referenced_entity_id() { |
| 18 | 18 | // Get the last 20 articles by post date. |
| 19 | 19 | // For each article get the entities they reference. |
| 20 | - $post_ids = get_posts( array( |
|
| 20 | + $post_ids = get_posts(array( |
|
| 21 | 21 | 'numberposts' => 20, |
| 22 | 22 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
| 23 | 23 | 'fields' => 'ids', // Only get post IDs. |
@@ -36,21 +36,21 @@ discard block |
||
| 36 | 36 | ), |
| 37 | 37 | 'orderby' => 'post_date', |
| 38 | 38 | 'order' => 'DESC', |
| 39 | - ) ); |
|
| 39 | + )); |
|
| 40 | 40 | |
| 41 | - if ( empty( $post_ids ) ) { |
|
| 41 | + if (empty($post_ids)) { |
|
| 42 | 42 | return null; |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | $entities = array(); |
| 46 | - foreach ( $post_ids as $id ) { |
|
| 47 | - $entities = array_merge( $entities, wl_core_get_related_entity_ids( $id ) ); |
|
| 46 | + foreach ($post_ids as $id) { |
|
| 47 | + $entities = array_merge($entities, wl_core_get_related_entity_ids($id)); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | - $famous_entities = array_count_values( $entities ); |
|
| 51 | - arsort( $famous_entities ); |
|
| 52 | - if ( sizeof( $famous_entities ) >= 1 ) { |
|
| 53 | - return key( $famous_entities ); |
|
| 50 | + $famous_entities = array_count_values($entities); |
|
| 51 | + arsort($famous_entities); |
|
| 52 | + if (sizeof($famous_entities) >= 1) { |
|
| 53 | + return key($famous_entities); |
|
| 54 | 54 | } else { |
| 55 | 55 | return $post_ids[0]; |
| 56 | 56 | } |
@@ -69,57 +69,56 @@ discard block |
||
| 69 | 69 | * @uses wl_core_get_related_post_ids() to get the list of post ids that reference an entity. |
| 70 | 70 | * |
| 71 | 71 | */ |
| 72 | -function wl_shortcode_chord_get_relations( $entity_id, $depth = 2, $related = null, $max_size = 9 ) { |
|
| 72 | +function wl_shortcode_chord_get_relations($entity_id, $depth = 2, $related = null, $max_size = 9) { |
|
| 73 | 73 | |
| 74 | - if ( ! is_null( $related ) ) { |
|
| 75 | - if ( 0 === $depth ) { |
|
| 74 | + if ( ! is_null($related)) { |
|
| 75 | + if (0 === $depth) { |
|
| 76 | 76 | return $related; |
| 77 | 77 | } |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - wl_write_log( "wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: " . ( is_null( $related ) ? 'yes' : 'no' ) . " ]" ); |
|
| 80 | + wl_write_log("wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: ".(is_null($related) ? 'yes' : 'no')." ]"); |
|
| 81 | 81 | |
| 82 | 82 | // Create a related array which will hold entities and relations. |
| 83 | - if ( is_null( $related ) ) { |
|
| 83 | + if (is_null($related)) { |
|
| 84 | 84 | $related = array( |
| 85 | - 'entities' => array( $entity_id ), |
|
| 85 | + 'entities' => array($entity_id), |
|
| 86 | 86 | 'relations' => array(), |
| 87 | 87 | ); |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | // Get related entities |
| 91 | - $related_entity_ids = wl_core_get_related_entity_ids( $entity_id, array( |
|
| 91 | + $related_entity_ids = wl_core_get_related_entity_ids($entity_id, array( |
|
| 92 | 92 | 'status' => 'publish', |
| 93 | - ) ); |
|
| 93 | + )); |
|
| 94 | 94 | |
| 95 | 95 | // If the current node is an entity, add related posts too |
| 96 | - $related_post_ids = ( Wordlift_Entity_Service::get_instance() |
|
| 97 | - ->is_entity( $entity_id ) ) ? |
|
| 98 | - wl_core_get_related_post_ids( $entity_id, array( |
|
| 96 | + $related_post_ids = (Wordlift_Entity_Service::get_instance() |
|
| 97 | + ->is_entity($entity_id)) ? |
|
| 98 | + wl_core_get_related_post_ids($entity_id, array( |
|
| 99 | 99 | 'status' => 'publish', |
| 100 | - ) ) : |
|
| 101 | - array(); |
|
| 100 | + )) : array(); |
|
| 102 | 101 | |
| 103 | 102 | // Merge results and remove duplicated entries |
| 104 | - $related_ids = array_unique( array_merge( $related_post_ids, $related_entity_ids ) ); |
|
| 103 | + $related_ids = array_unique(array_merge($related_post_ids, $related_entity_ids)); |
|
| 105 | 104 | |
| 106 | 105 | // TODO: List of entities ($rel) should be ordered by interest factors. |
| 107 | - shuffle( $related_ids ); |
|
| 106 | + shuffle($related_ids); |
|
| 108 | 107 | |
| 109 | 108 | // Now we have all the related IDs. |
| 110 | - foreach ( $related_ids as $related_id ) { |
|
| 109 | + foreach ($related_ids as $related_id) { |
|
| 111 | 110 | |
| 112 | - if ( count( $related['entities'] ) >= $max_size ) { |
|
| 111 | + if (count($related['entities']) >= $max_size) { |
|
| 113 | 112 | return $related; |
| 114 | 113 | } |
| 115 | 114 | |
| 116 | - $related['relations'][] = array( $entity_id, $related_id ); |
|
| 115 | + $related['relations'][] = array($entity_id, $related_id); |
|
| 117 | 116 | |
| 118 | - if ( ! in_array( $related_id, $related['entities'] ) ) { |
|
| 117 | + if ( ! in_array($related_id, $related['entities'])) { |
|
| 119 | 118 | // Found new related entity! |
| 120 | 119 | $related['entities'][] = $related_id; |
| 121 | 120 | |
| 122 | - $related = wl_shortcode_chord_get_relations( $related_id, ( $depth - 1 ), $related, $max_size ); |
|
| 121 | + $related = wl_shortcode_chord_get_relations($related_id, ($depth - 1), $related, $max_size); |
|
| 123 | 122 | } |
| 124 | 123 | } |
| 125 | 124 | |
@@ -136,52 +135,52 @@ discard block |
||
| 136 | 135 | * |
| 137 | 136 | * @return mixed|string |
| 138 | 137 | */ |
| 139 | -function wl_shortcode_chord_get_graph( $data ) { |
|
| 138 | +function wl_shortcode_chord_get_graph($data) { |
|
| 140 | 139 | |
| 141 | 140 | // Refactor the entities array in order to provide entities relevant data (uri, url, label, type, css_class). |
| 142 | - array_walk( $data['entities'], function ( &$item ) { |
|
| 143 | - $post = get_post( $item ); |
|
| 141 | + array_walk($data['entities'], function(&$item) { |
|
| 142 | + $post = get_post($item); |
|
| 144 | 143 | |
| 145 | 144 | // Skip non-existing posts. |
| 146 | - if ( is_null( $post ) ) { |
|
| 147 | - wl_write_log( "wl_shortcode_chord_get_graph : post not found [ post id :: $item ]" ); |
|
| 145 | + if (is_null($post)) { |
|
| 146 | + wl_write_log("wl_shortcode_chord_get_graph : post not found [ post id :: $item ]"); |
|
| 148 | 147 | |
| 149 | 148 | return $item; |
| 150 | 149 | } |
| 151 | 150 | |
| 152 | 151 | // Get the entity taxonomy bound to this post (if there's no taxonomy, no stylesheet will be set). |
| 153 | - $term = Wordlift_Entity_Type_Service::get_instance()->get( $item ); |
|
| 152 | + $term = Wordlift_Entity_Type_Service::get_instance()->get($item); |
|
| 154 | 153 | |
| 155 | 154 | // The following log may create a circular loop. |
| 156 | 155 | // wl_write_log( "wl_shortcode_chord_get_graph [ post id :: $post->ID ][ term :: " . var_export( $term, true ) . " ]" ); |
| 157 | 156 | |
| 158 | 157 | // TODO: get all images |
| 159 | 158 | $thumbnail = null; |
| 160 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 161 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 162 | - $attachment = wp_get_attachment_image_src( $thumbnail_id ); |
|
| 163 | - if ( false !== $attachment ) { |
|
| 164 | - $thumbnail = esc_attr( $attachment[0] ); |
|
| 159 | + $thumbnail_id = get_post_thumbnail_id($post->ID); |
|
| 160 | + if ('' !== $thumbnail_id && 0 !== $thumbnail_id) { |
|
| 161 | + $attachment = wp_get_attachment_image_src($thumbnail_id); |
|
| 162 | + if (false !== $attachment) { |
|
| 163 | + $thumbnail = esc_attr($attachment[0]); |
|
| 165 | 164 | } |
| 166 | 165 | } |
| 167 | 166 | |
| 168 | 167 | $entity = array( |
| 169 | - 'uri' => wl_get_entity_uri( $item ), |
|
| 170 | - 'url' => get_permalink( $item ), |
|
| 168 | + 'uri' => wl_get_entity_uri($item), |
|
| 169 | + 'url' => get_permalink($item), |
|
| 171 | 170 | 'label' => $post->post_title, |
| 172 | 171 | 'type' => $post->post_type, |
| 173 | - 'thumbnails' => array( $thumbnail ), |
|
| 174 | - 'css_class' => ( isset( $term['css_class'] ) ? $term['css_class'] : '' ), |
|
| 172 | + 'thumbnails' => array($thumbnail), |
|
| 173 | + 'css_class' => (isset($term['css_class']) ? $term['css_class'] : ''), |
|
| 175 | 174 | ); |
| 176 | 175 | |
| 177 | 176 | $item = $entity; |
| 178 | 177 | } ); |
| 179 | 178 | |
| 180 | 179 | // Refactor the relations. |
| 181 | - array_walk( $data['relations'], function ( &$item ) { |
|
| 180 | + array_walk($data['relations'], function(&$item) { |
|
| 182 | 181 | $relation = array( |
| 183 | - 's' => wl_get_entity_uri( $item[0] ), |
|
| 184 | - 'o' => wl_get_entity_uri( $item[1] ), |
|
| 182 | + 's' => wl_get_entity_uri($item[0]), |
|
| 183 | + 'o' => wl_get_entity_uri($item[1]), |
|
| 185 | 184 | ); |
| 186 | 185 | |
| 187 | 186 | $item = $relation; |
@@ -202,34 +201,34 @@ discard block |
||
| 202 | 201 | $post_id = $_REQUEST['post_id']; |
| 203 | 202 | $depth = $_REQUEST['depth']; |
| 204 | 203 | |
| 205 | - $relations = wl_shortcode_chord_get_relations( $post_id, $depth ); |
|
| 206 | - $graph = wl_shortcode_chord_get_graph( $relations ); |
|
| 204 | + $relations = wl_shortcode_chord_get_relations($post_id, $depth); |
|
| 205 | + $graph = wl_shortcode_chord_get_graph($relations); |
|
| 207 | 206 | |
| 208 | - wl_core_send_json( $graph ); |
|
| 207 | + wl_core_send_json($graph); |
|
| 209 | 208 | } |
| 210 | 209 | |
| 211 | -add_action( 'wp_ajax_wl_chord', 'wl_shortcode_chord_ajax' ); |
|
| 212 | -add_action( 'wp_ajax_nopriv_wl_chord', 'wl_shortcode_chord_ajax' ); |
|
| 210 | +add_action('wp_ajax_wl_chord', 'wl_shortcode_chord_ajax'); |
|
| 211 | +add_action('wp_ajax_nopriv_wl_chord', 'wl_shortcode_chord_ajax'); |
|
| 213 | 212 | |
| 214 | 213 | /** |
| 215 | 214 | * register_block_type for Gutenberg blocks |
| 216 | 215 | */ |
| 217 | -add_action( 'init', function () { |
|
| 216 | +add_action('init', function() { |
|
| 218 | 217 | |
| 219 | 218 | // Bail out if the `register_block_type` function isn't available. |
| 220 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 219 | + if ( ! function_exists('register_block_type')) { |
|
| 221 | 220 | return; |
| 222 | 221 | } |
| 223 | 222 | |
| 224 | - register_block_type( 'wordlift/chord', array( |
|
| 223 | + register_block_type('wordlift/chord', array( |
|
| 225 | 224 | 'editor_script' => 'wl-block-editor', |
| 226 | - 'render_callback' => function ( $attributes ) { |
|
| 225 | + 'render_callback' => function($attributes) { |
|
| 227 | 226 | $attr_code = ''; |
| 228 | - foreach ( $attributes as $key => $value ) { |
|
| 229 | - $attr_code .= $key . '="' . $value . '" '; |
|
| 227 | + foreach ($attributes as $key => $value) { |
|
| 228 | + $attr_code .= $key.'="'.$value.'" '; |
|
| 230 | 229 | } |
| 231 | 230 | |
| 232 | - return '[wl_chord ' . $attr_code . ']'; |
|
| 231 | + return '[wl_chord '.$attr_code.']'; |
|
| 233 | 232 | }, |
| 234 | 233 | 'attributes' => array( |
| 235 | 234 | 'width' => array( |
@@ -253,5 +252,5 @@ discard block |
||
| 253 | 252 | 'default' => false, |
| 254 | 253 | ), |
| 255 | 254 | ), |
| 256 | - ) ); |
|
| 255 | + )); |
|
| 257 | 256 | } ); |
@@ -18,320 +18,320 @@ |
||
| 18 | 18 | |
| 19 | 19 | class Post_Adapter { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * A {@link Wordlift_Log_Service} logging instance. |
|
| 23 | - * |
|
| 24 | - * @access private |
|
| 25 | - * @var \Wordlift_Log_Service A {@link Wordlift_Log_Service} logging instance. |
|
| 26 | - */ |
|
| 27 | - private $log; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 31 | - * |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Entity_Service A {@link Wordlift_Entity_Service} instance. |
|
| 34 | - */ |
|
| 35 | - private $entity_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * A {@link Entity_Store} instance. |
|
| 39 | - * |
|
| 40 | - * @access private |
|
| 41 | - * @var Entity_Store $entity_store A {@link Entity_Store} instance. |
|
| 42 | - */ |
|
| 43 | - private $entity_store; |
|
| 44 | - |
|
| 45 | - public function __construct() { |
|
| 46 | - |
|
| 47 | - // Bail out if block editor's functions aren't available. |
|
| 48 | - if ( ! function_exists( 'register_block_type' ) || ! function_exists( 'parse_blocks' ) ) { |
|
| 49 | - return; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 53 | - |
|
| 54 | - $this->entity_service = \Wordlift_Entity_Service::get_instance(); |
|
| 55 | - $this->entity_store = Entity_Store::get_instance(); |
|
| 56 | - |
|
| 57 | - add_action( 'init', array( $this, 'init' ) ); |
|
| 58 | - add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10, 2 ); |
|
| 59 | - |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Initialize by registering our block type `wordlift/classification`, required for {@link parse_blocks) to work |
|
| 64 | - * correctly. |
|
| 65 | - */ |
|
| 66 | - public function init() { |
|
| 67 | - |
|
| 68 | - register_block_type( 'wordlift/classification', array( |
|
| 69 | - 'editor_script' => 'wl-block-editor', |
|
| 70 | - 'attributes' => array( |
|
| 71 | - 'entities' => array( 'type' => 'array' ), |
|
| 72 | - ), |
|
| 73 | - ) ); |
|
| 74 | - |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * A sample structure: |
|
| 79 | - * |
|
| 80 | - * { |
|
| 81 | - * "entities": [ |
|
| 82 | - * { |
|
| 83 | - * "annotations": { |
|
| 84 | - * "urn:enhancement-7e8e66fc": { |
|
| 85 | - * "start": 3480, |
|
| 86 | - * "end": 3486, |
|
| 87 | - * "text": "libero" |
|
| 88 | - * } |
|
| 89 | - * }, |
|
| 90 | - * "description": "Le libero ou libéro est un poste défensif du volley-ball. Des règles particulières le concernant ont été introduites à la fin des années 1990. De par sa spécificité, le libéro a un statut à part au sein d’une équipe de volley-ball. Pour être identifié, il doit porter un uniforme qui contraste avec ceux des autres membres de son équipe, titulaires ou remplaçants.", |
|
| 91 | - * "id": "http://fr.dbpedia.org/resource/Libero_(volley-ball)", |
|
| 92 | - * "label": "Libero (volley-ball)", |
|
| 93 | - * "mainType": "other", |
|
| 94 | - * "occurrences": ["urn:enhancement-7e8e66fc"], |
|
| 95 | - * "sameAs": null, |
|
| 96 | - * "synonyms": [], |
|
| 97 | - * "types": ["other"] |
|
| 98 | - * } |
|
| 99 | - * ] |
|
| 100 | - * } |
|
| 101 | - * |
|
| 102 | - * @param array $data An array of slashed post data. |
|
| 103 | - * @param array $postarr An array of sanitized, but otherwise unmodified post data. |
|
| 104 | - * |
|
| 105 | - * @return array The data array. |
|
| 106 | - * @throws \Exception |
|
| 107 | - */ |
|
| 108 | - public function wp_insert_post_data( $data, $postarr ) { |
|
| 109 | - |
|
| 110 | - $this->log->trace( "The following data has been received by `wp_insert_post_data`:\n" |
|
| 111 | - . var_export( $data, true ) ); |
|
| 112 | - |
|
| 113 | - try { |
|
| 114 | - $entities = $this->parse_content( wp_unslash( $data['post_content'] ) ); |
|
| 115 | - |
|
| 116 | - foreach ( $entities as $entity ) { |
|
| 117 | - $this->create_or_update_entity( $entity, $data['post_status'] ); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - } catch ( \Exception $e ) { |
|
| 121 | - $this->log->error( $e->getMessage() ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return $data; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Parse the post content to find the `wordlift/classification` block and return the entities' data. |
|
| 129 | - * |
|
| 130 | - * @param string $post_content The post content. |
|
| 131 | - * |
|
| 132 | - * @return array An array of entities' structures. |
|
| 133 | - * @throws \Exception |
|
| 134 | - */ |
|
| 135 | - private function parse_content( $post_content ) { |
|
| 136 | - |
|
| 137 | - $all_blocks = parse_blocks( $post_content ); |
|
| 138 | - $this->log->trace( "The following blocks have been parsed while in `wp_insert_post`:\n" |
|
| 139 | - . var_export( $all_blocks, true ) ); |
|
| 140 | - |
|
| 141 | - $blocks = array_filter( $all_blocks, function ( $item ) { |
|
| 142 | - return ! empty( $item['blockName'] ) && 'wordlift/classification' === $item['blockName']; |
|
| 143 | - } ); |
|
| 144 | - |
|
| 145 | - // Bail out if the blocks' array is empty. |
|
| 146 | - if ( empty( $blocks ) ) { |
|
| 147 | - return array(); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - $block = current( $blocks ); |
|
| 151 | - $this->log->trace( "The following block has been found while in `wp_insert_post`:\n" |
|
| 152 | - . var_export( $block, true ) ); |
|
| 153 | - |
|
| 154 | - // Bail out if the entities array is empty. |
|
| 155 | - if ( empty( $block['attrs'] ) && empty( $block['attrs']['entities'] ) ) { |
|
| 156 | - return array(); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - return $block['attrs']['entities']; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Collect entity labels from the entity array. |
|
| 164 | - * |
|
| 165 | - * This function expects an array with the following keys: |
|
| 166 | - * |
|
| 167 | - * array( |
|
| 168 | - * 'label' => ..., |
|
| 169 | - * 'synonyms' => array( ... ), |
|
| 170 | - * 'annotations' => array( |
|
| 171 | - * ...id... => array( text => ... ), |
|
| 172 | - * ), |
|
| 173 | - * 'occurrences' => array( ... ), |
|
| 174 | - * ) |
|
| 175 | - * |
|
| 176 | - * and it is going to output an array with all the labels, keeping the `label` at first position: |
|
| 177 | - * |
|
| 178 | - * array( |
|
| 179 | - * ...label..., |
|
| 180 | - * ...synonyms..., |
|
| 181 | - * ...texts..., |
|
| 182 | - * ) |
|
| 183 | - * |
|
| 184 | - * This function is going to collect the label from the `label` property, from the `synonyms` property and from |
|
| 185 | - * `annotations` property. Since the `annotations` property contains all the annotations including those that |
|
| 186 | - * haven't been selected, this function is going to only get the `text` for the annotations property listed in |
|
| 187 | - * `occurrences`. |
|
| 188 | - * |
|
| 189 | - * @param array $entity { |
|
| 190 | - * The entity data. |
|
| 191 | - * |
|
| 192 | - * @type string $label The entity label. |
|
| 193 | - * @type array $synonyms The entity synonyms. |
|
| 194 | - * @type array $occurrences The selected occurrences. |
|
| 195 | - * @type array $annotations The annotations. |
|
| 196 | - * } |
|
| 197 | - * |
|
| 198 | - * @return array An array of labels. |
|
| 199 | - */ |
|
| 200 | - public function get_labels( $entity ) { |
|
| 201 | - |
|
| 202 | - $args = wp_parse_args( $entity, array( |
|
| 203 | - 'label' => array(), |
|
| 204 | - 'synonyms' => array(), |
|
| 205 | - 'annotations' => array(), |
|
| 206 | - 'occurrences' => array(), |
|
| 207 | - ) ); |
|
| 208 | - |
|
| 209 | - // We gather all the labels, occurrences texts and synonyms into one array. |
|
| 210 | - $initial = array_merge( |
|
| 211 | - (array) $args['label'], |
|
| 212 | - (array) $args['synonyms'] |
|
| 213 | - ); |
|
| 214 | - |
|
| 215 | - $annotations = $args['annotations']; |
|
| 216 | - |
|
| 217 | - return array_reduce( $args['occurrences'], function ( $carry, $item ) use ( $annotations ) { |
|
| 218 | - |
|
| 219 | - // Bail out if occurrences->$item->text isn't set or its contents are already |
|
| 220 | - // in `$carry`. |
|
| 221 | - if ( ! isset( $annotations[ $item ]['text'] ) |
|
| 222 | - || in_array( $annotations[ $item ]['text'], $carry ) ) { |
|
| 223 | - return $carry; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - // Push the label. |
|
| 227 | - $carry[] = $annotations[ $item ]['text']; |
|
| 228 | - |
|
| 229 | - return $carry; |
|
| 230 | - }, $initial ); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Create or update the entity. |
|
| 235 | - * |
|
| 236 | - * An entity lookup is performed on the local vocabulary using the `id` and `sameAs` URIs. If an entity is found |
|
| 237 | - * the {@link Entity_Store} update function is called to update the `labels` and the `sameAs` values. |
|
| 238 | - * |
|
| 239 | - * If an entity is not found the {@link Entity_Store} create function is called to create a new entity. |
|
| 240 | - * |
|
| 241 | - * @param array $entity { |
|
| 242 | - * The entity parameters. |
|
| 243 | - * |
|
| 244 | - * @type string The entity item id URI. |
|
| 245 | - * @type string|array The entity sameAs URI(s). |
|
| 246 | - * @type string $description The entity description. |
|
| 247 | - * } |
|
| 248 | - * |
|
| 249 | - * @param $string $post_status The post status, default 'draft'. |
|
| 250 | - * |
|
| 251 | - * @return int|\WP_Error |
|
| 252 | - * @throws \Exception |
|
| 253 | - */ |
|
| 254 | - private function create_or_update_entity( $entity, $post_status = 'draft' ) { |
|
| 255 | - |
|
| 256 | - // Get only valid IDs. |
|
| 257 | - $ids = array_filter( (array) $entity['id'], function ( $id ) { |
|
| 258 | - return preg_match( '|^https?://|', $id ); |
|
| 259 | - } ); |
|
| 260 | - |
|
| 261 | - $uris = array_merge( |
|
| 262 | - (array) $ids, |
|
| 263 | - (array) $entity['sameAs'] |
|
| 264 | - ); |
|
| 265 | - |
|
| 266 | - $post = $this->get_first_matching_entity_by_uri( $uris ); |
|
| 267 | - |
|
| 268 | - $this->log->trace( 'Entity' . ( empty( $post ) ? ' not' : '' ) . " found with the following URIs:\n" |
|
| 269 | - . var_export( $uris, true ) ); |
|
| 270 | - |
|
| 271 | - // Get the labels. |
|
| 272 | - $labels = $this->get_labels( $entity ); |
|
| 273 | - |
|
| 274 | - if ( empty( $post ) ) { |
|
| 275 | - // Create the entity if it doesn't exist. |
|
| 276 | - $post_id = $this->entity_store->create( array( |
|
| 277 | - 'labels' => $labels, |
|
| 278 | - 'description' => $entity['description'], |
|
| 279 | - 'same_as' => $uris, |
|
| 280 | - ), $post_status ); |
|
| 281 | - |
|
| 282 | - // Return the WP_Error if we got one. |
|
| 283 | - if ( is_wp_error( $post_id ) ) { |
|
| 284 | - return $post_id; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - // Add the entity type. |
|
| 288 | - if ( isset( $entity['mainType'] ) ) { |
|
| 289 | - wp_set_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - if ( isset( $entity['properties'] ) && isset( $entity['properties']['latitude'] ) && isset( $entity['properties']['longitude'] ) ) { |
|
| 293 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude'] ); |
|
| 294 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude'] ); |
|
| 295 | - } |
|
| 296 | - } else { |
|
| 297 | - // Update the entity otherwise. |
|
| 298 | - $post_id = $this->entity_store->update( array( |
|
| 299 | - 'ID' => $post->ID, |
|
| 300 | - 'labels' => $labels, |
|
| 301 | - 'same_as' => $uris, |
|
| 302 | - ) ); |
|
| 303 | - |
|
| 304 | - // Add the entity type. |
|
| 305 | - if ( isset( $entity['mainType'] ) ) { |
|
| 306 | - wp_add_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - return $post_id; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Get the first matching entity for the provided URI array. |
|
| 315 | - * |
|
| 316 | - * Entities IDs and sameAs are searched. |
|
| 317 | - * |
|
| 318 | - * @param array $uris An array of URIs. |
|
| 319 | - * |
|
| 320 | - * @return \WP_Post|null The entity WP_Post if found or null if not found. |
|
| 321 | - */ |
|
| 322 | - private function get_first_matching_entity_by_uri( $uris ) { |
|
| 323 | - |
|
| 324 | - foreach ( $uris as $uri ) { |
|
| 325 | - $existing_entity = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 326 | - if ( isset( $existing_entity ) ) { |
|
| 327 | - return $existing_entity; |
|
| 328 | - } |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - return null; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - // /** |
|
| 21 | + /** |
|
| 22 | + * A {@link Wordlift_Log_Service} logging instance. |
|
| 23 | + * |
|
| 24 | + * @access private |
|
| 25 | + * @var \Wordlift_Log_Service A {@link Wordlift_Log_Service} logging instance. |
|
| 26 | + */ |
|
| 27 | + private $log; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 31 | + * |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Entity_Service A {@link Wordlift_Entity_Service} instance. |
|
| 34 | + */ |
|
| 35 | + private $entity_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * A {@link Entity_Store} instance. |
|
| 39 | + * |
|
| 40 | + * @access private |
|
| 41 | + * @var Entity_Store $entity_store A {@link Entity_Store} instance. |
|
| 42 | + */ |
|
| 43 | + private $entity_store; |
|
| 44 | + |
|
| 45 | + public function __construct() { |
|
| 46 | + |
|
| 47 | + // Bail out if block editor's functions aren't available. |
|
| 48 | + if ( ! function_exists( 'register_block_type' ) || ! function_exists( 'parse_blocks' ) ) { |
|
| 49 | + return; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 53 | + |
|
| 54 | + $this->entity_service = \Wordlift_Entity_Service::get_instance(); |
|
| 55 | + $this->entity_store = Entity_Store::get_instance(); |
|
| 56 | + |
|
| 57 | + add_action( 'init', array( $this, 'init' ) ); |
|
| 58 | + add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10, 2 ); |
|
| 59 | + |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Initialize by registering our block type `wordlift/classification`, required for {@link parse_blocks) to work |
|
| 64 | + * correctly. |
|
| 65 | + */ |
|
| 66 | + public function init() { |
|
| 67 | + |
|
| 68 | + register_block_type( 'wordlift/classification', array( |
|
| 69 | + 'editor_script' => 'wl-block-editor', |
|
| 70 | + 'attributes' => array( |
|
| 71 | + 'entities' => array( 'type' => 'array' ), |
|
| 72 | + ), |
|
| 73 | + ) ); |
|
| 74 | + |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * A sample structure: |
|
| 79 | + * |
|
| 80 | + * { |
|
| 81 | + * "entities": [ |
|
| 82 | + * { |
|
| 83 | + * "annotations": { |
|
| 84 | + * "urn:enhancement-7e8e66fc": { |
|
| 85 | + * "start": 3480, |
|
| 86 | + * "end": 3486, |
|
| 87 | + * "text": "libero" |
|
| 88 | + * } |
|
| 89 | + * }, |
|
| 90 | + * "description": "Le libero ou libéro est un poste défensif du volley-ball. Des règles particulières le concernant ont été introduites à la fin des années 1990. De par sa spécificité, le libéro a un statut à part au sein d’une équipe de volley-ball. Pour être identifié, il doit porter un uniforme qui contraste avec ceux des autres membres de son équipe, titulaires ou remplaçants.", |
|
| 91 | + * "id": "http://fr.dbpedia.org/resource/Libero_(volley-ball)", |
|
| 92 | + * "label": "Libero (volley-ball)", |
|
| 93 | + * "mainType": "other", |
|
| 94 | + * "occurrences": ["urn:enhancement-7e8e66fc"], |
|
| 95 | + * "sameAs": null, |
|
| 96 | + * "synonyms": [], |
|
| 97 | + * "types": ["other"] |
|
| 98 | + * } |
|
| 99 | + * ] |
|
| 100 | + * } |
|
| 101 | + * |
|
| 102 | + * @param array $data An array of slashed post data. |
|
| 103 | + * @param array $postarr An array of sanitized, but otherwise unmodified post data. |
|
| 104 | + * |
|
| 105 | + * @return array The data array. |
|
| 106 | + * @throws \Exception |
|
| 107 | + */ |
|
| 108 | + public function wp_insert_post_data( $data, $postarr ) { |
|
| 109 | + |
|
| 110 | + $this->log->trace( "The following data has been received by `wp_insert_post_data`:\n" |
|
| 111 | + . var_export( $data, true ) ); |
|
| 112 | + |
|
| 113 | + try { |
|
| 114 | + $entities = $this->parse_content( wp_unslash( $data['post_content'] ) ); |
|
| 115 | + |
|
| 116 | + foreach ( $entities as $entity ) { |
|
| 117 | + $this->create_or_update_entity( $entity, $data['post_status'] ); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + } catch ( \Exception $e ) { |
|
| 121 | + $this->log->error( $e->getMessage() ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return $data; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Parse the post content to find the `wordlift/classification` block and return the entities' data. |
|
| 129 | + * |
|
| 130 | + * @param string $post_content The post content. |
|
| 131 | + * |
|
| 132 | + * @return array An array of entities' structures. |
|
| 133 | + * @throws \Exception |
|
| 134 | + */ |
|
| 135 | + private function parse_content( $post_content ) { |
|
| 136 | + |
|
| 137 | + $all_blocks = parse_blocks( $post_content ); |
|
| 138 | + $this->log->trace( "The following blocks have been parsed while in `wp_insert_post`:\n" |
|
| 139 | + . var_export( $all_blocks, true ) ); |
|
| 140 | + |
|
| 141 | + $blocks = array_filter( $all_blocks, function ( $item ) { |
|
| 142 | + return ! empty( $item['blockName'] ) && 'wordlift/classification' === $item['blockName']; |
|
| 143 | + } ); |
|
| 144 | + |
|
| 145 | + // Bail out if the blocks' array is empty. |
|
| 146 | + if ( empty( $blocks ) ) { |
|
| 147 | + return array(); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + $block = current( $blocks ); |
|
| 151 | + $this->log->trace( "The following block has been found while in `wp_insert_post`:\n" |
|
| 152 | + . var_export( $block, true ) ); |
|
| 153 | + |
|
| 154 | + // Bail out if the entities array is empty. |
|
| 155 | + if ( empty( $block['attrs'] ) && empty( $block['attrs']['entities'] ) ) { |
|
| 156 | + return array(); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + return $block['attrs']['entities']; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Collect entity labels from the entity array. |
|
| 164 | + * |
|
| 165 | + * This function expects an array with the following keys: |
|
| 166 | + * |
|
| 167 | + * array( |
|
| 168 | + * 'label' => ..., |
|
| 169 | + * 'synonyms' => array( ... ), |
|
| 170 | + * 'annotations' => array( |
|
| 171 | + * ...id... => array( text => ... ), |
|
| 172 | + * ), |
|
| 173 | + * 'occurrences' => array( ... ), |
|
| 174 | + * ) |
|
| 175 | + * |
|
| 176 | + * and it is going to output an array with all the labels, keeping the `label` at first position: |
|
| 177 | + * |
|
| 178 | + * array( |
|
| 179 | + * ...label..., |
|
| 180 | + * ...synonyms..., |
|
| 181 | + * ...texts..., |
|
| 182 | + * ) |
|
| 183 | + * |
|
| 184 | + * This function is going to collect the label from the `label` property, from the `synonyms` property and from |
|
| 185 | + * `annotations` property. Since the `annotations` property contains all the annotations including those that |
|
| 186 | + * haven't been selected, this function is going to only get the `text` for the annotations property listed in |
|
| 187 | + * `occurrences`. |
|
| 188 | + * |
|
| 189 | + * @param array $entity { |
|
| 190 | + * The entity data. |
|
| 191 | + * |
|
| 192 | + * @type string $label The entity label. |
|
| 193 | + * @type array $synonyms The entity synonyms. |
|
| 194 | + * @type array $occurrences The selected occurrences. |
|
| 195 | + * @type array $annotations The annotations. |
|
| 196 | + * } |
|
| 197 | + * |
|
| 198 | + * @return array An array of labels. |
|
| 199 | + */ |
|
| 200 | + public function get_labels( $entity ) { |
|
| 201 | + |
|
| 202 | + $args = wp_parse_args( $entity, array( |
|
| 203 | + 'label' => array(), |
|
| 204 | + 'synonyms' => array(), |
|
| 205 | + 'annotations' => array(), |
|
| 206 | + 'occurrences' => array(), |
|
| 207 | + ) ); |
|
| 208 | + |
|
| 209 | + // We gather all the labels, occurrences texts and synonyms into one array. |
|
| 210 | + $initial = array_merge( |
|
| 211 | + (array) $args['label'], |
|
| 212 | + (array) $args['synonyms'] |
|
| 213 | + ); |
|
| 214 | + |
|
| 215 | + $annotations = $args['annotations']; |
|
| 216 | + |
|
| 217 | + return array_reduce( $args['occurrences'], function ( $carry, $item ) use ( $annotations ) { |
|
| 218 | + |
|
| 219 | + // Bail out if occurrences->$item->text isn't set or its contents are already |
|
| 220 | + // in `$carry`. |
|
| 221 | + if ( ! isset( $annotations[ $item ]['text'] ) |
|
| 222 | + || in_array( $annotations[ $item ]['text'], $carry ) ) { |
|
| 223 | + return $carry; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + // Push the label. |
|
| 227 | + $carry[] = $annotations[ $item ]['text']; |
|
| 228 | + |
|
| 229 | + return $carry; |
|
| 230 | + }, $initial ); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Create or update the entity. |
|
| 235 | + * |
|
| 236 | + * An entity lookup is performed on the local vocabulary using the `id` and `sameAs` URIs. If an entity is found |
|
| 237 | + * the {@link Entity_Store} update function is called to update the `labels` and the `sameAs` values. |
|
| 238 | + * |
|
| 239 | + * If an entity is not found the {@link Entity_Store} create function is called to create a new entity. |
|
| 240 | + * |
|
| 241 | + * @param array $entity { |
|
| 242 | + * The entity parameters. |
|
| 243 | + * |
|
| 244 | + * @type string The entity item id URI. |
|
| 245 | + * @type string|array The entity sameAs URI(s). |
|
| 246 | + * @type string $description The entity description. |
|
| 247 | + * } |
|
| 248 | + * |
|
| 249 | + * @param $string $post_status The post status, default 'draft'. |
|
| 250 | + * |
|
| 251 | + * @return int|\WP_Error |
|
| 252 | + * @throws \Exception |
|
| 253 | + */ |
|
| 254 | + private function create_or_update_entity( $entity, $post_status = 'draft' ) { |
|
| 255 | + |
|
| 256 | + // Get only valid IDs. |
|
| 257 | + $ids = array_filter( (array) $entity['id'], function ( $id ) { |
|
| 258 | + return preg_match( '|^https?://|', $id ); |
|
| 259 | + } ); |
|
| 260 | + |
|
| 261 | + $uris = array_merge( |
|
| 262 | + (array) $ids, |
|
| 263 | + (array) $entity['sameAs'] |
|
| 264 | + ); |
|
| 265 | + |
|
| 266 | + $post = $this->get_first_matching_entity_by_uri( $uris ); |
|
| 267 | + |
|
| 268 | + $this->log->trace( 'Entity' . ( empty( $post ) ? ' not' : '' ) . " found with the following URIs:\n" |
|
| 269 | + . var_export( $uris, true ) ); |
|
| 270 | + |
|
| 271 | + // Get the labels. |
|
| 272 | + $labels = $this->get_labels( $entity ); |
|
| 273 | + |
|
| 274 | + if ( empty( $post ) ) { |
|
| 275 | + // Create the entity if it doesn't exist. |
|
| 276 | + $post_id = $this->entity_store->create( array( |
|
| 277 | + 'labels' => $labels, |
|
| 278 | + 'description' => $entity['description'], |
|
| 279 | + 'same_as' => $uris, |
|
| 280 | + ), $post_status ); |
|
| 281 | + |
|
| 282 | + // Return the WP_Error if we got one. |
|
| 283 | + if ( is_wp_error( $post_id ) ) { |
|
| 284 | + return $post_id; |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + // Add the entity type. |
|
| 288 | + if ( isset( $entity['mainType'] ) ) { |
|
| 289 | + wp_set_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + if ( isset( $entity['properties'] ) && isset( $entity['properties']['latitude'] ) && isset( $entity['properties']['longitude'] ) ) { |
|
| 293 | + add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude'] ); |
|
| 294 | + add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude'] ); |
|
| 295 | + } |
|
| 296 | + } else { |
|
| 297 | + // Update the entity otherwise. |
|
| 298 | + $post_id = $this->entity_store->update( array( |
|
| 299 | + 'ID' => $post->ID, |
|
| 300 | + 'labels' => $labels, |
|
| 301 | + 'same_as' => $uris, |
|
| 302 | + ) ); |
|
| 303 | + |
|
| 304 | + // Add the entity type. |
|
| 305 | + if ( isset( $entity['mainType'] ) ) { |
|
| 306 | + wp_add_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + return $post_id; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Get the first matching entity for the provided URI array. |
|
| 315 | + * |
|
| 316 | + * Entities IDs and sameAs are searched. |
|
| 317 | + * |
|
| 318 | + * @param array $uris An array of URIs. |
|
| 319 | + * |
|
| 320 | + * @return \WP_Post|null The entity WP_Post if found or null if not found. |
|
| 321 | + */ |
|
| 322 | + private function get_first_matching_entity_by_uri( $uris ) { |
|
| 323 | + |
|
| 324 | + foreach ( $uris as $uri ) { |
|
| 325 | + $existing_entity = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 326 | + if ( isset( $existing_entity ) ) { |
|
| 327 | + return $existing_entity; |
|
| 328 | + } |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + return null; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + // /** |
|
| 335 | 335 | // * @param int $post_ID Post ID. |
| 336 | 336 | // * @param \WP_Post $post Post object. |
| 337 | 337 | // * @param bool $update Whether this is an existing post being updated or not. |
@@ -45,17 +45,17 @@ discard block |
||
| 45 | 45 | public function __construct() { |
| 46 | 46 | |
| 47 | 47 | // Bail out if block editor's functions aren't available. |
| 48 | - if ( ! function_exists( 'register_block_type' ) || ! function_exists( 'parse_blocks' ) ) { |
|
| 48 | + if ( ! function_exists('register_block_type') || ! function_exists('parse_blocks')) { |
|
| 49 | 49 | return; |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 52 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
| 53 | 53 | |
| 54 | 54 | $this->entity_service = \Wordlift_Entity_Service::get_instance(); |
| 55 | 55 | $this->entity_store = Entity_Store::get_instance(); |
| 56 | 56 | |
| 57 | - add_action( 'init', array( $this, 'init' ) ); |
|
| 58 | - add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10, 2 ); |
|
| 57 | + add_action('init', array($this, 'init')); |
|
| 58 | + add_filter('wp_insert_post_data', array($this, 'wp_insert_post_data'), 10, 2); |
|
| 59 | 59 | |
| 60 | 60 | } |
| 61 | 61 | |
@@ -65,12 +65,12 @@ discard block |
||
| 65 | 65 | */ |
| 66 | 66 | public function init() { |
| 67 | 67 | |
| 68 | - register_block_type( 'wordlift/classification', array( |
|
| 68 | + register_block_type('wordlift/classification', array( |
|
| 69 | 69 | 'editor_script' => 'wl-block-editor', |
| 70 | 70 | 'attributes' => array( |
| 71 | - 'entities' => array( 'type' => 'array' ), |
|
| 71 | + 'entities' => array('type' => 'array'), |
|
| 72 | 72 | ), |
| 73 | - ) ); |
|
| 73 | + )); |
|
| 74 | 74 | |
| 75 | 75 | } |
| 76 | 76 | |
@@ -105,20 +105,20 @@ discard block |
||
| 105 | 105 | * @return array The data array. |
| 106 | 106 | * @throws \Exception |
| 107 | 107 | */ |
| 108 | - public function wp_insert_post_data( $data, $postarr ) { |
|
| 108 | + public function wp_insert_post_data($data, $postarr) { |
|
| 109 | 109 | |
| 110 | - $this->log->trace( "The following data has been received by `wp_insert_post_data`:\n" |
|
| 111 | - . var_export( $data, true ) ); |
|
| 110 | + $this->log->trace("The following data has been received by `wp_insert_post_data`:\n" |
|
| 111 | + . var_export($data, true)); |
|
| 112 | 112 | |
| 113 | 113 | try { |
| 114 | - $entities = $this->parse_content( wp_unslash( $data['post_content'] ) ); |
|
| 114 | + $entities = $this->parse_content(wp_unslash($data['post_content'])); |
|
| 115 | 115 | |
| 116 | - foreach ( $entities as $entity ) { |
|
| 117 | - $this->create_or_update_entity( $entity, $data['post_status'] ); |
|
| 116 | + foreach ($entities as $entity) { |
|
| 117 | + $this->create_or_update_entity($entity, $data['post_status']); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - } catch ( \Exception $e ) { |
|
| 121 | - $this->log->error( $e->getMessage() ); |
|
| 120 | + } catch (\Exception $e) { |
|
| 121 | + $this->log->error($e->getMessage()); |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | return $data; |
@@ -132,27 +132,27 @@ discard block |
||
| 132 | 132 | * @return array An array of entities' structures. |
| 133 | 133 | * @throws \Exception |
| 134 | 134 | */ |
| 135 | - private function parse_content( $post_content ) { |
|
| 135 | + private function parse_content($post_content) { |
|
| 136 | 136 | |
| 137 | - $all_blocks = parse_blocks( $post_content ); |
|
| 138 | - $this->log->trace( "The following blocks have been parsed while in `wp_insert_post`:\n" |
|
| 139 | - . var_export( $all_blocks, true ) ); |
|
| 137 | + $all_blocks = parse_blocks($post_content); |
|
| 138 | + $this->log->trace("The following blocks have been parsed while in `wp_insert_post`:\n" |
|
| 139 | + . var_export($all_blocks, true)); |
|
| 140 | 140 | |
| 141 | - $blocks = array_filter( $all_blocks, function ( $item ) { |
|
| 142 | - return ! empty( $item['blockName'] ) && 'wordlift/classification' === $item['blockName']; |
|
| 141 | + $blocks = array_filter($all_blocks, function($item) { |
|
| 142 | + return ! empty($item['blockName']) && 'wordlift/classification' === $item['blockName']; |
|
| 143 | 143 | } ); |
| 144 | 144 | |
| 145 | 145 | // Bail out if the blocks' array is empty. |
| 146 | - if ( empty( $blocks ) ) { |
|
| 146 | + if (empty($blocks)) { |
|
| 147 | 147 | return array(); |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | - $block = current( $blocks ); |
|
| 151 | - $this->log->trace( "The following block has been found while in `wp_insert_post`:\n" |
|
| 152 | - . var_export( $block, true ) ); |
|
| 150 | + $block = current($blocks); |
|
| 151 | + $this->log->trace("The following block has been found while in `wp_insert_post`:\n" |
|
| 152 | + . var_export($block, true)); |
|
| 153 | 153 | |
| 154 | 154 | // Bail out if the entities array is empty. |
| 155 | - if ( empty( $block['attrs'] ) && empty( $block['attrs']['entities'] ) ) { |
|
| 155 | + if (empty($block['attrs']) && empty($block['attrs']['entities'])) { |
|
| 156 | 156 | return array(); |
| 157 | 157 | } |
| 158 | 158 | |
@@ -197,14 +197,14 @@ discard block |
||
| 197 | 197 | * |
| 198 | 198 | * @return array An array of labels. |
| 199 | 199 | */ |
| 200 | - public function get_labels( $entity ) { |
|
| 200 | + public function get_labels($entity) { |
|
| 201 | 201 | |
| 202 | - $args = wp_parse_args( $entity, array( |
|
| 202 | + $args = wp_parse_args($entity, array( |
|
| 203 | 203 | 'label' => array(), |
| 204 | 204 | 'synonyms' => array(), |
| 205 | 205 | 'annotations' => array(), |
| 206 | 206 | 'occurrences' => array(), |
| 207 | - ) ); |
|
| 207 | + )); |
|
| 208 | 208 | |
| 209 | 209 | // We gather all the labels, occurrences texts and synonyms into one array. |
| 210 | 210 | $initial = array_merge( |
@@ -214,20 +214,20 @@ discard block |
||
| 214 | 214 | |
| 215 | 215 | $annotations = $args['annotations']; |
| 216 | 216 | |
| 217 | - return array_reduce( $args['occurrences'], function ( $carry, $item ) use ( $annotations ) { |
|
| 217 | + return array_reduce($args['occurrences'], function($carry, $item) use ($annotations) { |
|
| 218 | 218 | |
| 219 | 219 | // Bail out if occurrences->$item->text isn't set or its contents are already |
| 220 | 220 | // in `$carry`. |
| 221 | - if ( ! isset( $annotations[ $item ]['text'] ) |
|
| 222 | - || in_array( $annotations[ $item ]['text'], $carry ) ) { |
|
| 221 | + if ( ! isset($annotations[$item]['text']) |
|
| 222 | + || in_array($annotations[$item]['text'], $carry)) { |
|
| 223 | 223 | return $carry; |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | // Push the label. |
| 227 | - $carry[] = $annotations[ $item ]['text']; |
|
| 227 | + $carry[] = $annotations[$item]['text']; |
|
| 228 | 228 | |
| 229 | 229 | return $carry; |
| 230 | - }, $initial ); |
|
| 230 | + }, $initial); |
|
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | /** |
@@ -251,11 +251,11 @@ discard block |
||
| 251 | 251 | * @return int|\WP_Error |
| 252 | 252 | * @throws \Exception |
| 253 | 253 | */ |
| 254 | - private function create_or_update_entity( $entity, $post_status = 'draft' ) { |
|
| 254 | + private function create_or_update_entity($entity, $post_status = 'draft') { |
|
| 255 | 255 | |
| 256 | 256 | // Get only valid IDs. |
| 257 | - $ids = array_filter( (array) $entity['id'], function ( $id ) { |
|
| 258 | - return preg_match( '|^https?://|', $id ); |
|
| 257 | + $ids = array_filter((array) $entity['id'], function($id) { |
|
| 258 | + return preg_match('|^https?://|', $id); |
|
| 259 | 259 | } ); |
| 260 | 260 | |
| 261 | 261 | $uris = array_merge( |
@@ -263,47 +263,47 @@ discard block |
||
| 263 | 263 | (array) $entity['sameAs'] |
| 264 | 264 | ); |
| 265 | 265 | |
| 266 | - $post = $this->get_first_matching_entity_by_uri( $uris ); |
|
| 266 | + $post = $this->get_first_matching_entity_by_uri($uris); |
|
| 267 | 267 | |
| 268 | - $this->log->trace( 'Entity' . ( empty( $post ) ? ' not' : '' ) . " found with the following URIs:\n" |
|
| 269 | - . var_export( $uris, true ) ); |
|
| 268 | + $this->log->trace('Entity'.(empty($post) ? ' not' : '')." found with the following URIs:\n" |
|
| 269 | + . var_export($uris, true)); |
|
| 270 | 270 | |
| 271 | 271 | // Get the labels. |
| 272 | - $labels = $this->get_labels( $entity ); |
|
| 272 | + $labels = $this->get_labels($entity); |
|
| 273 | 273 | |
| 274 | - if ( empty( $post ) ) { |
|
| 274 | + if (empty($post)) { |
|
| 275 | 275 | // Create the entity if it doesn't exist. |
| 276 | - $post_id = $this->entity_store->create( array( |
|
| 276 | + $post_id = $this->entity_store->create(array( |
|
| 277 | 277 | 'labels' => $labels, |
| 278 | 278 | 'description' => $entity['description'], |
| 279 | 279 | 'same_as' => $uris, |
| 280 | - ), $post_status ); |
|
| 280 | + ), $post_status); |
|
| 281 | 281 | |
| 282 | 282 | // Return the WP_Error if we got one. |
| 283 | - if ( is_wp_error( $post_id ) ) { |
|
| 283 | + if (is_wp_error($post_id)) { |
|
| 284 | 284 | return $post_id; |
| 285 | 285 | } |
| 286 | 286 | |
| 287 | 287 | // Add the entity type. |
| 288 | - if ( isset( $entity['mainType'] ) ) { |
|
| 289 | - wp_set_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 288 | + if (isset($entity['mainType'])) { |
|
| 289 | + wp_set_object_terms($post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 290 | 290 | } |
| 291 | 291 | |
| 292 | - if ( isset( $entity['properties'] ) && isset( $entity['properties']['latitude'] ) && isset( $entity['properties']['longitude'] ) ) { |
|
| 293 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude'] ); |
|
| 294 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude'] ); |
|
| 292 | + if (isset($entity['properties']) && isset($entity['properties']['latitude']) && isset($entity['properties']['longitude'])) { |
|
| 293 | + add_post_meta($post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude']); |
|
| 294 | + add_post_meta($post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude']); |
|
| 295 | 295 | } |
| 296 | 296 | } else { |
| 297 | 297 | // Update the entity otherwise. |
| 298 | - $post_id = $this->entity_store->update( array( |
|
| 298 | + $post_id = $this->entity_store->update(array( |
|
| 299 | 299 | 'ID' => $post->ID, |
| 300 | 300 | 'labels' => $labels, |
| 301 | 301 | 'same_as' => $uris, |
| 302 | - ) ); |
|
| 302 | + )); |
|
| 303 | 303 | |
| 304 | 304 | // Add the entity type. |
| 305 | - if ( isset( $entity['mainType'] ) ) { |
|
| 306 | - wp_add_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 305 | + if (isset($entity['mainType'])) { |
|
| 306 | + wp_add_object_terms($post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 307 | 307 | } |
| 308 | 308 | } |
| 309 | 309 | |
@@ -319,11 +319,11 @@ discard block |
||
| 319 | 319 | * |
| 320 | 320 | * @return \WP_Post|null The entity WP_Post if found or null if not found. |
| 321 | 321 | */ |
| 322 | - private function get_first_matching_entity_by_uri( $uris ) { |
|
| 322 | + private function get_first_matching_entity_by_uri($uris) { |
|
| 323 | 323 | |
| 324 | - foreach ( $uris as $uri ) { |
|
| 325 | - $existing_entity = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 326 | - if ( isset( $existing_entity ) ) { |
|
| 324 | + foreach ($uris as $uri) { |
|
| 325 | + $existing_entity = $this->entity_service->get_entity_post_by_uri($uri); |
|
| 326 | + if (isset($existing_entity)) { |
|
| 327 | 327 | return $existing_entity; |
| 328 | 328 | } |
| 329 | 329 | } |
@@ -104,6 +104,11 @@ |
||
| 104 | 104 | } |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | + /** |
|
| 108 | + * @param string $url |
|
| 109 | + * |
|
| 110 | + * @return integer|null |
|
| 111 | + */ |
|
| 107 | 112 | static function url_to_postid( $url ) { |
| 108 | 113 | // Try with url_to_postid |
| 109 | 114 | $post_id = url_to_postid( $url ); |
@@ -9,113 +9,113 @@ |
||
| 9 | 9 | |
| 10 | 10 | class Wordlift_Context_Cards_Service { |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - private $endpoint; |
|
| 16 | - private $jsonld_endpoint; |
|
| 17 | - |
|
| 18 | - function __construct() { |
|
| 19 | - |
|
| 20 | - $this->endpoint = '/context-card'; |
|
| 21 | - $this->jsonld_endpoint = '/jsonld'; |
|
| 22 | - |
|
| 23 | - // PHP 5.3 compatibility as `$this` cannot be used in closures. |
|
| 24 | - $that = $this; |
|
| 25 | - |
|
| 26 | - add_action( 'rest_api_init', function () use ( $that ) { |
|
| 27 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, $that->endpoint, array( |
|
| 28 | - 'methods' => 'GET', |
|
| 29 | - 'callback' => array( $that, 'context_data' ), |
|
| 30 | - ) ); |
|
| 31 | - } ); |
|
| 32 | - |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - function format_response( $jsonld, $publisher = true ) { |
|
| 36 | - |
|
| 37 | - $response = array(); |
|
| 38 | - |
|
| 39 | - if ( ! isset( $jsonld ) || empty( $jsonld ) || empty( $jsonld[0] ) ) { |
|
| 40 | - return null; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - if ( isset( $jsonld[0]['description'] ) && ! empty( $jsonld[0]['description'] ) ) { |
|
| 44 | - if ( isset( $jsonld[0]['name'] ) && ! empty( $jsonld[0]['name'] ) ) { |
|
| 45 | - $title = $jsonld[0]['name']; |
|
| 46 | - $pos = strpos( $jsonld[0]['description'], $title ); |
|
| 47 | - $response['description'] = $jsonld[0]['description']; |
|
| 48 | - if ( $pos !== false ) { |
|
| 49 | - $response['description'] = substr_replace( $response['description'], "<strong>$title</strong>", $pos, strlen( $title ) ); |
|
| 50 | - } |
|
| 51 | - } else { |
|
| 52 | - $response['description'] = $jsonld[0]['description']; |
|
| 53 | - } |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - if ( isset( $jsonld[0]['name'] ) && ! empty( $jsonld[0]['name'] ) ) { |
|
| 57 | - $response['title'] = $jsonld[0]['name']; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - if ( isset( $jsonld[0]['url'] ) && ! empty( $jsonld[0]['url'] ) ) { |
|
| 61 | - $response['url'] = $jsonld[0]['url']; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - if ( isset( $jsonld[0]['image'] ) && |
|
| 65 | - isset( $jsonld[0]['image'][0]['url'] ) && |
|
| 66 | - isset( $jsonld[0]['image'][0]['width'] ) && |
|
| 67 | - isset( $jsonld[0]['image'][0]['height'] ) |
|
| 68 | - ) { |
|
| 69 | - $response['image'] = array( |
|
| 70 | - 'url' => $jsonld[0]['image'][0]['url'], |
|
| 71 | - 'width' => $jsonld[0]['image'][0]['width'], |
|
| 72 | - 'height' => $jsonld[0]['image'][0]['height'], |
|
| 73 | - ); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - if ( $publisher ) { |
|
| 77 | - $publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id(); |
|
| 78 | - $publisher_jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $publisher_id ); |
|
| 79 | - $response['publisher'] = $this->format_response( $publisher_jsonld, false ); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - return $response; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - public function context_data( $request ) { |
|
| 86 | - |
|
| 87 | - $entity_uri = urldecode( $request->get_param( 'entity_url' ) ); |
|
| 88 | - $entity_id = Wordlift_Context_Cards_Service::url_to_postid( $entity_uri ); |
|
| 89 | - $jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $entity_id ); |
|
| 90 | - |
|
| 91 | - return $this->format_response( $jsonld ); |
|
| 92 | - |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - public function enqueue_scripts() { |
|
| 96 | - $show_context_cards = apply_filters( 'wl_show_context_cards', true ); |
|
| 97 | - $base_url = apply_filters( 'wl_context_cards_base_url', get_rest_url() . WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint ); |
|
| 98 | - if ( $show_context_cards ) { |
|
| 99 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 100 | - wp_localize_script( 'wordlift-cloud', '_wlCloudSettings', array( |
|
| 101 | - 'selector' => 'a.wl-entity-page-link', |
|
| 102 | - 'url' => get_rest_url( NULL, WL_REST_ROUTE_DEFAULT_NAMESPACE . '/jsonld' ), |
|
| 103 | - ) ); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - static function url_to_postid( $url ) { |
|
| 108 | - // Try with url_to_postid |
|
| 109 | - $post_id = url_to_postid( $url ); |
|
| 110 | - if ( $post_id == 0 ) { |
|
| 111 | - // Try with get_page_by_path |
|
| 112 | - $post = get_page_by_path( basename( untrailingslashit( $url ) ), OBJECT, 'entity' ); |
|
| 113 | - if ( $post ) { |
|
| 114 | - $post_id = $post->ID; |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - return $post_id; |
|
| 119 | - } |
|
| 12 | + /** |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + private $endpoint; |
|
| 16 | + private $jsonld_endpoint; |
|
| 17 | + |
|
| 18 | + function __construct() { |
|
| 19 | + |
|
| 20 | + $this->endpoint = '/context-card'; |
|
| 21 | + $this->jsonld_endpoint = '/jsonld'; |
|
| 22 | + |
|
| 23 | + // PHP 5.3 compatibility as `$this` cannot be used in closures. |
|
| 24 | + $that = $this; |
|
| 25 | + |
|
| 26 | + add_action( 'rest_api_init', function () use ( $that ) { |
|
| 27 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, $that->endpoint, array( |
|
| 28 | + 'methods' => 'GET', |
|
| 29 | + 'callback' => array( $that, 'context_data' ), |
|
| 30 | + ) ); |
|
| 31 | + } ); |
|
| 32 | + |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + function format_response( $jsonld, $publisher = true ) { |
|
| 36 | + |
|
| 37 | + $response = array(); |
|
| 38 | + |
|
| 39 | + if ( ! isset( $jsonld ) || empty( $jsonld ) || empty( $jsonld[0] ) ) { |
|
| 40 | + return null; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + if ( isset( $jsonld[0]['description'] ) && ! empty( $jsonld[0]['description'] ) ) { |
|
| 44 | + if ( isset( $jsonld[0]['name'] ) && ! empty( $jsonld[0]['name'] ) ) { |
|
| 45 | + $title = $jsonld[0]['name']; |
|
| 46 | + $pos = strpos( $jsonld[0]['description'], $title ); |
|
| 47 | + $response['description'] = $jsonld[0]['description']; |
|
| 48 | + if ( $pos !== false ) { |
|
| 49 | + $response['description'] = substr_replace( $response['description'], "<strong>$title</strong>", $pos, strlen( $title ) ); |
|
| 50 | + } |
|
| 51 | + } else { |
|
| 52 | + $response['description'] = $jsonld[0]['description']; |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + if ( isset( $jsonld[0]['name'] ) && ! empty( $jsonld[0]['name'] ) ) { |
|
| 57 | + $response['title'] = $jsonld[0]['name']; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + if ( isset( $jsonld[0]['url'] ) && ! empty( $jsonld[0]['url'] ) ) { |
|
| 61 | + $response['url'] = $jsonld[0]['url']; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + if ( isset( $jsonld[0]['image'] ) && |
|
| 65 | + isset( $jsonld[0]['image'][0]['url'] ) && |
|
| 66 | + isset( $jsonld[0]['image'][0]['width'] ) && |
|
| 67 | + isset( $jsonld[0]['image'][0]['height'] ) |
|
| 68 | + ) { |
|
| 69 | + $response['image'] = array( |
|
| 70 | + 'url' => $jsonld[0]['image'][0]['url'], |
|
| 71 | + 'width' => $jsonld[0]['image'][0]['width'], |
|
| 72 | + 'height' => $jsonld[0]['image'][0]['height'], |
|
| 73 | + ); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + if ( $publisher ) { |
|
| 77 | + $publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id(); |
|
| 78 | + $publisher_jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $publisher_id ); |
|
| 79 | + $response['publisher'] = $this->format_response( $publisher_jsonld, false ); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + return $response; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + public function context_data( $request ) { |
|
| 86 | + |
|
| 87 | + $entity_uri = urldecode( $request->get_param( 'entity_url' ) ); |
|
| 88 | + $entity_id = Wordlift_Context_Cards_Service::url_to_postid( $entity_uri ); |
|
| 89 | + $jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $entity_id ); |
|
| 90 | + |
|
| 91 | + return $this->format_response( $jsonld ); |
|
| 92 | + |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + public function enqueue_scripts() { |
|
| 96 | + $show_context_cards = apply_filters( 'wl_show_context_cards', true ); |
|
| 97 | + $base_url = apply_filters( 'wl_context_cards_base_url', get_rest_url() . WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint ); |
|
| 98 | + if ( $show_context_cards ) { |
|
| 99 | + wp_enqueue_script( 'wordlift-cloud' ); |
|
| 100 | + wp_localize_script( 'wordlift-cloud', '_wlCloudSettings', array( |
|
| 101 | + 'selector' => 'a.wl-entity-page-link', |
|
| 102 | + 'url' => get_rest_url( NULL, WL_REST_ROUTE_DEFAULT_NAMESPACE . '/jsonld' ), |
|
| 103 | + ) ); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + static function url_to_postid( $url ) { |
|
| 108 | + // Try with url_to_postid |
|
| 109 | + $post_id = url_to_postid( $url ); |
|
| 110 | + if ( $post_id == 0 ) { |
|
| 111 | + // Try with get_page_by_path |
|
| 112 | + $post = get_page_by_path( basename( untrailingslashit( $url ) ), OBJECT, 'entity' ); |
|
| 113 | + if ( $post ) { |
|
| 114 | + $post_id = $post->ID; |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + return $post_id; |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | 121 | } |
@@ -23,48 +23,48 @@ discard block |
||
| 23 | 23 | // PHP 5.3 compatibility as `$this` cannot be used in closures. |
| 24 | 24 | $that = $this; |
| 25 | 25 | |
| 26 | - add_action( 'rest_api_init', function () use ( $that ) { |
|
| 27 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, $that->endpoint, array( |
|
| 26 | + add_action('rest_api_init', function() use ($that) { |
|
| 27 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, $that->endpoint, array( |
|
| 28 | 28 | 'methods' => 'GET', |
| 29 | - 'callback' => array( $that, 'context_data' ), |
|
| 30 | - ) ); |
|
| 29 | + 'callback' => array($that, 'context_data'), |
|
| 30 | + )); |
|
| 31 | 31 | } ); |
| 32 | 32 | |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | - function format_response( $jsonld, $publisher = true ) { |
|
| 35 | + function format_response($jsonld, $publisher = true) { |
|
| 36 | 36 | |
| 37 | 37 | $response = array(); |
| 38 | 38 | |
| 39 | - if ( ! isset( $jsonld ) || empty( $jsonld ) || empty( $jsonld[0] ) ) { |
|
| 39 | + if ( ! isset($jsonld) || empty($jsonld) || empty($jsonld[0])) { |
|
| 40 | 40 | return null; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - if ( isset( $jsonld[0]['description'] ) && ! empty( $jsonld[0]['description'] ) ) { |
|
| 44 | - if ( isset( $jsonld[0]['name'] ) && ! empty( $jsonld[0]['name'] ) ) { |
|
| 43 | + if (isset($jsonld[0]['description']) && ! empty($jsonld[0]['description'])) { |
|
| 44 | + if (isset($jsonld[0]['name']) && ! empty($jsonld[0]['name'])) { |
|
| 45 | 45 | $title = $jsonld[0]['name']; |
| 46 | - $pos = strpos( $jsonld[0]['description'], $title ); |
|
| 46 | + $pos = strpos($jsonld[0]['description'], $title); |
|
| 47 | 47 | $response['description'] = $jsonld[0]['description']; |
| 48 | - if ( $pos !== false ) { |
|
| 49 | - $response['description'] = substr_replace( $response['description'], "<strong>$title</strong>", $pos, strlen( $title ) ); |
|
| 48 | + if ($pos !== false) { |
|
| 49 | + $response['description'] = substr_replace($response['description'], "<strong>$title</strong>", $pos, strlen($title)); |
|
| 50 | 50 | } |
| 51 | 51 | } else { |
| 52 | 52 | $response['description'] = $jsonld[0]['description']; |
| 53 | 53 | } |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | - if ( isset( $jsonld[0]['name'] ) && ! empty( $jsonld[0]['name'] ) ) { |
|
| 56 | + if (isset($jsonld[0]['name']) && ! empty($jsonld[0]['name'])) { |
|
| 57 | 57 | $response['title'] = $jsonld[0]['name']; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - if ( isset( $jsonld[0]['url'] ) && ! empty( $jsonld[0]['url'] ) ) { |
|
| 60 | + if (isset($jsonld[0]['url']) && ! empty($jsonld[0]['url'])) { |
|
| 61 | 61 | $response['url'] = $jsonld[0]['url']; |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | - if ( isset( $jsonld[0]['image'] ) && |
|
| 65 | - isset( $jsonld[0]['image'][0]['url'] ) && |
|
| 66 | - isset( $jsonld[0]['image'][0]['width'] ) && |
|
| 67 | - isset( $jsonld[0]['image'][0]['height'] ) |
|
| 64 | + if (isset($jsonld[0]['image']) && |
|
| 65 | + isset($jsonld[0]['image'][0]['url']) && |
|
| 66 | + isset($jsonld[0]['image'][0]['width']) && |
|
| 67 | + isset($jsonld[0]['image'][0]['height']) |
|
| 68 | 68 | ) { |
| 69 | 69 | $response['image'] = array( |
| 70 | 70 | 'url' => $jsonld[0]['image'][0]['url'], |
@@ -73,44 +73,44 @@ discard block |
||
| 73 | 73 | ); |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | - if ( $publisher ) { |
|
| 76 | + if ($publisher) { |
|
| 77 | 77 | $publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id(); |
| 78 | - $publisher_jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $publisher_id ); |
|
| 79 | - $response['publisher'] = $this->format_response( $publisher_jsonld, false ); |
|
| 78 | + $publisher_jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld(false, $publisher_id); |
|
| 79 | + $response['publisher'] = $this->format_response($publisher_jsonld, false); |
|
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | return $response; |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - public function context_data( $request ) { |
|
| 85 | + public function context_data($request) { |
|
| 86 | 86 | |
| 87 | - $entity_uri = urldecode( $request->get_param( 'entity_url' ) ); |
|
| 88 | - $entity_id = Wordlift_Context_Cards_Service::url_to_postid( $entity_uri ); |
|
| 89 | - $jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $entity_id ); |
|
| 87 | + $entity_uri = urldecode($request->get_param('entity_url')); |
|
| 88 | + $entity_id = Wordlift_Context_Cards_Service::url_to_postid($entity_uri); |
|
| 89 | + $jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld(false, $entity_id); |
|
| 90 | 90 | |
| 91 | - return $this->format_response( $jsonld ); |
|
| 91 | + return $this->format_response($jsonld); |
|
| 92 | 92 | |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | public function enqueue_scripts() { |
| 96 | - $show_context_cards = apply_filters( 'wl_show_context_cards', true ); |
|
| 97 | - $base_url = apply_filters( 'wl_context_cards_base_url', get_rest_url() . WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint ); |
|
| 98 | - if ( $show_context_cards ) { |
|
| 99 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 100 | - wp_localize_script( 'wordlift-cloud', '_wlCloudSettings', array( |
|
| 96 | + $show_context_cards = apply_filters('wl_show_context_cards', true); |
|
| 97 | + $base_url = apply_filters('wl_context_cards_base_url', get_rest_url().WL_REST_ROUTE_DEFAULT_NAMESPACE.$this->endpoint); |
|
| 98 | + if ($show_context_cards) { |
|
| 99 | + wp_enqueue_script('wordlift-cloud'); |
|
| 100 | + wp_localize_script('wordlift-cloud', '_wlCloudSettings', array( |
|
| 101 | 101 | 'selector' => 'a.wl-entity-page-link', |
| 102 | - 'url' => get_rest_url( NULL, WL_REST_ROUTE_DEFAULT_NAMESPACE . '/jsonld' ), |
|
| 103 | - ) ); |
|
| 102 | + 'url' => get_rest_url(NULL, WL_REST_ROUTE_DEFAULT_NAMESPACE.'/jsonld'), |
|
| 103 | + )); |
|
| 104 | 104 | } |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | - static function url_to_postid( $url ) { |
|
| 107 | + static function url_to_postid($url) { |
|
| 108 | 108 | // Try with url_to_postid |
| 109 | - $post_id = url_to_postid( $url ); |
|
| 110 | - if ( $post_id == 0 ) { |
|
| 109 | + $post_id = url_to_postid($url); |
|
| 110 | + if ($post_id == 0) { |
|
| 111 | 111 | // Try with get_page_by_path |
| 112 | - $post = get_page_by_path( basename( untrailingslashit( $url ) ), OBJECT, 'entity' ); |
|
| 113 | - if ( $post ) { |
|
| 112 | + $post = get_page_by_path(basename(untrailingslashit($url)), OBJECT, 'entity'); |
|
| 113 | + if ($post) { |
|
| 114 | 114 | $post_id = $post->ID; |
| 115 | 115 | } |
| 116 | 116 | } |
@@ -22,109 +22,109 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | class Wordlift_Public { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * The ID of this plugin. |
|
| 27 | - * |
|
| 28 | - * @since 1.0.0 |
|
| 29 | - * @access private |
|
| 30 | - * @var string $plugin_name The ID of this plugin. |
|
| 31 | - */ |
|
| 32 | - private $plugin_name; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The version of this plugin. |
|
| 36 | - * |
|
| 37 | - * @since 1.0.0 |
|
| 38 | - * @access private |
|
| 39 | - * @var string $version The current version of this plugin. |
|
| 40 | - */ |
|
| 41 | - private $version; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Initialize the class and set its properties. |
|
| 45 | - * |
|
| 46 | - * @param string $plugin_name The name of the plugin. |
|
| 47 | - * @param string $version The version of this plugin. |
|
| 48 | - * |
|
| 49 | - * @since 1.0.0 |
|
| 50 | - * |
|
| 51 | - */ |
|
| 52 | - public function __construct( $plugin_name, $version ) { |
|
| 53 | - |
|
| 54 | - $this->plugin_name = $plugin_name; |
|
| 55 | - $this->version = $version; |
|
| 56 | - |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Register the stylesheets for the public-facing side of the site. |
|
| 61 | - * |
|
| 62 | - * @since 3.19.3 Register the `wordlift-ui` css. |
|
| 63 | - * @since 3.19.2 The call to this function is commented out in `class-wordlift.php` because `wordlift-public.css` |
|
| 64 | - * is empty. |
|
| 65 | - * @since 1.0.0 |
|
| 66 | - */ |
|
| 67 | - public function enqueue_styles() { |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * An instance of this class should be passed to the run() function |
|
| 71 | - * defined in Wordlift_Loader as all of the hooks are defined |
|
| 72 | - * in that particular class. |
|
| 73 | - * |
|
| 74 | - * The Wordlift_Loader will then create the relationship |
|
| 75 | - * between the defined hooks and the functions defined in this |
|
| 76 | - * class. |
|
| 77 | - */ |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Add the `wordlift-font-awesome` unless some 3rd party sets the flag to false. |
|
| 81 | - * |
|
| 82 | - * @param bool $include Whether to include or not font-awesome (default true). |
|
| 83 | - * |
|
| 84 | - * @since 3.19.3 |
|
| 85 | - * |
|
| 86 | - */ |
|
| 87 | - $deps = apply_filters( 'wl_include_font_awesome', true ) |
|
| 88 | - ? array( 'wordlift-font-awesome' ) |
|
| 89 | - : array(); |
|
| 90 | - wp_register_style( 'wordlift-font-awesome', plugin_dir_url( dirname( __FILE__ ) ) . 'css/wordlift-font-awesome' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.css', array(), $this->version, 'all' ); |
|
| 91 | - wp_register_style( 'wordlift-ui', plugin_dir_url( dirname( __FILE__ ) ) . 'css/wordlift-ui' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.css', $deps, $this->version, 'all' ); |
|
| 92 | - |
|
| 93 | - // You need to re-enable the enqueue_styles in `class-wordlift.php` to make this effective. |
|
| 94 | - // |
|
| 95 | - // @see https://github.com/insideout10/wordlift-plugin/issues/821 |
|
| 96 | - // |
|
| 97 | - // wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-public.css', array(), $this->version, 'all' ); |
|
| 98 | - |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Register the stylesheets for the public-facing side of the site. |
|
| 103 | - * |
|
| 104 | - * @since 1.0.0 |
|
| 105 | - */ |
|
| 106 | - public function enqueue_scripts() { |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * This function is provided for demonstration purposes only. |
|
| 110 | - * |
|
| 111 | - * An instance of this class should be passed to the run() function |
|
| 112 | - * defined in Wordlift_Loader as all of the hooks are defined |
|
| 113 | - * in that particular class. |
|
| 114 | - * |
|
| 115 | - * The Wordlift_Loader will then create the relationship |
|
| 116 | - * between the defined hooks and the functions defined in this |
|
| 117 | - * class. |
|
| 118 | - */ |
|
| 119 | - |
|
| 120 | - $settings = self::get_settings(); |
|
| 121 | - |
|
| 122 | - // Note that we switched the js to be loaded in footer, since it is loading |
|
| 123 | - // the json-ld representation. |
|
| 124 | - wp_enqueue_script( $this->plugin_name, self::get_public_js_url(), array(), $this->version, true ); |
|
| 125 | - wp_localize_script( $this->plugin_name, 'wlSettings', $settings ); |
|
| 126 | - |
|
| 127 | - /* |
|
| 25 | + /** |
|
| 26 | + * The ID of this plugin. |
|
| 27 | + * |
|
| 28 | + * @since 1.0.0 |
|
| 29 | + * @access private |
|
| 30 | + * @var string $plugin_name The ID of this plugin. |
|
| 31 | + */ |
|
| 32 | + private $plugin_name; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The version of this plugin. |
|
| 36 | + * |
|
| 37 | + * @since 1.0.0 |
|
| 38 | + * @access private |
|
| 39 | + * @var string $version The current version of this plugin. |
|
| 40 | + */ |
|
| 41 | + private $version; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Initialize the class and set its properties. |
|
| 45 | + * |
|
| 46 | + * @param string $plugin_name The name of the plugin. |
|
| 47 | + * @param string $version The version of this plugin. |
|
| 48 | + * |
|
| 49 | + * @since 1.0.0 |
|
| 50 | + * |
|
| 51 | + */ |
|
| 52 | + public function __construct( $plugin_name, $version ) { |
|
| 53 | + |
|
| 54 | + $this->plugin_name = $plugin_name; |
|
| 55 | + $this->version = $version; |
|
| 56 | + |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Register the stylesheets for the public-facing side of the site. |
|
| 61 | + * |
|
| 62 | + * @since 3.19.3 Register the `wordlift-ui` css. |
|
| 63 | + * @since 3.19.2 The call to this function is commented out in `class-wordlift.php` because `wordlift-public.css` |
|
| 64 | + * is empty. |
|
| 65 | + * @since 1.0.0 |
|
| 66 | + */ |
|
| 67 | + public function enqueue_styles() { |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * An instance of this class should be passed to the run() function |
|
| 71 | + * defined in Wordlift_Loader as all of the hooks are defined |
|
| 72 | + * in that particular class. |
|
| 73 | + * |
|
| 74 | + * The Wordlift_Loader will then create the relationship |
|
| 75 | + * between the defined hooks and the functions defined in this |
|
| 76 | + * class. |
|
| 77 | + */ |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Add the `wordlift-font-awesome` unless some 3rd party sets the flag to false. |
|
| 81 | + * |
|
| 82 | + * @param bool $include Whether to include or not font-awesome (default true). |
|
| 83 | + * |
|
| 84 | + * @since 3.19.3 |
|
| 85 | + * |
|
| 86 | + */ |
|
| 87 | + $deps = apply_filters( 'wl_include_font_awesome', true ) |
|
| 88 | + ? array( 'wordlift-font-awesome' ) |
|
| 89 | + : array(); |
|
| 90 | + wp_register_style( 'wordlift-font-awesome', plugin_dir_url( dirname( __FILE__ ) ) . 'css/wordlift-font-awesome' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.css', array(), $this->version, 'all' ); |
|
| 91 | + wp_register_style( 'wordlift-ui', plugin_dir_url( dirname( __FILE__ ) ) . 'css/wordlift-ui' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.css', $deps, $this->version, 'all' ); |
|
| 92 | + |
|
| 93 | + // You need to re-enable the enqueue_styles in `class-wordlift.php` to make this effective. |
|
| 94 | + // |
|
| 95 | + // @see https://github.com/insideout10/wordlift-plugin/issues/821 |
|
| 96 | + // |
|
| 97 | + // wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-public.css', array(), $this->version, 'all' ); |
|
| 98 | + |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Register the stylesheets for the public-facing side of the site. |
|
| 103 | + * |
|
| 104 | + * @since 1.0.0 |
|
| 105 | + */ |
|
| 106 | + public function enqueue_scripts() { |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * This function is provided for demonstration purposes only. |
|
| 110 | + * |
|
| 111 | + * An instance of this class should be passed to the run() function |
|
| 112 | + * defined in Wordlift_Loader as all of the hooks are defined |
|
| 113 | + * in that particular class. |
|
| 114 | + * |
|
| 115 | + * The Wordlift_Loader will then create the relationship |
|
| 116 | + * between the defined hooks and the functions defined in this |
|
| 117 | + * class. |
|
| 118 | + */ |
|
| 119 | + |
|
| 120 | + $settings = self::get_settings(); |
|
| 121 | + |
|
| 122 | + // Note that we switched the js to be loaded in footer, since it is loading |
|
| 123 | + // the json-ld representation. |
|
| 124 | + wp_enqueue_script( $this->plugin_name, self::get_public_js_url(), array(), $this->version, true ); |
|
| 125 | + wp_localize_script( $this->plugin_name, 'wlSettings', $settings ); |
|
| 126 | + |
|
| 127 | + /* |
|
| 128 | 128 | * Add WordLift's version. |
| 129 | 129 | * Can be disabled via filter 'wl_disable_version_js' since 3.21.1 |
| 130 | 130 | * |
@@ -133,96 +133,96 @@ discard block |
||
| 133 | 133 | * @see https://github.com/insideout10/wordlift-plugin/issues/843. |
| 134 | 134 | * @see https://github.com/insideout10/wordlift-plugin/issues/926. |
| 135 | 135 | */ |
| 136 | - $show_version_default = false; |
|
| 137 | - $show_version = apply_filters( 'wl_disable_version_js', $show_version_default ); |
|
| 136 | + $show_version_default = false; |
|
| 137 | + $show_version = apply_filters( 'wl_disable_version_js', $show_version_default ); |
|
| 138 | 138 | |
| 139 | - if ( $show_version ) { |
|
| 140 | - wp_localize_script( $this->plugin_name, 'wordlift', array( |
|
| 141 | - 'version' => $this->version, |
|
| 142 | - ) ); |
|
| 143 | - } |
|
| 139 | + if ( $show_version ) { |
|
| 140 | + wp_localize_script( $this->plugin_name, 'wordlift', array( |
|
| 141 | + 'version' => $this->version, |
|
| 142 | + ) ); |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - /* |
|
| 145 | + /* |
|
| 146 | 146 | * Register wordlift-cloud script which is shared by |
| 147 | 147 | * Context Cards and Navigator |
| 148 | 148 | * |
| 149 | 149 | * @since 3.22.0 |
| 150 | 150 | * |
| 151 | 151 | */ |
| 152 | - wp_register_script( 'wordlift-cloud', self::get_cloud_js_url(), array( 'wp-hooks' ), Wordlift::get_instance()->get_version(), true ); |
|
| 153 | - |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Get the settings array. |
|
| 158 | - * |
|
| 159 | - * @return array An array with the settings. |
|
| 160 | - * @since 3.19.1 |
|
| 161 | - * |
|
| 162 | - */ |
|
| 163 | - public static function get_settings() { |
|
| 164 | - |
|
| 165 | - // Prepare a settings array for client-side functions. |
|
| 166 | - $settings = array( |
|
| 167 | - 'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
|
| 168 | - 'apiUrl' => get_home_url( null, 'wl-api/' ), |
|
| 169 | - 'jsonld_url' => rest_url( '/wordlift/v1/jsonld/' ) |
|
| 170 | - ); |
|
| 171 | - |
|
| 172 | - // If we're in a single page, then print out the post id. |
|
| 173 | - if ( is_singular() ) { |
|
| 174 | - $settings['postId'] = get_the_ID(); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - // Add flag that we are on home/blog page. |
|
| 178 | - if ( is_home() || is_front_page() ) { |
|
| 179 | - $settings['isHome'] = true; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - // As of 2020-02-15, we publish the JSON-LD in the head, see Jsonld_Adaper. |
|
| 183 | - $settings['jsonld_enabled'] = false; |
|
| 184 | - |
|
| 185 | - // By default only enable JSON-LD on supported entity pages (includes |
|
| 186 | - // `page`, `post` and `entity` by default) and on the home page. |
|
| 187 | - // |
|
| 188 | - // @see https://github.com/insideout10/wordlift-plugin/issues/733 |
|
| 189 | - // $jsonld_enabled = is_home() || is_front_page() || Wordlift_Entity_Type_Service::is_valid_entity_post_type( get_post_type() ); |
|
| 190 | - |
|
| 191 | - // Add the JSON-LD enabled flag, when set to false, the JSON-LD won't |
|
| 192 | - // be loaded. |
|
| 193 | - // |
|
| 194 | - // @see https://github.com/insideout10/wordlift-plugin/issues/642. |
|
| 195 | - // $settings['jsonld_enabled'] = apply_filters( 'wl_jsonld_enabled', $jsonld_enabled ); |
|
| 196 | - |
|
| 197 | - return $settings; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Get the public JavaScript URL. |
|
| 202 | - * |
|
| 203 | - * Using this function is encouraged, since the public JavaScript is also used by the {@link Wordlift_WpRocket_Adapter} |
|
| 204 | - * in order to avoid breaking optimizations. |
|
| 205 | - * |
|
| 206 | - * @return string The URL to the public JavaScript. |
|
| 207 | - * @see https://github.com/insideout10/wordlift-plugin/issues/842. |
|
| 208 | - * |
|
| 209 | - * @since 3.19.4 |
|
| 210 | - * |
|
| 211 | - */ |
|
| 212 | - public static function get_public_js_url() { |
|
| 213 | - |
|
| 214 | - return plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/bundle.js'; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Get the Cloud JavaScript URL. |
|
| 219 | - * |
|
| 220 | - * @see https://github.com/insideout10/wordlift-plugin/issues/971 |
|
| 221 | - * @since 3.23.0 |
|
| 222 | - * @return string The URL to the Cloud JavaScript. |
|
| 223 | - */ |
|
| 224 | - public static function get_cloud_js_url() { |
|
| 225 | - |
|
| 226 | - return plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/wordlift-cloud.js'; |
|
| 227 | - } |
|
| 152 | + wp_register_script( 'wordlift-cloud', self::get_cloud_js_url(), array( 'wp-hooks' ), Wordlift::get_instance()->get_version(), true ); |
|
| 153 | + |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Get the settings array. |
|
| 158 | + * |
|
| 159 | + * @return array An array with the settings. |
|
| 160 | + * @since 3.19.1 |
|
| 161 | + * |
|
| 162 | + */ |
|
| 163 | + public static function get_settings() { |
|
| 164 | + |
|
| 165 | + // Prepare a settings array for client-side functions. |
|
| 166 | + $settings = array( |
|
| 167 | + 'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
|
| 168 | + 'apiUrl' => get_home_url( null, 'wl-api/' ), |
|
| 169 | + 'jsonld_url' => rest_url( '/wordlift/v1/jsonld/' ) |
|
| 170 | + ); |
|
| 171 | + |
|
| 172 | + // If we're in a single page, then print out the post id. |
|
| 173 | + if ( is_singular() ) { |
|
| 174 | + $settings['postId'] = get_the_ID(); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + // Add flag that we are on home/blog page. |
|
| 178 | + if ( is_home() || is_front_page() ) { |
|
| 179 | + $settings['isHome'] = true; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + // As of 2020-02-15, we publish the JSON-LD in the head, see Jsonld_Adaper. |
|
| 183 | + $settings['jsonld_enabled'] = false; |
|
| 184 | + |
|
| 185 | + // By default only enable JSON-LD on supported entity pages (includes |
|
| 186 | + // `page`, `post` and `entity` by default) and on the home page. |
|
| 187 | + // |
|
| 188 | + // @see https://github.com/insideout10/wordlift-plugin/issues/733 |
|
| 189 | + // $jsonld_enabled = is_home() || is_front_page() || Wordlift_Entity_Type_Service::is_valid_entity_post_type( get_post_type() ); |
|
| 190 | + |
|
| 191 | + // Add the JSON-LD enabled flag, when set to false, the JSON-LD won't |
|
| 192 | + // be loaded. |
|
| 193 | + // |
|
| 194 | + // @see https://github.com/insideout10/wordlift-plugin/issues/642. |
|
| 195 | + // $settings['jsonld_enabled'] = apply_filters( 'wl_jsonld_enabled', $jsonld_enabled ); |
|
| 196 | + |
|
| 197 | + return $settings; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Get the public JavaScript URL. |
|
| 202 | + * |
|
| 203 | + * Using this function is encouraged, since the public JavaScript is also used by the {@link Wordlift_WpRocket_Adapter} |
|
| 204 | + * in order to avoid breaking optimizations. |
|
| 205 | + * |
|
| 206 | + * @return string The URL to the public JavaScript. |
|
| 207 | + * @see https://github.com/insideout10/wordlift-plugin/issues/842. |
|
| 208 | + * |
|
| 209 | + * @since 3.19.4 |
|
| 210 | + * |
|
| 211 | + */ |
|
| 212 | + public static function get_public_js_url() { |
|
| 213 | + |
|
| 214 | + return plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/bundle.js'; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Get the Cloud JavaScript URL. |
|
| 219 | + * |
|
| 220 | + * @see https://github.com/insideout10/wordlift-plugin/issues/971 |
|
| 221 | + * @since 3.23.0 |
|
| 222 | + * @return string The URL to the Cloud JavaScript. |
|
| 223 | + */ |
|
| 224 | + public static function get_cloud_js_url() { |
|
| 225 | + |
|
| 226 | + return plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/wordlift-cloud.js'; |
|
| 227 | + } |
|
| 228 | 228 | } |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | * @since 1.0.0 |
| 50 | 50 | * |
| 51 | 51 | */ |
| 52 | - public function __construct( $plugin_name, $version ) { |
|
| 52 | + public function __construct($plugin_name, $version) { |
|
| 53 | 53 | |
| 54 | 54 | $this->plugin_name = $plugin_name; |
| 55 | 55 | $this->version = $version; |
@@ -84,11 +84,11 @@ discard block |
||
| 84 | 84 | * @since 3.19.3 |
| 85 | 85 | * |
| 86 | 86 | */ |
| 87 | - $deps = apply_filters( 'wl_include_font_awesome', true ) |
|
| 88 | - ? array( 'wordlift-font-awesome' ) |
|
| 87 | + $deps = apply_filters('wl_include_font_awesome', true) |
|
| 88 | + ? array('wordlift-font-awesome') |
|
| 89 | 89 | : array(); |
| 90 | - wp_register_style( 'wordlift-font-awesome', plugin_dir_url( dirname( __FILE__ ) ) . 'css/wordlift-font-awesome' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.css', array(), $this->version, 'all' ); |
|
| 91 | - wp_register_style( 'wordlift-ui', plugin_dir_url( dirname( __FILE__ ) ) . 'css/wordlift-ui' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.css', $deps, $this->version, 'all' ); |
|
| 90 | + wp_register_style('wordlift-font-awesome', plugin_dir_url(dirname(__FILE__)).'css/wordlift-font-awesome'.( ! defined('SCRIPT_DEBUG') || ! SCRIPT_DEBUG ? '.min' : '').'.css', array(), $this->version, 'all'); |
|
| 91 | + wp_register_style('wordlift-ui', plugin_dir_url(dirname(__FILE__)).'css/wordlift-ui'.( ! defined('SCRIPT_DEBUG') || ! SCRIPT_DEBUG ? '.min' : '').'.css', $deps, $this->version, 'all'); |
|
| 92 | 92 | |
| 93 | 93 | // You need to re-enable the enqueue_styles in `class-wordlift.php` to make this effective. |
| 94 | 94 | // |
@@ -121,8 +121,8 @@ discard block |
||
| 121 | 121 | |
| 122 | 122 | // Note that we switched the js to be loaded in footer, since it is loading |
| 123 | 123 | // the json-ld representation. |
| 124 | - wp_enqueue_script( $this->plugin_name, self::get_public_js_url(), array(), $this->version, true ); |
|
| 125 | - wp_localize_script( $this->plugin_name, 'wlSettings', $settings ); |
|
| 124 | + wp_enqueue_script($this->plugin_name, self::get_public_js_url(), array(), $this->version, true); |
|
| 125 | + wp_localize_script($this->plugin_name, 'wlSettings', $settings); |
|
| 126 | 126 | |
| 127 | 127 | /* |
| 128 | 128 | * Add WordLift's version. |
@@ -134,12 +134,12 @@ discard block |
||
| 134 | 134 | * @see https://github.com/insideout10/wordlift-plugin/issues/926. |
| 135 | 135 | */ |
| 136 | 136 | $show_version_default = false; |
| 137 | - $show_version = apply_filters( 'wl_disable_version_js', $show_version_default ); |
|
| 137 | + $show_version = apply_filters('wl_disable_version_js', $show_version_default); |
|
| 138 | 138 | |
| 139 | - if ( $show_version ) { |
|
| 140 | - wp_localize_script( $this->plugin_name, 'wordlift', array( |
|
| 139 | + if ($show_version) { |
|
| 140 | + wp_localize_script($this->plugin_name, 'wordlift', array( |
|
| 141 | 141 | 'version' => $this->version, |
| 142 | - ) ); |
|
| 142 | + )); |
|
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | /* |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | * @since 3.22.0 |
| 150 | 150 | * |
| 151 | 151 | */ |
| 152 | - wp_register_script( 'wordlift-cloud', self::get_cloud_js_url(), array( 'wp-hooks' ), Wordlift::get_instance()->get_version(), true ); |
|
| 152 | + wp_register_script('wordlift-cloud', self::get_cloud_js_url(), array('wp-hooks'), Wordlift::get_instance()->get_version(), true); |
|
| 153 | 153 | |
| 154 | 154 | } |
| 155 | 155 | |
@@ -164,18 +164,18 @@ discard block |
||
| 164 | 164 | |
| 165 | 165 | // Prepare a settings array for client-side functions. |
| 166 | 166 | $settings = array( |
| 167 | - 'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
|
| 168 | - 'apiUrl' => get_home_url( null, 'wl-api/' ), |
|
| 169 | - 'jsonld_url' => rest_url( '/wordlift/v1/jsonld/' ) |
|
| 167 | + 'ajaxUrl' => admin_url('admin-ajax.php'), |
|
| 168 | + 'apiUrl' => get_home_url(null, 'wl-api/'), |
|
| 169 | + 'jsonld_url' => rest_url('/wordlift/v1/jsonld/') |
|
| 170 | 170 | ); |
| 171 | 171 | |
| 172 | 172 | // If we're in a single page, then print out the post id. |
| 173 | - if ( is_singular() ) { |
|
| 173 | + if (is_singular()) { |
|
| 174 | 174 | $settings['postId'] = get_the_ID(); |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | // Add flag that we are on home/blog page. |
| 178 | - if ( is_home() || is_front_page() ) { |
|
| 178 | + if (is_home() || is_front_page()) { |
|
| 179 | 179 | $settings['isHome'] = true; |
| 180 | 180 | } |
| 181 | 181 | |
@@ -211,7 +211,7 @@ discard block |
||
| 211 | 211 | */ |
| 212 | 212 | public static function get_public_js_url() { |
| 213 | 213 | |
| 214 | - return plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/bundle.js'; |
|
| 214 | + return plugin_dir_url(dirname(__FILE__)).'js/dist/bundle.js'; |
|
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | /** |
@@ -223,6 +223,6 @@ discard block |
||
| 223 | 223 | */ |
| 224 | 224 | public static function get_cloud_js_url() { |
| 225 | 225 | |
| 226 | - return plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/wordlift-cloud.js'; |
|
| 226 | + return plugin_dir_url(dirname(__FILE__)).'js/dist/wordlift-cloud.js'; |
|
| 227 | 227 | } |
| 228 | 228 | } |
@@ -20,128 +20,128 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | class Wordlift_Admin_Post_Edit_Page { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Constants to be used instead of text inside FAQ |
|
| 25 | - * helper methods. |
|
| 26 | - */ |
|
| 27 | - const GUTENBERG = 'gutenberg'; |
|
| 28 | - const TINY_MCE = 'tiny_mce'; |
|
| 29 | - const FAQ_LIST_BOX_ID = 'wl-faq-meta-list-box'; |
|
| 30 | - |
|
| 31 | - /** Constant to be used for translation domain */ |
|
| 32 | - const WORDLIFT_TEXT_DOMAIN = 'wordlift'; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The {@link Wordlift} plugin instance. |
|
| 36 | - * |
|
| 37 | - * @since 3.11.0 |
|
| 38 | - * |
|
| 39 | - * @var \Wordlift $plugin The {@link Wordlift} plugin instance. |
|
| 40 | - */ |
|
| 41 | - private $plugin; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * A {@link Wordlift_Log_Service} instance. |
|
| 45 | - * |
|
| 46 | - * @since 3.15.4 |
|
| 47 | - * |
|
| 48 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 49 | - */ |
|
| 50 | - private $log; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Create the {@link Wordlift_Admin_Post_Edit_Page} instance. |
|
| 54 | - * |
|
| 55 | - * @param \Wordlift $plugin The {@link Wordlift} plugin instance. |
|
| 56 | - * |
|
| 57 | - * @since 3.11.0 |
|
| 58 | - * |
|
| 59 | - */ |
|
| 60 | - function __construct( $plugin ) { |
|
| 61 | - |
|
| 62 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 63 | - |
|
| 64 | - add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_scripts_gutenberg', ) ); |
|
| 65 | - |
|
| 66 | - // Bail out if we're in the UX Builder editor. |
|
| 67 | - if ( $this->is_ux_builder_editor() ) { |
|
| 68 | - $this->log->info( 'WordLift will not show, since we are in UX Builder editor.' ); |
|
| 69 | - |
|
| 70 | - return; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - // Define the callbacks. |
|
| 74 | - $callback = array( $this, 'enqueue_scripts', ); |
|
| 75 | - |
|
| 76 | - // Set a hook to enqueue scripts only when the edit page is displayed. |
|
| 77 | - add_action( 'admin_print_scripts-post.php', $callback ); |
|
| 78 | - add_action( 'admin_print_scripts-post-new.php', $callback ); |
|
| 79 | - |
|
| 80 | - $this->plugin = $plugin; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Check whether the current post opens with G'berg or not. |
|
| 85 | - * |
|
| 86 | - * @return bool True if G'berg is used otherwise false. |
|
| 87 | - * @since 3.22.3 |
|
| 88 | - */ |
|
| 89 | - function is_gutenberg_page() { |
|
| 90 | - if ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) { |
|
| 91 | - // The Gutenberg plugin is on. |
|
| 92 | - return TRUE; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - $current_screen = get_current_screen(); |
|
| 96 | - if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) { |
|
| 97 | - // Gutenberg page on 5+. |
|
| 98 | - return TRUE; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - return FALSE; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Check if we're in UX builder. |
|
| 106 | - * |
|
| 107 | - * @see https://github.com/insideout10/wordlift-plugin/issues/691 |
|
| 108 | - * |
|
| 109 | - * @since 3.15.4 |
|
| 110 | - * |
|
| 111 | - * @return bool True if we're in UX builder, otherwise false. |
|
| 112 | - */ |
|
| 113 | - private function is_ux_builder_editor() { |
|
| 114 | - |
|
| 115 | - return function_exists( 'ux_builder_is_editor' ) |
|
| 116 | - && ux_builder_is_editor(); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Enqueue scripts and styles for the edit page. |
|
| 121 | - * |
|
| 122 | - * @since 3.11.0 |
|
| 123 | - */ |
|
| 124 | - public function enqueue_scripts() { |
|
| 125 | - |
|
| 126 | - // Bail out if this is G'berg. |
|
| 127 | - if ( $this->is_gutenberg_page() ) { |
|
| 128 | - return; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - // Dequeue potentially conflicting ontrapages angular scripts which any *are not* used on the edit screen. |
|
| 132 | - // |
|
| 133 | - // @see https://github.com/insideout10/wordlift-plugin/issues/832 |
|
| 134 | - wp_dequeue_script( 'ontrapagesAngular' ); |
|
| 135 | - wp_dequeue_script( 'ontrapagesApp' ); |
|
| 136 | - wp_dequeue_script( 'ontrapagesController' ); |
|
| 137 | - |
|
| 138 | - // If Gutenberg is enabled for the post, do not load the legacy edit.js. |
|
| 139 | - if ( function_exists( 'use_block_editor_for_post' ) && use_block_editor_for_post( get_post() ) ) { |
|
| 140 | - return; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - |
|
| 144 | - /* |
|
| 23 | + /** |
|
| 24 | + * Constants to be used instead of text inside FAQ |
|
| 25 | + * helper methods. |
|
| 26 | + */ |
|
| 27 | + const GUTENBERG = 'gutenberg'; |
|
| 28 | + const TINY_MCE = 'tiny_mce'; |
|
| 29 | + const FAQ_LIST_BOX_ID = 'wl-faq-meta-list-box'; |
|
| 30 | + |
|
| 31 | + /** Constant to be used for translation domain */ |
|
| 32 | + const WORDLIFT_TEXT_DOMAIN = 'wordlift'; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The {@link Wordlift} plugin instance. |
|
| 36 | + * |
|
| 37 | + * @since 3.11.0 |
|
| 38 | + * |
|
| 39 | + * @var \Wordlift $plugin The {@link Wordlift} plugin instance. |
|
| 40 | + */ |
|
| 41 | + private $plugin; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * A {@link Wordlift_Log_Service} instance. |
|
| 45 | + * |
|
| 46 | + * @since 3.15.4 |
|
| 47 | + * |
|
| 48 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 49 | + */ |
|
| 50 | + private $log; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Create the {@link Wordlift_Admin_Post_Edit_Page} instance. |
|
| 54 | + * |
|
| 55 | + * @param \Wordlift $plugin The {@link Wordlift} plugin instance. |
|
| 56 | + * |
|
| 57 | + * @since 3.11.0 |
|
| 58 | + * |
|
| 59 | + */ |
|
| 60 | + function __construct( $plugin ) { |
|
| 61 | + |
|
| 62 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 63 | + |
|
| 64 | + add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_scripts_gutenberg', ) ); |
|
| 65 | + |
|
| 66 | + // Bail out if we're in the UX Builder editor. |
|
| 67 | + if ( $this->is_ux_builder_editor() ) { |
|
| 68 | + $this->log->info( 'WordLift will not show, since we are in UX Builder editor.' ); |
|
| 69 | + |
|
| 70 | + return; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + // Define the callbacks. |
|
| 74 | + $callback = array( $this, 'enqueue_scripts', ); |
|
| 75 | + |
|
| 76 | + // Set a hook to enqueue scripts only when the edit page is displayed. |
|
| 77 | + add_action( 'admin_print_scripts-post.php', $callback ); |
|
| 78 | + add_action( 'admin_print_scripts-post-new.php', $callback ); |
|
| 79 | + |
|
| 80 | + $this->plugin = $plugin; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Check whether the current post opens with G'berg or not. |
|
| 85 | + * |
|
| 86 | + * @return bool True if G'berg is used otherwise false. |
|
| 87 | + * @since 3.22.3 |
|
| 88 | + */ |
|
| 89 | + function is_gutenberg_page() { |
|
| 90 | + if ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) { |
|
| 91 | + // The Gutenberg plugin is on. |
|
| 92 | + return TRUE; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + $current_screen = get_current_screen(); |
|
| 96 | + if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) { |
|
| 97 | + // Gutenberg page on 5+. |
|
| 98 | + return TRUE; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + return FALSE; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Check if we're in UX builder. |
|
| 106 | + * |
|
| 107 | + * @see https://github.com/insideout10/wordlift-plugin/issues/691 |
|
| 108 | + * |
|
| 109 | + * @since 3.15.4 |
|
| 110 | + * |
|
| 111 | + * @return bool True if we're in UX builder, otherwise false. |
|
| 112 | + */ |
|
| 113 | + private function is_ux_builder_editor() { |
|
| 114 | + |
|
| 115 | + return function_exists( 'ux_builder_is_editor' ) |
|
| 116 | + && ux_builder_is_editor(); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Enqueue scripts and styles for the edit page. |
|
| 121 | + * |
|
| 122 | + * @since 3.11.0 |
|
| 123 | + */ |
|
| 124 | + public function enqueue_scripts() { |
|
| 125 | + |
|
| 126 | + // Bail out if this is G'berg. |
|
| 127 | + if ( $this->is_gutenberg_page() ) { |
|
| 128 | + return; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + // Dequeue potentially conflicting ontrapages angular scripts which any *are not* used on the edit screen. |
|
| 132 | + // |
|
| 133 | + // @see https://github.com/insideout10/wordlift-plugin/issues/832 |
|
| 134 | + wp_dequeue_script( 'ontrapagesAngular' ); |
|
| 135 | + wp_dequeue_script( 'ontrapagesApp' ); |
|
| 136 | + wp_dequeue_script( 'ontrapagesController' ); |
|
| 137 | + |
|
| 138 | + // If Gutenberg is enabled for the post, do not load the legacy edit.js. |
|
| 139 | + if ( function_exists( 'use_block_editor_for_post' ) && use_block_editor_for_post( get_post() ) ) { |
|
| 140 | + return; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + |
|
| 144 | + /* |
|
| 145 | 145 | * Enqueue the edit screen JavaScript. The `wordlift-admin.bundle.js` file |
| 146 | 146 | * is scheduled to replace the older `wordlift-admin.min.js` once client-side |
| 147 | 147 | * code is properly refactored. |
@@ -150,169 +150,169 @@ discard block |
||
| 150 | 150 | * |
| 151 | 151 | * @since 3.20.0 edit.js has been migrated to the new webpack configuration. |
| 152 | 152 | */ |
| 153 | - $script_name = plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/edit'; |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Scripts_Helper introduced. |
|
| 157 | - * |
|
| 158 | - * @since 3.25.0 Scripts are loaded using script helper to ensure WP 4.4 compatibiility. |
|
| 159 | - * @since 3.25.1 The handle is used to hook the wp_localize_script for the _wlEntityTypes global object. |
|
| 160 | - */ |
|
| 161 | - Scripts_Helper::enqueue_based_on_wordpress_version( |
|
| 162 | - 'wl-classic-editor', |
|
| 163 | - $script_name, |
|
| 164 | - array( |
|
| 165 | - $this->plugin->get_plugin_name(), |
|
| 166 | - 'jquery', |
|
| 167 | - // Require wp.ajax. |
|
| 168 | - 'wp-util', |
|
| 169 | - // @@todo: provide the following dependencies when we're in WP < 5.0 (i.e. when these dependencies aren't already defined). |
|
| 170 | - 'react', |
|
| 171 | - 'react-dom', |
|
| 172 | - 'wp-element', |
|
| 173 | - 'wp-polyfill', |
|
| 174 | - /* |
|
| 153 | + $script_name = plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/edit'; |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Scripts_Helper introduced. |
|
| 157 | + * |
|
| 158 | + * @since 3.25.0 Scripts are loaded using script helper to ensure WP 4.4 compatibiility. |
|
| 159 | + * @since 3.25.1 The handle is used to hook the wp_localize_script for the _wlEntityTypes global object. |
|
| 160 | + */ |
|
| 161 | + Scripts_Helper::enqueue_based_on_wordpress_version( |
|
| 162 | + 'wl-classic-editor', |
|
| 163 | + $script_name, |
|
| 164 | + array( |
|
| 165 | + $this->plugin->get_plugin_name(), |
|
| 166 | + 'jquery', |
|
| 167 | + // Require wp.ajax. |
|
| 168 | + 'wp-util', |
|
| 169 | + // @@todo: provide the following dependencies when we're in WP < 5.0 (i.e. when these dependencies aren't already defined). |
|
| 170 | + 'react', |
|
| 171 | + 'react-dom', |
|
| 172 | + 'wp-element', |
|
| 173 | + 'wp-polyfill', |
|
| 174 | + /* |
|
| 175 | 175 | * Angular isn't loaded anymore remotely, but it is loaded within wordlift-reloaded.js. |
| 176 | 176 | * |
| 177 | 177 | * See https://github.com/insideout10/wordlift-plugin/issues/865. |
| 178 | 178 | * |
| 179 | 179 | * @since 3.19.6 |
| 180 | 180 | */ |
| 181 | - // // Require Angular. |
|
| 182 | - // 'wl-angular', |
|
| 183 | - // 'wl-angular-geolocation', |
|
| 184 | - // 'wl-angular-touch', |
|
| 185 | - // 'wl-angular-animate', |
|
| 186 | - /** |
|
| 187 | - * We need the `wp.hooks` global to allow the edit.js script to send actions. |
|
| 188 | - * |
|
| 189 | - * @since 3.23.0 |
|
| 190 | - */ |
|
| 191 | - 'wp-hooks', |
|
| 192 | - ) |
|
| 193 | - ); |
|
| 194 | - |
|
| 195 | - wp_enqueue_style( 'wl-classic-editor', "$script_name.css", array(), $this->plugin->get_version() ); |
|
| 196 | - $this->load_faq_scripts_and_styles(); |
|
| 197 | - $this->load_faq_settings( self::TINY_MCE ); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Enqueue the scripts and styles needed for FAQ |
|
| 202 | - */ |
|
| 203 | - private function load_faq_scripts_and_styles() { |
|
| 204 | - wp_enqueue_style( |
|
| 205 | - 'wl-faq-metabox-style', |
|
| 206 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/faq.css', |
|
| 207 | - array() |
|
| 208 | - ); |
|
| 209 | - Scripts_Helper::enqueue_based_on_wordpress_version( |
|
| 210 | - 'wl-faq-metabox-script', |
|
| 211 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/faq', |
|
| 212 | - array( 'wp-polyfill' ), |
|
| 213 | - true |
|
| 214 | - ); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Get FAQ settings array |
|
| 219 | - * |
|
| 220 | - * @return array |
|
| 221 | - */ |
|
| 222 | - public function get_faq_settings() { |
|
| 223 | - return array( |
|
| 224 | - 'restUrl' => get_rest_url( NULL, WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faq' ), |
|
| 225 | - 'listBoxId' => self::FAQ_LIST_BOX_ID, |
|
| 226 | - 'nonce' => wp_create_nonce( 'wp_rest' ), |
|
| 227 | - 'postId' => get_the_ID(), |
|
| 228 | - // Translation for warning, error message. |
|
| 229 | - 'invalidTagMessage' => sprintf( __( 'Invalid tags %s is present in answer', self::WORDLIFT_TEXT_DOMAIN ), "{INVALID_TAGS}" ), |
|
| 230 | - 'invalidWordCountMessage' => sprintf( __( 'Answer word count must not exceed %s words', self::WORDLIFT_TEXT_DOMAIN ), "{ANSWER_WORD_COUNT_WARNING_LIMIT}" ), |
|
| 231 | - 'questionText' => __( 'Question', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 232 | - 'answerText' => __( 'Answer', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 233 | - 'addQuestionOrAnswerText' => __( 'Add Question / Answer', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 234 | - 'addQuestionText' => __( 'Add Question', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 235 | - 'addAnswerText' => __( 'Add Answer', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 236 | - 'noFaqItemsText' => __( 'Highlight a question in content, then click Add Question.', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 237 | - 'updatingText' => __( 'Updating...', self::WORDLIFT_TEXT_DOMAIN ) |
|
| 238 | - ); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Load FAQ settings to the add/edit post page |
|
| 243 | - * |
|
| 244 | - * @param $editor string specifying which text editor needed to be used. |
|
| 245 | - */ |
|
| 246 | - private function load_faq_settings( $editor ) { |
|
| 247 | - // This script also provides translations to gutenberg. |
|
| 248 | - wp_localize_script( 'wl-faq-metabox-script', '_wlFaqSettings', $this->get_faq_settings() ); |
|
| 249 | - |
|
| 250 | - // Enqueue the FAQ style |
|
| 251 | - if ( $editor === self::GUTENBERG ) { |
|
| 252 | - Scripts_Helper::enqueue_based_on_wordpress_version( |
|
| 253 | - 'wl-faq-gutenberg-plugin', |
|
| 254 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/block-editor-faq-plugin', |
|
| 255 | - array( 'wp-polyfill' ), |
|
| 256 | - TRUE |
|
| 257 | - ); |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Enqueue scripts and styles for the gutenberg edit page. |
|
| 263 | - * |
|
| 264 | - * @since 3.21.0 |
|
| 265 | - */ |
|
| 266 | - public function enqueue_scripts_gutenberg() { |
|
| 267 | - // Load FAQ settings. |
|
| 268 | - $this->load_faq_scripts_and_styles(); |
|
| 269 | - $this->load_faq_settings( self::GUTENBERG ); |
|
| 270 | - |
|
| 271 | - wp_register_script( |
|
| 272 | - 'wl-block-editor', |
|
| 273 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/block-editor.js', |
|
| 274 | - array( |
|
| 275 | - 'react', |
|
| 276 | - 'wordlift', |
|
| 277 | - 'wp-hooks', |
|
| 278 | - 'wp-data', |
|
| 279 | - 'wp-rich-text', |
|
| 280 | - 'wp-blocks', |
|
| 281 | - 'wp-plugins', |
|
| 282 | - 'wp-edit-post', |
|
| 283 | - ), |
|
| 284 | - $this->plugin->get_version() |
|
| 285 | - ); |
|
| 286 | - wp_localize_script( 'wl-block-editor', '_wlBlockEditorSettings', array( |
|
| 287 | - 'root' => esc_url_raw( rest_url() ), |
|
| 288 | - 'nonce' => wp_create_nonce( 'wp_rest' ) |
|
| 289 | - ) ); |
|
| 290 | - |
|
| 291 | - /* |
|
| 181 | + // // Require Angular. |
|
| 182 | + // 'wl-angular', |
|
| 183 | + // 'wl-angular-geolocation', |
|
| 184 | + // 'wl-angular-touch', |
|
| 185 | + // 'wl-angular-animate', |
|
| 186 | + /** |
|
| 187 | + * We need the `wp.hooks` global to allow the edit.js script to send actions. |
|
| 188 | + * |
|
| 189 | + * @since 3.23.0 |
|
| 190 | + */ |
|
| 191 | + 'wp-hooks', |
|
| 192 | + ) |
|
| 193 | + ); |
|
| 194 | + |
|
| 195 | + wp_enqueue_style( 'wl-classic-editor', "$script_name.css", array(), $this->plugin->get_version() ); |
|
| 196 | + $this->load_faq_scripts_and_styles(); |
|
| 197 | + $this->load_faq_settings( self::TINY_MCE ); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Enqueue the scripts and styles needed for FAQ |
|
| 202 | + */ |
|
| 203 | + private function load_faq_scripts_and_styles() { |
|
| 204 | + wp_enqueue_style( |
|
| 205 | + 'wl-faq-metabox-style', |
|
| 206 | + plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/faq.css', |
|
| 207 | + array() |
|
| 208 | + ); |
|
| 209 | + Scripts_Helper::enqueue_based_on_wordpress_version( |
|
| 210 | + 'wl-faq-metabox-script', |
|
| 211 | + plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/faq', |
|
| 212 | + array( 'wp-polyfill' ), |
|
| 213 | + true |
|
| 214 | + ); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Get FAQ settings array |
|
| 219 | + * |
|
| 220 | + * @return array |
|
| 221 | + */ |
|
| 222 | + public function get_faq_settings() { |
|
| 223 | + return array( |
|
| 224 | + 'restUrl' => get_rest_url( NULL, WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faq' ), |
|
| 225 | + 'listBoxId' => self::FAQ_LIST_BOX_ID, |
|
| 226 | + 'nonce' => wp_create_nonce( 'wp_rest' ), |
|
| 227 | + 'postId' => get_the_ID(), |
|
| 228 | + // Translation for warning, error message. |
|
| 229 | + 'invalidTagMessage' => sprintf( __( 'Invalid tags %s is present in answer', self::WORDLIFT_TEXT_DOMAIN ), "{INVALID_TAGS}" ), |
|
| 230 | + 'invalidWordCountMessage' => sprintf( __( 'Answer word count must not exceed %s words', self::WORDLIFT_TEXT_DOMAIN ), "{ANSWER_WORD_COUNT_WARNING_LIMIT}" ), |
|
| 231 | + 'questionText' => __( 'Question', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 232 | + 'answerText' => __( 'Answer', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 233 | + 'addQuestionOrAnswerText' => __( 'Add Question / Answer', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 234 | + 'addQuestionText' => __( 'Add Question', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 235 | + 'addAnswerText' => __( 'Add Answer', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 236 | + 'noFaqItemsText' => __( 'Highlight a question in content, then click Add Question.', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 237 | + 'updatingText' => __( 'Updating...', self::WORDLIFT_TEXT_DOMAIN ) |
|
| 238 | + ); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Load FAQ settings to the add/edit post page |
|
| 243 | + * |
|
| 244 | + * @param $editor string specifying which text editor needed to be used. |
|
| 245 | + */ |
|
| 246 | + private function load_faq_settings( $editor ) { |
|
| 247 | + // This script also provides translations to gutenberg. |
|
| 248 | + wp_localize_script( 'wl-faq-metabox-script', '_wlFaqSettings', $this->get_faq_settings() ); |
|
| 249 | + |
|
| 250 | + // Enqueue the FAQ style |
|
| 251 | + if ( $editor === self::GUTENBERG ) { |
|
| 252 | + Scripts_Helper::enqueue_based_on_wordpress_version( |
|
| 253 | + 'wl-faq-gutenberg-plugin', |
|
| 254 | + plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/block-editor-faq-plugin', |
|
| 255 | + array( 'wp-polyfill' ), |
|
| 256 | + TRUE |
|
| 257 | + ); |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Enqueue scripts and styles for the gutenberg edit page. |
|
| 263 | + * |
|
| 264 | + * @since 3.21.0 |
|
| 265 | + */ |
|
| 266 | + public function enqueue_scripts_gutenberg() { |
|
| 267 | + // Load FAQ settings. |
|
| 268 | + $this->load_faq_scripts_and_styles(); |
|
| 269 | + $this->load_faq_settings( self::GUTENBERG ); |
|
| 270 | + |
|
| 271 | + wp_register_script( |
|
| 272 | + 'wl-block-editor', |
|
| 273 | + plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/block-editor.js', |
|
| 274 | + array( |
|
| 275 | + 'react', |
|
| 276 | + 'wordlift', |
|
| 277 | + 'wp-hooks', |
|
| 278 | + 'wp-data', |
|
| 279 | + 'wp-rich-text', |
|
| 280 | + 'wp-blocks', |
|
| 281 | + 'wp-plugins', |
|
| 282 | + 'wp-edit-post', |
|
| 283 | + ), |
|
| 284 | + $this->plugin->get_version() |
|
| 285 | + ); |
|
| 286 | + wp_localize_script( 'wl-block-editor', '_wlBlockEditorSettings', array( |
|
| 287 | + 'root' => esc_url_raw( rest_url() ), |
|
| 288 | + 'nonce' => wp_create_nonce( 'wp_rest' ) |
|
| 289 | + ) ); |
|
| 290 | + |
|
| 291 | + /* |
|
| 292 | 292 | * @since 3.25.1 The hook is used by the wp_localize_script to register the _wlEntityTypes global object. |
| 293 | 293 | */ |
| 294 | - wp_enqueue_style( |
|
| 295 | - 'wl-block-editor', |
|
| 296 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/block-editor.css', |
|
| 297 | - array(), |
|
| 298 | - $this->plugin->get_version() |
|
| 299 | - ); |
|
| 300 | - |
|
| 301 | - wp_enqueue_script( |
|
| 302 | - 'wl-autocomplete-select', |
|
| 303 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/autocomplete-select.js', |
|
| 304 | - array(), |
|
| 305 | - $this->plugin->get_version(), |
|
| 306 | - TRUE |
|
| 307 | - ); |
|
| 308 | - |
|
| 309 | - wp_enqueue_style( |
|
| 310 | - 'wl-autocomplete-select', |
|
| 311 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/autocomplete-select.css', |
|
| 312 | - array(), |
|
| 313 | - $this->plugin->get_version() |
|
| 314 | - ); |
|
| 315 | - |
|
| 316 | - } |
|
| 294 | + wp_enqueue_style( |
|
| 295 | + 'wl-block-editor', |
|
| 296 | + plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/block-editor.css', |
|
| 297 | + array(), |
|
| 298 | + $this->plugin->get_version() |
|
| 299 | + ); |
|
| 300 | + |
|
| 301 | + wp_enqueue_script( |
|
| 302 | + 'wl-autocomplete-select', |
|
| 303 | + plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/autocomplete-select.js', |
|
| 304 | + array(), |
|
| 305 | + $this->plugin->get_version(), |
|
| 306 | + TRUE |
|
| 307 | + ); |
|
| 308 | + |
|
| 309 | + wp_enqueue_style( |
|
| 310 | + 'wl-autocomplete-select', |
|
| 311 | + plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/autocomplete-select.css', |
|
| 312 | + array(), |
|
| 313 | + $this->plugin->get_version() |
|
| 314 | + ); |
|
| 315 | + |
|
| 316 | + } |
|
| 317 | 317 | |
| 318 | 318 | } |
@@ -57,25 +57,25 @@ discard block |
||
| 57 | 57 | * @since 3.11.0 |
| 58 | 58 | * |
| 59 | 59 | */ |
| 60 | - function __construct( $plugin ) { |
|
| 60 | + function __construct($plugin) { |
|
| 61 | 61 | |
| 62 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 62 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 63 | 63 | |
| 64 | - add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_scripts_gutenberg', ) ); |
|
| 64 | + add_action('enqueue_block_editor_assets', array($this, 'enqueue_scripts_gutenberg',)); |
|
| 65 | 65 | |
| 66 | 66 | // Bail out if we're in the UX Builder editor. |
| 67 | - if ( $this->is_ux_builder_editor() ) { |
|
| 68 | - $this->log->info( 'WordLift will not show, since we are in UX Builder editor.' ); |
|
| 67 | + if ($this->is_ux_builder_editor()) { |
|
| 68 | + $this->log->info('WordLift will not show, since we are in UX Builder editor.'); |
|
| 69 | 69 | |
| 70 | 70 | return; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | // Define the callbacks. |
| 74 | - $callback = array( $this, 'enqueue_scripts', ); |
|
| 74 | + $callback = array($this, 'enqueue_scripts',); |
|
| 75 | 75 | |
| 76 | 76 | // Set a hook to enqueue scripts only when the edit page is displayed. |
| 77 | - add_action( 'admin_print_scripts-post.php', $callback ); |
|
| 78 | - add_action( 'admin_print_scripts-post-new.php', $callback ); |
|
| 77 | + add_action('admin_print_scripts-post.php', $callback); |
|
| 78 | + add_action('admin_print_scripts-post-new.php', $callback); |
|
| 79 | 79 | |
| 80 | 80 | $this->plugin = $plugin; |
| 81 | 81 | } |
@@ -87,13 +87,13 @@ discard block |
||
| 87 | 87 | * @since 3.22.3 |
| 88 | 88 | */ |
| 89 | 89 | function is_gutenberg_page() { |
| 90 | - if ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) { |
|
| 90 | + if (function_exists('is_gutenberg_page') && is_gutenberg_page()) { |
|
| 91 | 91 | // The Gutenberg plugin is on. |
| 92 | 92 | return TRUE; |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | $current_screen = get_current_screen(); |
| 96 | - if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) { |
|
| 96 | + if (method_exists($current_screen, 'is_block_editor') && $current_screen->is_block_editor()) { |
|
| 97 | 97 | // Gutenberg page on 5+. |
| 98 | 98 | return TRUE; |
| 99 | 99 | } |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | */ |
| 113 | 113 | private function is_ux_builder_editor() { |
| 114 | 114 | |
| 115 | - return function_exists( 'ux_builder_is_editor' ) |
|
| 115 | + return function_exists('ux_builder_is_editor') |
|
| 116 | 116 | && ux_builder_is_editor(); |
| 117 | 117 | } |
| 118 | 118 | |
@@ -124,19 +124,19 @@ discard block |
||
| 124 | 124 | public function enqueue_scripts() { |
| 125 | 125 | |
| 126 | 126 | // Bail out if this is G'berg. |
| 127 | - if ( $this->is_gutenberg_page() ) { |
|
| 127 | + if ($this->is_gutenberg_page()) { |
|
| 128 | 128 | return; |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | // Dequeue potentially conflicting ontrapages angular scripts which any *are not* used on the edit screen. |
| 132 | 132 | // |
| 133 | 133 | // @see https://github.com/insideout10/wordlift-plugin/issues/832 |
| 134 | - wp_dequeue_script( 'ontrapagesAngular' ); |
|
| 135 | - wp_dequeue_script( 'ontrapagesApp' ); |
|
| 136 | - wp_dequeue_script( 'ontrapagesController' ); |
|
| 134 | + wp_dequeue_script('ontrapagesAngular'); |
|
| 135 | + wp_dequeue_script('ontrapagesApp'); |
|
| 136 | + wp_dequeue_script('ontrapagesController'); |
|
| 137 | 137 | |
| 138 | 138 | // If Gutenberg is enabled for the post, do not load the legacy edit.js. |
| 139 | - if ( function_exists( 'use_block_editor_for_post' ) && use_block_editor_for_post( get_post() ) ) { |
|
| 139 | + if (function_exists('use_block_editor_for_post') && use_block_editor_for_post(get_post())) { |
|
| 140 | 140 | return; |
| 141 | 141 | } |
| 142 | 142 | |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | * |
| 151 | 151 | * @since 3.20.0 edit.js has been migrated to the new webpack configuration. |
| 152 | 152 | */ |
| 153 | - $script_name = plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/edit'; |
|
| 153 | + $script_name = plugin_dir_url(dirname(__FILE__)).'js/dist/edit'; |
|
| 154 | 154 | |
| 155 | 155 | /** |
| 156 | 156 | * Scripts_Helper introduced. |
@@ -192,9 +192,9 @@ discard block |
||
| 192 | 192 | ) |
| 193 | 193 | ); |
| 194 | 194 | |
| 195 | - wp_enqueue_style( 'wl-classic-editor', "$script_name.css", array(), $this->plugin->get_version() ); |
|
| 195 | + wp_enqueue_style('wl-classic-editor', "$script_name.css", array(), $this->plugin->get_version()); |
|
| 196 | 196 | $this->load_faq_scripts_and_styles(); |
| 197 | - $this->load_faq_settings( self::TINY_MCE ); |
|
| 197 | + $this->load_faq_settings(self::TINY_MCE); |
|
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | /** |
@@ -203,13 +203,13 @@ discard block |
||
| 203 | 203 | private function load_faq_scripts_and_styles() { |
| 204 | 204 | wp_enqueue_style( |
| 205 | 205 | 'wl-faq-metabox-style', |
| 206 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/faq.css', |
|
| 206 | + plugin_dir_url(dirname(__FILE__)).'js/dist/faq.css', |
|
| 207 | 207 | array() |
| 208 | 208 | ); |
| 209 | 209 | Scripts_Helper::enqueue_based_on_wordpress_version( |
| 210 | 210 | 'wl-faq-metabox-script', |
| 211 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/faq', |
|
| 212 | - array( 'wp-polyfill' ), |
|
| 211 | + plugin_dir_url(dirname(__FILE__)).'js/dist/faq', |
|
| 212 | + array('wp-polyfill'), |
|
| 213 | 213 | true |
| 214 | 214 | ); |
| 215 | 215 | } |
@@ -221,20 +221,20 @@ discard block |
||
| 221 | 221 | */ |
| 222 | 222 | public function get_faq_settings() { |
| 223 | 223 | return array( |
| 224 | - 'restUrl' => get_rest_url( NULL, WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faq' ), |
|
| 224 | + 'restUrl' => get_rest_url(NULL, WL_REST_ROUTE_DEFAULT_NAMESPACE.'/faq'), |
|
| 225 | 225 | 'listBoxId' => self::FAQ_LIST_BOX_ID, |
| 226 | - 'nonce' => wp_create_nonce( 'wp_rest' ), |
|
| 226 | + 'nonce' => wp_create_nonce('wp_rest'), |
|
| 227 | 227 | 'postId' => get_the_ID(), |
| 228 | 228 | // Translation for warning, error message. |
| 229 | - 'invalidTagMessage' => sprintf( __( 'Invalid tags %s is present in answer', self::WORDLIFT_TEXT_DOMAIN ), "{INVALID_TAGS}" ), |
|
| 230 | - 'invalidWordCountMessage' => sprintf( __( 'Answer word count must not exceed %s words', self::WORDLIFT_TEXT_DOMAIN ), "{ANSWER_WORD_COUNT_WARNING_LIMIT}" ), |
|
| 231 | - 'questionText' => __( 'Question', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 232 | - 'answerText' => __( 'Answer', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 233 | - 'addQuestionOrAnswerText' => __( 'Add Question / Answer', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 234 | - 'addQuestionText' => __( 'Add Question', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 235 | - 'addAnswerText' => __( 'Add Answer', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 236 | - 'noFaqItemsText' => __( 'Highlight a question in content, then click Add Question.', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 237 | - 'updatingText' => __( 'Updating...', self::WORDLIFT_TEXT_DOMAIN ) |
|
| 229 | + 'invalidTagMessage' => sprintf(__('Invalid tags %s is present in answer', self::WORDLIFT_TEXT_DOMAIN), "{INVALID_TAGS}"), |
|
| 230 | + 'invalidWordCountMessage' => sprintf(__('Answer word count must not exceed %s words', self::WORDLIFT_TEXT_DOMAIN), "{ANSWER_WORD_COUNT_WARNING_LIMIT}"), |
|
| 231 | + 'questionText' => __('Question', self::WORDLIFT_TEXT_DOMAIN), |
|
| 232 | + 'answerText' => __('Answer', self::WORDLIFT_TEXT_DOMAIN), |
|
| 233 | + 'addQuestionOrAnswerText' => __('Add Question / Answer', self::WORDLIFT_TEXT_DOMAIN), |
|
| 234 | + 'addQuestionText' => __('Add Question', self::WORDLIFT_TEXT_DOMAIN), |
|
| 235 | + 'addAnswerText' => __('Add Answer', self::WORDLIFT_TEXT_DOMAIN), |
|
| 236 | + 'noFaqItemsText' => __('Highlight a question in content, then click Add Question.', self::WORDLIFT_TEXT_DOMAIN), |
|
| 237 | + 'updatingText' => __('Updating...', self::WORDLIFT_TEXT_DOMAIN) |
|
| 238 | 238 | ); |
| 239 | 239 | } |
| 240 | 240 | |
@@ -243,16 +243,16 @@ discard block |
||
| 243 | 243 | * |
| 244 | 244 | * @param $editor string specifying which text editor needed to be used. |
| 245 | 245 | */ |
| 246 | - private function load_faq_settings( $editor ) { |
|
| 246 | + private function load_faq_settings($editor) { |
|
| 247 | 247 | // This script also provides translations to gutenberg. |
| 248 | - wp_localize_script( 'wl-faq-metabox-script', '_wlFaqSettings', $this->get_faq_settings() ); |
|
| 248 | + wp_localize_script('wl-faq-metabox-script', '_wlFaqSettings', $this->get_faq_settings()); |
|
| 249 | 249 | |
| 250 | 250 | // Enqueue the FAQ style |
| 251 | - if ( $editor === self::GUTENBERG ) { |
|
| 251 | + if ($editor === self::GUTENBERG) { |
|
| 252 | 252 | Scripts_Helper::enqueue_based_on_wordpress_version( |
| 253 | 253 | 'wl-faq-gutenberg-plugin', |
| 254 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/block-editor-faq-plugin', |
|
| 255 | - array( 'wp-polyfill' ), |
|
| 254 | + plugin_dir_url(dirname(__FILE__)).'js/dist/block-editor-faq-plugin', |
|
| 255 | + array('wp-polyfill'), |
|
| 256 | 256 | TRUE |
| 257 | 257 | ); |
| 258 | 258 | } |
@@ -266,11 +266,11 @@ discard block |
||
| 266 | 266 | public function enqueue_scripts_gutenberg() { |
| 267 | 267 | // Load FAQ settings. |
| 268 | 268 | $this->load_faq_scripts_and_styles(); |
| 269 | - $this->load_faq_settings( self::GUTENBERG ); |
|
| 269 | + $this->load_faq_settings(self::GUTENBERG); |
|
| 270 | 270 | |
| 271 | 271 | wp_register_script( |
| 272 | 272 | 'wl-block-editor', |
| 273 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/block-editor.js', |
|
| 273 | + plugin_dir_url(dirname(__FILE__)).'js/dist/block-editor.js', |
|
| 274 | 274 | array( |
| 275 | 275 | 'react', |
| 276 | 276 | 'wordlift', |
@@ -283,24 +283,24 @@ discard block |
||
| 283 | 283 | ), |
| 284 | 284 | $this->plugin->get_version() |
| 285 | 285 | ); |
| 286 | - wp_localize_script( 'wl-block-editor', '_wlBlockEditorSettings', array( |
|
| 287 | - 'root' => esc_url_raw( rest_url() ), |
|
| 288 | - 'nonce' => wp_create_nonce( 'wp_rest' ) |
|
| 289 | - ) ); |
|
| 286 | + wp_localize_script('wl-block-editor', '_wlBlockEditorSettings', array( |
|
| 287 | + 'root' => esc_url_raw(rest_url()), |
|
| 288 | + 'nonce' => wp_create_nonce('wp_rest') |
|
| 289 | + )); |
|
| 290 | 290 | |
| 291 | 291 | /* |
| 292 | 292 | * @since 3.25.1 The hook is used by the wp_localize_script to register the _wlEntityTypes global object. |
| 293 | 293 | */ |
| 294 | 294 | wp_enqueue_style( |
| 295 | 295 | 'wl-block-editor', |
| 296 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/block-editor.css', |
|
| 296 | + plugin_dir_url(dirname(__FILE__)).'js/dist/block-editor.css', |
|
| 297 | 297 | array(), |
| 298 | 298 | $this->plugin->get_version() |
| 299 | 299 | ); |
| 300 | 300 | |
| 301 | 301 | wp_enqueue_script( |
| 302 | 302 | 'wl-autocomplete-select', |
| 303 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/autocomplete-select.js', |
|
| 303 | + plugin_dir_url(dirname(__FILE__)).'js/dist/autocomplete-select.js', |
|
| 304 | 304 | array(), |
| 305 | 305 | $this->plugin->get_version(), |
| 306 | 306 | TRUE |
@@ -308,7 +308,7 @@ discard block |
||
| 308 | 308 | |
| 309 | 309 | wp_enqueue_style( |
| 310 | 310 | 'wl-autocomplete-select', |
| 311 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/autocomplete-select.css', |
|
| 311 | + plugin_dir_url(dirname(__FILE__)).'js/dist/autocomplete-select.css', |
|
| 312 | 312 | array(), |
| 313 | 313 | $this->plugin->get_version() |
| 314 | 314 | ); |
@@ -23,104 +23,104 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class Jsonld_By_Id_Endpoint { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * The {@link Wordlift_Jsonld_Service} instance. |
|
| 28 | - * |
|
| 29 | - * @var Wordlift_Jsonld_Service The {@link Wordlift_Jsonld_Service} instance. |
|
| 30 | - */ |
|
| 31 | - private $jsonld_service; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var \Wordlift_Entity_Uri_Service |
|
| 35 | - */ |
|
| 36 | - private $entity_uri_service; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Jsonld_Endpoint constructor. |
|
| 40 | - * |
|
| 41 | - * @param \Wordlift_Jsonld_Service $jsonld_service |
|
| 42 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service |
|
| 43 | - */ |
|
| 44 | - public function __construct( $jsonld_service, $entity_uri_service ) { |
|
| 45 | - |
|
| 46 | - $this->jsonld_service = $jsonld_service; |
|
| 47 | - $this->entity_uri_service = $entity_uri_service; |
|
| 48 | - |
|
| 49 | - add_action( 'rest_api_init', array( $this, 'register_routes', ) ); |
|
| 50 | - |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Get the JSON-LD. |
|
| 55 | - * |
|
| 56 | - * @param \WP_REST_Request $request The incoming {@link \WP_REST_Request}. |
|
| 57 | - * |
|
| 58 | - * @return WP_REST_Response The outgoing {@link WP_REST_Response}. |
|
| 59 | - * @throws \Exception |
|
| 60 | - */ |
|
| 61 | - public function jsonld_by_id( $request ) { |
|
| 62 | - |
|
| 63 | - // Get the ids. |
|
| 64 | - $ids = (array)$request->get_param( 'id' ); |
|
| 65 | - |
|
| 66 | - // Preload the URIs to reduce the number of DB roundtrips. |
|
| 67 | - $this->entity_uri_service->preload_uris( array_map( 'urldecode', $ids ) ); |
|
| 68 | - |
|
| 69 | - $that = $this; |
|
| 70 | - |
|
| 71 | - // Get the posts, filtering out those not found. |
|
| 72 | - $posts = array_filter( array_map( function ( $item ) use ( $that ) { |
|
| 73 | - return $that->entity_uri_service->get_entity( urldecode( $item ) ); |
|
| 74 | - }, $ids ) ); |
|
| 75 | - |
|
| 76 | - // Get the posts' IDs and make the unique. |
|
| 77 | - $post_ids = array_unique( array_map( function ( $item ) { |
|
| 78 | - return $item->ID; |
|
| 79 | - }, $posts ) ); |
|
| 80 | - |
|
| 81 | - // Get the JSON-LD. |
|
| 82 | - $data = array(); |
|
| 83 | - foreach ( $post_ids as $post_id ) { |
|
| 84 | - $data = array_merge( $data, $that->jsonld_service->get_jsonld( false, $post_id ) ); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - // Add the WebSite fragment if requested. |
|
| 88 | - if ( $request->get_param( 'website' ) ) { |
|
| 89 | - $data[] = $this->jsonld_service->get_jsonld( true ); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - return Jsonld_Response_Helper::to_response( $data ); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - public function register_routes() { |
|
| 96 | - |
|
| 97 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld', array( |
|
| 98 | - 'methods' => WP_REST_Server::READABLE, |
|
| 99 | - 'callback' => array( $this, 'jsonld_by_id' ), |
|
| 100 | - 'args' => array( |
|
| 101 | - 'id' => array( |
|
| 102 | - 'description' => __( 'One ore more itemids (e.g. http://data.wordlift.io/wordlift).', 'wordlift' ), |
|
| 103 | - // We expect an array of strings. |
|
| 104 | - 'type' => 'array', |
|
| 105 | - 'validate_callback' => function ( $values, $request, $param ) { |
|
| 106 | - |
|
| 107 | - if ( ! is_array( $values ) ) { |
|
| 108 | - return new WP_Error( 'rest_invalid_param', esc_html__( 'The id argument must be an array (try passing `id[]=...`.', 'wordlift' ), array( 'status' => 400 ) ); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - foreach ( $values as $value ) { |
|
| 112 | - if ( 0 !== strpos( $value, 'http' ) ) { |
|
| 113 | - return new WP_Error( 'rest_invalid_param', esc_html__( 'Ids must start with http.', 'wordlift' ), array( 'status' => 400 ) ); |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - ), |
|
| 118 | - 'website' => array( |
|
| 119 | - 'description' => __( 'Whether to include the WebSite markup.', 'wordlift' ), |
|
| 120 | - ) |
|
| 121 | - ) |
|
| 122 | - ) ); |
|
| 123 | - |
|
| 124 | - } |
|
| 26 | + /** |
|
| 27 | + * The {@link Wordlift_Jsonld_Service} instance. |
|
| 28 | + * |
|
| 29 | + * @var Wordlift_Jsonld_Service The {@link Wordlift_Jsonld_Service} instance. |
|
| 30 | + */ |
|
| 31 | + private $jsonld_service; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var \Wordlift_Entity_Uri_Service |
|
| 35 | + */ |
|
| 36 | + private $entity_uri_service; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Jsonld_Endpoint constructor. |
|
| 40 | + * |
|
| 41 | + * @param \Wordlift_Jsonld_Service $jsonld_service |
|
| 42 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service |
|
| 43 | + */ |
|
| 44 | + public function __construct( $jsonld_service, $entity_uri_service ) { |
|
| 45 | + |
|
| 46 | + $this->jsonld_service = $jsonld_service; |
|
| 47 | + $this->entity_uri_service = $entity_uri_service; |
|
| 48 | + |
|
| 49 | + add_action( 'rest_api_init', array( $this, 'register_routes', ) ); |
|
| 50 | + |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Get the JSON-LD. |
|
| 55 | + * |
|
| 56 | + * @param \WP_REST_Request $request The incoming {@link \WP_REST_Request}. |
|
| 57 | + * |
|
| 58 | + * @return WP_REST_Response The outgoing {@link WP_REST_Response}. |
|
| 59 | + * @throws \Exception |
|
| 60 | + */ |
|
| 61 | + public function jsonld_by_id( $request ) { |
|
| 62 | + |
|
| 63 | + // Get the ids. |
|
| 64 | + $ids = (array)$request->get_param( 'id' ); |
|
| 65 | + |
|
| 66 | + // Preload the URIs to reduce the number of DB roundtrips. |
|
| 67 | + $this->entity_uri_service->preload_uris( array_map( 'urldecode', $ids ) ); |
|
| 68 | + |
|
| 69 | + $that = $this; |
|
| 70 | + |
|
| 71 | + // Get the posts, filtering out those not found. |
|
| 72 | + $posts = array_filter( array_map( function ( $item ) use ( $that ) { |
|
| 73 | + return $that->entity_uri_service->get_entity( urldecode( $item ) ); |
|
| 74 | + }, $ids ) ); |
|
| 75 | + |
|
| 76 | + // Get the posts' IDs and make the unique. |
|
| 77 | + $post_ids = array_unique( array_map( function ( $item ) { |
|
| 78 | + return $item->ID; |
|
| 79 | + }, $posts ) ); |
|
| 80 | + |
|
| 81 | + // Get the JSON-LD. |
|
| 82 | + $data = array(); |
|
| 83 | + foreach ( $post_ids as $post_id ) { |
|
| 84 | + $data = array_merge( $data, $that->jsonld_service->get_jsonld( false, $post_id ) ); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + // Add the WebSite fragment if requested. |
|
| 88 | + if ( $request->get_param( 'website' ) ) { |
|
| 89 | + $data[] = $this->jsonld_service->get_jsonld( true ); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + return Jsonld_Response_Helper::to_response( $data ); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + public function register_routes() { |
|
| 96 | + |
|
| 97 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld', array( |
|
| 98 | + 'methods' => WP_REST_Server::READABLE, |
|
| 99 | + 'callback' => array( $this, 'jsonld_by_id' ), |
|
| 100 | + 'args' => array( |
|
| 101 | + 'id' => array( |
|
| 102 | + 'description' => __( 'One ore more itemids (e.g. http://data.wordlift.io/wordlift).', 'wordlift' ), |
|
| 103 | + // We expect an array of strings. |
|
| 104 | + 'type' => 'array', |
|
| 105 | + 'validate_callback' => function ( $values, $request, $param ) { |
|
| 106 | + |
|
| 107 | + if ( ! is_array( $values ) ) { |
|
| 108 | + return new WP_Error( 'rest_invalid_param', esc_html__( 'The id argument must be an array (try passing `id[]=...`.', 'wordlift' ), array( 'status' => 400 ) ); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + foreach ( $values as $value ) { |
|
| 112 | + if ( 0 !== strpos( $value, 'http' ) ) { |
|
| 113 | + return new WP_Error( 'rest_invalid_param', esc_html__( 'Ids must start with http.', 'wordlift' ), array( 'status' => 400 ) ); |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + ), |
|
| 118 | + 'website' => array( |
|
| 119 | + 'description' => __( 'Whether to include the WebSite markup.', 'wordlift' ), |
|
| 120 | + ) |
|
| 121 | + ) |
|
| 122 | + ) ); |
|
| 123 | + |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | 126 | } |
@@ -41,12 +41,12 @@ discard block |
||
| 41 | 41 | * @param \Wordlift_Jsonld_Service $jsonld_service |
| 42 | 42 | * @param \Wordlift_Entity_Uri_Service $entity_uri_service |
| 43 | 43 | */ |
| 44 | - public function __construct( $jsonld_service, $entity_uri_service ) { |
|
| 44 | + public function __construct($jsonld_service, $entity_uri_service) { |
|
| 45 | 45 | |
| 46 | 46 | $this->jsonld_service = $jsonld_service; |
| 47 | 47 | $this->entity_uri_service = $entity_uri_service; |
| 48 | 48 | |
| 49 | - add_action( 'rest_api_init', array( $this, 'register_routes', ) ); |
|
| 49 | + add_action('rest_api_init', array($this, 'register_routes',)); |
|
| 50 | 50 | |
| 51 | 51 | } |
| 52 | 52 | |
@@ -58,68 +58,68 @@ discard block |
||
| 58 | 58 | * @return WP_REST_Response The outgoing {@link WP_REST_Response}. |
| 59 | 59 | * @throws \Exception |
| 60 | 60 | */ |
| 61 | - public function jsonld_by_id( $request ) { |
|
| 61 | + public function jsonld_by_id($request) { |
|
| 62 | 62 | |
| 63 | 63 | // Get the ids. |
| 64 | - $ids = (array)$request->get_param( 'id' ); |
|
| 64 | + $ids = (array) $request->get_param('id'); |
|
| 65 | 65 | |
| 66 | 66 | // Preload the URIs to reduce the number of DB roundtrips. |
| 67 | - $this->entity_uri_service->preload_uris( array_map( 'urldecode', $ids ) ); |
|
| 67 | + $this->entity_uri_service->preload_uris(array_map('urldecode', $ids)); |
|
| 68 | 68 | |
| 69 | 69 | $that = $this; |
| 70 | 70 | |
| 71 | 71 | // Get the posts, filtering out those not found. |
| 72 | - $posts = array_filter( array_map( function ( $item ) use ( $that ) { |
|
| 73 | - return $that->entity_uri_service->get_entity( urldecode( $item ) ); |
|
| 74 | - }, $ids ) ); |
|
| 72 | + $posts = array_filter(array_map(function($item) use ($that) { |
|
| 73 | + return $that->entity_uri_service->get_entity(urldecode($item)); |
|
| 74 | + }, $ids)); |
|
| 75 | 75 | |
| 76 | 76 | // Get the posts' IDs and make the unique. |
| 77 | - $post_ids = array_unique( array_map( function ( $item ) { |
|
| 77 | + $post_ids = array_unique(array_map(function($item) { |
|
| 78 | 78 | return $item->ID; |
| 79 | - }, $posts ) ); |
|
| 79 | + }, $posts)); |
|
| 80 | 80 | |
| 81 | 81 | // Get the JSON-LD. |
| 82 | 82 | $data = array(); |
| 83 | - foreach ( $post_ids as $post_id ) { |
|
| 84 | - $data = array_merge( $data, $that->jsonld_service->get_jsonld( false, $post_id ) ); |
|
| 83 | + foreach ($post_ids as $post_id) { |
|
| 84 | + $data = array_merge($data, $that->jsonld_service->get_jsonld(false, $post_id)); |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | // Add the WebSite fragment if requested. |
| 88 | - if ( $request->get_param( 'website' ) ) { |
|
| 89 | - $data[] = $this->jsonld_service->get_jsonld( true ); |
|
| 88 | + if ($request->get_param('website')) { |
|
| 89 | + $data[] = $this->jsonld_service->get_jsonld(true); |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - return Jsonld_Response_Helper::to_response( $data ); |
|
| 92 | + return Jsonld_Response_Helper::to_response($data); |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | public function register_routes() { |
| 96 | 96 | |
| 97 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld', array( |
|
| 97 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld', array( |
|
| 98 | 98 | 'methods' => WP_REST_Server::READABLE, |
| 99 | - 'callback' => array( $this, 'jsonld_by_id' ), |
|
| 99 | + 'callback' => array($this, 'jsonld_by_id'), |
|
| 100 | 100 | 'args' => array( |
| 101 | 101 | 'id' => array( |
| 102 | - 'description' => __( 'One ore more itemids (e.g. http://data.wordlift.io/wordlift).', 'wordlift' ), |
|
| 102 | + 'description' => __('One ore more itemids (e.g. http://data.wordlift.io/wordlift).', 'wordlift'), |
|
| 103 | 103 | // We expect an array of strings. |
| 104 | 104 | 'type' => 'array', |
| 105 | - 'validate_callback' => function ( $values, $request, $param ) { |
|
| 105 | + 'validate_callback' => function($values, $request, $param) { |
|
| 106 | 106 | |
| 107 | - if ( ! is_array( $values ) ) { |
|
| 108 | - return new WP_Error( 'rest_invalid_param', esc_html__( 'The id argument must be an array (try passing `id[]=...`.', 'wordlift' ), array( 'status' => 400 ) ); |
|
| 107 | + if ( ! is_array($values)) { |
|
| 108 | + return new WP_Error('rest_invalid_param', esc_html__('The id argument must be an array (try passing `id[]=...`.', 'wordlift'), array('status' => 400)); |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - foreach ( $values as $value ) { |
|
| 112 | - if ( 0 !== strpos( $value, 'http' ) ) { |
|
| 113 | - return new WP_Error( 'rest_invalid_param', esc_html__( 'Ids must start with http.', 'wordlift' ), array( 'status' => 400 ) ); |
|
| 111 | + foreach ($values as $value) { |
|
| 112 | + if (0 !== strpos($value, 'http')) { |
|
| 113 | + return new WP_Error('rest_invalid_param', esc_html__('Ids must start with http.', 'wordlift'), array('status' => 400)); |
|
| 114 | 114 | } |
| 115 | 115 | } |
| 116 | 116 | } |
| 117 | 117 | ), |
| 118 | 118 | 'website' => array( |
| 119 | - 'description' => __( 'Whether to include the WebSite markup.', 'wordlift' ), |
|
| 119 | + 'description' => __('Whether to include the WebSite markup.', 'wordlift'), |
|
| 120 | 120 | ) |
| 121 | 121 | ) |
| 122 | - ) ); |
|
| 122 | + )); |
|
| 123 | 123 | |
| 124 | 124 | } |
| 125 | 125 | |