@@ -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 | } |
@@ -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,54 +20,54 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | function wl_entity_get_by_title( $title, $autocomplete = false, $include_alias = true ) { |
| 22 | 22 | |
| 23 | - global $wpdb; |
|
| 24 | - |
|
| 25 | - // Search by substring |
|
| 26 | - if ( $autocomplete ) { |
|
| 27 | - $title = '%' . $title . '%'; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - // The title is a LIKE query. |
|
| 31 | - $query = "SELECT DISTINCT p.ID AS id, p.post_title AS title, t.name AS schema_type_name, t.slug AS type_slug" |
|
| 32 | - . " FROM $wpdb->posts p, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t" |
|
| 33 | - . " WHERE p.post_title LIKE %s" |
|
| 34 | - . " AND t.term_id = tt.term_id" |
|
| 35 | - . " AND tt.taxonomy = %s" |
|
| 36 | - . " AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 37 | - . " AND tr.object_id = p.ID" |
|
| 38 | - // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
|
| 39 | - . " AND p.post_status != 'trash'"; |
|
| 40 | - |
|
| 41 | - $params = array( |
|
| 42 | - $title, |
|
| 43 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 44 | - ); |
|
| 45 | - |
|
| 46 | - if ( $include_alias ) { |
|
| 47 | - |
|
| 48 | - $query .= " UNION" |
|
| 49 | - . " SELECT DISTINCT p.ID AS id, CONCAT( m.meta_value, ' (', p.post_title, ')' ) AS title, t.name AS schema_type_name, t.slug AS type_slug" |
|
| 50 | - . " FROM $wpdb->posts p, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t, $wpdb->postmeta m" |
|
| 51 | - . " WHERE m.meta_key = %s AND m.meta_value LIKE %s" |
|
| 52 | - . " AND m.post_id = p.ID" |
|
| 53 | - . " AND t.term_id = tt.term_id" |
|
| 54 | - . " AND tt.taxonomy = %s" |
|
| 55 | - . " AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 56 | - . " AND tr.object_id = p.ID" |
|
| 57 | - // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
|
| 58 | - . " AND p.post_status != 'trash'"; |
|
| 59 | - |
|
| 60 | - $params = array_merge( $params, array( |
|
| 61 | - Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, |
|
| 62 | - $title, |
|
| 63 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 64 | - ) ); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - return $wpdb->get_results( $wpdb->prepare( |
|
| 68 | - $query, |
|
| 69 | - $params |
|
| 70 | - ) ); |
|
| 23 | + global $wpdb; |
|
| 24 | + |
|
| 25 | + // Search by substring |
|
| 26 | + if ( $autocomplete ) { |
|
| 27 | + $title = '%' . $title . '%'; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + // The title is a LIKE query. |
|
| 31 | + $query = "SELECT DISTINCT p.ID AS id, p.post_title AS title, t.name AS schema_type_name, t.slug AS type_slug" |
|
| 32 | + . " FROM $wpdb->posts p, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t" |
|
| 33 | + . " WHERE p.post_title LIKE %s" |
|
| 34 | + . " AND t.term_id = tt.term_id" |
|
| 35 | + . " AND tt.taxonomy = %s" |
|
| 36 | + . " AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 37 | + . " AND tr.object_id = p.ID" |
|
| 38 | + // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
|
| 39 | + . " AND p.post_status != 'trash'"; |
|
| 40 | + |
|
| 41 | + $params = array( |
|
| 42 | + $title, |
|
| 43 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 44 | + ); |
|
| 45 | + |
|
| 46 | + if ( $include_alias ) { |
|
| 47 | + |
|
| 48 | + $query .= " UNION" |
|
| 49 | + . " SELECT DISTINCT p.ID AS id, CONCAT( m.meta_value, ' (', p.post_title, ')' ) AS title, t.name AS schema_type_name, t.slug AS type_slug" |
|
| 50 | + . " FROM $wpdb->posts p, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t, $wpdb->postmeta m" |
|
| 51 | + . " WHERE m.meta_key = %s AND m.meta_value LIKE %s" |
|
| 52 | + . " AND m.post_id = p.ID" |
|
| 53 | + . " AND t.term_id = tt.term_id" |
|
| 54 | + . " AND tt.taxonomy = %s" |
|
| 55 | + . " AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 56 | + . " AND tr.object_id = p.ID" |
|
| 57 | + // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
|
| 58 | + . " AND p.post_status != 'trash'"; |
|
| 59 | + |
|
| 60 | + $params = array_merge( $params, array( |
|
| 61 | + Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, |
|
| 62 | + $title, |
|
| 63 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 64 | + ) ); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + return $wpdb->get_results( $wpdb->prepare( |
|
| 68 | + $query, |
|
| 69 | + $params |
|
| 70 | + ) ); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
@@ -77,42 +77,42 @@ discard block |
||
| 77 | 77 | */ |
| 78 | 78 | function wl_entity_ajax_get_by_title() { |
| 79 | 79 | |
| 80 | - // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
|
| 81 | - // |
|
| 82 | - // See https://github.com/insideout10/wordlift-plugin/issues/438. |
|
| 83 | - // Get the title to search. |
|
| 84 | - if ( empty( $_POST['title'] ) && empty( $_GET['title'] ) ) { |
|
| 85 | - @ob_clean(); |
|
| 86 | - wp_send_json_error( 'The title parameter is required.' ); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
|
| 90 | - // |
|
| 91 | - // See https://github.com/insideout10/wordlift-plugin/issues/438. |
|
| 92 | - $title = $_POST['title'] ?: $_GET['title']; |
|
| 93 | - $title = (string) $title; |
|
| 94 | - |
|
| 95 | - // Are we searching for a specific title or for a containing title? |
|
| 96 | - $autocomplete = isset( $_GET['autocomplete'] ); |
|
| 97 | - |
|
| 98 | - // Are we searching also for the aliases? |
|
| 99 | - $include_alias = isset( $_GET['alias'] ); |
|
| 100 | - |
|
| 101 | - // Get the edit link. |
|
| 102 | - $post_type_object = get_post_type_object( Wordlift_Entity_Service::TYPE_NAME ); |
|
| 103 | - $edit_link = $post_type_object->_edit_link . '&action=edit'; |
|
| 104 | - |
|
| 105 | - // Prepare the response with the edit link. |
|
| 106 | - $response = array( |
|
| 107 | - 'edit_link' => $edit_link, |
|
| 108 | - 'results' => wl_entity_get_by_title( $title, $autocomplete, $include_alias ), |
|
| 109 | - ); |
|
| 110 | - |
|
| 111 | - // Clean any buffer. |
|
| 112 | - @ob_clean(); |
|
| 113 | - |
|
| 114 | - // Send the success response. |
|
| 115 | - wp_send_json_success( $response ); |
|
| 80 | + // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
|
| 81 | + // |
|
| 82 | + // See https://github.com/insideout10/wordlift-plugin/issues/438. |
|
| 83 | + // Get the title to search. |
|
| 84 | + if ( empty( $_POST['title'] ) && empty( $_GET['title'] ) ) { |
|
| 85 | + @ob_clean(); |
|
| 86 | + wp_send_json_error( 'The title parameter is required.' ); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
|
| 90 | + // |
|
| 91 | + // See https://github.com/insideout10/wordlift-plugin/issues/438. |
|
| 92 | + $title = $_POST['title'] ?: $_GET['title']; |
|
| 93 | + $title = (string) $title; |
|
| 94 | + |
|
| 95 | + // Are we searching for a specific title or for a containing title? |
|
| 96 | + $autocomplete = isset( $_GET['autocomplete'] ); |
|
| 97 | + |
|
| 98 | + // Are we searching also for the aliases? |
|
| 99 | + $include_alias = isset( $_GET['alias'] ); |
|
| 100 | + |
|
| 101 | + // Get the edit link. |
|
| 102 | + $post_type_object = get_post_type_object( Wordlift_Entity_Service::TYPE_NAME ); |
|
| 103 | + $edit_link = $post_type_object->_edit_link . '&action=edit'; |
|
| 104 | + |
|
| 105 | + // Prepare the response with the edit link. |
|
| 106 | + $response = array( |
|
| 107 | + 'edit_link' => $edit_link, |
|
| 108 | + 'results' => wl_entity_get_by_title( $title, $autocomplete, $include_alias ), |
|
| 109 | + ); |
|
| 110 | + |
|
| 111 | + // Clean any buffer. |
|
| 112 | + @ob_clean(); |
|
| 113 | + |
|
| 114 | + // Send the success response. |
|
| 115 | + wp_send_json_success( $response ); |
|
| 116 | 116 | |
| 117 | 117 | } |
| 118 | 118 | |
@@ -18,13 +18,13 @@ discard block |
||
| 18 | 18 | * |
| 19 | 19 | * @return array An array of WP_Post instances. |
| 20 | 20 | */ |
| 21 | -function wl_entity_get_by_title( $title, $autocomplete = false, $include_alias = true ) { |
|
| 21 | +function wl_entity_get_by_title($title, $autocomplete = false, $include_alias = true) { |
|
| 22 | 22 | |
| 23 | 23 | global $wpdb; |
| 24 | 24 | |
| 25 | 25 | // Search by substring |
| 26 | - if ( $autocomplete ) { |
|
| 27 | - $title = '%' . $title . '%'; |
|
| 26 | + if ($autocomplete) { |
|
| 27 | + $title = '%'.$title.'%'; |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | // The title is a LIKE query. |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
| 44 | 44 | ); |
| 45 | 45 | |
| 46 | - if ( $include_alias ) { |
|
| 46 | + if ($include_alias) { |
|
| 47 | 47 | |
| 48 | 48 | $query .= " UNION" |
| 49 | 49 | . " SELECT DISTINCT p.ID AS id, CONCAT( m.meta_value, ' (', p.post_title, ')' ) AS title, t.name AS schema_type_name, t.slug AS type_slug" |
@@ -57,17 +57,17 @@ discard block |
||
| 57 | 57 | // Ensure we don't load entities from the trash, see https://github.com/insideout10/wordlift-plugin/issues/278. |
| 58 | 58 | . " AND p.post_status != 'trash'"; |
| 59 | 59 | |
| 60 | - $params = array_merge( $params, array( |
|
| 60 | + $params = array_merge($params, array( |
|
| 61 | 61 | Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, |
| 62 | 62 | $title, |
| 63 | 63 | Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
| 64 | - ) ); |
|
| 64 | + )); |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - return $wpdb->get_results( $wpdb->prepare( |
|
| 67 | + return $wpdb->get_results($wpdb->prepare( |
|
| 68 | 68 | $query, |
| 69 | 69 | $params |
| 70 | - ) ); |
|
| 70 | + )); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
@@ -81,9 +81,9 @@ discard block |
||
| 81 | 81 | // |
| 82 | 82 | // See https://github.com/insideout10/wordlift-plugin/issues/438. |
| 83 | 83 | // Get the title to search. |
| 84 | - if ( empty( $_POST['title'] ) && empty( $_GET['title'] ) ) { |
|
| 84 | + if (empty($_POST['title']) && empty($_GET['title'])) { |
|
| 85 | 85 | @ob_clean(); |
| 86 | - wp_send_json_error( 'The title parameter is required.' ); |
|
| 86 | + wp_send_json_error('The title parameter is required.'); |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | // `wl_entity_metaboxes_utilities.js` still uses `GET`. |
@@ -93,27 +93,27 @@ discard block |
||
| 93 | 93 | $title = (string) $title; |
| 94 | 94 | |
| 95 | 95 | // Are we searching for a specific title or for a containing title? |
| 96 | - $autocomplete = isset( $_GET['autocomplete'] ); |
|
| 96 | + $autocomplete = isset($_GET['autocomplete']); |
|
| 97 | 97 | |
| 98 | 98 | // Are we searching also for the aliases? |
| 99 | - $include_alias = isset( $_GET['alias'] ); |
|
| 99 | + $include_alias = isset($_GET['alias']); |
|
| 100 | 100 | |
| 101 | 101 | // Get the edit link. |
| 102 | - $post_type_object = get_post_type_object( Wordlift_Entity_Service::TYPE_NAME ); |
|
| 103 | - $edit_link = $post_type_object->_edit_link . '&action=edit'; |
|
| 102 | + $post_type_object = get_post_type_object(Wordlift_Entity_Service::TYPE_NAME); |
|
| 103 | + $edit_link = $post_type_object->_edit_link.'&action=edit'; |
|
| 104 | 104 | |
| 105 | 105 | // Prepare the response with the edit link. |
| 106 | 106 | $response = array( |
| 107 | 107 | 'edit_link' => $edit_link, |
| 108 | - 'results' => wl_entity_get_by_title( $title, $autocomplete, $include_alias ), |
|
| 108 | + 'results' => wl_entity_get_by_title($title, $autocomplete, $include_alias), |
|
| 109 | 109 | ); |
| 110 | 110 | |
| 111 | 111 | // Clean any buffer. |
| 112 | 112 | @ob_clean(); |
| 113 | 113 | |
| 114 | 114 | // Send the success response. |
| 115 | - wp_send_json_success( $response ); |
|
| 115 | + wp_send_json_success($response); |
|
| 116 | 116 | |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | -add_action( 'wp_ajax_entity_by_title', 'wl_entity_ajax_get_by_title' ); |
|
| 119 | +add_action('wp_ajax_entity_by_title', 'wl_entity_ajax_get_by_title'); |
|
@@ -13,62 +13,62 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Wordlift_Debug_Service { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * The {@link Wordlift_Entity_Service} instance. |
|
| 18 | - * |
|
| 19 | - * @since 3.7.2 |
|
| 20 | - * @access private |
|
| 21 | - * @var Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 22 | - */ |
|
| 23 | - private $entity_service; |
|
| 16 | + /** |
|
| 17 | + * The {@link Wordlift_Entity_Service} instance. |
|
| 18 | + * |
|
| 19 | + * @since 3.7.2 |
|
| 20 | + * @access private |
|
| 21 | + * @var Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 22 | + */ |
|
| 23 | + private $entity_service; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * A {@link Wordlift_Uri_Service} instance. |
|
| 27 | - * |
|
| 28 | - * @since 3.10.0 |
|
| 29 | - * @access private |
|
| 30 | - * @var \Wordlift_Uri_Service $uri_service A {@link Wordlift_Uri_Service} instance. |
|
| 31 | - */ |
|
| 32 | - private $uri_service; |
|
| 25 | + /** |
|
| 26 | + * A {@link Wordlift_Uri_Service} instance. |
|
| 27 | + * |
|
| 28 | + * @since 3.10.0 |
|
| 29 | + * @access private |
|
| 30 | + * @var \Wordlift_Uri_Service $uri_service A {@link Wordlift_Uri_Service} instance. |
|
| 31 | + */ |
|
| 32 | + private $uri_service; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Wordlift_Debug_Service constructor. |
|
| 36 | - * |
|
| 37 | - * @since 3.7.2 |
|
| 38 | - * |
|
| 39 | - * @param Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 40 | - * @param \Wordlift_Uri_Service $uri_service A {@link Wordlift_Uri_Service} instance. |
|
| 41 | - */ |
|
| 42 | - public function __construct( $entity_service, $uri_service ) { |
|
| 34 | + /** |
|
| 35 | + * Wordlift_Debug_Service constructor. |
|
| 36 | + * |
|
| 37 | + * @since 3.7.2 |
|
| 38 | + * |
|
| 39 | + * @param Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 40 | + * @param \Wordlift_Uri_Service $uri_service A {@link Wordlift_Uri_Service} instance. |
|
| 41 | + */ |
|
| 42 | + public function __construct( $entity_service, $uri_service ) { |
|
| 43 | 43 | |
| 44 | - $this->entity_service = $entity_service; |
|
| 45 | - $this->uri_service = $uri_service; |
|
| 44 | + $this->entity_service = $entity_service; |
|
| 45 | + $this->uri_service = $uri_service; |
|
| 46 | 46 | |
| 47 | - add_action( 'wp_ajax_wl_dump_uri', array( $this, 'dump_uri' ) ); |
|
| 47 | + add_action( 'wp_ajax_wl_dump_uri', array( $this, 'dump_uri' ) ); |
|
| 48 | 48 | |
| 49 | - } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - public function dump_uri() { |
|
| 51 | + public function dump_uri() { |
|
| 52 | 52 | |
| 53 | - if ( ! isset( $_GET['id'] ) ) { |
|
| 54 | - wp_send_json_error( 'id not set' ); |
|
| 55 | - } |
|
| 53 | + if ( ! isset( $_GET['id'] ) ) { |
|
| 54 | + wp_send_json_error( 'id not set' ); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - $post_id = (int) $_GET['id']; |
|
| 57 | + $post_id = (int) $_GET['id']; |
|
| 58 | 58 | |
| 59 | - $post = get_post( $post_id ); |
|
| 59 | + $post = get_post( $post_id ); |
|
| 60 | 60 | |
| 61 | - $uri = $this->entity_service->get_uri( $post_id ); |
|
| 62 | - $build_uri = $this->uri_service->build_uri( $post->post_title, $post->post_type ); |
|
| 61 | + $uri = $this->entity_service->get_uri( $post_id ); |
|
| 62 | + $build_uri = $this->uri_service->build_uri( $post->post_title, $post->post_type ); |
|
| 63 | 63 | |
| 64 | - wp_send_json_success( array( |
|
| 65 | - 'uri' => $uri, |
|
| 66 | - 'post_title' => sprintf( '%s (%s)', $post->post_title, mb_detect_encoding( $post->post_title ) ), |
|
| 67 | - 'post_title_ascii' => mb_convert_encoding( $post->post_title, 'ASCII' ), |
|
| 68 | - 'build_uri' => $build_uri, |
|
| 69 | - 'build_uri_convert' => mb_convert_encoding( $build_uri, 'ASCII' ), |
|
| 70 | - ) ); |
|
| 64 | + wp_send_json_success( array( |
|
| 65 | + 'uri' => $uri, |
|
| 66 | + 'post_title' => sprintf( '%s (%s)', $post->post_title, mb_detect_encoding( $post->post_title ) ), |
|
| 67 | + 'post_title_ascii' => mb_convert_encoding( $post->post_title, 'ASCII' ), |
|
| 68 | + 'build_uri' => $build_uri, |
|
| 69 | + 'build_uri_convert' => mb_convert_encoding( $build_uri, 'ASCII' ), |
|
| 70 | + ) ); |
|
| 71 | 71 | |
| 72 | - } |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | 74 | } |
| 75 | 75 | \ No newline at end of file |
@@ -39,35 +39,35 @@ |
||
| 39 | 39 | * @param Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
| 40 | 40 | * @param \Wordlift_Uri_Service $uri_service A {@link Wordlift_Uri_Service} instance. |
| 41 | 41 | */ |
| 42 | - public function __construct( $entity_service, $uri_service ) { |
|
| 42 | + public function __construct($entity_service, $uri_service) { |
|
| 43 | 43 | |
| 44 | 44 | $this->entity_service = $entity_service; |
| 45 | 45 | $this->uri_service = $uri_service; |
| 46 | 46 | |
| 47 | - add_action( 'wp_ajax_wl_dump_uri', array( $this, 'dump_uri' ) ); |
|
| 47 | + add_action('wp_ajax_wl_dump_uri', array($this, 'dump_uri')); |
|
| 48 | 48 | |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | public function dump_uri() { |
| 52 | 52 | |
| 53 | - if ( ! isset( $_GET['id'] ) ) { |
|
| 54 | - wp_send_json_error( 'id not set' ); |
|
| 53 | + if ( ! isset($_GET['id'])) { |
|
| 54 | + wp_send_json_error('id not set'); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | $post_id = (int) $_GET['id']; |
| 58 | 58 | |
| 59 | - $post = get_post( $post_id ); |
|
| 59 | + $post = get_post($post_id); |
|
| 60 | 60 | |
| 61 | - $uri = $this->entity_service->get_uri( $post_id ); |
|
| 62 | - $build_uri = $this->uri_service->build_uri( $post->post_title, $post->post_type ); |
|
| 61 | + $uri = $this->entity_service->get_uri($post_id); |
|
| 62 | + $build_uri = $this->uri_service->build_uri($post->post_title, $post->post_type); |
|
| 63 | 63 | |
| 64 | - wp_send_json_success( array( |
|
| 64 | + wp_send_json_success(array( |
|
| 65 | 65 | 'uri' => $uri, |
| 66 | - 'post_title' => sprintf( '%s (%s)', $post->post_title, mb_detect_encoding( $post->post_title ) ), |
|
| 67 | - 'post_title_ascii' => mb_convert_encoding( $post->post_title, 'ASCII' ), |
|
| 66 | + 'post_title' => sprintf('%s (%s)', $post->post_title, mb_detect_encoding($post->post_title)), |
|
| 67 | + 'post_title_ascii' => mb_convert_encoding($post->post_title, 'ASCII'), |
|
| 68 | 68 | 'build_uri' => $build_uri, |
| 69 | - 'build_uri_convert' => mb_convert_encoding( $build_uri, 'ASCII' ), |
|
| 70 | - ) ); |
|
| 69 | + 'build_uri_convert' => mb_convert_encoding($build_uri, 'ASCII'), |
|
| 70 | + )); |
|
| 71 | 71 | |
| 72 | 72 | } |
| 73 | 73 | |
@@ -16,367 +16,367 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Timeline_Service { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The Log service. |
|
| 21 | - * |
|
| 22 | - * @since 3.1.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 25 | - */ |
|
| 26 | - private $log; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The Entity service. |
|
| 30 | - * |
|
| 31 | - * @since 3.1.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Entity_Service $entity_service The Entity service. |
|
| 34 | - */ |
|
| 35 | - private $entity_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The number of words to use for the excerpt, set in the `to_json` function |
|
| 39 | - * and used by a filter. |
|
| 40 | - * |
|
| 41 | - * @since 3.7.0 |
|
| 42 | - * @access private |
|
| 43 | - * @var int $excerpt_length The number of words to use for the excerpt. |
|
| 44 | - */ |
|
| 45 | - private $excerpt_length; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * The {@link Wordlift_Entity_Type_Service} instance. |
|
| 49 | - * |
|
| 50 | - * @since 3.15.0 |
|
| 51 | - * @access private |
|
| 52 | - * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 53 | - */ |
|
| 54 | - private $entity_type_service; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * A singleton instance of the Timeline service (useful for unit tests). |
|
| 58 | - * |
|
| 59 | - * @since 3.1.0 |
|
| 60 | - * @access private |
|
| 61 | - * @var \Wordlift_Timeline_Service $instance The singleton instance. |
|
| 62 | - */ |
|
| 63 | - private static $instance; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Create a Wordlift_Timeline_Service instance. |
|
| 67 | - * |
|
| 68 | - * @param \Wordlift_Entity_Service $entity_service The Entity service. |
|
| 69 | - * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 70 | - * |
|
| 71 | - * @since 3.1.0 |
|
| 72 | - * |
|
| 73 | - */ |
|
| 74 | - public function __construct( $entity_service, $entity_type_service ) { |
|
| 75 | - |
|
| 76 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' ); |
|
| 77 | - |
|
| 78 | - $this->entity_service = $entity_service; |
|
| 79 | - $this->entity_type_service = $entity_type_service; |
|
| 80 | - |
|
| 81 | - self::$instance = $this; |
|
| 82 | - |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Get the singleton instance of the Wordlift_Timeline_Service |
|
| 87 | - * |
|
| 88 | - * @return \Wordlift_Timeline_Service The singleton instance of the Wordlift_Timeline_Service. |
|
| 89 | - * @since 3.1.0 |
|
| 90 | - * |
|
| 91 | - */ |
|
| 92 | - public static function get_instance() { |
|
| 93 | - |
|
| 94 | - return self::$instance; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Retrieve timeline events and output them in JSON. |
|
| 99 | - * |
|
| 100 | - * @since 3.1.0 |
|
| 101 | - */ |
|
| 102 | - public function ajax_timeline() { |
|
| 103 | - |
|
| 104 | - // Get the ID of the post who requested the timeline. |
|
| 105 | - $post_id = ( isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : null ); |
|
| 106 | - |
|
| 107 | - // Get the events and transform them for the JSON response, then send them to the client. |
|
| 108 | - wp_send_json( $this->to_json( $this->get_events( $post_id ) ) ); |
|
| 109 | - |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Retrieve timeline events. |
|
| 114 | - * |
|
| 115 | - * @param int $post_id The post ID. |
|
| 116 | - * |
|
| 117 | - * @return array An array of event posts. |
|
| 118 | - * @since 3.1.0 |
|
| 119 | - * |
|
| 120 | - * @uses wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post. |
|
| 121 | - * |
|
| 122 | - */ |
|
| 123 | - public function get_events( $post_id = null ) { |
|
| 124 | - |
|
| 125 | - // Get the entity IDs either from the entities related to the specified post or from the last 50 published |
|
| 126 | - // posts if no post has been specified. |
|
| 127 | - $ids = ( is_numeric( $post_id ) |
|
| 128 | - ? wl_core_get_related_entity_ids( $post_id ) |
|
| 129 | - : $this->get_all_related_to_last_50_published_posts() ); |
|
| 130 | - |
|
| 131 | - // Add the post itself if it's an entity. |
|
| 132 | - if ( is_numeric( $post_id ) && $this->entity_service->is_entity( $post_id ) ) { |
|
| 133 | - $ids[] = $post_id; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - // If there's no entities, return an empty array right away. |
|
| 137 | - if ( 0 === sizeof( $ids ) ) { |
|
| 138 | - $this->log->trace( "No events found [ post id :: $post_id ]" ); |
|
| 139 | - |
|
| 140 | - return array(); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - $this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' ); |
|
| 144 | - |
|
| 145 | - $args = array( |
|
| 146 | - 'post__in' => $ids, |
|
| 147 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 148 | - 'post_status' => 'publish', |
|
| 149 | - 'posts_per_page' => - 1, |
|
| 150 | - 'meta_query' => array( |
|
| 151 | - 'relation' => 'AND', |
|
| 152 | - array( |
|
| 153 | - 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 154 | - 'value' => null, |
|
| 155 | - 'compare' => '!=', |
|
| 156 | - ), |
|
| 157 | - array( |
|
| 158 | - 'key' => Wordlift_Schema_Service::FIELD_DATE_END, |
|
| 159 | - 'value' => null, |
|
| 160 | - 'compare' => '!=', |
|
| 161 | - ), |
|
| 162 | - ), |
|
| 163 | - 'tax_query' => array( |
|
| 164 | - array( |
|
| 165 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 166 | - 'field' => 'slug', |
|
| 167 | - 'terms' => 'event', |
|
| 168 | - ), |
|
| 169 | - ), |
|
| 170 | - 'orderby' => 'meta_value', |
|
| 171 | - 'order' => 'DESC', |
|
| 172 | - 'meta_key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 173 | - ); |
|
| 174 | - |
|
| 175 | - return get_posts( $args ); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Convert timeline events to JSON. This function sets the global post in order |
|
| 180 | - * to get an automatic excerpt. Since we're being called inside an AJAX request, |
|
| 181 | - * we're not taking care of restoring any previous post: there isn't any. |
|
| 182 | - * |
|
| 183 | - * @param array $posts An array of posts. |
|
| 184 | - * |
|
| 185 | - * @return array|string An array of timeline events or an empty string if no posts are provided. |
|
| 186 | - * @since 3.1.0 |
|
| 187 | - * |
|
| 188 | - */ |
|
| 189 | - public function to_json( $posts ) { |
|
| 190 | - |
|
| 191 | - // If there are no events, return empty JSON |
|
| 192 | - if ( empty( $posts ) || is_null( $posts ) ) { |
|
| 193 | - return ''; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background. |
|
| 197 | - $display_images_as = isset( $_REQUEST['display_images_as'] ) ? (string) $_REQUEST['display_images_as'] : 'media'; |
|
| 198 | - |
|
| 199 | - // The number of words for the excerpt (by default 55, as WordPress). |
|
| 200 | - $this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? $_REQUEST['excerpt_length'] : 55; |
|
| 201 | - add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 202 | - |
|
| 203 | - // Add a filter to remove the [...] after excerpts, since we're adding |
|
| 204 | - // a link to the post itself. |
|
| 205 | - add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) ); |
|
| 206 | - |
|
| 207 | - // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates. |
|
| 208 | - $start_at_slide = 0; |
|
| 209 | - $event_index = - 1; |
|
| 210 | - $now = time(); |
|
| 211 | - |
|
| 212 | - // Prepare the timeline variable. |
|
| 213 | - $timeline = array(); |
|
| 214 | - |
|
| 215 | - // Populate the arrays. |
|
| 216 | - $timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) { |
|
| 217 | - |
|
| 218 | - // Get the start and end dates. |
|
| 219 | - $start_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ); |
|
| 220 | - $end_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ); |
|
| 221 | - |
|
| 222 | - // Set the starting slide. |
|
| 223 | - $event_index ++; |
|
| 224 | - if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) { |
|
| 225 | - $start_at_slide = $event_index; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - // Load thumbnail |
|
| 229 | - $thumbnail_id = get_post_thumbnail_id( $item->ID ); |
|
| 230 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id |
|
| 231 | - && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) ) |
|
| 232 | - ) { |
|
| 233 | - |
|
| 234 | - // Set the thumbnail URL. |
|
| 235 | - if ( 'background' === $display_images_as ) { |
|
| 236 | - $date['background'] = array( 'url' => $attachment[0] ); |
|
| 237 | - $date['media'] = array( 'thumbnail' => $attachment[0] ); |
|
| 238 | - } else { |
|
| 239 | - $date['media'] = array( |
|
| 240 | - 'url' => $attachment[0], |
|
| 241 | - 'thumbnail' => $attachment[0], |
|
| 242 | - ); |
|
| 243 | - } |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - // Set the start/end dates by converting them to TimelineJS required format. |
|
| 247 | - $date['start_date'] = Wordlift_Timeline_Service::date( $start_date ); |
|
| 248 | - $date['end_date'] = Wordlift_Timeline_Service::date( $end_date ); |
|
| 249 | - |
|
| 250 | - setup_postdata( $GLOBALS['post'] = $item ); |
|
| 251 | - |
|
| 252 | - $more_link_text = sprintf( |
|
| 253 | - '<span aria-label="%1$s">%2$s</span>', |
|
| 254 | - sprintf( |
|
| 255 | - /* translators: %s: Name of current post */ |
|
| 256 | - __( 'Continue reading %s' ), |
|
| 257 | - the_title_attribute( array( 'echo' => false ) ) |
|
| 258 | - ), |
|
| 259 | - __( '(more…)' ) |
|
| 260 | - ); |
|
| 261 | - |
|
| 262 | - // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352). |
|
| 263 | - $date['text'] = array( |
|
| 264 | - 'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>', |
|
| 265 | - ); |
|
| 266 | - |
|
| 267 | - // If we have an excerpt, set it. |
|
| 268 | - if ( 0 < $excerpt_length ) { |
|
| 269 | - $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text ); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - return $date; |
|
| 273 | - |
|
| 274 | - }, $posts ); |
|
| 275 | - |
|
| 276 | - // Finally remove the excerpt filter. |
|
| 277 | - remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 278 | - |
|
| 279 | - // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html |
|
| 280 | - return array( |
|
| 281 | - 'timeline' => $timeline, |
|
| 282 | - 'start_at_slide' => $start_at_slide, |
|
| 283 | - ); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * This function filters {@link excerpt_more} by removing it, since we're |
|
| 288 | - * adding the 'read more' link. This filter is set by {@see to_json}. |
|
| 289 | - * |
|
| 290 | - * @param string $excerpt_more The excerpt more preset. |
|
| 291 | - * |
|
| 292 | - * @return string An empty string. |
|
| 293 | - * @since 3.7.0 |
|
| 294 | - * |
|
| 295 | - */ |
|
| 296 | - public function excerpt_more( $excerpt_more ) { |
|
| 297 | - |
|
| 298 | - return ''; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * A filter for the excerpt length, set by the `to_json` function, to tailor |
|
| 303 | - * how many words to return according to the client setting. |
|
| 304 | - * |
|
| 305 | - * @param int $length The preset number of words. |
|
| 306 | - * |
|
| 307 | - * @return int The number of words for the preset. |
|
| 308 | - * @since 3.7.0 |
|
| 309 | - * |
|
| 310 | - */ |
|
| 311 | - public function excerpt_length( $length ) { |
|
| 312 | - |
|
| 313 | - return $this->excerpt_length; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * Convert the date to a date array. |
|
| 319 | - * |
|
| 320 | - * @param $value int A date value. |
|
| 321 | - * |
|
| 322 | - * @return array An array containing year, month and day values. |
|
| 323 | - * @since 3.7.0 |
|
| 324 | - * |
|
| 325 | - */ |
|
| 326 | - public static function date( $value ) { |
|
| 327 | - |
|
| 328 | - return array( |
|
| 329 | - 'year' => (int) date( 'Y', $value ), |
|
| 330 | - 'month' => (int) date( 'm', $value ), |
|
| 331 | - 'day' => (int) date( 'd', $value ), |
|
| 332 | - |
|
| 333 | - ); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to |
|
| 338 | - * its specific function). |
|
| 339 | - * |
|
| 340 | - * @return array An array of post IDs. |
|
| 341 | - * @since 3.1.0 |
|
| 342 | - * |
|
| 343 | - */ |
|
| 344 | - public function get_all_related_to_last_50_published_posts() { |
|
| 345 | - |
|
| 346 | - // Global timeline. Get entities from the latest posts. |
|
| 347 | - $latest_posts_ids = get_posts( array( |
|
| 348 | - 'numberposts' => 50, |
|
| 349 | - 'fields' => 'ids', //only get post IDs |
|
| 350 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 351 | - 'tax_query' => array( |
|
| 352 | - 'relation' => 'OR', |
|
| 353 | - array( |
|
| 354 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 355 | - 'operator' => 'NOT EXISTS', |
|
| 356 | - ), |
|
| 357 | - array( |
|
| 358 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 359 | - 'field' => 'slug', |
|
| 360 | - 'terms' => 'article', |
|
| 361 | - ), |
|
| 362 | - ), |
|
| 363 | - 'post_status' => 'publish', |
|
| 364 | - ) ); |
|
| 365 | - |
|
| 366 | - if ( empty( $latest_posts_ids ) ) { |
|
| 367 | - // There are no posts. |
|
| 368 | - return array(); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - // Collect entities related to latest posts |
|
| 372 | - $entity_ids = array(); |
|
| 373 | - foreach ( $latest_posts_ids as $id ) { |
|
| 374 | - $entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array( |
|
| 375 | - 'status' => 'publish', |
|
| 376 | - ) ) ); |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - return $entity_ids; |
|
| 380 | - } |
|
| 19 | + /** |
|
| 20 | + * The Log service. |
|
| 21 | + * |
|
| 22 | + * @since 3.1.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 25 | + */ |
|
| 26 | + private $log; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The Entity service. |
|
| 30 | + * |
|
| 31 | + * @since 3.1.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Entity_Service $entity_service The Entity service. |
|
| 34 | + */ |
|
| 35 | + private $entity_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The number of words to use for the excerpt, set in the `to_json` function |
|
| 39 | + * and used by a filter. |
|
| 40 | + * |
|
| 41 | + * @since 3.7.0 |
|
| 42 | + * @access private |
|
| 43 | + * @var int $excerpt_length The number of words to use for the excerpt. |
|
| 44 | + */ |
|
| 45 | + private $excerpt_length; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * The {@link Wordlift_Entity_Type_Service} instance. |
|
| 49 | + * |
|
| 50 | + * @since 3.15.0 |
|
| 51 | + * @access private |
|
| 52 | + * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 53 | + */ |
|
| 54 | + private $entity_type_service; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * A singleton instance of the Timeline service (useful for unit tests). |
|
| 58 | + * |
|
| 59 | + * @since 3.1.0 |
|
| 60 | + * @access private |
|
| 61 | + * @var \Wordlift_Timeline_Service $instance The singleton instance. |
|
| 62 | + */ |
|
| 63 | + private static $instance; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Create a Wordlift_Timeline_Service instance. |
|
| 67 | + * |
|
| 68 | + * @param \Wordlift_Entity_Service $entity_service The Entity service. |
|
| 69 | + * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 70 | + * |
|
| 71 | + * @since 3.1.0 |
|
| 72 | + * |
|
| 73 | + */ |
|
| 74 | + public function __construct( $entity_service, $entity_type_service ) { |
|
| 75 | + |
|
| 76 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' ); |
|
| 77 | + |
|
| 78 | + $this->entity_service = $entity_service; |
|
| 79 | + $this->entity_type_service = $entity_type_service; |
|
| 80 | + |
|
| 81 | + self::$instance = $this; |
|
| 82 | + |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Get the singleton instance of the Wordlift_Timeline_Service |
|
| 87 | + * |
|
| 88 | + * @return \Wordlift_Timeline_Service The singleton instance of the Wordlift_Timeline_Service. |
|
| 89 | + * @since 3.1.0 |
|
| 90 | + * |
|
| 91 | + */ |
|
| 92 | + public static function get_instance() { |
|
| 93 | + |
|
| 94 | + return self::$instance; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Retrieve timeline events and output them in JSON. |
|
| 99 | + * |
|
| 100 | + * @since 3.1.0 |
|
| 101 | + */ |
|
| 102 | + public function ajax_timeline() { |
|
| 103 | + |
|
| 104 | + // Get the ID of the post who requested the timeline. |
|
| 105 | + $post_id = ( isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : null ); |
|
| 106 | + |
|
| 107 | + // Get the events and transform them for the JSON response, then send them to the client. |
|
| 108 | + wp_send_json( $this->to_json( $this->get_events( $post_id ) ) ); |
|
| 109 | + |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Retrieve timeline events. |
|
| 114 | + * |
|
| 115 | + * @param int $post_id The post ID. |
|
| 116 | + * |
|
| 117 | + * @return array An array of event posts. |
|
| 118 | + * @since 3.1.0 |
|
| 119 | + * |
|
| 120 | + * @uses wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post. |
|
| 121 | + * |
|
| 122 | + */ |
|
| 123 | + public function get_events( $post_id = null ) { |
|
| 124 | + |
|
| 125 | + // Get the entity IDs either from the entities related to the specified post or from the last 50 published |
|
| 126 | + // posts if no post has been specified. |
|
| 127 | + $ids = ( is_numeric( $post_id ) |
|
| 128 | + ? wl_core_get_related_entity_ids( $post_id ) |
|
| 129 | + : $this->get_all_related_to_last_50_published_posts() ); |
|
| 130 | + |
|
| 131 | + // Add the post itself if it's an entity. |
|
| 132 | + if ( is_numeric( $post_id ) && $this->entity_service->is_entity( $post_id ) ) { |
|
| 133 | + $ids[] = $post_id; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + // If there's no entities, return an empty array right away. |
|
| 137 | + if ( 0 === sizeof( $ids ) ) { |
|
| 138 | + $this->log->trace( "No events found [ post id :: $post_id ]" ); |
|
| 139 | + |
|
| 140 | + return array(); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + $this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' ); |
|
| 144 | + |
|
| 145 | + $args = array( |
|
| 146 | + 'post__in' => $ids, |
|
| 147 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 148 | + 'post_status' => 'publish', |
|
| 149 | + 'posts_per_page' => - 1, |
|
| 150 | + 'meta_query' => array( |
|
| 151 | + 'relation' => 'AND', |
|
| 152 | + array( |
|
| 153 | + 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 154 | + 'value' => null, |
|
| 155 | + 'compare' => '!=', |
|
| 156 | + ), |
|
| 157 | + array( |
|
| 158 | + 'key' => Wordlift_Schema_Service::FIELD_DATE_END, |
|
| 159 | + 'value' => null, |
|
| 160 | + 'compare' => '!=', |
|
| 161 | + ), |
|
| 162 | + ), |
|
| 163 | + 'tax_query' => array( |
|
| 164 | + array( |
|
| 165 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 166 | + 'field' => 'slug', |
|
| 167 | + 'terms' => 'event', |
|
| 168 | + ), |
|
| 169 | + ), |
|
| 170 | + 'orderby' => 'meta_value', |
|
| 171 | + 'order' => 'DESC', |
|
| 172 | + 'meta_key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 173 | + ); |
|
| 174 | + |
|
| 175 | + return get_posts( $args ); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Convert timeline events to JSON. This function sets the global post in order |
|
| 180 | + * to get an automatic excerpt. Since we're being called inside an AJAX request, |
|
| 181 | + * we're not taking care of restoring any previous post: there isn't any. |
|
| 182 | + * |
|
| 183 | + * @param array $posts An array of posts. |
|
| 184 | + * |
|
| 185 | + * @return array|string An array of timeline events or an empty string if no posts are provided. |
|
| 186 | + * @since 3.1.0 |
|
| 187 | + * |
|
| 188 | + */ |
|
| 189 | + public function to_json( $posts ) { |
|
| 190 | + |
|
| 191 | + // If there are no events, return empty JSON |
|
| 192 | + if ( empty( $posts ) || is_null( $posts ) ) { |
|
| 193 | + return ''; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background. |
|
| 197 | + $display_images_as = isset( $_REQUEST['display_images_as'] ) ? (string) $_REQUEST['display_images_as'] : 'media'; |
|
| 198 | + |
|
| 199 | + // The number of words for the excerpt (by default 55, as WordPress). |
|
| 200 | + $this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? $_REQUEST['excerpt_length'] : 55; |
|
| 201 | + add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 202 | + |
|
| 203 | + // Add a filter to remove the [...] after excerpts, since we're adding |
|
| 204 | + // a link to the post itself. |
|
| 205 | + add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) ); |
|
| 206 | + |
|
| 207 | + // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates. |
|
| 208 | + $start_at_slide = 0; |
|
| 209 | + $event_index = - 1; |
|
| 210 | + $now = time(); |
|
| 211 | + |
|
| 212 | + // Prepare the timeline variable. |
|
| 213 | + $timeline = array(); |
|
| 214 | + |
|
| 215 | + // Populate the arrays. |
|
| 216 | + $timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) { |
|
| 217 | + |
|
| 218 | + // Get the start and end dates. |
|
| 219 | + $start_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ); |
|
| 220 | + $end_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ); |
|
| 221 | + |
|
| 222 | + // Set the starting slide. |
|
| 223 | + $event_index ++; |
|
| 224 | + if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) { |
|
| 225 | + $start_at_slide = $event_index; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + // Load thumbnail |
|
| 229 | + $thumbnail_id = get_post_thumbnail_id( $item->ID ); |
|
| 230 | + if ( '' !== $thumbnail_id && 0 !== $thumbnail_id |
|
| 231 | + && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) ) |
|
| 232 | + ) { |
|
| 233 | + |
|
| 234 | + // Set the thumbnail URL. |
|
| 235 | + if ( 'background' === $display_images_as ) { |
|
| 236 | + $date['background'] = array( 'url' => $attachment[0] ); |
|
| 237 | + $date['media'] = array( 'thumbnail' => $attachment[0] ); |
|
| 238 | + } else { |
|
| 239 | + $date['media'] = array( |
|
| 240 | + 'url' => $attachment[0], |
|
| 241 | + 'thumbnail' => $attachment[0], |
|
| 242 | + ); |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + // Set the start/end dates by converting them to TimelineJS required format. |
|
| 247 | + $date['start_date'] = Wordlift_Timeline_Service::date( $start_date ); |
|
| 248 | + $date['end_date'] = Wordlift_Timeline_Service::date( $end_date ); |
|
| 249 | + |
|
| 250 | + setup_postdata( $GLOBALS['post'] = $item ); |
|
| 251 | + |
|
| 252 | + $more_link_text = sprintf( |
|
| 253 | + '<span aria-label="%1$s">%2$s</span>', |
|
| 254 | + sprintf( |
|
| 255 | + /* translators: %s: Name of current post */ |
|
| 256 | + __( 'Continue reading %s' ), |
|
| 257 | + the_title_attribute( array( 'echo' => false ) ) |
|
| 258 | + ), |
|
| 259 | + __( '(more…)' ) |
|
| 260 | + ); |
|
| 261 | + |
|
| 262 | + // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352). |
|
| 263 | + $date['text'] = array( |
|
| 264 | + 'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>', |
|
| 265 | + ); |
|
| 266 | + |
|
| 267 | + // If we have an excerpt, set it. |
|
| 268 | + if ( 0 < $excerpt_length ) { |
|
| 269 | + $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text ); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + return $date; |
|
| 273 | + |
|
| 274 | + }, $posts ); |
|
| 275 | + |
|
| 276 | + // Finally remove the excerpt filter. |
|
| 277 | + remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 278 | + |
|
| 279 | + // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html |
|
| 280 | + return array( |
|
| 281 | + 'timeline' => $timeline, |
|
| 282 | + 'start_at_slide' => $start_at_slide, |
|
| 283 | + ); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * This function filters {@link excerpt_more} by removing it, since we're |
|
| 288 | + * adding the 'read more' link. This filter is set by {@see to_json}. |
|
| 289 | + * |
|
| 290 | + * @param string $excerpt_more The excerpt more preset. |
|
| 291 | + * |
|
| 292 | + * @return string An empty string. |
|
| 293 | + * @since 3.7.0 |
|
| 294 | + * |
|
| 295 | + */ |
|
| 296 | + public function excerpt_more( $excerpt_more ) { |
|
| 297 | + |
|
| 298 | + return ''; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * A filter for the excerpt length, set by the `to_json` function, to tailor |
|
| 303 | + * how many words to return according to the client setting. |
|
| 304 | + * |
|
| 305 | + * @param int $length The preset number of words. |
|
| 306 | + * |
|
| 307 | + * @return int The number of words for the preset. |
|
| 308 | + * @since 3.7.0 |
|
| 309 | + * |
|
| 310 | + */ |
|
| 311 | + public function excerpt_length( $length ) { |
|
| 312 | + |
|
| 313 | + return $this->excerpt_length; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * Convert the date to a date array. |
|
| 319 | + * |
|
| 320 | + * @param $value int A date value. |
|
| 321 | + * |
|
| 322 | + * @return array An array containing year, month and day values. |
|
| 323 | + * @since 3.7.0 |
|
| 324 | + * |
|
| 325 | + */ |
|
| 326 | + public static function date( $value ) { |
|
| 327 | + |
|
| 328 | + return array( |
|
| 329 | + 'year' => (int) date( 'Y', $value ), |
|
| 330 | + 'month' => (int) date( 'm', $value ), |
|
| 331 | + 'day' => (int) date( 'd', $value ), |
|
| 332 | + |
|
| 333 | + ); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to |
|
| 338 | + * its specific function). |
|
| 339 | + * |
|
| 340 | + * @return array An array of post IDs. |
|
| 341 | + * @since 3.1.0 |
|
| 342 | + * |
|
| 343 | + */ |
|
| 344 | + public function get_all_related_to_last_50_published_posts() { |
|
| 345 | + |
|
| 346 | + // Global timeline. Get entities from the latest posts. |
|
| 347 | + $latest_posts_ids = get_posts( array( |
|
| 348 | + 'numberposts' => 50, |
|
| 349 | + 'fields' => 'ids', //only get post IDs |
|
| 350 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 351 | + 'tax_query' => array( |
|
| 352 | + 'relation' => 'OR', |
|
| 353 | + array( |
|
| 354 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 355 | + 'operator' => 'NOT EXISTS', |
|
| 356 | + ), |
|
| 357 | + array( |
|
| 358 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 359 | + 'field' => 'slug', |
|
| 360 | + 'terms' => 'article', |
|
| 361 | + ), |
|
| 362 | + ), |
|
| 363 | + 'post_status' => 'publish', |
|
| 364 | + ) ); |
|
| 365 | + |
|
| 366 | + if ( empty( $latest_posts_ids ) ) { |
|
| 367 | + // There are no posts. |
|
| 368 | + return array(); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + // Collect entities related to latest posts |
|
| 372 | + $entity_ids = array(); |
|
| 373 | + foreach ( $latest_posts_ids as $id ) { |
|
| 374 | + $entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array( |
|
| 375 | + 'status' => 'publish', |
|
| 376 | + ) ) ); |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + return $entity_ids; |
|
| 380 | + } |
|
| 381 | 381 | |
| 382 | 382 | } |
@@ -71,9 +71,9 @@ discard block |
||
| 71 | 71 | * @since 3.1.0 |
| 72 | 72 | * |
| 73 | 73 | */ |
| 74 | - public function __construct( $entity_service, $entity_type_service ) { |
|
| 74 | + public function __construct($entity_service, $entity_type_service) { |
|
| 75 | 75 | |
| 76 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' ); |
|
| 76 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Timeline_Service'); |
|
| 77 | 77 | |
| 78 | 78 | $this->entity_service = $entity_service; |
| 79 | 79 | $this->entity_type_service = $entity_type_service; |
@@ -102,10 +102,10 @@ discard block |
||
| 102 | 102 | public function ajax_timeline() { |
| 103 | 103 | |
| 104 | 104 | // Get the ID of the post who requested the timeline. |
| 105 | - $post_id = ( isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : null ); |
|
| 105 | + $post_id = (isset($_REQUEST['post_id']) ? (int) $_REQUEST['post_id'] : null); |
|
| 106 | 106 | |
| 107 | 107 | // Get the events and transform them for the JSON response, then send them to the client. |
| 108 | - wp_send_json( $this->to_json( $this->get_events( $post_id ) ) ); |
|
| 108 | + wp_send_json($this->to_json($this->get_events($post_id))); |
|
| 109 | 109 | |
| 110 | 110 | } |
| 111 | 111 | |
@@ -120,33 +120,33 @@ discard block |
||
| 120 | 120 | * @uses wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post. |
| 121 | 121 | * |
| 122 | 122 | */ |
| 123 | - public function get_events( $post_id = null ) { |
|
| 123 | + public function get_events($post_id = null) { |
|
| 124 | 124 | |
| 125 | 125 | // Get the entity IDs either from the entities related to the specified post or from the last 50 published |
| 126 | 126 | // posts if no post has been specified. |
| 127 | - $ids = ( is_numeric( $post_id ) |
|
| 128 | - ? wl_core_get_related_entity_ids( $post_id ) |
|
| 129 | - : $this->get_all_related_to_last_50_published_posts() ); |
|
| 127 | + $ids = (is_numeric($post_id) |
|
| 128 | + ? wl_core_get_related_entity_ids($post_id) |
|
| 129 | + : $this->get_all_related_to_last_50_published_posts()); |
|
| 130 | 130 | |
| 131 | 131 | // Add the post itself if it's an entity. |
| 132 | - if ( is_numeric( $post_id ) && $this->entity_service->is_entity( $post_id ) ) { |
|
| 132 | + if (is_numeric($post_id) && $this->entity_service->is_entity($post_id)) { |
|
| 133 | 133 | $ids[] = $post_id; |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | // If there's no entities, return an empty array right away. |
| 137 | - if ( 0 === sizeof( $ids ) ) { |
|
| 138 | - $this->log->trace( "No events found [ post id :: $post_id ]" ); |
|
| 137 | + if (0 === sizeof($ids)) { |
|
| 138 | + $this->log->trace("No events found [ post id :: $post_id ]"); |
|
| 139 | 139 | |
| 140 | 140 | return array(); |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | - $this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' ); |
|
| 143 | + $this->log->trace('Getting events [ entity ids :: '.join(', ', $ids).' ]'); |
|
| 144 | 144 | |
| 145 | 145 | $args = array( |
| 146 | 146 | 'post__in' => $ids, |
| 147 | 147 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
| 148 | 148 | 'post_status' => 'publish', |
| 149 | - 'posts_per_page' => - 1, |
|
| 149 | + 'posts_per_page' => -1, |
|
| 150 | 150 | 'meta_query' => array( |
| 151 | 151 | 'relation' => 'AND', |
| 152 | 152 | array( |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | 'meta_key' => Wordlift_Schema_Service::FIELD_DATE_START, |
| 173 | 173 | ); |
| 174 | 174 | |
| 175 | - return get_posts( $args ); |
|
| 175 | + return get_posts($args); |
|
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | /** |
@@ -186,23 +186,23 @@ discard block |
||
| 186 | 186 | * @since 3.1.0 |
| 187 | 187 | * |
| 188 | 188 | */ |
| 189 | - public function to_json( $posts ) { |
|
| 189 | + public function to_json($posts) { |
|
| 190 | 190 | |
| 191 | 191 | // If there are no events, return empty JSON |
| 192 | - if ( empty( $posts ) || is_null( $posts ) ) { |
|
| 192 | + if (empty($posts) || is_null($posts)) { |
|
| 193 | 193 | return ''; |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background. |
| 197 | - $display_images_as = isset( $_REQUEST['display_images_as'] ) ? (string) $_REQUEST['display_images_as'] : 'media'; |
|
| 197 | + $display_images_as = isset($_REQUEST['display_images_as']) ? (string) $_REQUEST['display_images_as'] : 'media'; |
|
| 198 | 198 | |
| 199 | 199 | // The number of words for the excerpt (by default 55, as WordPress). |
| 200 | - $this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? $_REQUEST['excerpt_length'] : 55; |
|
| 201 | - add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 200 | + $this->excerpt_length = $excerpt_length = isset($_REQUEST['excerpt_length']) && is_numeric($_REQUEST['excerpt_length']) ? $_REQUEST['excerpt_length'] : 55; |
|
| 201 | + add_filter('excerpt_length', array($this, 'excerpt_length')); |
|
| 202 | 202 | |
| 203 | 203 | // Add a filter to remove the [...] after excerpts, since we're adding |
| 204 | 204 | // a link to the post itself. |
| 205 | - add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) ); |
|
| 205 | + add_filter('excerpt_more', array($this, 'excerpt_more')); |
|
| 206 | 206 | |
| 207 | 207 | // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates. |
| 208 | 208 | $start_at_slide = 0; |
@@ -213,28 +213,28 @@ discard block |
||
| 213 | 213 | $timeline = array(); |
| 214 | 214 | |
| 215 | 215 | // Populate the arrays. |
| 216 | - $timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) { |
|
| 216 | + $timeline['events'] = array_map(function($item) use (&$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length) { |
|
| 217 | 217 | |
| 218 | 218 | // Get the start and end dates. |
| 219 | - $start_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ); |
|
| 220 | - $end_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ); |
|
| 219 | + $start_date = strtotime(get_post_meta($item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true)); |
|
| 220 | + $end_date = strtotime(get_post_meta($item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true)); |
|
| 221 | 221 | |
| 222 | 222 | // Set the starting slide. |
| 223 | - $event_index ++; |
|
| 224 | - if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) { |
|
| 223 | + $event_index++; |
|
| 224 | + if (0 === $start_at_slide && $now >= $start_date && $now <= $end_date) { |
|
| 225 | 225 | $start_at_slide = $event_index; |
| 226 | 226 | } |
| 227 | 227 | |
| 228 | 228 | // Load thumbnail |
| 229 | - $thumbnail_id = get_post_thumbnail_id( $item->ID ); |
|
| 230 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id |
|
| 231 | - && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) ) |
|
| 229 | + $thumbnail_id = get_post_thumbnail_id($item->ID); |
|
| 230 | + if ('' !== $thumbnail_id && 0 !== $thumbnail_id |
|
| 231 | + && false !== ($attachment = wp_get_attachment_image_src($thumbnail_id)) |
|
| 232 | 232 | ) { |
| 233 | 233 | |
| 234 | 234 | // Set the thumbnail URL. |
| 235 | - if ( 'background' === $display_images_as ) { |
|
| 236 | - $date['background'] = array( 'url' => $attachment[0] ); |
|
| 237 | - $date['media'] = array( 'thumbnail' => $attachment[0] ); |
|
| 235 | + if ('background' === $display_images_as) { |
|
| 236 | + $date['background'] = array('url' => $attachment[0]); |
|
| 237 | + $date['media'] = array('thumbnail' => $attachment[0]); |
|
| 238 | 238 | } else { |
| 239 | 239 | $date['media'] = array( |
| 240 | 240 | 'url' => $attachment[0], |
@@ -244,37 +244,37 @@ discard block |
||
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | // Set the start/end dates by converting them to TimelineJS required format. |
| 247 | - $date['start_date'] = Wordlift_Timeline_Service::date( $start_date ); |
|
| 248 | - $date['end_date'] = Wordlift_Timeline_Service::date( $end_date ); |
|
| 247 | + $date['start_date'] = Wordlift_Timeline_Service::date($start_date); |
|
| 248 | + $date['end_date'] = Wordlift_Timeline_Service::date($end_date); |
|
| 249 | 249 | |
| 250 | - setup_postdata( $GLOBALS['post'] = $item ); |
|
| 250 | + setup_postdata($GLOBALS['post'] = $item); |
|
| 251 | 251 | |
| 252 | 252 | $more_link_text = sprintf( |
| 253 | 253 | '<span aria-label="%1$s">%2$s</span>', |
| 254 | 254 | sprintf( |
| 255 | 255 | /* translators: %s: Name of current post */ |
| 256 | - __( 'Continue reading %s' ), |
|
| 257 | - the_title_attribute( array( 'echo' => false ) ) |
|
| 256 | + __('Continue reading %s'), |
|
| 257 | + the_title_attribute(array('echo' => false)) |
|
| 258 | 258 | ), |
| 259 | - __( '(more…)' ) |
|
| 259 | + __('(more…)') |
|
| 260 | 260 | ); |
| 261 | 261 | |
| 262 | 262 | // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352). |
| 263 | 263 | $date['text'] = array( |
| 264 | - 'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>', |
|
| 264 | + 'headline' => '<a href="'.get_permalink($item->ID).'">'.$item->post_title.'</a>', |
|
| 265 | 265 | ); |
| 266 | 266 | |
| 267 | 267 | // If we have an excerpt, set it. |
| 268 | - if ( 0 < $excerpt_length ) { |
|
| 269 | - $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text ); |
|
| 268 | + if (0 < $excerpt_length) { |
|
| 269 | + $date['text']['text'] = sprintf('%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text); |
|
| 270 | 270 | } |
| 271 | 271 | |
| 272 | 272 | return $date; |
| 273 | 273 | |
| 274 | - }, $posts ); |
|
| 274 | + }, $posts); |
|
| 275 | 275 | |
| 276 | 276 | // Finally remove the excerpt filter. |
| 277 | - remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 277 | + remove_filter('excerpt_length', array($this, 'excerpt_length')); |
|
| 278 | 278 | |
| 279 | 279 | // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html |
| 280 | 280 | return array( |
@@ -293,7 +293,7 @@ discard block |
||
| 293 | 293 | * @since 3.7.0 |
| 294 | 294 | * |
| 295 | 295 | */ |
| 296 | - public function excerpt_more( $excerpt_more ) { |
|
| 296 | + public function excerpt_more($excerpt_more) { |
|
| 297 | 297 | |
| 298 | 298 | return ''; |
| 299 | 299 | } |
@@ -308,7 +308,7 @@ discard block |
||
| 308 | 308 | * @since 3.7.0 |
| 309 | 309 | * |
| 310 | 310 | */ |
| 311 | - public function excerpt_length( $length ) { |
|
| 311 | + public function excerpt_length($length) { |
|
| 312 | 312 | |
| 313 | 313 | return $this->excerpt_length; |
| 314 | 314 | } |
@@ -323,12 +323,12 @@ discard block |
||
| 323 | 323 | * @since 3.7.0 |
| 324 | 324 | * |
| 325 | 325 | */ |
| 326 | - public static function date( $value ) { |
|
| 326 | + public static function date($value) { |
|
| 327 | 327 | |
| 328 | 328 | return array( |
| 329 | - 'year' => (int) date( 'Y', $value ), |
|
| 330 | - 'month' => (int) date( 'm', $value ), |
|
| 331 | - 'day' => (int) date( 'd', $value ), |
|
| 329 | + 'year' => (int) date('Y', $value), |
|
| 330 | + 'month' => (int) date('m', $value), |
|
| 331 | + 'day' => (int) date('d', $value), |
|
| 332 | 332 | |
| 333 | 333 | ); |
| 334 | 334 | } |
@@ -344,7 +344,7 @@ discard block |
||
| 344 | 344 | public function get_all_related_to_last_50_published_posts() { |
| 345 | 345 | |
| 346 | 346 | // Global timeline. Get entities from the latest posts. |
| 347 | - $latest_posts_ids = get_posts( array( |
|
| 347 | + $latest_posts_ids = get_posts(array( |
|
| 348 | 348 | 'numberposts' => 50, |
| 349 | 349 | 'fields' => 'ids', //only get post IDs |
| 350 | 350 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
@@ -361,19 +361,19 @@ discard block |
||
| 361 | 361 | ), |
| 362 | 362 | ), |
| 363 | 363 | 'post_status' => 'publish', |
| 364 | - ) ); |
|
| 364 | + )); |
|
| 365 | 365 | |
| 366 | - if ( empty( $latest_posts_ids ) ) { |
|
| 366 | + if (empty($latest_posts_ids)) { |
|
| 367 | 367 | // There are no posts. |
| 368 | 368 | return array(); |
| 369 | 369 | } |
| 370 | 370 | |
| 371 | 371 | // Collect entities related to latest posts |
| 372 | 372 | $entity_ids = array(); |
| 373 | - foreach ( $latest_posts_ids as $id ) { |
|
| 374 | - $entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array( |
|
| 373 | + foreach ($latest_posts_ids as $id) { |
|
| 374 | + $entity_ids = array_merge($entity_ids, wl_core_get_related_entity_ids($id, array( |
|
| 375 | 375 | 'status' => 'publish', |
| 376 | - ) ) ); |
|
| 376 | + ))); |
|
| 377 | 377 | } |
| 378 | 378 | |
| 379 | 379 | return $entity_ids; |
@@ -16,55 +16,55 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Publisher_Ajax_Adapter { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The {@link Wordlift_Publisher_Service} instance. |
|
| 21 | - * |
|
| 22 | - * @since 3.11.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
| 25 | - */ |
|
| 26 | - private $publisher_service; |
|
| 19 | + /** |
|
| 20 | + * The {@link Wordlift_Publisher_Service} instance. |
|
| 21 | + * |
|
| 22 | + * @since 3.11.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
| 25 | + */ |
|
| 26 | + private $publisher_service; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Create a {@link Wordlift_Publisher_Ajax_Adapter} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.11.0 |
|
| 32 | - * |
|
| 33 | - * @param \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
| 34 | - */ |
|
| 35 | - function __construct( $publisher_service ) { |
|
| 28 | + /** |
|
| 29 | + * Create a {@link Wordlift_Publisher_Ajax_Adapter} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.11.0 |
|
| 32 | + * |
|
| 33 | + * @param \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
| 34 | + */ |
|
| 35 | + function __construct( $publisher_service ) { |
|
| 36 | 36 | |
| 37 | - $this->publisher_service = $publisher_service; |
|
| 37 | + $this->publisher_service = $publisher_service; |
|
| 38 | 38 | |
| 39 | - } |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * The publisher AJAX action. This function is hook to the `wl_publisher` |
|
| 43 | - * action. |
|
| 44 | - * |
|
| 45 | - * @since 3.11.0 |
|
| 46 | - */ |
|
| 47 | - public function publisher() { |
|
| 41 | + /** |
|
| 42 | + * The publisher AJAX action. This function is hook to the `wl_publisher` |
|
| 43 | + * action. |
|
| 44 | + * |
|
| 45 | + * @since 3.11.0 |
|
| 46 | + */ |
|
| 47 | + public function publisher() { |
|
| 48 | 48 | |
| 49 | - // Ensure we don't have garbage before us. |
|
| 50 | - ob_clean(); |
|
| 49 | + // Ensure we don't have garbage before us. |
|
| 50 | + ob_clean(); |
|
| 51 | 51 | |
| 52 | - // Check if the current user can `manage_options`. |
|
| 53 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
| 54 | - wp_send_json_error( 'Access denied.' ); |
|
| 55 | - } |
|
| 52 | + // Check if the current user can `manage_options`. |
|
| 53 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
| 54 | + wp_send_json_error( 'Access denied.' ); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - // No actual search parameter was passed, bail out. |
|
| 58 | - if ( ! isset( $_POST['q'] ) || empty( $_POST['q'] ) ) { |
|
| 59 | - wp_send_json_error( 'The q parameter is required.' ); |
|
| 60 | - } |
|
| 57 | + // No actual search parameter was passed, bail out. |
|
| 58 | + if ( ! isset( $_POST['q'] ) || empty( $_POST['q'] ) ) { |
|
| 59 | + wp_send_json_error( 'The q parameter is required.' ); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - // Get the response. |
|
| 63 | - $response = $this->publisher_service->query( (string) $_POST['q'] ); |
|
| 62 | + // Get the response. |
|
| 63 | + $response = $this->publisher_service->query( (string) $_POST['q'] ); |
|
| 64 | 64 | |
| 65 | - // Finally output the response. |
|
| 66 | - wp_send_json_success( $response ); |
|
| 65 | + // Finally output the response. |
|
| 66 | + wp_send_json_success( $response ); |
|
| 67 | 67 | |
| 68 | - } |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | 70 | } |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | * |
| 33 | 33 | * @param \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
| 34 | 34 | */ |
| 35 | - function __construct( $publisher_service ) { |
|
| 35 | + function __construct($publisher_service) { |
|
| 36 | 36 | |
| 37 | 37 | $this->publisher_service = $publisher_service; |
| 38 | 38 | |
@@ -50,20 +50,20 @@ discard block |
||
| 50 | 50 | ob_clean(); |
| 51 | 51 | |
| 52 | 52 | // Check if the current user can `manage_options`. |
| 53 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
| 54 | - wp_send_json_error( 'Access denied.' ); |
|
| 53 | + if ( ! current_user_can('manage_options')) { |
|
| 54 | + wp_send_json_error('Access denied.'); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | // No actual search parameter was passed, bail out. |
| 58 | - if ( ! isset( $_POST['q'] ) || empty( $_POST['q'] ) ) { |
|
| 59 | - wp_send_json_error( 'The q parameter is required.' ); |
|
| 58 | + if ( ! isset($_POST['q']) || empty($_POST['q'])) { |
|
| 59 | + wp_send_json_error('The q parameter is required.'); |
|
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | // Get the response. |
| 63 | - $response = $this->publisher_service->query( (string) $_POST['q'] ); |
|
| 63 | + $response = $this->publisher_service->query((string) $_POST['q']); |
|
| 64 | 64 | |
| 65 | 65 | // Finally output the response. |
| 66 | - wp_send_json_success( $response ); |
|
| 66 | + wp_send_json_success($response); |
|
| 67 | 67 | |
| 68 | 68 | } |
| 69 | 69 | |