@@ -4,8 +4,8 @@ discard block |
||
| 4 | 4 | use Wordlift\Videoobject\Provider\Client\Youtube_Client; |
| 5 | 5 | |
| 6 | 6 | |
| 7 | -if ( isset( $_POST['wordlift_videoobject_youtube_api_key'] ) |
|
| 8 | - || isset( $_POST['wordlift_videoobject_vimeo_api_key'] ) ) { |
|
| 7 | +if (isset($_POST['wordlift_videoobject_youtube_api_key']) |
|
| 8 | + || isset($_POST['wordlift_videoobject_vimeo_api_key'])) { |
|
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | 11 | * @todo: does this fields need to be encrypted before saving ? |
@@ -13,84 +13,84 @@ discard block |
||
| 13 | 13 | $youtube_api_key = (string) $_POST['wordlift_videoobject_youtube_api_key']; |
| 14 | 14 | $vimeo_api_key = (string) $_POST['wordlift_videoobject_vimeo_api_key']; |
| 15 | 15 | |
| 16 | - if ( $youtube_api_key ) { |
|
| 17 | - update_option( Youtube_Client::get_api_key_option_name(), $youtube_api_key ); |
|
| 16 | + if ($youtube_api_key) { |
|
| 17 | + update_option(Youtube_Client::get_api_key_option_name(), $youtube_api_key); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | - if ( $vimeo_api_key ) { |
|
| 21 | - update_option( Vimeo_Client::get_api_key_option_name(), $vimeo_api_key ); |
|
| 20 | + if ($vimeo_api_key) { |
|
| 21 | + update_option(Vimeo_Client::get_api_key_option_name(), $vimeo_api_key); |
|
| 22 | 22 | } |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | -if ( isset( $_POST['submit'] ) ) { |
|
| 26 | - if ( isset( $_POST['wl_enable_video_sitemap'] ) ) { |
|
| 27 | - update_option( "_wl_video_sitemap_generation", 1 ); |
|
| 25 | +if (isset($_POST['submit'])) { |
|
| 26 | + if (isset($_POST['wl_enable_video_sitemap'])) { |
|
| 27 | + update_option("_wl_video_sitemap_generation", 1); |
|
| 28 | 28 | // flush the rewrite rules |
| 29 | 29 | flush_rewrite_rules(); |
| 30 | 30 | } else { |
| 31 | - update_option( "_wl_video_sitemap_generation", 0 ); |
|
| 31 | + update_option("_wl_video_sitemap_generation", 0); |
|
| 32 | 32 | // flush the rewrite rules |
| 33 | 33 | flush_rewrite_rules(); |
| 34 | 34 | } |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | ?> |
| 38 | -<h1><?php _e( 'API Settings', 'wordlift' ); ?></h1> |
|
| 39 | -<p><?php _e( 'To let WordLift access metadata from YouTube or Vimeo you will need to add here your API Key.', 'wordlift' ); ?></p> |
|
| 38 | +<h1><?php _e('API Settings', 'wordlift'); ?></h1> |
|
| 39 | +<p><?php _e('To let WordLift access metadata from YouTube or Vimeo you will need to add here your API Key.', 'wordlift'); ?></p> |
|
| 40 | 40 | <form method="post"> |
| 41 | 41 | <table> |
| 42 | 42 | <tr> |
| 43 | 43 | <td> |
| 44 | - <?php _e( 'YouTube API Key', 'wordlift' ); ?> |
|
| 44 | + <?php _e('YouTube API Key', 'wordlift'); ?> |
|
| 45 | 45 | </td> |
| 46 | 46 | <td> |
| 47 | 47 | <?php |
| 48 | 48 | $element = new Wordlift_Admin_Input_Element(); |
| 49 | - $element->render( array( |
|
| 49 | + $element->render(array( |
|
| 50 | 50 | 'id' => 'wordlift_videoobject_youtube_api_key', |
| 51 | 51 | 'name' => 'wordlift_videoobject_youtube_api_key', |
| 52 | 52 | 'value' => Youtube_Client::get_api_key() |
| 53 | - ) ); |
|
| 53 | + )); |
|
| 54 | 54 | ?> |
| 55 | 55 | </td> |
| 56 | 56 | <td> |
| 57 | - <a href="https://developers.google.com/youtube/registering_an_application"><?php _e( 'Click here', 'wordlift' ); ?></a> |
|
| 58 | - <?php _e( ' for instructions on getting your YouTube API Key', 'wordlift' ); ?> |
|
| 57 | + <a href="https://developers.google.com/youtube/registering_an_application"><?php _e('Click here', 'wordlift'); ?></a> |
|
| 58 | + <?php _e(' for instructions on getting your YouTube API Key', 'wordlift'); ?> |
|
| 59 | 59 | </td> |
| 60 | 60 | </tr> |
| 61 | 61 | |
| 62 | 62 | <tr> |
| 63 | 63 | <td> |
| 64 | - <?php _e( 'Vimeo API Key', 'wordlift' ); ?> |
|
| 64 | + <?php _e('Vimeo API Key', 'wordlift'); ?> |
|
| 65 | 65 | </td> |
| 66 | 66 | <td> |
| 67 | 67 | <?php |
| 68 | 68 | $element = new Wordlift_Admin_Input_Element(); |
| 69 | - $element->render( array( |
|
| 69 | + $element->render(array( |
|
| 70 | 70 | 'id' => 'wordlift_videoobject_vimeo_api_key', |
| 71 | 71 | 'name' => 'wordlift_videoobject_vimeo_api_key', |
| 72 | 72 | 'value' => Vimeo_Client::get_api_key() |
| 73 | - ) ); |
|
| 73 | + )); |
|
| 74 | 74 | ?> |
| 75 | 75 | </td> |
| 76 | 76 | <td> |
| 77 | - <a href="https://developer.vimeo.com/api/guides/start"><?php _e( 'Click here', 'wordlift' ); ?></a> |
|
| 78 | - <?php _e( ' for instructions on getting your Vimeo API Key', 'wordlift' ); ?> |
|
| 77 | + <a href="https://developer.vimeo.com/api/guides/start"><?php _e('Click here', 'wordlift'); ?></a> |
|
| 78 | + <?php _e(' for instructions on getting your Vimeo API Key', 'wordlift'); ?> |
|
| 79 | 79 | </td> |
| 80 | 80 | </tr> |
| 81 | 81 | |
| 82 | 82 | </table> |
| 83 | - <h1><?php _e( 'Video Sitemap', 'wordlift' ); ?></h1> |
|
| 83 | + <h1><?php _e('Video Sitemap', 'wordlift'); ?></h1> |
|
| 84 | 84 | <p> |
| 85 | - <?php _e( 'The Video Sitemap works like any other XML Sitemap. Search engines will use it to display rich snippets in result pages.', 'wordlift' ); ?> |
|
| 85 | + <?php _e('The Video Sitemap works like any other XML Sitemap. Search engines will use it to display rich snippets in result pages.', 'wordlift'); ?> |
|
| 86 | 86 | </p> |
| 87 | - <?php $is_sitemap_enabled = get_option( '_wl_video_sitemap_generation', false ) ? 'checked' : '' ?> |
|
| 88 | - <p> <?php _e( 'Enable Video Sitemap' ); ?> |
|
| 87 | + <?php $is_sitemap_enabled = get_option('_wl_video_sitemap_generation', false) ? 'checked' : '' ?> |
|
| 88 | + <p> <?php _e('Enable Video Sitemap'); ?> |
|
| 89 | 89 | <input type="checkbox" name="wl_enable_video_sitemap" value="1" <?php echo $is_sitemap_enabled; ?> ></p> |
| 90 | 90 | <p> <?php |
| 91 | - if ( $is_sitemap_enabled ) { |
|
| 92 | - $sitemap_link = get_home_url( null, 'wl-video-sitemap.xml' ); |
|
| 93 | - printf( __( 'Here is <a href="%s">link</a> to your Video Sitemap. Add it now, to Google Search Console.', 'wordlift' ), $sitemap_link ); |
|
| 91 | + if ($is_sitemap_enabled) { |
|
| 92 | + $sitemap_link = get_home_url(null, 'wl-video-sitemap.xml'); |
|
| 93 | + printf(__('Here is <a href="%s">link</a> to your Video Sitemap. Add it now, to Google Search Console.', 'wordlift'), $sitemap_link); |
|
| 94 | 94 | } |
| 95 | 95 | ?></p> |
| 96 | 96 | |
@@ -16,57 +16,57 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | function wordlift_ajax_related_posts( $http_raw_data = null ) { |
| 18 | 18 | |
| 19 | - // Extract filtering conditions |
|
| 20 | - if ( ! isset( $_GET["post_id"] ) || ! is_numeric( $_GET["post_id"] ) ) { |
|
| 21 | - wp_die( 'Post id missing or invalid!' ); |
|
| 19 | + // Extract filtering conditions |
|
| 20 | + if ( ! isset( $_GET["post_id"] ) || ! is_numeric( $_GET["post_id"] ) ) { |
|
| 21 | + wp_die( 'Post id missing or invalid!' ); |
|
| 22 | 22 | |
| 23 | - return; |
|
| 24 | - } |
|
| 23 | + return; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - // Get the current post |
|
| 27 | - $post_id = (int) $_GET["post_id"]; |
|
| 28 | - $post = get_post( $post_id ); |
|
| 26 | + // Get the current post |
|
| 27 | + $post_id = (int) $_GET["post_id"]; |
|
| 28 | + $post = get_post( $post_id ); |
|
| 29 | 29 | |
| 30 | - Wordlift_Log_Service::get_logger( 'wordlift_ajax_related_posts' )->trace( "Going to find posts related to current with post id: $post_id ..." ); |
|
| 30 | + Wordlift_Log_Service::get_logger( 'wordlift_ajax_related_posts' )->trace( "Going to find posts related to current with post id: $post_id ..." ); |
|
| 31 | 31 | |
| 32 | - // Extract filtering conditions |
|
| 33 | - $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( "php://input" ) : $http_raw_data; |
|
| 34 | - $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
| 32 | + // Extract filtering conditions |
|
| 33 | + $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( "php://input" ) : $http_raw_data; |
|
| 34 | + $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
| 35 | 35 | |
| 36 | - $filtering_entity_ids = wl_get_entity_post_ids_by_uris( $filtering_entity_uris ); |
|
| 37 | - $related_posts = array(); |
|
| 36 | + $filtering_entity_ids = wl_get_entity_post_ids_by_uris( $filtering_entity_uris ); |
|
| 37 | + $related_posts = array(); |
|
| 38 | 38 | |
| 39 | - // If the current post is an entity, related posts to the current entity are |
|
| 40 | - // returned. |
|
| 41 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 39 | + // If the current post is an entity, related posts to the current entity are |
|
| 40 | + // returned. |
|
| 41 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 42 | 42 | |
| 43 | - if ( $entity_service->is_entity( $post->ID ) ) { |
|
| 44 | - $filtering_entity_ids = array( $post_id ); |
|
| 45 | - } |
|
| 43 | + if ( $entity_service->is_entity( $post->ID ) ) { |
|
| 44 | + $filtering_entity_ids = array( $post_id ); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - if ( ! empty( $filtering_entity_ids ) ) { |
|
| 47 | + if ( ! empty( $filtering_entity_ids ) ) { |
|
| 48 | 48 | |
| 49 | - $related_posts = Wordlift_Relation_Service::get_instance() |
|
| 50 | - ->get_article_subjects( $filtering_entity_ids, '*', null, 'publish', array( $post_id ), 5 ); |
|
| 49 | + $related_posts = Wordlift_Relation_Service::get_instance() |
|
| 50 | + ->get_article_subjects( $filtering_entity_ids, '*', null, 'publish', array( $post_id ), 5 ); |
|
| 51 | 51 | |
| 52 | - foreach ( $related_posts as $post_obj ) { |
|
| 52 | + foreach ( $related_posts as $post_obj ) { |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Use the thumbnail. |
|
| 56 | - * |
|
| 57 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 58 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 59 | - * |
|
| 60 | - * @since 3.19.3 We're using the medium size image. |
|
| 61 | - */ |
|
| 62 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 63 | - $post_obj->thumbnail = ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 64 | - $post_obj->link = get_edit_post_link( $post_obj->ID, 'none' ); |
|
| 65 | - $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
| 66 | - } |
|
| 67 | - } |
|
| 54 | + /** |
|
| 55 | + * Use the thumbnail. |
|
| 56 | + * |
|
| 57 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 58 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 59 | + * |
|
| 60 | + * @since 3.19.3 We're using the medium size image. |
|
| 61 | + */ |
|
| 62 | + $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 63 | + $post_obj->thumbnail = ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 64 | + $post_obj->link = get_edit_post_link( $post_obj->ID, 'none' ); |
|
| 65 | + $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - wl_core_send_json( $related_posts ); |
|
| 69 | + wl_core_send_json( $related_posts ); |
|
| 70 | 70 | |
| 71 | 71 | } |
| 72 | 72 | |
@@ -14,42 +14,42 @@ discard block |
||
| 14 | 14 | * |
| 15 | 15 | * @param null $http_raw_data |
| 16 | 16 | */ |
| 17 | -function wordlift_ajax_related_posts( $http_raw_data = null ) { |
|
| 17 | +function wordlift_ajax_related_posts($http_raw_data = null) { |
|
| 18 | 18 | |
| 19 | 19 | // Extract filtering conditions |
| 20 | - if ( ! isset( $_GET["post_id"] ) || ! is_numeric( $_GET["post_id"] ) ) { |
|
| 21 | - wp_die( 'Post id missing or invalid!' ); |
|
| 20 | + if ( ! isset($_GET["post_id"]) || ! is_numeric($_GET["post_id"])) { |
|
| 21 | + wp_die('Post id missing or invalid!'); |
|
| 22 | 22 | |
| 23 | 23 | return; |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | // Get the current post |
| 27 | 27 | $post_id = (int) $_GET["post_id"]; |
| 28 | - $post = get_post( $post_id ); |
|
| 28 | + $post = get_post($post_id); |
|
| 29 | 29 | |
| 30 | - Wordlift_Log_Service::get_logger( 'wordlift_ajax_related_posts' )->trace( "Going to find posts related to current with post id: $post_id ..." ); |
|
| 30 | + Wordlift_Log_Service::get_logger('wordlift_ajax_related_posts')->trace("Going to find posts related to current with post id: $post_id ..."); |
|
| 31 | 31 | |
| 32 | 32 | // Extract filtering conditions |
| 33 | - $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( "php://input" ) : $http_raw_data; |
|
| 34 | - $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
| 33 | + $filtering_entity_uris = (null == $http_raw_data) ? file_get_contents("php://input") : $http_raw_data; |
|
| 34 | + $filtering_entity_uris = json_decode($filtering_entity_uris); |
|
| 35 | 35 | |
| 36 | - $filtering_entity_ids = wl_get_entity_post_ids_by_uris( $filtering_entity_uris ); |
|
| 36 | + $filtering_entity_ids = wl_get_entity_post_ids_by_uris($filtering_entity_uris); |
|
| 37 | 37 | $related_posts = array(); |
| 38 | 38 | |
| 39 | 39 | // If the current post is an entity, related posts to the current entity are |
| 40 | 40 | // returned. |
| 41 | 41 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 42 | 42 | |
| 43 | - if ( $entity_service->is_entity( $post->ID ) ) { |
|
| 44 | - $filtering_entity_ids = array( $post_id ); |
|
| 43 | + if ($entity_service->is_entity($post->ID)) { |
|
| 44 | + $filtering_entity_ids = array($post_id); |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | - if ( ! empty( $filtering_entity_ids ) ) { |
|
| 47 | + if ( ! empty($filtering_entity_ids)) { |
|
| 48 | 48 | |
| 49 | 49 | $related_posts = Wordlift_Relation_Service::get_instance() |
| 50 | - ->get_article_subjects( $filtering_entity_ids, '*', null, 'publish', array( $post_id ), 5 ); |
|
| 50 | + ->get_article_subjects($filtering_entity_ids, '*', null, 'publish', array($post_id), 5); |
|
| 51 | 51 | |
| 52 | - foreach ( $related_posts as $post_obj ) { |
|
| 52 | + foreach ($related_posts as $post_obj) { |
|
| 53 | 53 | |
| 54 | 54 | /** |
| 55 | 55 | * Use the thumbnail. |
@@ -59,15 +59,15 @@ discard block |
||
| 59 | 59 | * |
| 60 | 60 | * @since 3.19.3 We're using the medium size image. |
| 61 | 61 | */ |
| 62 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 63 | - $post_obj->thumbnail = ( $thumbnail ) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 64 | - $post_obj->link = get_edit_post_link( $post_obj->ID, 'none' ); |
|
| 65 | - $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
| 62 | + $thumbnail = get_the_post_thumbnail_url($post_obj, 'medium'); |
|
| 63 | + $post_obj->thumbnail = ($thumbnail) ? $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 64 | + $post_obj->link = get_edit_post_link($post_obj->ID, 'none'); |
|
| 65 | + $post_obj->permalink = get_post_permalink($post_obj->ID); |
|
| 66 | 66 | } |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | - wl_core_send_json( $related_posts ); |
|
| 69 | + wl_core_send_json($related_posts); |
|
| 70 | 70 | |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | -add_action( 'wp_ajax_wordlift_related_posts', 'wordlift_ajax_related_posts' ); |
|
| 73 | +add_action('wp_ajax_wordlift_related_posts', 'wordlift_ajax_related_posts'); |
|
@@ -16,78 +16,78 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Admin_Term_Adapter { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The meta key holding the entity id. |
|
| 21 | - * |
|
| 22 | - * @since 3.20.0 |
|
| 23 | - */ |
|
| 24 | - const META_KEY = '_wl_entity_id'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Create a Wordlift_Admin_Term_Adapter instance. |
|
| 28 | - * |
|
| 29 | - * @since 3.20.0 |
|
| 30 | - */ |
|
| 31 | - public function __construct() { |
|
| 32 | - |
|
| 33 | - add_action( 'registered_taxonomy', array( $this, 'add_action', ) ); |
|
| 34 | - add_action( 'edit_term', array( $this, 'edit_term', ), 10, 3 ); |
|
| 35 | - $this->add_settings(); |
|
| 36 | - |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Hook in to WordLift admin settings and add the term page specific |
|
| 41 | - * settings. |
|
| 42 | - * @since 3.26.1 |
|
| 43 | - */ |
|
| 44 | - public function add_settings() { |
|
| 45 | - add_filter( 'wl_admin_settings', function ( $params ) { |
|
| 46 | - $params['show_local_entities'] = true; |
|
| 47 | - return $params; |
|
| 48 | - } ); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Add the form fields to the entity edit screen. |
|
| 53 | - * |
|
| 54 | - * @param object $tag Current taxonomy term object. |
|
| 55 | - * @param string $taxonomy Current taxonomy slug. |
|
| 56 | - * |
|
| 57 | - * @since 3.20.0 |
|
| 58 | - * |
|
| 59 | - */ |
|
| 60 | - public function edit_form_fields( $tag, $taxonomy ) { |
|
| 61 | - |
|
| 62 | - // If disabled via filter, return; |
|
| 63 | - if ( ! apply_filters( 'wl_feature__enable__term-entity', true ) ) { |
|
| 64 | - return; |
|
| 19 | + /** |
|
| 20 | + * The meta key holding the entity id. |
|
| 21 | + * |
|
| 22 | + * @since 3.20.0 |
|
| 23 | + */ |
|
| 24 | + const META_KEY = '_wl_entity_id'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Create a Wordlift_Admin_Term_Adapter instance. |
|
| 28 | + * |
|
| 29 | + * @since 3.20.0 |
|
| 30 | + */ |
|
| 31 | + public function __construct() { |
|
| 32 | + |
|
| 33 | + add_action( 'registered_taxonomy', array( $this, 'add_action', ) ); |
|
| 34 | + add_action( 'edit_term', array( $this, 'edit_term', ), 10, 3 ); |
|
| 35 | + $this->add_settings(); |
|
| 36 | + |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Hook in to WordLift admin settings and add the term page specific |
|
| 41 | + * settings. |
|
| 42 | + * @since 3.26.1 |
|
| 43 | + */ |
|
| 44 | + public function add_settings() { |
|
| 45 | + add_filter( 'wl_admin_settings', function ( $params ) { |
|
| 46 | + $params['show_local_entities'] = true; |
|
| 47 | + return $params; |
|
| 48 | + } ); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Add the form fields to the entity edit screen. |
|
| 53 | + * |
|
| 54 | + * @param object $tag Current taxonomy term object. |
|
| 55 | + * @param string $taxonomy Current taxonomy slug. |
|
| 56 | + * |
|
| 57 | + * @since 3.20.0 |
|
| 58 | + * |
|
| 59 | + */ |
|
| 60 | + public function edit_form_fields( $tag, $taxonomy ) { |
|
| 61 | + |
|
| 62 | + // If disabled via filter, return; |
|
| 63 | + if ( ! apply_filters( 'wl_feature__enable__term-entity', true ) ) { |
|
| 64 | + return; |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - global $wp_version; |
|
| 67 | + global $wp_version; |
|
| 68 | 68 | |
| 69 | - // Enqueue the JavaScript app. |
|
| 70 | - if ( version_compare( $wp_version, '5.0', '>=' ) ) { |
|
| 71 | - $term_asset = include plugin_dir_path( dirname( __FILE__ ) ) . 'js/dist/term.asset.php'; |
|
| 72 | - wp_enqueue_script( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.js', array_merge( array( 'wp-util' ), $term_asset['dependencies'] ), Wordlift::get_instance()->get_version(), true ); |
|
| 73 | - } else { |
|
| 74 | - wp_enqueue_script( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.full.js', array( 'wp-util' ), Wordlift::get_instance()->get_version(), true ); |
|
| 75 | - } |
|
| 69 | + // Enqueue the JavaScript app. |
|
| 70 | + if ( version_compare( $wp_version, '5.0', '>=' ) ) { |
|
| 71 | + $term_asset = include plugin_dir_path( dirname( __FILE__ ) ) . 'js/dist/term.asset.php'; |
|
| 72 | + wp_enqueue_script( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.js', array_merge( array( 'wp-util' ), $term_asset['dependencies'] ), Wordlift::get_instance()->get_version(), true ); |
|
| 73 | + } else { |
|
| 74 | + wp_enqueue_script( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.full.js', array( 'wp-util' ), Wordlift::get_instance()->get_version(), true ); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - wp_enqueue_style( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.css', array(), Wordlift::get_instance()->get_version() ); |
|
| 77 | + wp_enqueue_style( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.css', array(), Wordlift::get_instance()->get_version() ); |
|
| 78 | 78 | |
| 79 | - $values = get_term_meta( $tag->term_id, self::META_KEY ); |
|
| 79 | + $values = get_term_meta( $tag->term_id, self::META_KEY ); |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * @since 3.31.3 |
|
| 81 | + /** |
|
| 82 | + * @since 3.31.3 |
|
| 83 | 83 | * @see https://github.com/insideout10/wordlift-plugin/issues/1446 |
| 84 | 84 | * This field should be hidden by default |
| 85 | - */ |
|
| 86 | - if ( ! $values ) { |
|
| 87 | - return; |
|
| 85 | + */ |
|
| 86 | + if ( ! $values ) { |
|
| 87 | + return; |
|
| 88 | 88 | } |
| 89 | 89 | |
| 90 | - ?> |
|
| 90 | + ?> |
|
| 91 | 91 | <tr class="form-field term-name-wrap"> |
| 92 | 92 | <th scope="row"><label for="wl-entity-id"><?php _ex( 'Entity', 'term entity', 'wordlift' ); ?></label></th> |
| 93 | 93 | <td> |
@@ -99,69 +99,69 @@ discard block |
||
| 99 | 99 | </td> |
| 100 | 100 | </tr> |
| 101 | 101 | <?php |
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Bind the new fields to the edit term screen. |
|
| 106 | - * |
|
| 107 | - * @since 3.20.0 |
|
| 108 | - * |
|
| 109 | - * @param string $taxonomy The taxonomy name. |
|
| 110 | - */ |
|
| 111 | - public function add_action( $taxonomy ) { |
|
| 112 | - /** |
|
| 113 | - * Filter wl_feature__enable__taxonomy_term_entity_mapping renamed to wl_feature__enable__term-entity. |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Bind the new fields to the edit term screen. |
|
| 106 | + * |
|
| 107 | + * @since 3.20.0 |
|
| 108 | + * |
|
| 109 | + * @param string $taxonomy The taxonomy name. |
|
| 110 | + */ |
|
| 111 | + public function add_action( $taxonomy ) { |
|
| 112 | + /** |
|
| 113 | + * Filter wl_feature__enable__taxonomy_term_entity_mapping renamed to wl_feature__enable__term-entity. |
|
| 114 | 114 | **/ |
| 115 | 115 | |
| 116 | - add_action( "{$taxonomy}_edit_form_fields", array( $this, 'edit_form_fields' ), 10, 2 ); |
|
| 117 | - |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Hook to the edit term to handle our own custom fields. |
|
| 122 | - * |
|
| 123 | - * @since 3.20.0 |
|
| 124 | - * |
|
| 125 | - * @param int $term_id The term id. |
|
| 126 | - * @param int $tt_id The term taxonomy id. |
|
| 127 | - * @param string $taxonomy The taxonomy. |
|
| 128 | - */ |
|
| 129 | - public function edit_term( $term_id, $tt_id, $taxonomy ) { |
|
| 130 | - |
|
| 131 | - // Bail if the action isn't related to the term currently being edited. |
|
| 132 | - if ( ! isset( $_POST['tag_ID'] ) || $term_id !== (int) $_POST['tag_ID'] ) { |
|
| 133 | - return; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - // Delete. |
|
| 137 | - if ( ! isset( $_POST['wl_entity_id'] ) || ! is_array( $_POST['wl_entity_id'] ) || empty( $_POST['wl_entity_id'] ) ) { |
|
| 138 | - delete_term_meta( $term_id, self::META_KEY ); |
|
| 139 | - |
|
| 140 | - return; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - // Update. |
|
| 144 | - // |
|
| 145 | - // Only use mb_* functions when mbstring is available. |
|
| 146 | - // |
|
| 147 | - // See https://github.com/insideout10/wordlift-plugin/issues/693. |
|
| 148 | - if ( extension_loaded( 'mbstring' ) ) { |
|
| 149 | - mb_regex_encoding( 'UTF-8' ); |
|
| 150 | - |
|
| 151 | - $merged = array_reduce( (array) $_POST['wl_entity_id'], function ( $carry, $item ) { |
|
| 152 | - return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) ); |
|
| 153 | - }, array() ); |
|
| 154 | - } else { |
|
| 155 | - $merged = array_reduce( (array) $_POST['wl_entity_id'], function ( $carry, $item ) { |
|
| 156 | - return array_merge( $carry, preg_split( "/\x{2063}/u", wp_unslash( $item ) ) ); |
|
| 157 | - }, array() ); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - delete_term_meta( $term_id, self::META_KEY ); |
|
| 161 | - foreach ( array_unique( array_filter( $merged ) ) as $single ) { |
|
| 162 | - add_term_meta( $term_id, self::META_KEY, $single ); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - } |
|
| 116 | + add_action( "{$taxonomy}_edit_form_fields", array( $this, 'edit_form_fields' ), 10, 2 ); |
|
| 117 | + |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Hook to the edit term to handle our own custom fields. |
|
| 122 | + * |
|
| 123 | + * @since 3.20.0 |
|
| 124 | + * |
|
| 125 | + * @param int $term_id The term id. |
|
| 126 | + * @param int $tt_id The term taxonomy id. |
|
| 127 | + * @param string $taxonomy The taxonomy. |
|
| 128 | + */ |
|
| 129 | + public function edit_term( $term_id, $tt_id, $taxonomy ) { |
|
| 130 | + |
|
| 131 | + // Bail if the action isn't related to the term currently being edited. |
|
| 132 | + if ( ! isset( $_POST['tag_ID'] ) || $term_id !== (int) $_POST['tag_ID'] ) { |
|
| 133 | + return; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + // Delete. |
|
| 137 | + if ( ! isset( $_POST['wl_entity_id'] ) || ! is_array( $_POST['wl_entity_id'] ) || empty( $_POST['wl_entity_id'] ) ) { |
|
| 138 | + delete_term_meta( $term_id, self::META_KEY ); |
|
| 139 | + |
|
| 140 | + return; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + // Update. |
|
| 144 | + // |
|
| 145 | + // Only use mb_* functions when mbstring is available. |
|
| 146 | + // |
|
| 147 | + // See https://github.com/insideout10/wordlift-plugin/issues/693. |
|
| 148 | + if ( extension_loaded( 'mbstring' ) ) { |
|
| 149 | + mb_regex_encoding( 'UTF-8' ); |
|
| 150 | + |
|
| 151 | + $merged = array_reduce( (array) $_POST['wl_entity_id'], function ( $carry, $item ) { |
|
| 152 | + return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) ); |
|
| 153 | + }, array() ); |
|
| 154 | + } else { |
|
| 155 | + $merged = array_reduce( (array) $_POST['wl_entity_id'], function ( $carry, $item ) { |
|
| 156 | + return array_merge( $carry, preg_split( "/\x{2063}/u", wp_unslash( $item ) ) ); |
|
| 157 | + }, array() ); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + delete_term_meta( $term_id, self::META_KEY ); |
|
| 161 | + foreach ( array_unique( array_filter( $merged ) ) as $single ) { |
|
| 162 | + add_term_meta( $term_id, self::META_KEY, $single ); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | 167 | } |
@@ -30,8 +30,8 @@ discard block |
||
| 30 | 30 | */ |
| 31 | 31 | public function __construct() { |
| 32 | 32 | |
| 33 | - add_action( 'registered_taxonomy', array( $this, 'add_action', ) ); |
|
| 34 | - add_action( 'edit_term', array( $this, 'edit_term', ), 10, 3 ); |
|
| 33 | + add_action('registered_taxonomy', array($this, 'add_action',)); |
|
| 34 | + add_action('edit_term', array($this, 'edit_term',), 10, 3); |
|
| 35 | 35 | $this->add_settings(); |
| 36 | 36 | |
| 37 | 37 | } |
@@ -42,7 +42,7 @@ discard block |
||
| 42 | 42 | * @since 3.26.1 |
| 43 | 43 | */ |
| 44 | 44 | public function add_settings() { |
| 45 | - add_filter( 'wl_admin_settings', function ( $params ) { |
|
| 45 | + add_filter('wl_admin_settings', function($params) { |
|
| 46 | 46 | $params['show_local_entities'] = true; |
| 47 | 47 | return $params; |
| 48 | 48 | } ); |
@@ -57,45 +57,45 @@ discard block |
||
| 57 | 57 | * @since 3.20.0 |
| 58 | 58 | * |
| 59 | 59 | */ |
| 60 | - public function edit_form_fields( $tag, $taxonomy ) { |
|
| 60 | + public function edit_form_fields($tag, $taxonomy) { |
|
| 61 | 61 | |
| 62 | 62 | // If disabled via filter, return; |
| 63 | - if ( ! apply_filters( 'wl_feature__enable__term-entity', true ) ) { |
|
| 63 | + if ( ! apply_filters('wl_feature__enable__term-entity', true)) { |
|
| 64 | 64 | return; |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | global $wp_version; |
| 68 | 68 | |
| 69 | 69 | // Enqueue the JavaScript app. |
| 70 | - if ( version_compare( $wp_version, '5.0', '>=' ) ) { |
|
| 71 | - $term_asset = include plugin_dir_path( dirname( __FILE__ ) ) . 'js/dist/term.asset.php'; |
|
| 72 | - wp_enqueue_script( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.js', array_merge( array( 'wp-util' ), $term_asset['dependencies'] ), Wordlift::get_instance()->get_version(), true ); |
|
| 70 | + if (version_compare($wp_version, '5.0', '>=')) { |
|
| 71 | + $term_asset = include plugin_dir_path(dirname(__FILE__)).'js/dist/term.asset.php'; |
|
| 72 | + wp_enqueue_script('wl-term', plugin_dir_url(dirname(__FILE__)).'js/dist/term.js', array_merge(array('wp-util'), $term_asset['dependencies']), Wordlift::get_instance()->get_version(), true); |
|
| 73 | 73 | } else { |
| 74 | - wp_enqueue_script( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.full.js', array( 'wp-util' ), Wordlift::get_instance()->get_version(), true ); |
|
| 74 | + wp_enqueue_script('wl-term', plugin_dir_url(dirname(__FILE__)).'js/dist/term.full.js', array('wp-util'), Wordlift::get_instance()->get_version(), true); |
|
| 75 | 75 | } |
| 76 | 76 | |
| 77 | - wp_enqueue_style( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.css', array(), Wordlift::get_instance()->get_version() ); |
|
| 77 | + wp_enqueue_style('wl-term', plugin_dir_url(dirname(__FILE__)).'js/dist/term.css', array(), Wordlift::get_instance()->get_version()); |
|
| 78 | 78 | |
| 79 | - $values = get_term_meta( $tag->term_id, self::META_KEY ); |
|
| 79 | + $values = get_term_meta($tag->term_id, self::META_KEY); |
|
| 80 | 80 | |
| 81 | 81 | /** |
| 82 | 82 | * @since 3.31.3 |
| 83 | 83 | * @see https://github.com/insideout10/wordlift-plugin/issues/1446 |
| 84 | 84 | * This field should be hidden by default |
| 85 | 85 | */ |
| 86 | - if ( ! $values ) { |
|
| 86 | + if ( ! $values) { |
|
| 87 | 87 | return; |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | ?> |
| 91 | 91 | <tr class="form-field term-name-wrap"> |
| 92 | - <th scope="row"><label for="wl-entity-id"><?php _ex( 'Entity', 'term entity', 'wordlift' ); ?></label></th> |
|
| 92 | + <th scope="row"><label for="wl-entity-id"><?php _ex('Entity', 'term entity', 'wordlift'); ?></label></th> |
|
| 93 | 93 | <td> |
| 94 | - <?php foreach ( $values as $value ) { ?> |
|
| 95 | - <input type="text" name="wl_entity_id[]" value="<?php echo esc_attr( $value ); ?>"/> |
|
| 94 | + <?php foreach ($values as $value) { ?> |
|
| 95 | + <input type="text" name="wl_entity_id[]" value="<?php echo esc_attr($value); ?>"/> |
|
| 96 | 96 | <?php } ?> |
| 97 | 97 | <div id="wl-term-entity-id"></div> |
| 98 | - <p class="description"><?php _e( 'The entity bound to the term.', 'wordlift' ); ?></p> |
|
| 98 | + <p class="description"><?php _e('The entity bound to the term.', 'wordlift'); ?></p> |
|
| 99 | 99 | </td> |
| 100 | 100 | </tr> |
| 101 | 101 | <?php |
@@ -108,12 +108,12 @@ discard block |
||
| 108 | 108 | * |
| 109 | 109 | * @param string $taxonomy The taxonomy name. |
| 110 | 110 | */ |
| 111 | - public function add_action( $taxonomy ) { |
|
| 111 | + public function add_action($taxonomy) { |
|
| 112 | 112 | /** |
| 113 | 113 | * Filter wl_feature__enable__taxonomy_term_entity_mapping renamed to wl_feature__enable__term-entity. |
| 114 | 114 | **/ |
| 115 | 115 | |
| 116 | - add_action( "{$taxonomy}_edit_form_fields", array( $this, 'edit_form_fields' ), 10, 2 ); |
|
| 116 | + add_action("{$taxonomy}_edit_form_fields", array($this, 'edit_form_fields'), 10, 2); |
|
| 117 | 117 | |
| 118 | 118 | } |
| 119 | 119 | |
@@ -126,16 +126,16 @@ discard block |
||
| 126 | 126 | * @param int $tt_id The term taxonomy id. |
| 127 | 127 | * @param string $taxonomy The taxonomy. |
| 128 | 128 | */ |
| 129 | - public function edit_term( $term_id, $tt_id, $taxonomy ) { |
|
| 129 | + public function edit_term($term_id, $tt_id, $taxonomy) { |
|
| 130 | 130 | |
| 131 | 131 | // Bail if the action isn't related to the term currently being edited. |
| 132 | - if ( ! isset( $_POST['tag_ID'] ) || $term_id !== (int) $_POST['tag_ID'] ) { |
|
| 132 | + if ( ! isset($_POST['tag_ID']) || $term_id !== (int) $_POST['tag_ID']) { |
|
| 133 | 133 | return; |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | // Delete. |
| 137 | - if ( ! isset( $_POST['wl_entity_id'] ) || ! is_array( $_POST['wl_entity_id'] ) || empty( $_POST['wl_entity_id'] ) ) { |
|
| 138 | - delete_term_meta( $term_id, self::META_KEY ); |
|
| 137 | + if ( ! isset($_POST['wl_entity_id']) || ! is_array($_POST['wl_entity_id']) || empty($_POST['wl_entity_id'])) { |
|
| 138 | + delete_term_meta($term_id, self::META_KEY); |
|
| 139 | 139 | |
| 140 | 140 | return; |
| 141 | 141 | } |
@@ -145,21 +145,21 @@ discard block |
||
| 145 | 145 | // Only use mb_* functions when mbstring is available. |
| 146 | 146 | // |
| 147 | 147 | // See https://github.com/insideout10/wordlift-plugin/issues/693. |
| 148 | - if ( extension_loaded( 'mbstring' ) ) { |
|
| 149 | - mb_regex_encoding( 'UTF-8' ); |
|
| 148 | + if (extension_loaded('mbstring')) { |
|
| 149 | + mb_regex_encoding('UTF-8'); |
|
| 150 | 150 | |
| 151 | - $merged = array_reduce( (array) $_POST['wl_entity_id'], function ( $carry, $item ) { |
|
| 152 | - return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) ); |
|
| 153 | - }, array() ); |
|
| 151 | + $merged = array_reduce((array) $_POST['wl_entity_id'], function($carry, $item) { |
|
| 152 | + return array_merge($carry, mb_split("\x{2063}", wp_unslash($item))); |
|
| 153 | + }, array()); |
|
| 154 | 154 | } else { |
| 155 | - $merged = array_reduce( (array) $_POST['wl_entity_id'], function ( $carry, $item ) { |
|
| 156 | - return array_merge( $carry, preg_split( "/\x{2063}/u", wp_unslash( $item ) ) ); |
|
| 157 | - }, array() ); |
|
| 155 | + $merged = array_reduce((array) $_POST['wl_entity_id'], function($carry, $item) { |
|
| 156 | + return array_merge($carry, preg_split("/\x{2063}/u", wp_unslash($item))); |
|
| 157 | + }, array()); |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | - delete_term_meta( $term_id, self::META_KEY ); |
|
| 161 | - foreach ( array_unique( array_filter( $merged ) ) as $single ) { |
|
| 162 | - add_term_meta( $term_id, self::META_KEY, $single ); |
|
| 160 | + delete_term_meta($term_id, self::META_KEY); |
|
| 161 | + foreach (array_unique(array_filter($merged)) as $single) { |
|
| 162 | + add_term_meta($term_id, self::META_KEY, $single); |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | } |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | - exit; |
|
| 16 | + exit; |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | /** |
@@ -30,217 +30,217 @@ discard block |
||
| 30 | 30 | */ |
| 31 | 31 | class Wordlift_Admin_Entity_Type_Settings { |
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * A {@link Wordlift_Log_Service} instance. |
|
| 35 | - * |
|
| 36 | - * @since 3.14.0 |
|
| 37 | - * @access private |
|
| 38 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 39 | - */ |
|
| 40 | - private $log; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Create a {@link Wordlift_Admin_Entity_Type_Settings} instance. |
|
| 44 | - * |
|
| 45 | - * @since 3.14.0 |
|
| 46 | - */ |
|
| 47 | - public function __construct() { |
|
| 48 | - |
|
| 49 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Admin_Entity_Type_Settings' ); |
|
| 50 | - |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Handle menu registration. |
|
| 55 | - * |
|
| 56 | - * The registration is required, although we do not want to actually to add |
|
| 57 | - * an item to the menu, in order to "whitelist" the access to the settings page in |
|
| 58 | - * the admin. |
|
| 59 | - * |
|
| 60 | - * @since 3.11.0 |
|
| 61 | - */ |
|
| 62 | - public function admin_menu() { |
|
| 63 | - |
|
| 64 | - /* |
|
| 33 | + /** |
|
| 34 | + * A {@link Wordlift_Log_Service} instance. |
|
| 35 | + * |
|
| 36 | + * @since 3.14.0 |
|
| 37 | + * @access private |
|
| 38 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 39 | + */ |
|
| 40 | + private $log; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Create a {@link Wordlift_Admin_Entity_Type_Settings} instance. |
|
| 44 | + * |
|
| 45 | + * @since 3.14.0 |
|
| 46 | + */ |
|
| 47 | + public function __construct() { |
|
| 48 | + |
|
| 49 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Admin_Entity_Type_Settings' ); |
|
| 50 | + |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Handle menu registration. |
|
| 55 | + * |
|
| 56 | + * The registration is required, although we do not want to actually to add |
|
| 57 | + * an item to the menu, in order to "whitelist" the access to the settings page in |
|
| 58 | + * the admin. |
|
| 59 | + * |
|
| 60 | + * @since 3.11.0 |
|
| 61 | + */ |
|
| 62 | + public function admin_menu() { |
|
| 63 | + |
|
| 64 | + /* |
|
| 65 | 65 | * Before anything else check if an settings form was submitted. |
| 66 | 66 | * This has to be done before any output happens in order to be able to |
| 67 | 67 | * display proper "die" error messages and redirect. |
| 68 | 68 | */ |
| 69 | - if ( isset( $_GET['page'] ) && ( 'wl_entity_type_settings' === $_GET['page'] ) ) { |
|
| 70 | - |
|
| 71 | - // Validate inputs. Do not return on invalid parameters or capabilities. |
|
| 72 | - $this->validate_proper_term(); |
|
| 73 | - |
|
| 74 | - // If proper form submission, handle it and redirect back to the settings page. |
|
| 75 | - if ( isset( $_POST['action'] ) && ( 'wl_edit_entity_type_term' === $_POST['action'] ) ) { |
|
| 76 | - $this->handle_form_submission(); |
|
| 77 | - } |
|
| 78 | - /** |
|
| 79 | - * Filter: wl_feature__enable__notices. |
|
| 80 | - * |
|
| 81 | - * @param bool whether the notices needs to be enabled or not. |
|
| 82 | - * |
|
| 83 | - * @return bool |
|
| 84 | - * @since 3.27.6 |
|
| 85 | - */ |
|
| 86 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 87 | - // Register admin notices handler. |
|
| 88 | - add_action( 'admin_notices', array( $this, 'admin_notice' ) ); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /* |
|
| 69 | + if ( isset( $_GET['page'] ) && ( 'wl_entity_type_settings' === $_GET['page'] ) ) { |
|
| 70 | + |
|
| 71 | + // Validate inputs. Do not return on invalid parameters or capabilities. |
|
| 72 | + $this->validate_proper_term(); |
|
| 73 | + |
|
| 74 | + // If proper form submission, handle it and redirect back to the settings page. |
|
| 75 | + if ( isset( $_POST['action'] ) && ( 'wl_edit_entity_type_term' === $_POST['action'] ) ) { |
|
| 76 | + $this->handle_form_submission(); |
|
| 77 | + } |
|
| 78 | + /** |
|
| 79 | + * Filter: wl_feature__enable__notices. |
|
| 80 | + * |
|
| 81 | + * @param bool whether the notices needs to be enabled or not. |
|
| 82 | + * |
|
| 83 | + * @return bool |
|
| 84 | + * @since 3.27.6 |
|
| 85 | + */ |
|
| 86 | + if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 87 | + // Register admin notices handler. |
|
| 88 | + add_action( 'admin_notices', array( $this, 'admin_notice' ) ); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /* |
|
| 94 | 94 | * Use a null parent slug to prevent the menu from actually appearing |
| 95 | 95 | * in the admin menu. |
| 96 | 96 | */ |
| 97 | - // @todo: use the new {@link Wordlift_Admin_Page}. |
|
| 98 | - add_submenu_page( |
|
| 99 | - null, |
|
| 100 | - __( 'Edit Entity term', 'wordlift' ), |
|
| 101 | - __( 'Edit Entity term', 'wordlift' ), |
|
| 102 | - 'manage_options', |
|
| 103 | - 'wl_entity_type_settings', |
|
| 104 | - array( $this, 'render' ) |
|
| 105 | - ); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Output admin notices if needed, based on the message url parameter. |
|
| 110 | - * A value of 1 indicates that a successful save was done. |
|
| 111 | - * |
|
| 112 | - * @since 3.11.0 |
|
| 113 | - */ |
|
| 114 | - function admin_notice() { |
|
| 115 | - if ( isset( $_GET['message'] ) && ( '1' === $_GET['message'] ) ) { |
|
| 116 | - ?> |
|
| 97 | + // @todo: use the new {@link Wordlift_Admin_Page}. |
|
| 98 | + add_submenu_page( |
|
| 99 | + null, |
|
| 100 | + __( 'Edit Entity term', 'wordlift' ), |
|
| 101 | + __( 'Edit Entity term', 'wordlift' ), |
|
| 102 | + 'manage_options', |
|
| 103 | + 'wl_entity_type_settings', |
|
| 104 | + array( $this, 'render' ) |
|
| 105 | + ); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Output admin notices if needed, based on the message url parameter. |
|
| 110 | + * A value of 1 indicates that a successful save was done. |
|
| 111 | + * |
|
| 112 | + * @since 3.11.0 |
|
| 113 | + */ |
|
| 114 | + function admin_notice() { |
|
| 115 | + if ( isset( $_GET['message'] ) && ( '1' === $_GET['message'] ) ) { |
|
| 116 | + ?> |
|
| 117 | 117 | <div class="notice notice-success is-dismissible"> |
| 118 | 118 | <p><?php esc_html_e( 'Settings saved', 'wordlift' ) ?></p> |
| 119 | 119 | </div> |
| 120 | 120 | <?php |
| 121 | - } |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Validate the existence of the entity type indicated by the tag_ID url |
|
| 126 | - * parameter before doing any processing. Done before any output to mimic |
|
| 127 | - * the way WordPress handles same situation with "normal" term editing screens. |
|
| 128 | - * |
|
| 129 | - * @since 3.11.0 |
|
| 130 | - */ |
|
| 131 | - function validate_proper_term() { |
|
| 132 | - |
|
| 133 | - // Validate capabilities. |
|
| 134 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
| 135 | - wp_die( |
|
| 136 | - '<h1>' . __( 'Cheatin’ uh?' ) . '</h1>' . |
|
| 137 | - '<p>' . __( 'Sorry, you are not allowed to edit this item.' ) . '</p>', |
|
| 138 | - 403 |
|
| 139 | - ); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - // Get the term id and the actual term. |
|
| 143 | - $term_id = (int) $_REQUEST['tag_ID']; |
|
| 144 | - |
|
| 145 | - if ( ! term_exists( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) ) { |
|
| 146 | - wp_die( __( 'You attempted to edit an entity type term that doesn’t exist.', 'wordlift' ) ); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Handle the form submission of the settings form. On successful |
|
| 153 | - * handling redirect tp the setting edit page. |
|
| 154 | - * |
|
| 155 | - * @since 3.11.0 |
|
| 156 | - */ |
|
| 157 | - function handle_form_submission() { |
|
| 158 | - |
|
| 159 | - $term_id = (int) $_POST['tag_ID']; |
|
| 160 | - |
|
| 161 | - // Check the nonce. |
|
| 162 | - check_admin_referer( 'update-entity_type_term_' . $term_id ); |
|
| 163 | - |
|
| 164 | - $term = get_term( $term_id, 'wl_entity_type' ); |
|
| 165 | - |
|
| 166 | - $this->set_setting( |
|
| 167 | - $term_id, |
|
| 168 | - trim( wp_unslash( (string) $_POST['title'] ) ), |
|
| 169 | - wp_unslash( (string) $_POST['description'] ) |
|
| 170 | - ); |
|
| 171 | - |
|
| 172 | - // Redirect back to the term settings page and indicate a save was done. |
|
| 173 | - $url = admin_url( "admin.php?page=wl_entity_type_settings&tag_ID=$term->term_id&message=1" ); |
|
| 174 | - |
|
| 175 | - wp_redirect( $url ); |
|
| 176 | - exit; |
|
| 177 | - |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Render the settings page for the term. |
|
| 182 | - * |
|
| 183 | - * Access and parameter validity is assumed to be done earlier. |
|
| 184 | - * |
|
| 185 | - * @since 3.11.0 |
|
| 186 | - */ |
|
| 187 | - function render() { |
|
| 188 | - |
|
| 189 | - // Set variables used by the partial |
|
| 190 | - $term_id = absint( $_REQUEST['tag_ID'] ); |
|
| 191 | - $settings = $this->get_setting( $term_id ); |
|
| 192 | - |
|
| 193 | - include plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/wordlift-admin-entity-type-settings.php'; |
|
| 194 | - |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Store the entity type term settings in the DB |
|
| 199 | - * |
|
| 200 | - * @param integer $term_id The ID of the entity type term |
|
| 201 | - * @param string $title The override for the terms title. |
|
| 202 | - * @param string $description The override for the terms description. |
|
| 203 | - * |
|
| 204 | - * @since 3.11.0 |
|
| 205 | - * |
|
| 206 | - */ |
|
| 207 | - function set_setting( $term_id, $title, $description ) { |
|
| 208 | - |
|
| 209 | - $settings = get_option( 'wl_entity_type_settings', array() ); |
|
| 210 | - $settings[ $term_id ] = array( |
|
| 211 | - 'title' => $title, |
|
| 212 | - 'description' => $description, |
|
| 213 | - ); |
|
| 214 | - update_option( 'wl_entity_type_settings', $settings ); |
|
| 215 | - |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Retrieve the entity type term settings from the DB |
|
| 220 | - * |
|
| 221 | - * @param integer $term_id The ID of the entity type term |
|
| 222 | - * |
|
| 223 | - * @return null|array { |
|
| 224 | - * null is returned when there are no settings otherwise |
|
| 225 | - * an array is returned with following fields |
|
| 226 | - * |
|
| 227 | - * @type string title The overriding title for the term |
|
| 228 | - * @type string description The overriding description for the term |
|
| 229 | - * } |
|
| 230 | - * @since 3.11.0 |
|
| 231 | - * |
|
| 232 | - */ |
|
| 233 | - function get_setting( $term_id ) { |
|
| 234 | - |
|
| 235 | - $settings = get_option( 'wl_entity_type_settings', array() ); |
|
| 236 | - |
|
| 237 | - if ( isset( $settings[ $term_id ] ) ) { |
|
| 238 | - return $settings[ $term_id ]; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $this->log->warn( "No settings found for term id $term_id." ); |
|
| 242 | - |
|
| 243 | - return null; |
|
| 244 | - } |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Validate the existence of the entity type indicated by the tag_ID url |
|
| 126 | + * parameter before doing any processing. Done before any output to mimic |
|
| 127 | + * the way WordPress handles same situation with "normal" term editing screens. |
|
| 128 | + * |
|
| 129 | + * @since 3.11.0 |
|
| 130 | + */ |
|
| 131 | + function validate_proper_term() { |
|
| 132 | + |
|
| 133 | + // Validate capabilities. |
|
| 134 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
| 135 | + wp_die( |
|
| 136 | + '<h1>' . __( 'Cheatin’ uh?' ) . '</h1>' . |
|
| 137 | + '<p>' . __( 'Sorry, you are not allowed to edit this item.' ) . '</p>', |
|
| 138 | + 403 |
|
| 139 | + ); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + // Get the term id and the actual term. |
|
| 143 | + $term_id = (int) $_REQUEST['tag_ID']; |
|
| 144 | + |
|
| 145 | + if ( ! term_exists( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) ) { |
|
| 146 | + wp_die( __( 'You attempted to edit an entity type term that doesn’t exist.', 'wordlift' ) ); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Handle the form submission of the settings form. On successful |
|
| 153 | + * handling redirect tp the setting edit page. |
|
| 154 | + * |
|
| 155 | + * @since 3.11.0 |
|
| 156 | + */ |
|
| 157 | + function handle_form_submission() { |
|
| 158 | + |
|
| 159 | + $term_id = (int) $_POST['tag_ID']; |
|
| 160 | + |
|
| 161 | + // Check the nonce. |
|
| 162 | + check_admin_referer( 'update-entity_type_term_' . $term_id ); |
|
| 163 | + |
|
| 164 | + $term = get_term( $term_id, 'wl_entity_type' ); |
|
| 165 | + |
|
| 166 | + $this->set_setting( |
|
| 167 | + $term_id, |
|
| 168 | + trim( wp_unslash( (string) $_POST['title'] ) ), |
|
| 169 | + wp_unslash( (string) $_POST['description'] ) |
|
| 170 | + ); |
|
| 171 | + |
|
| 172 | + // Redirect back to the term settings page and indicate a save was done. |
|
| 173 | + $url = admin_url( "admin.php?page=wl_entity_type_settings&tag_ID=$term->term_id&message=1" ); |
|
| 174 | + |
|
| 175 | + wp_redirect( $url ); |
|
| 176 | + exit; |
|
| 177 | + |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Render the settings page for the term. |
|
| 182 | + * |
|
| 183 | + * Access and parameter validity is assumed to be done earlier. |
|
| 184 | + * |
|
| 185 | + * @since 3.11.0 |
|
| 186 | + */ |
|
| 187 | + function render() { |
|
| 188 | + |
|
| 189 | + // Set variables used by the partial |
|
| 190 | + $term_id = absint( $_REQUEST['tag_ID'] ); |
|
| 191 | + $settings = $this->get_setting( $term_id ); |
|
| 192 | + |
|
| 193 | + include plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/wordlift-admin-entity-type-settings.php'; |
|
| 194 | + |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Store the entity type term settings in the DB |
|
| 199 | + * |
|
| 200 | + * @param integer $term_id The ID of the entity type term |
|
| 201 | + * @param string $title The override for the terms title. |
|
| 202 | + * @param string $description The override for the terms description. |
|
| 203 | + * |
|
| 204 | + * @since 3.11.0 |
|
| 205 | + * |
|
| 206 | + */ |
|
| 207 | + function set_setting( $term_id, $title, $description ) { |
|
| 208 | + |
|
| 209 | + $settings = get_option( 'wl_entity_type_settings', array() ); |
|
| 210 | + $settings[ $term_id ] = array( |
|
| 211 | + 'title' => $title, |
|
| 212 | + 'description' => $description, |
|
| 213 | + ); |
|
| 214 | + update_option( 'wl_entity_type_settings', $settings ); |
|
| 215 | + |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Retrieve the entity type term settings from the DB |
|
| 220 | + * |
|
| 221 | + * @param integer $term_id The ID of the entity type term |
|
| 222 | + * |
|
| 223 | + * @return null|array { |
|
| 224 | + * null is returned when there are no settings otherwise |
|
| 225 | + * an array is returned with following fields |
|
| 226 | + * |
|
| 227 | + * @type string title The overriding title for the term |
|
| 228 | + * @type string description The overriding description for the term |
|
| 229 | + * } |
|
| 230 | + * @since 3.11.0 |
|
| 231 | + * |
|
| 232 | + */ |
|
| 233 | + function get_setting( $term_id ) { |
|
| 234 | + |
|
| 235 | + $settings = get_option( 'wl_entity_type_settings', array() ); |
|
| 236 | + |
|
| 237 | + if ( isset( $settings[ $term_id ] ) ) { |
|
| 238 | + return $settings[ $term_id ]; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $this->log->warn( "No settings found for term id $term_id." ); |
|
| 242 | + |
|
| 243 | + return null; |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | 246 | } |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | * @since 3.11.0 |
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 15 | +if ( ! defined('ABSPATH')) { |
|
| 16 | 16 | exit; |
| 17 | 17 | } |
| 18 | 18 | |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | */ |
| 47 | 47 | public function __construct() { |
| 48 | 48 | |
| 49 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Admin_Entity_Type_Settings' ); |
|
| 49 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Admin_Entity_Type_Settings'); |
|
| 50 | 50 | |
| 51 | 51 | } |
| 52 | 52 | |
@@ -66,13 +66,13 @@ discard block |
||
| 66 | 66 | * This has to be done before any output happens in order to be able to |
| 67 | 67 | * display proper "die" error messages and redirect. |
| 68 | 68 | */ |
| 69 | - if ( isset( $_GET['page'] ) && ( 'wl_entity_type_settings' === $_GET['page'] ) ) { |
|
| 69 | + if (isset($_GET['page']) && ('wl_entity_type_settings' === $_GET['page'])) { |
|
| 70 | 70 | |
| 71 | 71 | // Validate inputs. Do not return on invalid parameters or capabilities. |
| 72 | 72 | $this->validate_proper_term(); |
| 73 | 73 | |
| 74 | 74 | // If proper form submission, handle it and redirect back to the settings page. |
| 75 | - if ( isset( $_POST['action'] ) && ( 'wl_edit_entity_type_term' === $_POST['action'] ) ) { |
|
| 75 | + if (isset($_POST['action']) && ('wl_edit_entity_type_term' === $_POST['action'])) { |
|
| 76 | 76 | $this->handle_form_submission(); |
| 77 | 77 | } |
| 78 | 78 | /** |
@@ -83,9 +83,9 @@ discard block |
||
| 83 | 83 | * @return bool |
| 84 | 84 | * @since 3.27.6 |
| 85 | 85 | */ |
| 86 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 86 | + if (apply_filters('wl_feature__enable__notices', true)) { |
|
| 87 | 87 | // Register admin notices handler. |
| 88 | - add_action( 'admin_notices', array( $this, 'admin_notice' ) ); |
|
| 88 | + add_action('admin_notices', array($this, 'admin_notice')); |
|
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | } |
@@ -97,11 +97,11 @@ discard block |
||
| 97 | 97 | // @todo: use the new {@link Wordlift_Admin_Page}. |
| 98 | 98 | add_submenu_page( |
| 99 | 99 | null, |
| 100 | - __( 'Edit Entity term', 'wordlift' ), |
|
| 101 | - __( 'Edit Entity term', 'wordlift' ), |
|
| 100 | + __('Edit Entity term', 'wordlift'), |
|
| 101 | + __('Edit Entity term', 'wordlift'), |
|
| 102 | 102 | 'manage_options', |
| 103 | 103 | 'wl_entity_type_settings', |
| 104 | - array( $this, 'render' ) |
|
| 104 | + array($this, 'render') |
|
| 105 | 105 | ); |
| 106 | 106 | } |
| 107 | 107 | |
@@ -112,10 +112,10 @@ discard block |
||
| 112 | 112 | * @since 3.11.0 |
| 113 | 113 | */ |
| 114 | 114 | function admin_notice() { |
| 115 | - if ( isset( $_GET['message'] ) && ( '1' === $_GET['message'] ) ) { |
|
| 115 | + if (isset($_GET['message']) && ('1' === $_GET['message'])) { |
|
| 116 | 116 | ?> |
| 117 | 117 | <div class="notice notice-success is-dismissible"> |
| 118 | - <p><?php esc_html_e( 'Settings saved', 'wordlift' ) ?></p> |
|
| 118 | + <p><?php esc_html_e('Settings saved', 'wordlift') ?></p> |
|
| 119 | 119 | </div> |
| 120 | 120 | <?php |
| 121 | 121 | } |
@@ -131,10 +131,10 @@ discard block |
||
| 131 | 131 | function validate_proper_term() { |
| 132 | 132 | |
| 133 | 133 | // Validate capabilities. |
| 134 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
| 134 | + if ( ! current_user_can('manage_options')) { |
|
| 135 | 135 | wp_die( |
| 136 | - '<h1>' . __( 'Cheatin’ uh?' ) . '</h1>' . |
|
| 137 | - '<p>' . __( 'Sorry, you are not allowed to edit this item.' ) . '</p>', |
|
| 136 | + '<h1>'.__('Cheatin’ uh?').'</h1>'. |
|
| 137 | + '<p>'.__('Sorry, you are not allowed to edit this item.').'</p>', |
|
| 138 | 138 | 403 |
| 139 | 139 | ); |
| 140 | 140 | } |
@@ -142,8 +142,8 @@ discard block |
||
| 142 | 142 | // Get the term id and the actual term. |
| 143 | 143 | $term_id = (int) $_REQUEST['tag_ID']; |
| 144 | 144 | |
| 145 | - if ( ! term_exists( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) ) { |
|
| 146 | - wp_die( __( 'You attempted to edit an entity type term that doesn’t exist.', 'wordlift' ) ); |
|
| 145 | + if ( ! term_exists($term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME)) { |
|
| 146 | + wp_die(__('You attempted to edit an entity type term that doesn’t exist.', 'wordlift')); |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | } |
@@ -159,20 +159,20 @@ discard block |
||
| 159 | 159 | $term_id = (int) $_POST['tag_ID']; |
| 160 | 160 | |
| 161 | 161 | // Check the nonce. |
| 162 | - check_admin_referer( 'update-entity_type_term_' . $term_id ); |
|
| 162 | + check_admin_referer('update-entity_type_term_'.$term_id); |
|
| 163 | 163 | |
| 164 | - $term = get_term( $term_id, 'wl_entity_type' ); |
|
| 164 | + $term = get_term($term_id, 'wl_entity_type'); |
|
| 165 | 165 | |
| 166 | 166 | $this->set_setting( |
| 167 | 167 | $term_id, |
| 168 | - trim( wp_unslash( (string) $_POST['title'] ) ), |
|
| 169 | - wp_unslash( (string) $_POST['description'] ) |
|
| 168 | + trim(wp_unslash((string) $_POST['title'])), |
|
| 169 | + wp_unslash((string) $_POST['description']) |
|
| 170 | 170 | ); |
| 171 | 171 | |
| 172 | 172 | // Redirect back to the term settings page and indicate a save was done. |
| 173 | - $url = admin_url( "admin.php?page=wl_entity_type_settings&tag_ID=$term->term_id&message=1" ); |
|
| 173 | + $url = admin_url("admin.php?page=wl_entity_type_settings&tag_ID=$term->term_id&message=1"); |
|
| 174 | 174 | |
| 175 | - wp_redirect( $url ); |
|
| 175 | + wp_redirect($url); |
|
| 176 | 176 | exit; |
| 177 | 177 | |
| 178 | 178 | } |
@@ -187,10 +187,10 @@ discard block |
||
| 187 | 187 | function render() { |
| 188 | 188 | |
| 189 | 189 | // Set variables used by the partial |
| 190 | - $term_id = absint( $_REQUEST['tag_ID'] ); |
|
| 191 | - $settings = $this->get_setting( $term_id ); |
|
| 190 | + $term_id = absint($_REQUEST['tag_ID']); |
|
| 191 | + $settings = $this->get_setting($term_id); |
|
| 192 | 192 | |
| 193 | - include plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/wordlift-admin-entity-type-settings.php'; |
|
| 193 | + include plugin_dir_path(dirname(__FILE__)).'admin/partials/wordlift-admin-entity-type-settings.php'; |
|
| 194 | 194 | |
| 195 | 195 | } |
| 196 | 196 | |
@@ -204,14 +204,14 @@ discard block |
||
| 204 | 204 | * @since 3.11.0 |
| 205 | 205 | * |
| 206 | 206 | */ |
| 207 | - function set_setting( $term_id, $title, $description ) { |
|
| 207 | + function set_setting($term_id, $title, $description) { |
|
| 208 | 208 | |
| 209 | - $settings = get_option( 'wl_entity_type_settings', array() ); |
|
| 210 | - $settings[ $term_id ] = array( |
|
| 209 | + $settings = get_option('wl_entity_type_settings', array()); |
|
| 210 | + $settings[$term_id] = array( |
|
| 211 | 211 | 'title' => $title, |
| 212 | 212 | 'description' => $description, |
| 213 | 213 | ); |
| 214 | - update_option( 'wl_entity_type_settings', $settings ); |
|
| 214 | + update_option('wl_entity_type_settings', $settings); |
|
| 215 | 215 | |
| 216 | 216 | } |
| 217 | 217 | |
@@ -230,15 +230,15 @@ discard block |
||
| 230 | 230 | * @since 3.11.0 |
| 231 | 231 | * |
| 232 | 232 | */ |
| 233 | - function get_setting( $term_id ) { |
|
| 233 | + function get_setting($term_id) { |
|
| 234 | 234 | |
| 235 | - $settings = get_option( 'wl_entity_type_settings', array() ); |
|
| 235 | + $settings = get_option('wl_entity_type_settings', array()); |
|
| 236 | 236 | |
| 237 | - if ( isset( $settings[ $term_id ] ) ) { |
|
| 238 | - return $settings[ $term_id ]; |
|
| 237 | + if (isset($settings[$term_id])) { |
|
| 238 | + return $settings[$term_id]; |
|
| 239 | 239 | } |
| 240 | 240 | |
| 241 | - $this->log->warn( "No settings found for term id $term_id." ); |
|
| 241 | + $this->log->warn("No settings found for term id $term_id."); |
|
| 242 | 242 | |
| 243 | 243 | return null; |
| 244 | 244 | } |
@@ -20,32 +20,32 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | function wl_shortcode_navigator_data() { |
| 22 | 22 | |
| 23 | - // Create the cache key. |
|
| 24 | - $cache_key_params = $_REQUEST; |
|
| 25 | - unset( $cache_key_params['uniqid'] ); |
|
| 26 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 23 | + // Create the cache key. |
|
| 24 | + $cache_key_params = $_REQUEST; |
|
| 25 | + unset( $cache_key_params['uniqid'] ); |
|
| 26 | + $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 27 | 27 | |
| 28 | - // Create the TTL cache and try to get the results. |
|
| 29 | - $cache = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 30 | - $cache_results = $cache->get( $cache_key ); |
|
| 28 | + // Create the TTL cache and try to get the results. |
|
| 29 | + $cache = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 30 | + $cache_results = $cache->get( $cache_key ); |
|
| 31 | 31 | |
| 32 | - // So that the endpoint can be used remotely |
|
| 33 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 32 | + // So that the endpoint can be used remotely |
|
| 33 | + header( 'Access-Control-Allow-Origin: *' ); |
|
| 34 | 34 | |
| 35 | - if ( isset( $cache_results ) ) { |
|
| 36 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 35 | + if ( isset( $cache_results ) ) { |
|
| 36 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 37 | 37 | |
| 38 | - return $cache_results; |
|
| 39 | - } |
|
| 38 | + return $cache_results; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 41 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 42 | 42 | |
| 43 | - $results = _wl_navigator_get_data(); |
|
| 43 | + $results = _wl_navigator_get_data(); |
|
| 44 | 44 | |
| 45 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 46 | - $cache->put( $cache_key, $results ); |
|
| 45 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 46 | + $cache->put( $cache_key, $results ); |
|
| 47 | 47 | |
| 48 | - return $results; |
|
| 48 | + return $results; |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | /** |
@@ -60,240 +60,240 @@ discard block |
||
| 60 | 60 | */ |
| 61 | 61 | function wl_network_navigator_wp_json( $request ) { |
| 62 | 62 | |
| 63 | - // Create the cache key. |
|
| 64 | - $cache_key_params = $_REQUEST; |
|
| 65 | - unset( $cache_key_params['uniqid'] ); |
|
| 66 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 63 | + // Create the cache key. |
|
| 64 | + $cache_key_params = $_REQUEST; |
|
| 65 | + unset( $cache_key_params['uniqid'] ); |
|
| 66 | + $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 67 | 67 | |
| 68 | - // Create the TTL cache and try to get the results. |
|
| 69 | - $cache = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 70 | - $cache_results = $cache->get( $cache_key ); |
|
| 68 | + // Create the TTL cache and try to get the results. |
|
| 69 | + $cache = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 70 | + $cache_results = $cache->get( $cache_key ); |
|
| 71 | 71 | |
| 72 | - if ( isset( $cache_results ) ) { |
|
| 73 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 72 | + if ( isset( $cache_results ) ) { |
|
| 73 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 74 | 74 | |
| 75 | - return $cache_results; |
|
| 76 | - } |
|
| 75 | + return $cache_results; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 78 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 79 | 79 | |
| 80 | - $results = _wl_network_navigator_get_data( $request ); |
|
| 80 | + $results = _wl_network_navigator_get_data( $request ); |
|
| 81 | 81 | |
| 82 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 83 | - $cache->put( $cache_key, $results ); |
|
| 82 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 83 | + $cache->put( $cache_key, $results ); |
|
| 84 | 84 | |
| 85 | - return $results; |
|
| 85 | + return $results; |
|
| 86 | 86 | |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | function _wl_navigator_get_data() { |
| 90 | 90 | |
| 91 | - // Post ID must be defined |
|
| 92 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 93 | - wp_send_json_error( 'No post_id given' ); |
|
| 94 | - |
|
| 95 | - return array(); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - // Post ID must be defined |
|
| 99 | - if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 100 | - wp_send_json_error( 'No uniqid given' ); |
|
| 101 | - |
|
| 102 | - return array(); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - // Limit the results (defaults to 4) |
|
| 106 | - $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 107 | - $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 108 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 109 | - $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 110 | - $post_types = explode( ',', $post_types ); |
|
| 111 | - $existing_post_types = get_post_types(); |
|
| 112 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 113 | - $current_post_id = (int) $_GET['post_id']; |
|
| 114 | - $current_post = get_post( $current_post_id ); |
|
| 115 | - |
|
| 116 | - $navigator_id = (string) $_GET['uniqid']; |
|
| 117 | - |
|
| 118 | - // Post ID has to match an existing item |
|
| 119 | - if ( null === $current_post ) { |
|
| 120 | - wp_send_json_error( 'No valid post_id given' ); |
|
| 121 | - |
|
| 122 | - return array(); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // Determine navigator type and call respective _get_results |
|
| 126 | - if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 127 | - |
|
| 128 | - $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array( |
|
| 129 | - 'ID', |
|
| 130 | - 'post_title', |
|
| 131 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 132 | - } else { |
|
| 133 | - $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array( |
|
| 134 | - 'ID', |
|
| 135 | - 'post_title', |
|
| 136 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 137 | - |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 141 | - $results = array(); |
|
| 142 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 143 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Use the thumbnail. |
|
| 147 | - * |
|
| 148 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 149 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 150 | - * |
|
| 151 | - * @since 3.19.3 We're using the medium size image. |
|
| 152 | - */ |
|
| 153 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 154 | - |
|
| 155 | - $result = array( |
|
| 156 | - 'post' => array( |
|
| 157 | - 'id' => $referencing_post->ID, |
|
| 158 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 159 | - 'title' => html_entity_decode( $referencing_post->post_title, ENT_QUOTES, 'UTF-8' ), |
|
| 160 | - 'thumbnail' => $thumbnail, |
|
| 161 | - 'srcset' => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 162 | - ), |
|
| 163 | - 'entity' => array( |
|
| 164 | - 'id' => $referencing_post->entity_id, |
|
| 165 | - 'label' => $serialized_entity['label'], |
|
| 166 | - 'mainType' => $serialized_entity['mainType'], |
|
| 167 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 168 | - ), |
|
| 169 | - ); |
|
| 170 | - |
|
| 171 | - $results[] = $result; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - |
|
| 175 | - if ( count( $results ) < $navigator_length ) { |
|
| 176 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - // Add filler posts if needed |
|
| 180 | - $filler_count = $navigator_length - count( $results ); |
|
| 181 | - if ( $filler_count > 0 ) { |
|
| 182 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 183 | - return $p->ID; |
|
| 184 | - }, $referencing_posts ); |
|
| 185 | - /** |
|
| 186 | - * @since 3.27.8 |
|
| 187 | - * Filler posts are fetched using this util. |
|
| 188 | - */ |
|
| 189 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id, $post_types ); |
|
| 190 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 191 | - $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 192 | - $results = array_merge( $results, $filler_posts ); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - // Apply filters after fillers are added |
|
| 196 | - foreach ( $results as $result_index => $result ) { |
|
| 197 | - $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 198 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - $results = apply_filters( 'wl_navigator_results', $results, $navigator_id ); |
|
| 202 | - |
|
| 203 | - return $results; |
|
| 91 | + // Post ID must be defined |
|
| 92 | + if ( ! isset( $_GET['post_id'] ) ) { |
|
| 93 | + wp_send_json_error( 'No post_id given' ); |
|
| 94 | + |
|
| 95 | + return array(); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + // Post ID must be defined |
|
| 99 | + if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 100 | + wp_send_json_error( 'No uniqid given' ); |
|
| 101 | + |
|
| 102 | + return array(); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + // Limit the results (defaults to 4) |
|
| 106 | + $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 107 | + $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 108 | + $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 109 | + $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 110 | + $post_types = explode( ',', $post_types ); |
|
| 111 | + $existing_post_types = get_post_types(); |
|
| 112 | + $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 113 | + $current_post_id = (int) $_GET['post_id']; |
|
| 114 | + $current_post = get_post( $current_post_id ); |
|
| 115 | + |
|
| 116 | + $navigator_id = (string) $_GET['uniqid']; |
|
| 117 | + |
|
| 118 | + // Post ID has to match an existing item |
|
| 119 | + if ( null === $current_post ) { |
|
| 120 | + wp_send_json_error( 'No valid post_id given' ); |
|
| 121 | + |
|
| 122 | + return array(); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // Determine navigator type and call respective _get_results |
|
| 126 | + if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 127 | + |
|
| 128 | + $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array( |
|
| 129 | + 'ID', |
|
| 130 | + 'post_title', |
|
| 131 | + ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 132 | + } else { |
|
| 133 | + $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array( |
|
| 134 | + 'ID', |
|
| 135 | + 'post_title', |
|
| 136 | + ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 137 | + |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 141 | + $results = array(); |
|
| 142 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 143 | + $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Use the thumbnail. |
|
| 147 | + * |
|
| 148 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 149 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 150 | + * |
|
| 151 | + * @since 3.19.3 We're using the medium size image. |
|
| 152 | + */ |
|
| 153 | + $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 154 | + |
|
| 155 | + $result = array( |
|
| 156 | + 'post' => array( |
|
| 157 | + 'id' => $referencing_post->ID, |
|
| 158 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 159 | + 'title' => html_entity_decode( $referencing_post->post_title, ENT_QUOTES, 'UTF-8' ), |
|
| 160 | + 'thumbnail' => $thumbnail, |
|
| 161 | + 'srcset' => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 162 | + ), |
|
| 163 | + 'entity' => array( |
|
| 164 | + 'id' => $referencing_post->entity_id, |
|
| 165 | + 'label' => $serialized_entity['label'], |
|
| 166 | + 'mainType' => $serialized_entity['mainType'], |
|
| 167 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 168 | + ), |
|
| 169 | + ); |
|
| 170 | + |
|
| 171 | + $results[] = $result; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + |
|
| 175 | + if ( count( $results ) < $navigator_length ) { |
|
| 176 | + $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + // Add filler posts if needed |
|
| 180 | + $filler_count = $navigator_length - count( $results ); |
|
| 181 | + if ( $filler_count > 0 ) { |
|
| 182 | + $referencing_post_ids = array_map( function ( $p ) { |
|
| 183 | + return $p->ID; |
|
| 184 | + }, $referencing_posts ); |
|
| 185 | + /** |
|
| 186 | + * @since 3.27.8 |
|
| 187 | + * Filler posts are fetched using this util. |
|
| 188 | + */ |
|
| 189 | + $filler_posts_util = new Filler_Posts_Util( $current_post_id, $post_types ); |
|
| 190 | + $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 191 | + $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 192 | + $results = array_merge( $results, $filler_posts ); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + // Apply filters after fillers are added |
|
| 196 | + foreach ( $results as $result_index => $result ) { |
|
| 197 | + $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 198 | + $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + $results = apply_filters( 'wl_navigator_results', $results, $navigator_id ); |
|
| 202 | + |
|
| 203 | + return $results; |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | function _wl_network_navigator_get_data( $request ) { |
| 207 | 207 | |
| 208 | - // Limit the results (defaults to 4) |
|
| 209 | - $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 210 | - $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 211 | - $navigator_id = $request['uniqid']; |
|
| 212 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 213 | - |
|
| 214 | - $entities = $request['entities']; |
|
| 215 | - |
|
| 216 | - // Post ID has to match an existing item |
|
| 217 | - if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 218 | - wp_send_json_error( 'No valid entities provided' ); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 222 | - 'ID', |
|
| 223 | - 'post_title', |
|
| 224 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 225 | - |
|
| 226 | - // loop over them and take the first one which is not already in the $related_posts |
|
| 227 | - $results = array(); |
|
| 228 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 229 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Use the thumbnail. |
|
| 233 | - * |
|
| 234 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 235 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 236 | - * |
|
| 237 | - * @since 3.19.3 We're using the medium size image. |
|
| 238 | - */ |
|
| 239 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 240 | - |
|
| 241 | - $result = array( |
|
| 242 | - 'post' => array( |
|
| 243 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 244 | - 'title' => $referencing_post->post_title, |
|
| 245 | - 'thumbnail' => $thumbnail, |
|
| 246 | - ), |
|
| 247 | - 'entity' => array( |
|
| 248 | - 'label' => $serialized_entity['label'], |
|
| 249 | - 'mainType' => $serialized_entity['mainType'], |
|
| 250 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 251 | - ), |
|
| 252 | - ); |
|
| 253 | - |
|
| 254 | - $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 255 | - $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 256 | - |
|
| 257 | - $results[] = $result; |
|
| 258 | - |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - if ( count( $results ) < $navigator_length ) { |
|
| 262 | - $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - $results = apply_filters( 'wl_network_navigator_results', $results, $navigator_id ); |
|
| 266 | - |
|
| 267 | - return $results; |
|
| 208 | + // Limit the results (defaults to 4) |
|
| 209 | + $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 210 | + $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 211 | + $navigator_id = $request['uniqid']; |
|
| 212 | + $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 213 | + |
|
| 214 | + $entities = $request['entities']; |
|
| 215 | + |
|
| 216 | + // Post ID has to match an existing item |
|
| 217 | + if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 218 | + wp_send_json_error( 'No valid entities provided' ); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 222 | + 'ID', |
|
| 223 | + 'post_title', |
|
| 224 | + ), $order_by, $navigator_length, $navigator_offset ); |
|
| 225 | + |
|
| 226 | + // loop over them and take the first one which is not already in the $related_posts |
|
| 227 | + $results = array(); |
|
| 228 | + foreach ( $referencing_posts as $referencing_post ) { |
|
| 229 | + $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Use the thumbnail. |
|
| 233 | + * |
|
| 234 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 235 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 236 | + * |
|
| 237 | + * @since 3.19.3 We're using the medium size image. |
|
| 238 | + */ |
|
| 239 | + $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 240 | + |
|
| 241 | + $result = array( |
|
| 242 | + 'post' => array( |
|
| 243 | + 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 244 | + 'title' => $referencing_post->post_title, |
|
| 245 | + 'thumbnail' => $thumbnail, |
|
| 246 | + ), |
|
| 247 | + 'entity' => array( |
|
| 248 | + 'label' => $serialized_entity['label'], |
|
| 249 | + 'mainType' => $serialized_entity['mainType'], |
|
| 250 | + 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 251 | + ), |
|
| 252 | + ); |
|
| 253 | + |
|
| 254 | + $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 255 | + $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 256 | + |
|
| 257 | + $results[] = $result; |
|
| 258 | + |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + if ( count( $results ) < $navigator_length ) { |
|
| 262 | + $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + $results = apply_filters( 'wl_network_navigator_results', $results, $navigator_id ); |
|
| 266 | + |
|
| 267 | + return $results; |
|
| 268 | 268 | |
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | function _wl_network_navigator_get_results( |
| 272 | - $entities, $fields = array( |
|
| 273 | - 'ID', |
|
| 274 | - 'post_title', |
|
| 272 | + $entities, $fields = array( |
|
| 273 | + 'ID', |
|
| 274 | + 'post_title', |
|
| 275 | 275 | ), $order_by = 'ID DESC', $limit = 10, $offset = 0 |
| 276 | 276 | ) { |
| 277 | - global $wpdb; |
|
| 278 | - |
|
| 279 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 280 | - return "p.$item AS $item"; |
|
| 281 | - }, (array) $fields ) ); |
|
| 282 | - |
|
| 283 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 284 | - return "p.$item"; |
|
| 285 | - }, (array) $order_by ) ); |
|
| 286 | - |
|
| 287 | - $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 288 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 289 | - if ( isset( $entity ) ) { |
|
| 290 | - return $entity->ID; |
|
| 291 | - } |
|
| 292 | - }, $entities ) ); |
|
| 293 | - |
|
| 294 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 295 | - return $wpdb->get_results( |
|
| 296 | - $wpdb->prepare( <<<EOF |
|
| 277 | + global $wpdb; |
|
| 278 | + |
|
| 279 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 280 | + return "p.$item AS $item"; |
|
| 281 | + }, (array) $fields ) ); |
|
| 282 | + |
|
| 283 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 284 | + return "p.$item"; |
|
| 285 | + }, (array) $order_by ) ); |
|
| 286 | + |
|
| 287 | + $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 288 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 289 | + if ( isset( $entity ) ) { |
|
| 290 | + return $entity->ID; |
|
| 291 | + } |
|
| 292 | + }, $entities ) ); |
|
| 293 | + |
|
| 294 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 295 | + return $wpdb->get_results( |
|
| 296 | + $wpdb->prepare( <<<EOF |
|
| 297 | 297 | SELECT %3\$s, p2.ID as entity_id |
| 298 | 298 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 299 | 299 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -319,8 +319,8 @@ discard block |
||
| 319 | 319 | LIMIT %1\$d |
| 320 | 320 | OFFSET %2\$d |
| 321 | 321 | EOF |
| 322 | - , $limit, $offset, $select, $order_by ) |
|
| 323 | - ); |
|
| 322 | + , $limit, $offset, $select, $order_by ) |
|
| 323 | + ); |
|
| 324 | 324 | |
| 325 | 325 | } |
| 326 | 326 | |
@@ -332,9 +332,9 @@ discard block |
||
| 332 | 332 | */ |
| 333 | 333 | function wl_shortcode_navigator_ajax() { |
| 334 | 334 | |
| 335 | - // Temporary blocking the Navigator. |
|
| 336 | - $results = wl_shortcode_navigator_data(); |
|
| 337 | - wl_core_send_json( $results ); |
|
| 335 | + // Temporary blocking the Navigator. |
|
| 336 | + $results = wl_shortcode_navigator_data(); |
|
| 337 | + wl_core_send_json( $results ); |
|
| 338 | 338 | |
| 339 | 339 | } |
| 340 | 340 | |
@@ -346,16 +346,16 @@ discard block |
||
| 346 | 346 | */ |
| 347 | 347 | function wl_shortcode_navigator_wp_json() { |
| 348 | 348 | |
| 349 | - $results = wl_shortcode_navigator_data(); |
|
| 350 | - if ( ob_get_contents() ) { |
|
| 351 | - ob_clean(); |
|
| 352 | - } |
|
| 349 | + $results = wl_shortcode_navigator_data(); |
|
| 350 | + if ( ob_get_contents() ) { |
|
| 351 | + ob_clean(); |
|
| 352 | + } |
|
| 353 | 353 | |
| 354 | - return array( |
|
| 355 | - 'items' => array( |
|
| 356 | - array( 'values' => $results ), |
|
| 357 | - ), |
|
| 358 | - ); |
|
| 354 | + return array( |
|
| 355 | + 'items' => array( |
|
| 356 | + array( 'values' => $results ), |
|
| 357 | + ), |
|
| 358 | + ); |
|
| 359 | 359 | |
| 360 | 360 | } |
| 361 | 361 | |
@@ -363,22 +363,22 @@ discard block |
||
| 363 | 363 | * Adding `rest_api_init` action for amp backend of navigator |
| 364 | 364 | */ |
| 365 | 365 | add_action( 'rest_api_init', function () { |
| 366 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 367 | - 'methods' => 'GET', |
|
| 368 | - 'permission_callback' => '__return_true', |
|
| 369 | - 'callback' => 'wl_shortcode_navigator_wp_json' |
|
| 370 | - ) ); |
|
| 366 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 367 | + 'methods' => 'GET', |
|
| 368 | + 'permission_callback' => '__return_true', |
|
| 369 | + 'callback' => 'wl_shortcode_navigator_wp_json' |
|
| 370 | + ) ); |
|
| 371 | 371 | } ); |
| 372 | 372 | |
| 373 | 373 | /** |
| 374 | 374 | * Adding `rest_api_init` action for backend of network navigator |
| 375 | 375 | */ |
| 376 | 376 | add_action( 'rest_api_init', function () { |
| 377 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 378 | - 'methods' => 'GET', |
|
| 379 | - 'callback' => 'wl_network_navigator_wp_json', |
|
| 380 | - 'permission_callback' => '__return_true', |
|
| 381 | - ) ); |
|
| 377 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 378 | + 'methods' => 'GET', |
|
| 379 | + 'callback' => 'wl_network_navigator_wp_json', |
|
| 380 | + 'permission_callback' => '__return_true', |
|
| 381 | + ) ); |
|
| 382 | 382 | } ); |
| 383 | 383 | |
| 384 | 384 | /** |
@@ -387,23 +387,23 @@ discard block |
||
| 387 | 387 | * @since 2.2.0 |
| 388 | 388 | */ |
| 389 | 389 | add_action( 'plugins_loaded', function () { |
| 390 | - $action = array_key_exists( 'action', $_REQUEST ) ? (string) $_REQUEST['action'] : ''; |
|
| 391 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 392 | - return; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 396 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 397 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 390 | + $action = array_key_exists( 'action', $_REQUEST ) ? (string) $_REQUEST['action'] : ''; |
|
| 391 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 392 | + return; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 396 | + remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 397 | + remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 398 | 398 | }, 0 ); |
| 399 | 399 | |
| 400 | 400 | add_action( 'init', function () { |
| 401 | - $action = array_key_exists( 'action', $_REQUEST ) ? (string) $_REQUEST['action'] : ''; |
|
| 402 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 403 | - return; |
|
| 404 | - } |
|
| 401 | + $action = array_key_exists( 'action', $_REQUEST ) ? (string) $_REQUEST['action'] : ''; |
|
| 402 | + if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 403 | + return; |
|
| 404 | + } |
|
| 405 | 405 | |
| 406 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 407 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 406 | + remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 407 | + remove_action( 'init', 'gglcptch_init' ); |
|
| 408 | 408 | }, 0 ); |
| 409 | 409 | |
@@ -22,28 +22,28 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | // Create the cache key. |
| 24 | 24 | $cache_key_params = $_REQUEST; |
| 25 | - unset( $cache_key_params['uniqid'] ); |
|
| 26 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 25 | + unset($cache_key_params['uniqid']); |
|
| 26 | + $cache_key = array('request_params' => $cache_key_params); |
|
| 27 | 27 | |
| 28 | 28 | // Create the TTL cache and try to get the results. |
| 29 | - $cache = new Ttl_Cache( "navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 30 | - $cache_results = $cache->get( $cache_key ); |
|
| 29 | + $cache = new Ttl_Cache("navigator", 8 * 60 * 60); // 8 hours. |
|
| 30 | + $cache_results = $cache->get($cache_key); |
|
| 31 | 31 | |
| 32 | 32 | // So that the endpoint can be used remotely |
| 33 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 33 | + header('Access-Control-Allow-Origin: *'); |
|
| 34 | 34 | |
| 35 | - if ( isset( $cache_results ) ) { |
|
| 36 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 35 | + if (isset($cache_results)) { |
|
| 36 | + header('X-WordLift-Cache: HIT'); |
|
| 37 | 37 | |
| 38 | 38 | return $cache_results; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 41 | + header('X-WordLift-Cache: MISS'); |
|
| 42 | 42 | |
| 43 | 43 | $results = _wl_navigator_get_data(); |
| 44 | 44 | |
| 45 | 45 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 46 | - $cache->put( $cache_key, $results ); |
|
| 46 | + $cache->put($cache_key, $results); |
|
| 47 | 47 | |
| 48 | 48 | return $results; |
| 49 | 49 | } |
@@ -58,29 +58,29 @@ discard block |
||
| 58 | 58 | * @since 3.22.6 |
| 59 | 59 | * |
| 60 | 60 | */ |
| 61 | -function wl_network_navigator_wp_json( $request ) { |
|
| 61 | +function wl_network_navigator_wp_json($request) { |
|
| 62 | 62 | |
| 63 | 63 | // Create the cache key. |
| 64 | 64 | $cache_key_params = $_REQUEST; |
| 65 | - unset( $cache_key_params['uniqid'] ); |
|
| 66 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 65 | + unset($cache_key_params['uniqid']); |
|
| 66 | + $cache_key = array('request_params' => $cache_key_params); |
|
| 67 | 67 | |
| 68 | 68 | // Create the TTL cache and try to get the results. |
| 69 | - $cache = new Ttl_Cache( "network-navigator", 8 * 60 * 60 ); // 8 hours. |
|
| 70 | - $cache_results = $cache->get( $cache_key ); |
|
| 69 | + $cache = new Ttl_Cache("network-navigator", 8 * 60 * 60); // 8 hours. |
|
| 70 | + $cache_results = $cache->get($cache_key); |
|
| 71 | 71 | |
| 72 | - if ( isset( $cache_results ) ) { |
|
| 73 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 72 | + if (isset($cache_results)) { |
|
| 73 | + header('X-WordLift-Cache: HIT'); |
|
| 74 | 74 | |
| 75 | 75 | return $cache_results; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 78 | + header('X-WordLift-Cache: MISS'); |
|
| 79 | 79 | |
| 80 | - $results = _wl_network_navigator_get_data( $request ); |
|
| 80 | + $results = _wl_network_navigator_get_data($request); |
|
| 81 | 81 | |
| 82 | 82 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 83 | - $cache->put( $cache_key, $results ); |
|
| 83 | + $cache->put($cache_key, $results); |
|
| 84 | 84 | |
| 85 | 85 | return $results; |
| 86 | 86 | |
@@ -89,58 +89,58 @@ discard block |
||
| 89 | 89 | function _wl_navigator_get_data() { |
| 90 | 90 | |
| 91 | 91 | // Post ID must be defined |
| 92 | - if ( ! isset( $_GET['post_id'] ) ) { |
|
| 93 | - wp_send_json_error( 'No post_id given' ); |
|
| 92 | + if ( ! isset($_GET['post_id'])) { |
|
| 93 | + wp_send_json_error('No post_id given'); |
|
| 94 | 94 | |
| 95 | 95 | return array(); |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | // Post ID must be defined |
| 99 | - if ( ! isset( $_GET['uniqid'] ) ) { |
|
| 100 | - wp_send_json_error( 'No uniqid given' ); |
|
| 99 | + if ( ! isset($_GET['uniqid'])) { |
|
| 100 | + wp_send_json_error('No uniqid given'); |
|
| 101 | 101 | |
| 102 | 102 | return array(); |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | // Limit the results (defaults to 4) |
| 106 | - $navigator_length = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : 4; |
|
| 107 | - $navigator_offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; |
|
| 108 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 109 | - $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 110 | - $post_types = explode( ',', $post_types ); |
|
| 106 | + $navigator_length = isset($_GET['limit']) ? intval($_GET['limit']) : 4; |
|
| 107 | + $navigator_offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0; |
|
| 108 | + $order_by = isset($_GET['sort']) ? sanitize_sql_orderby($_GET['sort']) : 'ID DESC'; |
|
| 109 | + $post_types = isset($_GET['post_types']) ? (string) $_GET['post_types'] : ''; |
|
| 110 | + $post_types = explode(',', $post_types); |
|
| 111 | 111 | $existing_post_types = get_post_types(); |
| 112 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 112 | + $post_types = array_values(array_intersect($existing_post_types, $post_types)); |
|
| 113 | 113 | $current_post_id = (int) $_GET['post_id']; |
| 114 | - $current_post = get_post( $current_post_id ); |
|
| 114 | + $current_post = get_post($current_post_id); |
|
| 115 | 115 | |
| 116 | 116 | $navigator_id = (string) $_GET['uniqid']; |
| 117 | 117 | |
| 118 | 118 | // Post ID has to match an existing item |
| 119 | - if ( null === $current_post ) { |
|
| 120 | - wp_send_json_error( 'No valid post_id given' ); |
|
| 119 | + if (null === $current_post) { |
|
| 120 | + wp_send_json_error('No valid post_id given'); |
|
| 121 | 121 | |
| 122 | 122 | return array(); |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // Determine navigator type and call respective _get_results |
| 126 | - if ( get_post_type( $current_post_id ) === Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 126 | + if (get_post_type($current_post_id) === Wordlift_Entity_Service::TYPE_NAME) { |
|
| 127 | 127 | |
| 128 | - $referencing_posts = Navigator_Data::entity_navigator_get_results( $current_post_id, array( |
|
| 128 | + $referencing_posts = Navigator_Data::entity_navigator_get_results($current_post_id, array( |
|
| 129 | 129 | 'ID', |
| 130 | 130 | 'post_title', |
| 131 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 131 | + ), $order_by, $navigator_length, $navigator_offset, $post_types); |
|
| 132 | 132 | } else { |
| 133 | - $referencing_posts = Navigator_Data::post_navigator_get_results( $current_post_id, array( |
|
| 133 | + $referencing_posts = Navigator_Data::post_navigator_get_results($current_post_id, array( |
|
| 134 | 134 | 'ID', |
| 135 | 135 | 'post_title', |
| 136 | - ), $order_by, $navigator_length, $navigator_offset, $post_types ); |
|
| 136 | + ), $order_by, $navigator_length, $navigator_offset, $post_types); |
|
| 137 | 137 | |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | // loop over them and take the first one which is not already in the $related_posts |
| 141 | 141 | $results = array(); |
| 142 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 143 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 142 | + foreach ($referencing_posts as $referencing_post) { |
|
| 143 | + $serialized_entity = wl_serialize_entity($referencing_post->entity_id); |
|
| 144 | 144 | |
| 145 | 145 | /** |
| 146 | 146 | * Use the thumbnail. |
@@ -150,21 +150,21 @@ discard block |
||
| 150 | 150 | * |
| 151 | 151 | * @since 3.19.3 We're using the medium size image. |
| 152 | 152 | */ |
| 153 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 153 | + $thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium'); |
|
| 154 | 154 | |
| 155 | 155 | $result = array( |
| 156 | 156 | 'post' => array( |
| 157 | 157 | 'id' => $referencing_post->ID, |
| 158 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 159 | - 'title' => html_entity_decode( $referencing_post->post_title, ENT_QUOTES, 'UTF-8' ), |
|
| 158 | + 'permalink' => get_permalink($referencing_post->ID), |
|
| 159 | + 'title' => html_entity_decode($referencing_post->post_title, ENT_QUOTES, 'UTF-8'), |
|
| 160 | 160 | 'thumbnail' => $thumbnail, |
| 161 | - 'srcset' => Srcset_Util::get_srcset( $referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET ) |
|
| 161 | + 'srcset' => Srcset_Util::get_srcset($referencing_post->ID, Srcset_Util::NAVIGATOR_WIDGET) |
|
| 162 | 162 | ), |
| 163 | 163 | 'entity' => array( |
| 164 | 164 | 'id' => $referencing_post->entity_id, |
| 165 | 165 | 'label' => $serialized_entity['label'], |
| 166 | 166 | 'mainType' => $serialized_entity['mainType'], |
| 167 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 167 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
| 168 | 168 | ), |
| 169 | 169 | ); |
| 170 | 170 | |
@@ -172,61 +172,61 @@ discard block |
||
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | |
| 175 | - if ( count( $results ) < $navigator_length ) { |
|
| 176 | - $results = apply_filters( 'wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 175 | + if (count($results) < $navigator_length) { |
|
| 176 | + $results = apply_filters('wl_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | // Add filler posts if needed |
| 180 | - $filler_count = $navigator_length - count( $results ); |
|
| 181 | - if ( $filler_count > 0 ) { |
|
| 182 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 180 | + $filler_count = $navigator_length - count($results); |
|
| 181 | + if ($filler_count > 0) { |
|
| 182 | + $referencing_post_ids = array_map(function($p) { |
|
| 183 | 183 | return $p->ID; |
| 184 | - }, $referencing_posts ); |
|
| 184 | + }, $referencing_posts); |
|
| 185 | 185 | /** |
| 186 | 186 | * @since 3.27.8 |
| 187 | 187 | * Filler posts are fetched using this util. |
| 188 | 188 | */ |
| 189 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id, $post_types ); |
|
| 190 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 191 | - $filler_posts = $filler_posts_util->get_filler_response( $filler_count, $post_ids_to_be_excluded ); |
|
| 192 | - $results = array_merge( $results, $filler_posts ); |
|
| 189 | + $filler_posts_util = new Filler_Posts_Util($current_post_id, $post_types); |
|
| 190 | + $post_ids_to_be_excluded = array_merge(array($current_post_id), $referencing_post_ids); |
|
| 191 | + $filler_posts = $filler_posts_util->get_filler_response($filler_count, $post_ids_to_be_excluded); |
|
| 192 | + $results = array_merge($results, $filler_posts); |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | // Apply filters after fillers are added |
| 196 | - foreach ( $results as $result_index => $result ) { |
|
| 197 | - $results[ $result_index ]['post'] = apply_filters( 'wl_navigator_data_post', $result['post'], intval( $result['post']['id'] ), $navigator_id ); |
|
| 198 | - $results[ $result_index ]['entity'] = apply_filters( 'wl_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id ); |
|
| 196 | + foreach ($results as $result_index => $result) { |
|
| 197 | + $results[$result_index]['post'] = apply_filters('wl_navigator_data_post', $result['post'], intval($result['post']['id']), $navigator_id); |
|
| 198 | + $results[$result_index]['entity'] = apply_filters('wl_navigator_data_entity', $result['entity'], intval($result['entity']['id']), $navigator_id); |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | - $results = apply_filters( 'wl_navigator_results', $results, $navigator_id ); |
|
| 201 | + $results = apply_filters('wl_navigator_results', $results, $navigator_id); |
|
| 202 | 202 | |
| 203 | 203 | return $results; |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | -function _wl_network_navigator_get_data( $request ) { |
|
| 206 | +function _wl_network_navigator_get_data($request) { |
|
| 207 | 207 | |
| 208 | 208 | // Limit the results (defaults to 4) |
| 209 | - $navigator_length = isset( $request['limit'] ) ? intval( $request['limit'] ) : 4; |
|
| 210 | - $navigator_offset = isset( $request['offset'] ) ? intval( $request['offset'] ) : 0; |
|
| 209 | + $navigator_length = isset($request['limit']) ? intval($request['limit']) : 4; |
|
| 210 | + $navigator_offset = isset($request['offset']) ? intval($request['offset']) : 0; |
|
| 211 | 211 | $navigator_id = $request['uniqid']; |
| 212 | - $order_by = isset( $_GET['sort'] ) ? sanitize_sql_orderby( $_GET['sort'] ) : 'ID DESC'; |
|
| 212 | + $order_by = isset($_GET['sort']) ? sanitize_sql_orderby($_GET['sort']) : 'ID DESC'; |
|
| 213 | 213 | |
| 214 | 214 | $entities = $request['entities']; |
| 215 | 215 | |
| 216 | 216 | // Post ID has to match an existing item |
| 217 | - if ( ! isset( $entities ) || empty( $entities ) ) { |
|
| 218 | - wp_send_json_error( 'No valid entities provided' ); |
|
| 217 | + if ( ! isset($entities) || empty($entities)) { |
|
| 218 | + wp_send_json_error('No valid entities provided'); |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | - $referencing_posts = _wl_network_navigator_get_results( $entities, array( |
|
| 221 | + $referencing_posts = _wl_network_navigator_get_results($entities, array( |
|
| 222 | 222 | 'ID', |
| 223 | 223 | 'post_title', |
| 224 | - ), $order_by, $navigator_length, $navigator_offset ); |
|
| 224 | + ), $order_by, $navigator_length, $navigator_offset); |
|
| 225 | 225 | |
| 226 | 226 | // loop over them and take the first one which is not already in the $related_posts |
| 227 | 227 | $results = array(); |
| 228 | - foreach ( $referencing_posts as $referencing_post ) { |
|
| 229 | - $serialized_entity = wl_serialize_entity( $referencing_post->entity_id ); |
|
| 228 | + foreach ($referencing_posts as $referencing_post) { |
|
| 229 | + $serialized_entity = wl_serialize_entity($referencing_post->entity_id); |
|
| 230 | 230 | |
| 231 | 231 | /** |
| 232 | 232 | * Use the thumbnail. |
@@ -236,33 +236,33 @@ discard block |
||
| 236 | 236 | * |
| 237 | 237 | * @since 3.19.3 We're using the medium size image. |
| 238 | 238 | */ |
| 239 | - $thumbnail = get_the_post_thumbnail_url( $referencing_post, 'medium' ); |
|
| 239 | + $thumbnail = get_the_post_thumbnail_url($referencing_post, 'medium'); |
|
| 240 | 240 | |
| 241 | 241 | $result = array( |
| 242 | 242 | 'post' => array( |
| 243 | - 'permalink' => get_permalink( $referencing_post->ID ), |
|
| 243 | + 'permalink' => get_permalink($referencing_post->ID), |
|
| 244 | 244 | 'title' => $referencing_post->post_title, |
| 245 | 245 | 'thumbnail' => $thumbnail, |
| 246 | 246 | ), |
| 247 | 247 | 'entity' => array( |
| 248 | 248 | 'label' => $serialized_entity['label'], |
| 249 | 249 | 'mainType' => $serialized_entity['mainType'], |
| 250 | - 'permalink' => get_permalink( $referencing_post->entity_id ), |
|
| 250 | + 'permalink' => get_permalink($referencing_post->entity_id), |
|
| 251 | 251 | ), |
| 252 | 252 | ); |
| 253 | 253 | |
| 254 | - $result['post'] = apply_filters( 'wl_network_navigator_data_post', $result['post'], intval( $referencing_post->ID ), $navigator_id ); |
|
| 255 | - $result['entity'] = apply_filters( 'wl_network_navigator_data_entity', $result['entity'], intval( $referencing_post->entity_id ), $navigator_id ); |
|
| 254 | + $result['post'] = apply_filters('wl_network_navigator_data_post', $result['post'], intval($referencing_post->ID), $navigator_id); |
|
| 255 | + $result['entity'] = apply_filters('wl_network_navigator_data_entity', $result['entity'], intval($referencing_post->entity_id), $navigator_id); |
|
| 256 | 256 | |
| 257 | 257 | $results[] = $result; |
| 258 | 258 | |
| 259 | 259 | } |
| 260 | 260 | |
| 261 | - if ( count( $results ) < $navigator_length ) { |
|
| 262 | - $results = apply_filters( 'wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length ); |
|
| 261 | + if (count($results) < $navigator_length) { |
|
| 262 | + $results = apply_filters('wl_network_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length); |
|
| 263 | 263 | } |
| 264 | 264 | |
| 265 | - $results = apply_filters( 'wl_network_navigator_results', $results, $navigator_id ); |
|
| 265 | + $results = apply_filters('wl_network_navigator_results', $results, $navigator_id); |
|
| 266 | 266 | |
| 267 | 267 | return $results; |
| 268 | 268 | |
@@ -276,24 +276,24 @@ discard block |
||
| 276 | 276 | ) { |
| 277 | 277 | global $wpdb; |
| 278 | 278 | |
| 279 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 279 | + $select = implode(', ', array_map(function($item) { |
|
| 280 | 280 | return "p.$item AS $item"; |
| 281 | - }, (array) $fields ) ); |
|
| 281 | + }, (array) $fields)); |
|
| 282 | 282 | |
| 283 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 283 | + $order_by = implode(', ', array_map(function($item) { |
|
| 284 | 284 | return "p.$item"; |
| 285 | - }, (array) $order_by ) ); |
|
| 285 | + }, (array) $order_by)); |
|
| 286 | 286 | |
| 287 | - $entities_in = implode( ',', array_map( function ( $item ) { |
|
| 288 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( urldecode( $item ) ); |
|
| 289 | - if ( isset( $entity ) ) { |
|
| 287 | + $entities_in = implode(',', array_map(function($item) { |
|
| 288 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri(urldecode($item)); |
|
| 289 | + if (isset($entity)) { |
|
| 290 | 290 | return $entity->ID; |
| 291 | 291 | } |
| 292 | - }, $entities ) ); |
|
| 292 | + }, $entities)); |
|
| 293 | 293 | |
| 294 | 294 | /** @noinspection SqlNoDataSourceInspection */ |
| 295 | 295 | return $wpdb->get_results( |
| 296 | - $wpdb->prepare( <<<EOF |
|
| 296 | + $wpdb->prepare(<<<EOF |
|
| 297 | 297 | SELECT %3\$s, p2.ID as entity_id |
| 298 | 298 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 299 | 299 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -319,7 +319,7 @@ discard block |
||
| 319 | 319 | LIMIT %1\$d |
| 320 | 320 | OFFSET %2\$d |
| 321 | 321 | EOF |
| 322 | - , $limit, $offset, $select, $order_by ) |
|
| 322 | + , $limit, $offset, $select, $order_by) |
|
| 323 | 323 | ); |
| 324 | 324 | |
| 325 | 325 | } |
@@ -334,12 +334,12 @@ discard block |
||
| 334 | 334 | |
| 335 | 335 | // Temporary blocking the Navigator. |
| 336 | 336 | $results = wl_shortcode_navigator_data(); |
| 337 | - wl_core_send_json( $results ); |
|
| 337 | + wl_core_send_json($results); |
|
| 338 | 338 | |
| 339 | 339 | } |
| 340 | 340 | |
| 341 | -add_action( 'wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 342 | -add_action( 'wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax' ); |
|
| 341 | +add_action('wp_ajax_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 342 | +add_action('wp_ajax_nopriv_wl_navigator', 'wl_shortcode_navigator_ajax'); |
|
| 343 | 343 | |
| 344 | 344 | /** |
| 345 | 345 | * wp-json call for the navigator widget |
@@ -347,13 +347,13 @@ discard block |
||
| 347 | 347 | function wl_shortcode_navigator_wp_json() { |
| 348 | 348 | |
| 349 | 349 | $results = wl_shortcode_navigator_data(); |
| 350 | - if ( ob_get_contents() ) { |
|
| 350 | + if (ob_get_contents()) { |
|
| 351 | 351 | ob_clean(); |
| 352 | 352 | } |
| 353 | 353 | |
| 354 | 354 | return array( |
| 355 | 355 | 'items' => array( |
| 356 | - array( 'values' => $results ), |
|
| 356 | + array('values' => $results), |
|
| 357 | 357 | ), |
| 358 | 358 | ); |
| 359 | 359 | |
@@ -362,23 +362,23 @@ discard block |
||
| 362 | 362 | /** |
| 363 | 363 | * Adding `rest_api_init` action for amp backend of navigator |
| 364 | 364 | */ |
| 365 | -add_action( 'rest_api_init', function () { |
|
| 366 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 365 | +add_action('rest_api_init', function() { |
|
| 366 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/navigator', array( |
|
| 367 | 367 | 'methods' => 'GET', |
| 368 | 368 | 'permission_callback' => '__return_true', |
| 369 | 369 | 'callback' => 'wl_shortcode_navigator_wp_json' |
| 370 | - ) ); |
|
| 370 | + )); |
|
| 371 | 371 | } ); |
| 372 | 372 | |
| 373 | 373 | /** |
| 374 | 374 | * Adding `rest_api_init` action for backend of network navigator |
| 375 | 375 | */ |
| 376 | -add_action( 'rest_api_init', function () { |
|
| 377 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 376 | +add_action('rest_api_init', function() { |
|
| 377 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/network-navigator', array( |
|
| 378 | 378 | 'methods' => 'GET', |
| 379 | 379 | 'callback' => 'wl_network_navigator_wp_json', |
| 380 | 380 | 'permission_callback' => '__return_true', |
| 381 | - ) ); |
|
| 381 | + )); |
|
| 382 | 382 | } ); |
| 383 | 383 | |
| 384 | 384 | /** |
@@ -386,24 +386,24 @@ discard block |
||
| 386 | 386 | * |
| 387 | 387 | * @since 2.2.0 |
| 388 | 388 | */ |
| 389 | -add_action( 'plugins_loaded', function () { |
|
| 390 | - $action = array_key_exists( 'action', $_REQUEST ) ? (string) $_REQUEST['action'] : ''; |
|
| 391 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 389 | +add_action('plugins_loaded', function() { |
|
| 390 | + $action = array_key_exists('action', $_REQUEST) ? (string) $_REQUEST['action'] : ''; |
|
| 391 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $action) { |
|
| 392 | 392 | return; |
| 393 | 393 | } |
| 394 | 394 | |
| 395 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 396 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 397 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 398 | -}, 0 ); |
|
| 395 | + remove_action('plugins_loaded', 'rocket_init'); |
|
| 396 | + remove_action('plugins_loaded', 'wpseo_premium_init', 14); |
|
| 397 | + remove_action('plugins_loaded', 'wpseo_init', 14); |
|
| 398 | +}, 0); |
|
| 399 | 399 | |
| 400 | -add_action( 'init', function () { |
|
| 401 | - $action = array_key_exists( 'action', $_REQUEST ) ? (string) $_REQUEST['action'] : ''; |
|
| 402 | - if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || 'wl_navigator' !== $action ) { |
|
| 400 | +add_action('init', function() { |
|
| 401 | + $action = array_key_exists('action', $_REQUEST) ? (string) $_REQUEST['action'] : ''; |
|
| 402 | + if ( ! defined('DOING_AJAX') || ! DOING_AJAX || 'wl_navigator' !== $action) { |
|
| 403 | 403 | return; |
| 404 | 404 | } |
| 405 | 405 | |
| 406 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 407 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 408 | -}, 0 ); |
|
| 406 | + remove_action('init', 'wp_widgets_init', 1); |
|
| 407 | + remove_action('init', 'gglcptch_init'); |
|
| 408 | +}, 0); |
|
| 409 | 409 | |
@@ -11,42 +11,42 @@ discard block |
||
| 11 | 11 | * Print both global or post related places in json. It's executed via Ajax |
| 12 | 12 | */ |
| 13 | 13 | function wl_shortcode_geomap_ajax() { |
| 14 | - // Get the post Id. |
|
| 15 | - $post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null ); |
|
| 14 | + // Get the post Id. |
|
| 15 | + $post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null ); |
|
| 16 | 16 | |
| 17 | - $map_data = ( is_numeric( $post_id ) |
|
| 18 | - ? wl_shortcode_geomap_ajax_single_post( (int) $post_id ) |
|
| 19 | - : wl_shortcode_geomap_ajax_all_posts() ); |
|
| 17 | + $map_data = ( is_numeric( $post_id ) |
|
| 18 | + ? wl_shortcode_geomap_ajax_single_post( (int) $post_id ) |
|
| 19 | + : wl_shortcode_geomap_ajax_all_posts() ); |
|
| 20 | 20 | |
| 21 | - wl_core_send_json( wl_shortcode_geomap_format_results( $map_data, $post_id ) ); |
|
| 21 | + wl_core_send_json( wl_shortcode_geomap_format_results( $map_data, $post_id ) ); |
|
| 22 | 22 | |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | function wl_shortcode_geomap_ajax_all_posts() { |
| 26 | - global $wpdb; |
|
| 27 | - |
|
| 28 | - return $wpdb->get_results( $wpdb->prepare( |
|
| 29 | - "SELECT DISTINCT p1.ID, p1.post_title, pm1.meta_value AS longitude, pm2.meta_value AS latitude" |
|
| 30 | - . " FROM {$wpdb->posts} p1 " |
|
| 31 | - . " INNER JOIN {$wpdb->prefix}wl_relation_instances ri" |
|
| 32 | - . " ON ri.object_id = p1.ID AND ri.predicate = %s" |
|
| 33 | - . " INNER JOIN {$wpdb->postmeta} pm1" |
|
| 34 | - . " ON pm1.post_id = p1.ID AND pm1.meta_key = %s AND '0' != pm1.meta_value" |
|
| 35 | - . " INNER JOIN {$wpdb->postmeta} pm2" |
|
| 36 | - . " ON pm2.post_id = p1.ID AND pm2.meta_key = %s AND '0' != pm2.meta_value" |
|
| 37 | - . " WHERE p1.post_status = %s", |
|
| 38 | - 'where', |
|
| 39 | - 'wl_geo_latitude', |
|
| 40 | - 'wl_geo_longitude', |
|
| 41 | - 'publish' |
|
| 42 | - ) ); |
|
| 26 | + global $wpdb; |
|
| 27 | + |
|
| 28 | + return $wpdb->get_results( $wpdb->prepare( |
|
| 29 | + "SELECT DISTINCT p1.ID, p1.post_title, pm1.meta_value AS longitude, pm2.meta_value AS latitude" |
|
| 30 | + . " FROM {$wpdb->posts} p1 " |
|
| 31 | + . " INNER JOIN {$wpdb->prefix}wl_relation_instances ri" |
|
| 32 | + . " ON ri.object_id = p1.ID AND ri.predicate = %s" |
|
| 33 | + . " INNER JOIN {$wpdb->postmeta} pm1" |
|
| 34 | + . " ON pm1.post_id = p1.ID AND pm1.meta_key = %s AND '0' != pm1.meta_value" |
|
| 35 | + . " INNER JOIN {$wpdb->postmeta} pm2" |
|
| 36 | + . " ON pm2.post_id = p1.ID AND pm2.meta_key = %s AND '0' != pm2.meta_value" |
|
| 37 | + . " WHERE p1.post_status = %s", |
|
| 38 | + 'where', |
|
| 39 | + 'wl_geo_latitude', |
|
| 40 | + 'wl_geo_longitude', |
|
| 41 | + 'publish' |
|
| 42 | + ) ); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | function wl_shortcode_geomap_ajax_single_post( $post_id ) { |
| 46 | - global $wpdb; |
|
| 46 | + global $wpdb; |
|
| 47 | 47 | |
| 48 | - return $wpdb->get_results( $wpdb->prepare( |
|
| 49 | - " |
|
| 48 | + return $wpdb->get_results( $wpdb->prepare( |
|
| 49 | + " |
|
| 50 | 50 | SELECT p2.ID, p2.post_title, pm1.meta_value AS longitude, pm2.meta_value AS latitude |
| 51 | 51 | FROM {$wpdb->prefix}wl_relation_instances ri |
| 52 | 52 | INNER JOIN {$wpdb->posts} p2 |
@@ -65,69 +65,69 @@ discard block |
||
| 65 | 65 | ON pm2.post_id = p.ID AND pm2.meta_key = %s AND '0' != pm2.meta_value |
| 66 | 66 | WHERE p.ID = %s |
| 67 | 67 | ", |
| 68 | - 'publish', |
|
| 69 | - 'wl_geo_latitude', |
|
| 70 | - 'wl_geo_longitude', |
|
| 71 | - $post_id, |
|
| 72 | - 'where', |
|
| 73 | - // UNION |
|
| 74 | - 'wl_geo_latitude', |
|
| 75 | - 'wl_geo_longitude', |
|
| 76 | - $post_id |
|
| 77 | - ) ); |
|
| 68 | + 'publish', |
|
| 69 | + 'wl_geo_latitude', |
|
| 70 | + 'wl_geo_longitude', |
|
| 71 | + $post_id, |
|
| 72 | + 'where', |
|
| 73 | + // UNION |
|
| 74 | + 'wl_geo_latitude', |
|
| 75 | + 'wl_geo_longitude', |
|
| 76 | + $post_id |
|
| 77 | + ) ); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | function wl_shortcode_geomap_get_subjects( $post_id, $exclude_post_id ) { |
| 81 | 81 | |
| 82 | - global $wpdb; |
|
| 83 | - |
|
| 84 | - return $wpdb->get_results( $wpdb->prepare( |
|
| 85 | - "SELECT p.ID, p.post_title" |
|
| 86 | - . " FROM {$wpdb->prefix}wl_relation_instances ri" . |
|
| 87 | - " INNER JOIN {$wpdb->posts} p" . |
|
| 88 | - " ON p.ID = ri.subject_id AND p.post_status = %s AND p.ID != %d" . |
|
| 89 | - " WHERE ri.object_id = %d AND ri.predicate = %s", |
|
| 90 | - 'publish', |
|
| 91 | - $exclude_post_id, |
|
| 92 | - $post_id, |
|
| 93 | - 'where' |
|
| 94 | - ) ); |
|
| 82 | + global $wpdb; |
|
| 83 | + |
|
| 84 | + return $wpdb->get_results( $wpdb->prepare( |
|
| 85 | + "SELECT p.ID, p.post_title" |
|
| 86 | + . " FROM {$wpdb->prefix}wl_relation_instances ri" . |
|
| 87 | + " INNER JOIN {$wpdb->posts} p" . |
|
| 88 | + " ON p.ID = ri.subject_id AND p.post_status = %s AND p.ID != %d" . |
|
| 89 | + " WHERE ri.object_id = %d AND ri.predicate = %s", |
|
| 90 | + 'publish', |
|
| 91 | + $exclude_post_id, |
|
| 92 | + $post_id, |
|
| 93 | + 'where' |
|
| 94 | + ) ); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | function wl_shortcode_geomap_format_results( $results, $post_id = null ) { |
| 98 | 98 | |
| 99 | - $boundaries = array(); |
|
| 100 | - $features = array_map( function ( $item ) use ( &$boundaries, $post_id ) { |
|
| 99 | + $boundaries = array(); |
|
| 100 | + $features = array_map( function ( $item ) use ( &$boundaries, $post_id ) { |
|
| 101 | 101 | |
| 102 | - $thumbnail_url = get_the_post_thumbnail_url( $item->ID ); |
|
| 103 | - $thumbnail_html = ( $thumbnail_url ? "<img src='$thumbnail_url' width='100%'>" : '' ); |
|
| 102 | + $thumbnail_url = get_the_post_thumbnail_url( $item->ID ); |
|
| 103 | + $thumbnail_html = ( $thumbnail_url ? "<img src='$thumbnail_url' width='100%'>" : '' ); |
|
| 104 | 104 | |
| 105 | - // Related posts. |
|
| 106 | - $subjects = wl_shortcode_geomap_get_subjects( $item->ID, $post_id ); |
|
| 107 | - $subjects_inner_html = array_reduce( $subjects, function ( $carry, $subject ) { |
|
| 105 | + // Related posts. |
|
| 106 | + $subjects = wl_shortcode_geomap_get_subjects( $item->ID, $post_id ); |
|
| 107 | + $subjects_inner_html = array_reduce( $subjects, function ( $carry, $subject ) { |
|
| 108 | 108 | |
| 109 | - $permalink = get_permalink( $subject->ID ); |
|
| 109 | + $permalink = get_permalink( $subject->ID ); |
|
| 110 | 110 | |
| 111 | - return $carry . sprintf( '<li><a href="%s">%s</a></li>', $permalink, esc_html( $subject->post_title ) ); |
|
| 112 | - }, '' ); |
|
| 113 | - $subjects_html = ( ! empty( $subjects_inner_html ) ? '<ul>' . $subjects_inner_html . '</ul>' : '' ); |
|
| 111 | + return $carry . sprintf( '<li><a href="%s">%s</a></li>', $permalink, esc_html( $subject->post_title ) ); |
|
| 112 | + }, '' ); |
|
| 113 | + $subjects_html = ( ! empty( $subjects_inner_html ) ? '<ul>' . $subjects_inner_html . '</ul>' : '' ); |
|
| 114 | 114 | |
| 115 | - $popup_content = sprintf( '<a href="%s"><h6>%s</h6>%s</a>%s', get_permalink( $item->ID ), $thumbnail_html, esc_html( $item->post_title ), $subjects_html ); |
|
| 116 | - $latitude = floatval( $item->latitude ); |
|
| 117 | - $longitude = floatval( $item->longitude ); |
|
| 118 | - $coordinates = array( $latitude, $longitude, ); |
|
| 119 | - $geometry = array( 'type' => 'Point', 'coordinates' => $coordinates, ); |
|
| 115 | + $popup_content = sprintf( '<a href="%s"><h6>%s</h6>%s</a>%s', get_permalink( $item->ID ), $thumbnail_html, esc_html( $item->post_title ), $subjects_html ); |
|
| 116 | + $latitude = floatval( $item->latitude ); |
|
| 117 | + $longitude = floatval( $item->longitude ); |
|
| 118 | + $coordinates = array( $latitude, $longitude, ); |
|
| 119 | + $geometry = array( 'type' => 'Point', 'coordinates' => $coordinates, ); |
|
| 120 | 120 | |
| 121 | - $boundaries[] = array( $longitude, $latitude, ); |
|
| 121 | + $boundaries[] = array( $longitude, $latitude, ); |
|
| 122 | 122 | |
| 123 | - return array( |
|
| 124 | - 'type' => 'Feature', |
|
| 125 | - 'properties' => array( 'popupContent' => $popup_content ), |
|
| 126 | - 'geometry' => $geometry, |
|
| 127 | - ); |
|
| 128 | - }, $results ); |
|
| 123 | + return array( |
|
| 124 | + 'type' => 'Feature', |
|
| 125 | + 'properties' => array( 'popupContent' => $popup_content ), |
|
| 126 | + 'geometry' => $geometry, |
|
| 127 | + ); |
|
| 128 | + }, $results ); |
|
| 129 | 129 | |
| 130 | - return array( 'features' => $features, 'boundaries' => $boundaries, ); |
|
| 130 | + return array( 'features' => $features, 'boundaries' => $boundaries, ); |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | add_action( 'wp_ajax_wl_geomap', 'wl_shortcode_geomap_ajax' ); |
@@ -12,20 +12,20 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | function wl_shortcode_geomap_ajax() { |
| 14 | 14 | // Get the post Id. |
| 15 | - $post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null ); |
|
| 15 | + $post_id = (isset($_REQUEST['post_id']) ? $_REQUEST['post_id'] : null); |
|
| 16 | 16 | |
| 17 | - $map_data = ( is_numeric( $post_id ) |
|
| 18 | - ? wl_shortcode_geomap_ajax_single_post( (int) $post_id ) |
|
| 19 | - : wl_shortcode_geomap_ajax_all_posts() ); |
|
| 17 | + $map_data = (is_numeric($post_id) |
|
| 18 | + ? wl_shortcode_geomap_ajax_single_post((int) $post_id) |
|
| 19 | + : wl_shortcode_geomap_ajax_all_posts()); |
|
| 20 | 20 | |
| 21 | - wl_core_send_json( wl_shortcode_geomap_format_results( $map_data, $post_id ) ); |
|
| 21 | + wl_core_send_json(wl_shortcode_geomap_format_results($map_data, $post_id)); |
|
| 22 | 22 | |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | function wl_shortcode_geomap_ajax_all_posts() { |
| 26 | 26 | global $wpdb; |
| 27 | 27 | |
| 28 | - return $wpdb->get_results( $wpdb->prepare( |
|
| 28 | + return $wpdb->get_results($wpdb->prepare( |
|
| 29 | 29 | "SELECT DISTINCT p1.ID, p1.post_title, pm1.meta_value AS longitude, pm2.meta_value AS latitude" |
| 30 | 30 | . " FROM {$wpdb->posts} p1 " |
| 31 | 31 | . " INNER JOIN {$wpdb->prefix}wl_relation_instances ri" |
@@ -39,13 +39,13 @@ discard block |
||
| 39 | 39 | 'wl_geo_latitude', |
| 40 | 40 | 'wl_geo_longitude', |
| 41 | 41 | 'publish' |
| 42 | - ) ); |
|
| 42 | + )); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | -function wl_shortcode_geomap_ajax_single_post( $post_id ) { |
|
| 45 | +function wl_shortcode_geomap_ajax_single_post($post_id) { |
|
| 46 | 46 | global $wpdb; |
| 47 | 47 | |
| 48 | - return $wpdb->get_results( $wpdb->prepare( |
|
| 48 | + return $wpdb->get_results($wpdb->prepare( |
|
| 49 | 49 | " |
| 50 | 50 | SELECT p2.ID, p2.post_title, pm1.meta_value AS longitude, pm2.meta_value AS latitude |
| 51 | 51 | FROM {$wpdb->prefix}wl_relation_instances ri |
@@ -74,61 +74,61 @@ discard block |
||
| 74 | 74 | 'wl_geo_latitude', |
| 75 | 75 | 'wl_geo_longitude', |
| 76 | 76 | $post_id |
| 77 | - ) ); |
|
| 77 | + )); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | -function wl_shortcode_geomap_get_subjects( $post_id, $exclude_post_id ) { |
|
| 80 | +function wl_shortcode_geomap_get_subjects($post_id, $exclude_post_id) { |
|
| 81 | 81 | |
| 82 | 82 | global $wpdb; |
| 83 | 83 | |
| 84 | - return $wpdb->get_results( $wpdb->prepare( |
|
| 84 | + return $wpdb->get_results($wpdb->prepare( |
|
| 85 | 85 | "SELECT p.ID, p.post_title" |
| 86 | - . " FROM {$wpdb->prefix}wl_relation_instances ri" . |
|
| 87 | - " INNER JOIN {$wpdb->posts} p" . |
|
| 88 | - " ON p.ID = ri.subject_id AND p.post_status = %s AND p.ID != %d" . |
|
| 86 | + . " FROM {$wpdb->prefix}wl_relation_instances ri". |
|
| 87 | + " INNER JOIN {$wpdb->posts} p". |
|
| 88 | + " ON p.ID = ri.subject_id AND p.post_status = %s AND p.ID != %d". |
|
| 89 | 89 | " WHERE ri.object_id = %d AND ri.predicate = %s", |
| 90 | 90 | 'publish', |
| 91 | 91 | $exclude_post_id, |
| 92 | 92 | $post_id, |
| 93 | 93 | 'where' |
| 94 | - ) ); |
|
| 94 | + )); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | -function wl_shortcode_geomap_format_results( $results, $post_id = null ) { |
|
| 97 | +function wl_shortcode_geomap_format_results($results, $post_id = null) { |
|
| 98 | 98 | |
| 99 | 99 | $boundaries = array(); |
| 100 | - $features = array_map( function ( $item ) use ( &$boundaries, $post_id ) { |
|
| 100 | + $features = array_map(function($item) use (&$boundaries, $post_id) { |
|
| 101 | 101 | |
| 102 | - $thumbnail_url = get_the_post_thumbnail_url( $item->ID ); |
|
| 103 | - $thumbnail_html = ( $thumbnail_url ? "<img src='$thumbnail_url' width='100%'>" : '' ); |
|
| 102 | + $thumbnail_url = get_the_post_thumbnail_url($item->ID); |
|
| 103 | + $thumbnail_html = ($thumbnail_url ? "<img src='$thumbnail_url' width='100%'>" : ''); |
|
| 104 | 104 | |
| 105 | 105 | // Related posts. |
| 106 | - $subjects = wl_shortcode_geomap_get_subjects( $item->ID, $post_id ); |
|
| 107 | - $subjects_inner_html = array_reduce( $subjects, function ( $carry, $subject ) { |
|
| 106 | + $subjects = wl_shortcode_geomap_get_subjects($item->ID, $post_id); |
|
| 107 | + $subjects_inner_html = array_reduce($subjects, function($carry, $subject) { |
|
| 108 | 108 | |
| 109 | - $permalink = get_permalink( $subject->ID ); |
|
| 109 | + $permalink = get_permalink($subject->ID); |
|
| 110 | 110 | |
| 111 | - return $carry . sprintf( '<li><a href="%s">%s</a></li>', $permalink, esc_html( $subject->post_title ) ); |
|
| 112 | - }, '' ); |
|
| 113 | - $subjects_html = ( ! empty( $subjects_inner_html ) ? '<ul>' . $subjects_inner_html . '</ul>' : '' ); |
|
| 111 | + return $carry.sprintf('<li><a href="%s">%s</a></li>', $permalink, esc_html($subject->post_title)); |
|
| 112 | + }, ''); |
|
| 113 | + $subjects_html = ( ! empty($subjects_inner_html) ? '<ul>'.$subjects_inner_html.'</ul>' : ''); |
|
| 114 | 114 | |
| 115 | - $popup_content = sprintf( '<a href="%s"><h6>%s</h6>%s</a>%s', get_permalink( $item->ID ), $thumbnail_html, esc_html( $item->post_title ), $subjects_html ); |
|
| 116 | - $latitude = floatval( $item->latitude ); |
|
| 117 | - $longitude = floatval( $item->longitude ); |
|
| 118 | - $coordinates = array( $latitude, $longitude, ); |
|
| 119 | - $geometry = array( 'type' => 'Point', 'coordinates' => $coordinates, ); |
|
| 115 | + $popup_content = sprintf('<a href="%s"><h6>%s</h6>%s</a>%s', get_permalink($item->ID), $thumbnail_html, esc_html($item->post_title), $subjects_html); |
|
| 116 | + $latitude = floatval($item->latitude); |
|
| 117 | + $longitude = floatval($item->longitude); |
|
| 118 | + $coordinates = array($latitude, $longitude,); |
|
| 119 | + $geometry = array('type' => 'Point', 'coordinates' => $coordinates,); |
|
| 120 | 120 | |
| 121 | - $boundaries[] = array( $longitude, $latitude, ); |
|
| 121 | + $boundaries[] = array($longitude, $latitude,); |
|
| 122 | 122 | |
| 123 | 123 | return array( |
| 124 | 124 | 'type' => 'Feature', |
| 125 | - 'properties' => array( 'popupContent' => $popup_content ), |
|
| 125 | + 'properties' => array('popupContent' => $popup_content), |
|
| 126 | 126 | 'geometry' => $geometry, |
| 127 | 127 | ); |
| 128 | - }, $results ); |
|
| 128 | + }, $results); |
|
| 129 | 129 | |
| 130 | - return array( 'features' => $features, 'boundaries' => $boundaries, ); |
|
| 130 | + return array('features' => $features, 'boundaries' => $boundaries,); |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | -add_action( 'wp_ajax_wl_geomap', 'wl_shortcode_geomap_ajax' ); |
|
| 134 | -add_action( 'wp_ajax_nopriv_wl_geomap', 'wl_shortcode_geomap_ajax' ); |
|
| 133 | +add_action('wp_ajax_wl_geomap', 'wl_shortcode_geomap_ajax'); |
|
| 134 | +add_action('wp_ajax_nopriv_wl_geomap', 'wl_shortcode_geomap_ajax'); |
|
@@ -10,116 +10,116 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | abstract class Wordlift_Shortcode_REST { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * The cache_ttl, set by extending classes. |
|
| 15 | - */ |
|
| 16 | - const CACHE_TTL = 86400; // 24 hours |
|
| 13 | + /** |
|
| 14 | + * The cache_ttl, set by extending classes. |
|
| 15 | + */ |
|
| 16 | + const CACHE_TTL = 86400; // 24 hours |
|
| 17 | 17 | |
| 18 | - public function __construct( $endpoint, $args ) { |
|
| 18 | + public function __construct( $endpoint, $args ) { |
|
| 19 | 19 | |
| 20 | - $scope = $this; |
|
| 21 | - $this->endpoint = $endpoint; |
|
| 22 | - $this->args = $args; |
|
| 20 | + $scope = $this; |
|
| 21 | + $this->endpoint = $endpoint; |
|
| 22 | + $this->args = $args; |
|
| 23 | 23 | |
| 24 | - // Register rest route with callback |
|
| 25 | - add_action( 'rest_api_init', function () use ( $scope ) { |
|
| 26 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, $scope->endpoint, array( |
|
| 27 | - 'methods' => WP_REST_Server::READABLE, |
|
| 28 | - 'permission_callback' => '__return_true', |
|
| 29 | - 'callback' => array( $scope, 'rest_callback' ), |
|
| 30 | - 'args' => $scope->args |
|
| 31 | - ) ); |
|
| 32 | - } ); |
|
| 24 | + // Register rest route with callback |
|
| 25 | + add_action( 'rest_api_init', function () use ( $scope ) { |
|
| 26 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, $scope->endpoint, array( |
|
| 27 | + 'methods' => WP_REST_Server::READABLE, |
|
| 28 | + 'permission_callback' => '__return_true', |
|
| 29 | + 'callback' => array( $scope, 'rest_callback' ), |
|
| 30 | + 'args' => $scope->args |
|
| 31 | + ) ); |
|
| 32 | + } ); |
|
| 33 | 33 | |
| 34 | - // Optimizations: disable unneeded plugins on this specific REST call. WPSeo is slowing down the responses quite a bit. |
|
| 35 | - add_action( 'plugins_loaded', function () use ( $scope ) { |
|
| 34 | + // Optimizations: disable unneeded plugins on this specific REST call. WPSeo is slowing down the responses quite a bit. |
|
| 35 | + add_action( 'plugins_loaded', function () use ( $scope ) { |
|
| 36 | 36 | |
| 37 | - if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) { |
|
| 38 | - return; |
|
| 39 | - } |
|
| 37 | + if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) { |
|
| 38 | + return; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 42 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 43 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 44 | - }, 0 ); |
|
| 41 | + remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 42 | + remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 43 | + remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 44 | + }, 0 ); |
|
| 45 | 45 | |
| 46 | - add_action( 'init', function () use ( $scope ) { |
|
| 46 | + add_action( 'init', function () use ( $scope ) { |
|
| 47 | 47 | |
| 48 | - if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) { |
|
| 49 | - return; |
|
| 50 | - } |
|
| 48 | + if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) { |
|
| 49 | + return; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 53 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 54 | - }, 0 ); |
|
| 52 | + remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 53 | + remove_action( 'init', 'gglcptch_init' ); |
|
| 54 | + }, 0 ); |
|
| 55 | 55 | |
| 56 | - } |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - public abstract function get_data( $request ); |
|
| 58 | + public abstract function get_data( $request ); |
|
| 59 | 59 | |
| 60 | - public function rest_callback( WP_REST_Request $request ) { |
|
| 60 | + public function rest_callback( WP_REST_Request $request ) { |
|
| 61 | 61 | |
| 62 | - // Respond from origin if TTL is 0 |
|
| 63 | - if ( static::CACHE_TTL == 0 ) { |
|
| 62 | + // Respond from origin if TTL is 0 |
|
| 63 | + if ( static::CACHE_TTL == 0 ) { |
|
| 64 | 64 | |
| 65 | - $data = $this->get_data( $request ); |
|
| 66 | - $response = rest_ensure_response( $data ); |
|
| 67 | - if ( is_wp_error( $data ) ) { |
|
| 68 | - return $response; |
|
| 69 | - } |
|
| 70 | - $response->header( 'Access-Control-Allow-Origin', '*' ); |
|
| 71 | - $response->header( 'X-WordLift-Cache', 'MISS' ); |
|
| 65 | + $data = $this->get_data( $request ); |
|
| 66 | + $response = rest_ensure_response( $data ); |
|
| 67 | + if ( is_wp_error( $data ) ) { |
|
| 68 | + return $response; |
|
| 69 | + } |
|
| 70 | + $response->header( 'Access-Control-Allow-Origin', '*' ); |
|
| 71 | + $response->header( 'X-WordLift-Cache', 'MISS' ); |
|
| 72 | 72 | |
| 73 | - return $response; |
|
| 74 | - } |
|
| 73 | + return $response; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - // Create the cache key. |
|
| 77 | - $cache_key_params = $request->get_params(); |
|
| 78 | - unset( $cache_key_params['uniqid'] ); |
|
| 79 | - unset( $cache_key_params['rest_route'] ); |
|
| 80 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 76 | + // Create the cache key. |
|
| 77 | + $cache_key_params = $request->get_params(); |
|
| 78 | + unset( $cache_key_params['uniqid'] ); |
|
| 79 | + unset( $cache_key_params['rest_route'] ); |
|
| 80 | + $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 81 | 81 | |
| 82 | - // Create the TTL cache and try to get the results. |
|
| 83 | - $cache = new Ttl_Cache( $this->endpoint, static::CACHE_TTL ); |
|
| 84 | - $cache_results = $cache->get( $cache_key ); |
|
| 82 | + // Create the TTL cache and try to get the results. |
|
| 83 | + $cache = new Ttl_Cache( $this->endpoint, static::CACHE_TTL ); |
|
| 84 | + $cache_results = $cache->get( $cache_key ); |
|
| 85 | 85 | |
| 86 | - if ( isset( $cache_results ) ) { |
|
| 86 | + if ( isset( $cache_results ) ) { |
|
| 87 | 87 | |
| 88 | - $response = rest_ensure_response( $cache_results ); |
|
| 89 | - $response->header( 'Access-Control-Allow-Origin', '*' ); |
|
| 90 | - $response->header( 'X-WordLift-Cache', 'HIT' ); |
|
| 88 | + $response = rest_ensure_response( $cache_results ); |
|
| 89 | + $response->header( 'Access-Control-Allow-Origin', '*' ); |
|
| 90 | + $response->header( 'X-WordLift-Cache', 'HIT' ); |
|
| 91 | 91 | |
| 92 | - return $response; |
|
| 93 | - } |
|
| 92 | + return $response; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - $data = $this->get_data( $request ); |
|
| 96 | - $response = rest_ensure_response( $data ); |
|
| 97 | - if ( is_wp_error( $data ) ) { |
|
| 98 | - return $response; |
|
| 99 | - } |
|
| 100 | - $response->header( 'Access-Control-Allow-Origin', '*' ); |
|
| 101 | - $response->header( 'X-WordLift-Cache', 'MISS' ); |
|
| 95 | + $data = $this->get_data( $request ); |
|
| 96 | + $response = rest_ensure_response( $data ); |
|
| 97 | + if ( is_wp_error( $data ) ) { |
|
| 98 | + return $response; |
|
| 99 | + } |
|
| 100 | + $response->header( 'Access-Control-Allow-Origin', '*' ); |
|
| 101 | + $response->header( 'X-WordLift-Cache', 'MISS' ); |
|
| 102 | 102 | |
| 103 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 104 | - $cache->put( $cache_key, $data ); |
|
| 103 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 104 | + $cache->put( $cache_key, $data ); |
|
| 105 | 105 | |
| 106 | - return $response; |
|
| 106 | + return $response; |
|
| 107 | 107 | |
| 108 | - } |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - private function is_endpoint() { |
|
| 111 | - $compare_route = WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint; |
|
| 110 | + private function is_endpoint() { |
|
| 111 | + $compare_route = WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint; |
|
| 112 | 112 | |
| 113 | - // Directly accessing $_SERVER['REQUEST_URI'] or $_GET['rest_route'] here as it's too early to use global $wp reliably |
|
| 113 | + // Directly accessing $_SERVER['REQUEST_URI'] or $_GET['rest_route'] here as it's too early to use global $wp reliably |
|
| 114 | 114 | |
| 115 | - if ( $_SERVER['REQUEST_URI'] && strpos( (string) $_SERVER['REQUEST_URI'], $compare_route ) ) { |
|
| 116 | - return true; |
|
| 117 | - } |
|
| 118 | - if ( ! empty( $_GET['rest_route'] ) && strpos( (string) $_GET['rest_route'], $compare_route ) ) { |
|
| 119 | - return true; |
|
| 120 | - } |
|
| 115 | + if ( $_SERVER['REQUEST_URI'] && strpos( (string) $_SERVER['REQUEST_URI'], $compare_route ) ) { |
|
| 116 | + return true; |
|
| 117 | + } |
|
| 118 | + if ( ! empty( $_GET['rest_route'] ) && strpos( (string) $_GET['rest_route'], $compare_route ) ) { |
|
| 119 | + return true; |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - return false; |
|
| 123 | - } |
|
| 122 | + return false; |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | 125 | } |
@@ -15,107 +15,107 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | const CACHE_TTL = 86400; // 24 hours |
| 17 | 17 | |
| 18 | - public function __construct( $endpoint, $args ) { |
|
| 18 | + public function __construct($endpoint, $args) { |
|
| 19 | 19 | |
| 20 | 20 | $scope = $this; |
| 21 | 21 | $this->endpoint = $endpoint; |
| 22 | 22 | $this->args = $args; |
| 23 | 23 | |
| 24 | 24 | // Register rest route with callback |
| 25 | - add_action( 'rest_api_init', function () use ( $scope ) { |
|
| 26 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, $scope->endpoint, array( |
|
| 25 | + add_action('rest_api_init', function() use ($scope) { |
|
| 26 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, $scope->endpoint, array( |
|
| 27 | 27 | 'methods' => WP_REST_Server::READABLE, |
| 28 | 28 | 'permission_callback' => '__return_true', |
| 29 | - 'callback' => array( $scope, 'rest_callback' ), |
|
| 29 | + 'callback' => array($scope, 'rest_callback'), |
|
| 30 | 30 | 'args' => $scope->args |
| 31 | - ) ); |
|
| 31 | + )); |
|
| 32 | 32 | } ); |
| 33 | 33 | |
| 34 | 34 | // Optimizations: disable unneeded plugins on this specific REST call. WPSeo is slowing down the responses quite a bit. |
| 35 | - add_action( 'plugins_loaded', function () use ( $scope ) { |
|
| 35 | + add_action('plugins_loaded', function() use ($scope) { |
|
| 36 | 36 | |
| 37 | - if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) { |
|
| 37 | + if ( ! defined('REST_REQUEST') || ! REST_REQUEST || ! $scope->is_endpoint()) { |
|
| 38 | 38 | return; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 42 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 43 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 44 | - }, 0 ); |
|
| 41 | + remove_action('plugins_loaded', 'rocket_init'); |
|
| 42 | + remove_action('plugins_loaded', 'wpseo_premium_init', 14); |
|
| 43 | + remove_action('plugins_loaded', 'wpseo_init', 14); |
|
| 44 | + }, 0); |
|
| 45 | 45 | |
| 46 | - add_action( 'init', function () use ( $scope ) { |
|
| 46 | + add_action('init', function() use ($scope) { |
|
| 47 | 47 | |
| 48 | - if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) { |
|
| 48 | + if ( ! defined('REST_REQUEST') || ! REST_REQUEST || ! $scope->is_endpoint()) { |
|
| 49 | 49 | return; |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 53 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 54 | - }, 0 ); |
|
| 52 | + remove_action('init', 'wp_widgets_init', 1); |
|
| 53 | + remove_action('init', 'gglcptch_init'); |
|
| 54 | + }, 0); |
|
| 55 | 55 | |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - public abstract function get_data( $request ); |
|
| 58 | + public abstract function get_data($request); |
|
| 59 | 59 | |
| 60 | - public function rest_callback( WP_REST_Request $request ) { |
|
| 60 | + public function rest_callback(WP_REST_Request $request) { |
|
| 61 | 61 | |
| 62 | 62 | // Respond from origin if TTL is 0 |
| 63 | - if ( static::CACHE_TTL == 0 ) { |
|
| 63 | + if (static::CACHE_TTL == 0) { |
|
| 64 | 64 | |
| 65 | - $data = $this->get_data( $request ); |
|
| 66 | - $response = rest_ensure_response( $data ); |
|
| 67 | - if ( is_wp_error( $data ) ) { |
|
| 65 | + $data = $this->get_data($request); |
|
| 66 | + $response = rest_ensure_response($data); |
|
| 67 | + if (is_wp_error($data)) { |
|
| 68 | 68 | return $response; |
| 69 | 69 | } |
| 70 | - $response->header( 'Access-Control-Allow-Origin', '*' ); |
|
| 71 | - $response->header( 'X-WordLift-Cache', 'MISS' ); |
|
| 70 | + $response->header('Access-Control-Allow-Origin', '*'); |
|
| 71 | + $response->header('X-WordLift-Cache', 'MISS'); |
|
| 72 | 72 | |
| 73 | 73 | return $response; |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | // Create the cache key. |
| 77 | 77 | $cache_key_params = $request->get_params(); |
| 78 | - unset( $cache_key_params['uniqid'] ); |
|
| 79 | - unset( $cache_key_params['rest_route'] ); |
|
| 80 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 78 | + unset($cache_key_params['uniqid']); |
|
| 79 | + unset($cache_key_params['rest_route']); |
|
| 80 | + $cache_key = array('request_params' => $cache_key_params); |
|
| 81 | 81 | |
| 82 | 82 | // Create the TTL cache and try to get the results. |
| 83 | - $cache = new Ttl_Cache( $this->endpoint, static::CACHE_TTL ); |
|
| 84 | - $cache_results = $cache->get( $cache_key ); |
|
| 83 | + $cache = new Ttl_Cache($this->endpoint, static::CACHE_TTL); |
|
| 84 | + $cache_results = $cache->get($cache_key); |
|
| 85 | 85 | |
| 86 | - if ( isset( $cache_results ) ) { |
|
| 86 | + if (isset($cache_results)) { |
|
| 87 | 87 | |
| 88 | - $response = rest_ensure_response( $cache_results ); |
|
| 89 | - $response->header( 'Access-Control-Allow-Origin', '*' ); |
|
| 90 | - $response->header( 'X-WordLift-Cache', 'HIT' ); |
|
| 88 | + $response = rest_ensure_response($cache_results); |
|
| 89 | + $response->header('Access-Control-Allow-Origin', '*'); |
|
| 90 | + $response->header('X-WordLift-Cache', 'HIT'); |
|
| 91 | 91 | |
| 92 | 92 | return $response; |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | - $data = $this->get_data( $request ); |
|
| 96 | - $response = rest_ensure_response( $data ); |
|
| 97 | - if ( is_wp_error( $data ) ) { |
|
| 95 | + $data = $this->get_data($request); |
|
| 96 | + $response = rest_ensure_response($data); |
|
| 97 | + if (is_wp_error($data)) { |
|
| 98 | 98 | return $response; |
| 99 | 99 | } |
| 100 | - $response->header( 'Access-Control-Allow-Origin', '*' ); |
|
| 101 | - $response->header( 'X-WordLift-Cache', 'MISS' ); |
|
| 100 | + $response->header('Access-Control-Allow-Origin', '*'); |
|
| 101 | + $response->header('X-WordLift-Cache', 'MISS'); |
|
| 102 | 102 | |
| 103 | 103 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 104 | - $cache->put( $cache_key, $data ); |
|
| 104 | + $cache->put($cache_key, $data); |
|
| 105 | 105 | |
| 106 | 106 | return $response; |
| 107 | 107 | |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | private function is_endpoint() { |
| 111 | - $compare_route = WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint; |
|
| 111 | + $compare_route = WL_REST_ROUTE_DEFAULT_NAMESPACE.$this->endpoint; |
|
| 112 | 112 | |
| 113 | 113 | // Directly accessing $_SERVER['REQUEST_URI'] or $_GET['rest_route'] here as it's too early to use global $wp reliably |
| 114 | 114 | |
| 115 | - if ( $_SERVER['REQUEST_URI'] && strpos( (string) $_SERVER['REQUEST_URI'], $compare_route ) ) { |
|
| 115 | + if ($_SERVER['REQUEST_URI'] && strpos((string) $_SERVER['REQUEST_URI'], $compare_route)) { |
|
| 116 | 116 | return true; |
| 117 | 117 | } |
| 118 | - if ( ! empty( $_GET['rest_route'] ) && strpos( (string) $_GET['rest_route'], $compare_route ) ) { |
|
| 118 | + if ( ! empty($_GET['rest_route']) && strpos((string) $_GET['rest_route'], $compare_route)) { |
|
| 119 | 119 | return true; |
| 120 | 120 | } |
| 121 | 121 | |
@@ -20,33 +20,33 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | function wl_shortcode_faceted_search( $request ) { |
| 22 | 22 | |
| 23 | - // Create the cache key. |
|
| 24 | - $cache_key = array( |
|
| 25 | - 'request_params' => $_GET, |
|
| 26 | - ); |
|
| 23 | + // Create the cache key. |
|
| 24 | + $cache_key = array( |
|
| 25 | + 'request_params' => $_GET, |
|
| 26 | + ); |
|
| 27 | 27 | |
| 28 | - // Create the TTL cache and try to get the results. |
|
| 29 | - $cache = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours. |
|
| 30 | - $cache_results = $cache->get( $cache_key ); |
|
| 28 | + // Create the TTL cache and try to get the results. |
|
| 29 | + $cache = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours. |
|
| 30 | + $cache_results = $cache->get( $cache_key ); |
|
| 31 | 31 | |
| 32 | - // So that the endpoint can be used remotely |
|
| 33 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 32 | + // So that the endpoint can be used remotely |
|
| 33 | + header( 'Access-Control-Allow-Origin: *' ); |
|
| 34 | 34 | |
| 35 | - if ( isset( $cache_results ) ) { |
|
| 36 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 37 | - wl_core_send_json( $cache_results ); |
|
| 35 | + if ( isset( $cache_results ) ) { |
|
| 36 | + header( 'X-WordLift-Cache: HIT' ); |
|
| 37 | + wl_core_send_json( $cache_results ); |
|
| 38 | 38 | |
| 39 | - return; |
|
| 40 | - } |
|
| 39 | + return; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 42 | + header( 'X-WordLift-Cache: MISS' ); |
|
| 43 | 43 | |
| 44 | - $results = wl_shortcode_faceted_search_origin( $request ); |
|
| 44 | + $results = wl_shortcode_faceted_search_origin( $request ); |
|
| 45 | 45 | |
| 46 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 47 | - $cache->put( $cache_key, $results ); |
|
| 46 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 47 | + $cache->put( $cache_key, $results ); |
|
| 48 | 48 | |
| 49 | - wl_core_send_json( $results ); |
|
| 49 | + wl_core_send_json( $results ); |
|
| 50 | 50 | |
| 51 | 51 | } |
| 52 | 52 | |
@@ -57,151 +57,151 @@ discard block |
||
| 57 | 57 | * @since 3.26.0 |
| 58 | 58 | */ |
| 59 | 59 | function wl_shortcode_faceted_search_origin( $request ) { |
| 60 | - // Post ID must be defined. |
|
| 61 | - if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 62 | - wp_die( 'No post_id given' ); |
|
| 63 | - |
|
| 64 | - return; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - $current_post_id = (int) $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 68 | - $current_post = get_post( $current_post_id ); |
|
| 69 | - $faceted_id = (string) $_GET['uniqid']; |
|
| 70 | - |
|
| 71 | - $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 72 | - $post_types = explode( ',', $post_types ); |
|
| 73 | - $existing_post_types = get_post_types(); |
|
| 74 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 75 | - |
|
| 76 | - |
|
| 77 | - // Post ID has to match an existing item. |
|
| 78 | - if ( null === $current_post ) { |
|
| 79 | - wp_die( 'No valid post_id given' ); |
|
| 80 | - |
|
| 81 | - return; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - // If the current post is an entity, |
|
| 85 | - // the current post is used as main entity. |
|
| 86 | - // Otherwise, current post related entities are used. |
|
| 87 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 88 | - |
|
| 89 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 90 | - array( $current_post->ID ) : |
|
| 91 | - $entity_service->get_related_entities( $current_post->ID ); |
|
| 92 | - |
|
| 93 | - // If there are no entities we cannot render the widget. |
|
| 94 | - if ( 0 === count( $entity_ids ) ) { |
|
| 95 | - /** |
|
| 96 | - * If this function is not called from ajax |
|
| 97 | - * then this should not throw an error. |
|
| 98 | - * Note: Used in scripbox longtail project on json endpoint. |
|
| 99 | - */ |
|
| 100 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 101 | - wp_die( 'No entities available' ); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 107 | - $amp = ( isset( $_GET['amp'] ) ) ? true : false; |
|
| 108 | - |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * see https://github.com/insideout10/wordlift-plugin/issues/1181 |
|
| 112 | - * The ordering should be descending by date on default. |
|
| 113 | - */ |
|
| 114 | - $order_by = 'DESC'; |
|
| 115 | - if ( isset( $_GET['sort'] ) && is_string( $_GET['sort'] ) ) { |
|
| 116 | - $order_by = (string) $_GET['sort']; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 121 | - $entity_ids, |
|
| 122 | - '*', |
|
| 123 | - null, |
|
| 124 | - 'publish', |
|
| 125 | - array( $current_post_id ), |
|
| 126 | - $limit, |
|
| 127 | - null, |
|
| 128 | - $order_by, |
|
| 129 | - $post_types |
|
| 130 | - ); |
|
| 131 | - |
|
| 132 | - |
|
| 133 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 134 | - return $p->ID; |
|
| 135 | - }, $referencing_posts ); |
|
| 136 | - |
|
| 137 | - $post_results = array(); |
|
| 138 | - $entity_results = array(); |
|
| 139 | - |
|
| 140 | - // Populate $post_results |
|
| 141 | - |
|
| 142 | - $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 143 | - $referencing_posts : |
|
| 144 | - Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 145 | - wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 146 | - '*', |
|
| 147 | - null, |
|
| 148 | - null, |
|
| 149 | - array(), |
|
| 150 | - null, |
|
| 151 | - $referencing_post_ids |
|
| 152 | - ); |
|
| 153 | - |
|
| 154 | - if ( $filtered_posts ) { |
|
| 155 | - foreach ( $filtered_posts as $post_obj ) { |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Use the thumbnail. |
|
| 159 | - * |
|
| 160 | - * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 161 | - * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 162 | - * |
|
| 163 | - * @since 3.19.3 We're using the medium size image. |
|
| 164 | - */ |
|
| 165 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 166 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 167 | - $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 168 | - $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 169 | - $post_obj->srcset = Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 170 | - $post_obj->post_title = strip_tags( html_entity_decode( $post_obj->post_title, ENT_QUOTES, 'UTF-8' ) ); |
|
| 171 | - $result = $post_obj; |
|
| 172 | - $post_results[] = $result; |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - // Add filler posts if needed |
|
| 177 | - |
|
| 178 | - $filler_count = $limit - count( $post_results ); |
|
| 179 | - if ( 0 < apply_filters( 'wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids ) ) { |
|
| 180 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 181 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 182 | - $filler_posts = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 183 | - |
|
| 184 | - $post_results = array_merge( $post_results, $filler_posts ); |
|
| 185 | - } |
|
| 186 | - $referencing_post_ids = array_map( function ( $post ) { |
|
| 187 | - return $post->ID; |
|
| 188 | - }, $post_results ); |
|
| 189 | - |
|
| 190 | - // Populate $entity_results |
|
| 191 | - |
|
| 192 | - global $wpdb; |
|
| 193 | - |
|
| 194 | - // Retrieve Wordlift relation instances table name. |
|
| 195 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 196 | - |
|
| 197 | - /* |
|
| 60 | + // Post ID must be defined. |
|
| 61 | + if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 62 | + wp_die( 'No post_id given' ); |
|
| 63 | + |
|
| 64 | + return; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + $current_post_id = (int) $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 68 | + $current_post = get_post( $current_post_id ); |
|
| 69 | + $faceted_id = (string) $_GET['uniqid']; |
|
| 70 | + |
|
| 71 | + $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 72 | + $post_types = explode( ',', $post_types ); |
|
| 73 | + $existing_post_types = get_post_types(); |
|
| 74 | + $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 75 | + |
|
| 76 | + |
|
| 77 | + // Post ID has to match an existing item. |
|
| 78 | + if ( null === $current_post ) { |
|
| 79 | + wp_die( 'No valid post_id given' ); |
|
| 80 | + |
|
| 81 | + return; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + // If the current post is an entity, |
|
| 85 | + // the current post is used as main entity. |
|
| 86 | + // Otherwise, current post related entities are used. |
|
| 87 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 88 | + |
|
| 89 | + $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 90 | + array( $current_post->ID ) : |
|
| 91 | + $entity_service->get_related_entities( $current_post->ID ); |
|
| 92 | + |
|
| 93 | + // If there are no entities we cannot render the widget. |
|
| 94 | + if ( 0 === count( $entity_ids ) ) { |
|
| 95 | + /** |
|
| 96 | + * If this function is not called from ajax |
|
| 97 | + * then this should not throw an error. |
|
| 98 | + * Note: Used in scripbox longtail project on json endpoint. |
|
| 99 | + */ |
|
| 100 | + if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 101 | + wp_die( 'No entities available' ); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 107 | + $amp = ( isset( $_GET['amp'] ) ) ? true : false; |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * see https://github.com/insideout10/wordlift-plugin/issues/1181 |
|
| 112 | + * The ordering should be descending by date on default. |
|
| 113 | + */ |
|
| 114 | + $order_by = 'DESC'; |
|
| 115 | + if ( isset( $_GET['sort'] ) && is_string( $_GET['sort'] ) ) { |
|
| 116 | + $order_by = (string) $_GET['sort']; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 121 | + $entity_ids, |
|
| 122 | + '*', |
|
| 123 | + null, |
|
| 124 | + 'publish', |
|
| 125 | + array( $current_post_id ), |
|
| 126 | + $limit, |
|
| 127 | + null, |
|
| 128 | + $order_by, |
|
| 129 | + $post_types |
|
| 130 | + ); |
|
| 131 | + |
|
| 132 | + |
|
| 133 | + $referencing_post_ids = array_map( function ( $p ) { |
|
| 134 | + return $p->ID; |
|
| 135 | + }, $referencing_posts ); |
|
| 136 | + |
|
| 137 | + $post_results = array(); |
|
| 138 | + $entity_results = array(); |
|
| 139 | + |
|
| 140 | + // Populate $post_results |
|
| 141 | + |
|
| 142 | + $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 143 | + $referencing_posts : |
|
| 144 | + Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 145 | + wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 146 | + '*', |
|
| 147 | + null, |
|
| 148 | + null, |
|
| 149 | + array(), |
|
| 150 | + null, |
|
| 151 | + $referencing_post_ids |
|
| 152 | + ); |
|
| 153 | + |
|
| 154 | + if ( $filtered_posts ) { |
|
| 155 | + foreach ( $filtered_posts as $post_obj ) { |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Use the thumbnail. |
|
| 159 | + * |
|
| 160 | + * @see https://github.com/insideout10/wordlift-plugin/issues/825 related issue. |
|
| 161 | + * @see https://github.com/insideout10/wordlift-plugin/issues/837 |
|
| 162 | + * |
|
| 163 | + * @since 3.19.3 We're using the medium size image. |
|
| 164 | + */ |
|
| 165 | + $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 166 | + $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 167 | + $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 168 | + $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 169 | + $post_obj->srcset = Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 170 | + $post_obj->post_title = strip_tags( html_entity_decode( $post_obj->post_title, ENT_QUOTES, 'UTF-8' ) ); |
|
| 171 | + $result = $post_obj; |
|
| 172 | + $post_results[] = $result; |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + // Add filler posts if needed |
|
| 177 | + |
|
| 178 | + $filler_count = $limit - count( $post_results ); |
|
| 179 | + if ( 0 < apply_filters( 'wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids ) ) { |
|
| 180 | + $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 181 | + $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 182 | + $filler_posts = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 183 | + |
|
| 184 | + $post_results = array_merge( $post_results, $filler_posts ); |
|
| 185 | + } |
|
| 186 | + $referencing_post_ids = array_map( function ( $post ) { |
|
| 187 | + return $post->ID; |
|
| 188 | + }, $post_results ); |
|
| 189 | + |
|
| 190 | + // Populate $entity_results |
|
| 191 | + |
|
| 192 | + global $wpdb; |
|
| 193 | + |
|
| 194 | + // Retrieve Wordlift relation instances table name. |
|
| 195 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 196 | + |
|
| 197 | + /* |
|
| 198 | 198 | * Make sure we have some referenced post, otherwise the IN parts of |
| 199 | 199 | * the SQL will produce an SQL error. |
| 200 | 200 | */ |
| 201 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 202 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 201 | + if ( ! empty( $referencing_post_ids ) ) { |
|
| 202 | + $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 203 | 203 | |
| 204 | - $query = " |
|
| 204 | + $query = " |
|
| 205 | 205 | SELECT |
| 206 | 206 | object_id AS ID, |
| 207 | 207 | count( object_id ) AS counter |
@@ -213,68 +213,68 @@ discard block |
||
| 213 | 213 | LIMIT $limit; |
| 214 | 214 | "; |
| 215 | 215 | |
| 216 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 216 | + wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 217 | 217 | |
| 218 | - $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 218 | + $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 219 | 219 | |
| 220 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 220 | + wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 221 | 221 | |
| 222 | - foreach ( $entities as $obj ) { |
|
| 222 | + foreach ( $entities as $obj ) { |
|
| 223 | 223 | |
| 224 | - $entity = get_post( $obj->ID ); |
|
| 224 | + $entity = get_post( $obj->ID ); |
|
| 225 | 225 | |
| 226 | - // Ensure only valid and published entities are returned. |
|
| 227 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 226 | + // Ensure only valid and published entities are returned. |
|
| 227 | + if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 228 | 228 | |
| 229 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 230 | - $serialized_entity['label'] = html_entity_decode( wl_shortcode_faceted_search_get_the_title( $obj->ID ), ENT_QUOTES, 'UTF-8' ); |
|
| 231 | - $serialized_entity['counter'] = $obj->counter; |
|
| 232 | - $serialized_entity['createdAt'] = $entity->post_date; |
|
| 233 | - $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 234 | - $obj->ID, |
|
| 235 | - 'ids', |
|
| 236 | - null, |
|
| 237 | - null, |
|
| 238 | - array(), |
|
| 239 | - null, |
|
| 240 | - $referencing_post_ids |
|
| 241 | - ); |
|
| 242 | - $entity_results[] = $serialized_entity; |
|
| 243 | - } |
|
| 244 | - } |
|
| 245 | - } |
|
| 229 | + $serialized_entity = wl_serialize_entity( $entity ); |
|
| 230 | + $serialized_entity['label'] = html_entity_decode( wl_shortcode_faceted_search_get_the_title( $obj->ID ), ENT_QUOTES, 'UTF-8' ); |
|
| 231 | + $serialized_entity['counter'] = $obj->counter; |
|
| 232 | + $serialized_entity['createdAt'] = $entity->post_date; |
|
| 233 | + $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 234 | + $obj->ID, |
|
| 235 | + 'ids', |
|
| 236 | + null, |
|
| 237 | + null, |
|
| 238 | + array(), |
|
| 239 | + null, |
|
| 240 | + $referencing_post_ids |
|
| 241 | + ); |
|
| 242 | + $entity_results[] = $serialized_entity; |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | + } |
|
| 246 | 246 | |
| 247 | - $post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id ); |
|
| 247 | + $post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id ); |
|
| 248 | 248 | |
| 249 | - // Add srcset attribute. |
|
| 250 | - $post_results = array_map( function ( $post ) { |
|
| 251 | - $post->srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 249 | + // Add srcset attribute. |
|
| 250 | + $post_results = array_map( function ( $post ) { |
|
| 251 | + $post->srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 252 | 252 | |
| 253 | - return $post; |
|
| 254 | - }, $post_results ); |
|
| 253 | + return $post; |
|
| 254 | + }, $post_results ); |
|
| 255 | 255 | |
| 256 | - $entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id ); |
|
| 256 | + $entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id ); |
|
| 257 | 257 | |
| 258 | - return array( |
|
| 259 | - 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
| 260 | - 'entities' => $entity_results |
|
| 261 | - ); |
|
| 258 | + return array( |
|
| 259 | + 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
| 260 | + 'entities' => $entity_results |
|
| 261 | + ); |
|
| 262 | 262 | |
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | function wl_shortcode_faceted_search_get_the_title( $post_id ) { |
| 266 | 266 | |
| 267 | - $title = strip_tags( get_the_title( $post_id ) ); |
|
| 267 | + $title = strip_tags( get_the_title( $post_id ) ); |
|
| 268 | 268 | |
| 269 | - if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 270 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 269 | + if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 270 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 271 | 271 | |
| 272 | - if ( count( $alternative_labels ) > 0 ) { |
|
| 273 | - $title = $alternative_labels[0]; |
|
| 274 | - } |
|
| 275 | - } |
|
| 272 | + if ( count( $alternative_labels ) > 0 ) { |
|
| 273 | + $title = $alternative_labels[0]; |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | 276 | |
| 277 | - return remove_accents( $title ); |
|
| 277 | + return remove_accents( $title ); |
|
| 278 | 278 | |
| 279 | 279 | } |
| 280 | 280 | |
@@ -282,9 +282,9 @@ discard block |
||
| 282 | 282 | * Adding `rest_api_init` action for network faceted-search |
| 283 | 283 | */ |
| 284 | 284 | add_action( 'rest_api_init', function () { |
| 285 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 286 | - 'methods' => 'GET', |
|
| 287 | - 'callback' => 'wl_shortcode_faceted_search', |
|
| 288 | - 'permission_callback' => '__return_true', |
|
| 289 | - ) ); |
|
| 285 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 286 | + 'methods' => 'GET', |
|
| 287 | + 'callback' => 'wl_shortcode_faceted_search', |
|
| 288 | + 'permission_callback' => '__return_true', |
|
| 289 | + ) ); |
|
| 290 | 290 | } ); |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | * @since 3.21.4 fix the cache key by also using the request body. |
| 19 | 19 | * @since 3.21.2 add a caching layer. |
| 20 | 20 | */ |
| 21 | -function wl_shortcode_faceted_search( $request ) { |
|
| 21 | +function wl_shortcode_faceted_search($request) { |
|
| 22 | 22 | |
| 23 | 23 | // Create the cache key. |
| 24 | 24 | $cache_key = array( |
@@ -26,27 +26,27 @@ discard block |
||
| 26 | 26 | ); |
| 27 | 27 | |
| 28 | 28 | // Create the TTL cache and try to get the results. |
| 29 | - $cache = new Ttl_Cache( "faceted-search", 8 * 60 * 60 ); // 8 hours. |
|
| 30 | - $cache_results = $cache->get( $cache_key ); |
|
| 29 | + $cache = new Ttl_Cache("faceted-search", 8 * 60 * 60); // 8 hours. |
|
| 30 | + $cache_results = $cache->get($cache_key); |
|
| 31 | 31 | |
| 32 | 32 | // So that the endpoint can be used remotely |
| 33 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 33 | + header('Access-Control-Allow-Origin: *'); |
|
| 34 | 34 | |
| 35 | - if ( isset( $cache_results ) ) { |
|
| 36 | - header( 'X-WordLift-Cache: HIT' ); |
|
| 37 | - wl_core_send_json( $cache_results ); |
|
| 35 | + if (isset($cache_results)) { |
|
| 36 | + header('X-WordLift-Cache: HIT'); |
|
| 37 | + wl_core_send_json($cache_results); |
|
| 38 | 38 | |
| 39 | 39 | return; |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - header( 'X-WordLift-Cache: MISS' ); |
|
| 42 | + header('X-WordLift-Cache: MISS'); |
|
| 43 | 43 | |
| 44 | - $results = wl_shortcode_faceted_search_origin( $request ); |
|
| 44 | + $results = wl_shortcode_faceted_search_origin($request); |
|
| 45 | 45 | |
| 46 | 46 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 47 | - $cache->put( $cache_key, $results ); |
|
| 47 | + $cache->put($cache_key, $results); |
|
| 48 | 48 | |
| 49 | - wl_core_send_json( $results ); |
|
| 49 | + wl_core_send_json($results); |
|
| 50 | 50 | |
| 51 | 51 | } |
| 52 | 52 | |
@@ -56,27 +56,27 @@ discard block |
||
| 56 | 56 | * @return array $results |
| 57 | 57 | * @since 3.26.0 |
| 58 | 58 | */ |
| 59 | -function wl_shortcode_faceted_search_origin( $request ) { |
|
| 59 | +function wl_shortcode_faceted_search_origin($request) { |
|
| 60 | 60 | // Post ID must be defined. |
| 61 | - if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 62 | - wp_die( 'No post_id given' ); |
|
| 61 | + if ( ! isset($_GET['post_id'])) { // WPCS: input var ok; CSRF ok. |
|
| 62 | + wp_die('No post_id given'); |
|
| 63 | 63 | |
| 64 | 64 | return; |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | $current_post_id = (int) $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
| 68 | - $current_post = get_post( $current_post_id ); |
|
| 68 | + $current_post = get_post($current_post_id); |
|
| 69 | 69 | $faceted_id = (string) $_GET['uniqid']; |
| 70 | 70 | |
| 71 | - $post_types = isset( $_GET['post_types'] ) ? (string) $_GET['post_types'] : ''; |
|
| 72 | - $post_types = explode( ',', $post_types ); |
|
| 71 | + $post_types = isset($_GET['post_types']) ? (string) $_GET['post_types'] : ''; |
|
| 72 | + $post_types = explode(',', $post_types); |
|
| 73 | 73 | $existing_post_types = get_post_types(); |
| 74 | - $post_types = array_values( array_intersect( $existing_post_types, $post_types ) ); |
|
| 74 | + $post_types = array_values(array_intersect($existing_post_types, $post_types)); |
|
| 75 | 75 | |
| 76 | 76 | |
| 77 | 77 | // Post ID has to match an existing item. |
| 78 | - if ( null === $current_post ) { |
|
| 79 | - wp_die( 'No valid post_id given' ); |
|
| 78 | + if (null === $current_post) { |
|
| 79 | + wp_die('No valid post_id given'); |
|
| 80 | 80 | |
| 81 | 81 | return; |
| 82 | 82 | } |
@@ -86,25 +86,24 @@ discard block |
||
| 86 | 86 | // Otherwise, current post related entities are used. |
| 87 | 87 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 88 | 88 | |
| 89 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 90 | - array( $current_post->ID ) : |
|
| 91 | - $entity_service->get_related_entities( $current_post->ID ); |
|
| 89 | + $entity_ids = $entity_service->is_entity($current_post->ID) ? |
|
| 90 | + array($current_post->ID) : $entity_service->get_related_entities($current_post->ID); |
|
| 92 | 91 | |
| 93 | 92 | // If there are no entities we cannot render the widget. |
| 94 | - if ( 0 === count( $entity_ids ) ) { |
|
| 93 | + if (0 === count($entity_ids)) { |
|
| 95 | 94 | /** |
| 96 | 95 | * If this function is not called from ajax |
| 97 | 96 | * then this should not throw an error. |
| 98 | 97 | * Note: Used in scripbox longtail project on json endpoint. |
| 99 | 98 | */ |
| 100 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 101 | - wp_die( 'No entities available' ); |
|
| 99 | + if (apply_filters('wp_doing_ajax', defined('DOING_AJAX') && DOING_AJAX)) { |
|
| 100 | + wp_die('No entities available'); |
|
| 102 | 101 | } |
| 103 | 102 | |
| 104 | 103 | } |
| 105 | 104 | |
| 106 | - $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 107 | - $amp = ( isset( $_GET['amp'] ) ) ? true : false; |
|
| 105 | + $limit = (isset($_GET['limit'])) ? (int) $_GET['limit'] : 4; // WPCS: input var ok; CSRF ok. |
|
| 106 | + $amp = (isset($_GET['amp'])) ? true : false; |
|
| 108 | 107 | |
| 109 | 108 | |
| 110 | 109 | /** |
@@ -112,7 +111,7 @@ discard block |
||
| 112 | 111 | * The ordering should be descending by date on default. |
| 113 | 112 | */ |
| 114 | 113 | $order_by = 'DESC'; |
| 115 | - if ( isset( $_GET['sort'] ) && is_string( $_GET['sort'] ) ) { |
|
| 114 | + if (isset($_GET['sort']) && is_string($_GET['sort'])) { |
|
| 116 | 115 | $order_by = (string) $_GET['sort']; |
| 117 | 116 | } |
| 118 | 117 | |
@@ -122,7 +121,7 @@ discard block |
||
| 122 | 121 | '*', |
| 123 | 122 | null, |
| 124 | 123 | 'publish', |
| 125 | - array( $current_post_id ), |
|
| 124 | + array($current_post_id), |
|
| 126 | 125 | $limit, |
| 127 | 126 | null, |
| 128 | 127 | $order_by, |
@@ -130,19 +129,18 @@ discard block |
||
| 130 | 129 | ); |
| 131 | 130 | |
| 132 | 131 | |
| 133 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 132 | + $referencing_post_ids = array_map(function($p) { |
|
| 134 | 133 | return $p->ID; |
| 135 | - }, $referencing_posts ); |
|
| 134 | + }, $referencing_posts); |
|
| 136 | 135 | |
| 137 | 136 | $post_results = array(); |
| 138 | 137 | $entity_results = array(); |
| 139 | 138 | |
| 140 | 139 | // Populate $post_results |
| 141 | 140 | |
| 142 | - $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 143 | - $referencing_posts : |
|
| 144 | - Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 145 | - wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 141 | + $filtered_posts = (empty($filtering_entity_uris)) ? |
|
| 142 | + $referencing_posts : Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 143 | + wl_get_entity_post_ids_by_uris($filtering_entity_uris), |
|
| 146 | 144 | '*', |
| 147 | 145 | null, |
| 148 | 146 | null, |
@@ -151,8 +149,8 @@ discard block |
||
| 151 | 149 | $referencing_post_ids |
| 152 | 150 | ); |
| 153 | 151 | |
| 154 | - if ( $filtered_posts ) { |
|
| 155 | - foreach ( $filtered_posts as $post_obj ) { |
|
| 152 | + if ($filtered_posts) { |
|
| 153 | + foreach ($filtered_posts as $post_obj) { |
|
| 156 | 154 | |
| 157 | 155 | /** |
| 158 | 156 | * Use the thumbnail. |
@@ -162,12 +160,12 @@ discard block |
||
| 162 | 160 | * |
| 163 | 161 | * @since 3.19.3 We're using the medium size image. |
| 164 | 162 | */ |
| 165 | - $thumbnail = get_the_post_thumbnail_url( $post_obj, 'medium' ); |
|
| 166 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 163 | + $thumbnail = get_the_post_thumbnail_url($post_obj, 'medium'); |
|
| 164 | + $post_obj->thumbnail = ($thumbnail) ? |
|
| 167 | 165 | $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
| 168 | - $post_obj->permalink = get_permalink( $post_obj->ID ); |
|
| 169 | - $post_obj->srcset = Srcset_Util::get_srcset( $post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 170 | - $post_obj->post_title = strip_tags( html_entity_decode( $post_obj->post_title, ENT_QUOTES, 'UTF-8' ) ); |
|
| 166 | + $post_obj->permalink = get_permalink($post_obj->ID); |
|
| 167 | + $post_obj->srcset = Srcset_Util::get_srcset($post_obj->ID, Srcset_Util::FACETED_SEARCH_WIDGET); |
|
| 168 | + $post_obj->post_title = strip_tags(html_entity_decode($post_obj->post_title, ENT_QUOTES, 'UTF-8')); |
|
| 171 | 169 | $result = $post_obj; |
| 172 | 170 | $post_results[] = $result; |
| 173 | 171 | } |
@@ -175,17 +173,17 @@ discard block |
||
| 175 | 173 | |
| 176 | 174 | // Add filler posts if needed |
| 177 | 175 | |
| 178 | - $filler_count = $limit - count( $post_results ); |
|
| 179 | - if ( 0 < apply_filters( 'wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids ) ) { |
|
| 180 | - $filler_posts_util = new Filler_Posts_Util( $current_post_id ); |
|
| 181 | - $post_ids_to_be_excluded = array_merge( array( $current_post_id ), $referencing_post_ids ); |
|
| 182 | - $filler_posts = $filler_posts_util->get_filler_posts( $filler_count, $post_ids_to_be_excluded ); |
|
| 176 | + $filler_count = $limit - count($post_results); |
|
| 177 | + if (0 < apply_filters('wl_faceted_search__filler_count', $filler_count, $current_post_id, $referencing_post_ids)) { |
|
| 178 | + $filler_posts_util = new Filler_Posts_Util($current_post_id); |
|
| 179 | + $post_ids_to_be_excluded = array_merge(array($current_post_id), $referencing_post_ids); |
|
| 180 | + $filler_posts = $filler_posts_util->get_filler_posts($filler_count, $post_ids_to_be_excluded); |
|
| 183 | 181 | |
| 184 | - $post_results = array_merge( $post_results, $filler_posts ); |
|
| 182 | + $post_results = array_merge($post_results, $filler_posts); |
|
| 185 | 183 | } |
| 186 | - $referencing_post_ids = array_map( function ( $post ) { |
|
| 184 | + $referencing_post_ids = array_map(function($post) { |
|
| 187 | 185 | return $post->ID; |
| 188 | - }, $post_results ); |
|
| 186 | + }, $post_results); |
|
| 189 | 187 | |
| 190 | 188 | // Populate $entity_results |
| 191 | 189 | |
@@ -198,8 +196,8 @@ discard block |
||
| 198 | 196 | * Make sure we have some referenced post, otherwise the IN parts of |
| 199 | 197 | * the SQL will produce an SQL error. |
| 200 | 198 | */ |
| 201 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 202 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 199 | + if ( ! empty($referencing_post_ids)) { |
|
| 200 | + $subject_ids = implode(',', $referencing_post_ids); |
|
| 203 | 201 | |
| 204 | 202 | $query = " |
| 205 | 203 | SELECT |
@@ -213,21 +211,21 @@ discard block |
||
| 213 | 211 | LIMIT $limit; |
| 214 | 212 | "; |
| 215 | 213 | |
| 216 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 214 | + wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]"); |
|
| 217 | 215 | |
| 218 | - $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 216 | + $entities = $wpdb->get_results($query, OBJECT); // No cache ok. |
|
| 219 | 217 | |
| 220 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 218 | + wl_write_log('Entities found '.count($entities)); |
|
| 221 | 219 | |
| 222 | - foreach ( $entities as $obj ) { |
|
| 220 | + foreach ($entities as $obj) { |
|
| 223 | 221 | |
| 224 | - $entity = get_post( $obj->ID ); |
|
| 222 | + $entity = get_post($obj->ID); |
|
| 225 | 223 | |
| 226 | 224 | // Ensure only valid and published entities are returned. |
| 227 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 225 | + if ((null !== $entity) && ('publish' === $entity->post_status)) { |
|
| 228 | 226 | |
| 229 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 230 | - $serialized_entity['label'] = html_entity_decode( wl_shortcode_faceted_search_get_the_title( $obj->ID ), ENT_QUOTES, 'UTF-8' ); |
|
| 227 | + $serialized_entity = wl_serialize_entity($entity); |
|
| 228 | + $serialized_entity['label'] = html_entity_decode(wl_shortcode_faceted_search_get_the_title($obj->ID), ENT_QUOTES, 'UTF-8'); |
|
| 231 | 229 | $serialized_entity['counter'] = $obj->counter; |
| 232 | 230 | $serialized_entity['createdAt'] = $entity->post_date; |
| 233 | 231 | $serialized_entity['referencedPosts'] = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
@@ -239,52 +237,52 @@ discard block |
||
| 239 | 237 | null, |
| 240 | 238 | $referencing_post_ids |
| 241 | 239 | ); |
| 242 | - $entity_results[] = $serialized_entity; |
|
| 240 | + $entity_results[] = $serialized_entity; |
|
| 243 | 241 | } |
| 244 | 242 | } |
| 245 | 243 | } |
| 246 | 244 | |
| 247 | - $post_results = apply_filters( 'wl_faceted_data_posts', $post_results, $faceted_id ); |
|
| 245 | + $post_results = apply_filters('wl_faceted_data_posts', $post_results, $faceted_id); |
|
| 248 | 246 | |
| 249 | 247 | // Add srcset attribute. |
| 250 | - $post_results = array_map( function ( $post ) { |
|
| 251 | - $post->srcset = Srcset_Util::get_srcset( $post->ID, Srcset_Util::FACETED_SEARCH_WIDGET ); |
|
| 248 | + $post_results = array_map(function($post) { |
|
| 249 | + $post->srcset = Srcset_Util::get_srcset($post->ID, Srcset_Util::FACETED_SEARCH_WIDGET); |
|
| 252 | 250 | |
| 253 | 251 | return $post; |
| 254 | - }, $post_results ); |
|
| 252 | + }, $post_results); |
|
| 255 | 253 | |
| 256 | - $entity_results = apply_filters( 'wl_faceted_data_entities', $entity_results, $faceted_id ); |
|
| 254 | + $entity_results = apply_filters('wl_faceted_data_entities', $entity_results, $faceted_id); |
|
| 257 | 255 | |
| 258 | 256 | return array( |
| 259 | - 'posts' => $amp ? array( array( 'values' => $post_results ) ) : $post_results, |
|
| 257 | + 'posts' => $amp ? array(array('values' => $post_results)) : $post_results, |
|
| 260 | 258 | 'entities' => $entity_results |
| 261 | 259 | ); |
| 262 | 260 | |
| 263 | 261 | } |
| 264 | 262 | |
| 265 | -function wl_shortcode_faceted_search_get_the_title( $post_id ) { |
|
| 263 | +function wl_shortcode_faceted_search_get_the_title($post_id) { |
|
| 266 | 264 | |
| 267 | - $title = strip_tags( get_the_title( $post_id ) ); |
|
| 265 | + $title = strip_tags(get_the_title($post_id)); |
|
| 268 | 266 | |
| 269 | - if ( get_post_type( $post_id ) !== Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 270 | - $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels( $post_id ); |
|
| 267 | + if (get_post_type($post_id) !== Wordlift_Entity_Service::TYPE_NAME) { |
|
| 268 | + $alternative_labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels($post_id); |
|
| 271 | 269 | |
| 272 | - if ( count( $alternative_labels ) > 0 ) { |
|
| 270 | + if (count($alternative_labels) > 0) { |
|
| 273 | 271 | $title = $alternative_labels[0]; |
| 274 | 272 | } |
| 275 | 273 | } |
| 276 | 274 | |
| 277 | - return remove_accents( $title ); |
|
| 275 | + return remove_accents($title); |
|
| 278 | 276 | |
| 279 | 277 | } |
| 280 | 278 | |
| 281 | 279 | /** |
| 282 | 280 | * Adding `rest_api_init` action for network faceted-search |
| 283 | 281 | */ |
| 284 | -add_action( 'rest_api_init', function () { |
|
| 285 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 282 | +add_action('rest_api_init', function() { |
|
| 283 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/faceted-search', array( |
|
| 286 | 284 | 'methods' => 'GET', |
| 287 | 285 | 'callback' => 'wl_shortcode_faceted_search', |
| 288 | 286 | 'permission_callback' => '__return_true', |
| 289 | - ) ); |
|
| 287 | + )); |
|
| 290 | 288 | } ); |
@@ -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 = (int) $_REQUEST['post_id']; |
|
| 203 | - $depth = (int) $_REQUEST['depth']; |
|
| 202 | + $post_id = (int) $_REQUEST['post_id']; |
|
| 203 | + $depth = (int) $_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' ); |
@@ -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,11 +201,11 @@ discard block |
||
| 202 | 201 | $post_id = (int) $_REQUEST['post_id']; |
| 203 | 202 | $depth = (int) $_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'); |
|