@@ -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'); |
|
@@ -20,60 +20,60 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | function wl_linked_data_save_post( $post_id ) { |
| 22 | 22 | |
| 23 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' ); |
|
| 24 | - |
|
| 25 | - $log->trace( "Saving post $post_id to Linked Data..." ); |
|
| 26 | - |
|
| 27 | - // If it's not numeric exit from here. |
|
| 28 | - if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
| 29 | - $log->debug( "Skipping $post_id, because id is not numeric or is a post revision." ); |
|
| 30 | - |
|
| 31 | - return; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - // Get the post type and check whether it supports the editor. |
|
| 35 | - // |
|
| 36 | - // @see https://github.com/insideout10/wordlift-plugin/issues/659. |
|
| 37 | - $post_type = get_post_type( $post_id ); |
|
| 38 | - /** |
|
| 39 | - * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting. |
|
| 40 | - * |
|
| 41 | - * @since 3.19.4 |
|
| 42 | - * |
|
| 43 | - * @see https://github.com/insideout10/wordlift-plugin/issues/847. |
|
| 44 | - */ |
|
| 45 | - $is_editor_supported = wl_post_type_supports_editor( $post_type ); |
|
| 46 | - |
|
| 47 | - // Bail out if it's not an entity. |
|
| 48 | - if ( ! $is_editor_supported ) { |
|
| 49 | - $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." ); |
|
| 50 | - |
|
| 51 | - return; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Only process valid post types |
|
| 56 | - * |
|
| 57 | - * @since 3.25.6 |
|
| 58 | - * |
|
| 59 | - */ |
|
| 60 | - $supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 61 | - |
|
| 62 | - // Bail out if it's not a valid entity. |
|
| 63 | - if ( ! in_array( $post_type, $supported_types ) ) { |
|
| 64 | - $log->debug( "Skipping $post_id, because $post_type is not a valid entity." ); |
|
| 65 | - |
|
| 66 | - return; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - // Unhook this function so it doesn't loop infinitely. |
|
| 70 | - remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 71 | - |
|
| 72 | - // raise the *wl_linked_data_save_post* event. |
|
| 73 | - do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 74 | - |
|
| 75 | - // Re-hook this function. |
|
| 76 | - add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 23 | + $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' ); |
|
| 24 | + |
|
| 25 | + $log->trace( "Saving post $post_id to Linked Data..." ); |
|
| 26 | + |
|
| 27 | + // If it's not numeric exit from here. |
|
| 28 | + if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
| 29 | + $log->debug( "Skipping $post_id, because id is not numeric or is a post revision." ); |
|
| 30 | + |
|
| 31 | + return; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + // Get the post type and check whether it supports the editor. |
|
| 35 | + // |
|
| 36 | + // @see https://github.com/insideout10/wordlift-plugin/issues/659. |
|
| 37 | + $post_type = get_post_type( $post_id ); |
|
| 38 | + /** |
|
| 39 | + * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting. |
|
| 40 | + * |
|
| 41 | + * @since 3.19.4 |
|
| 42 | + * |
|
| 43 | + * @see https://github.com/insideout10/wordlift-plugin/issues/847. |
|
| 44 | + */ |
|
| 45 | + $is_editor_supported = wl_post_type_supports_editor( $post_type ); |
|
| 46 | + |
|
| 47 | + // Bail out if it's not an entity. |
|
| 48 | + if ( ! $is_editor_supported ) { |
|
| 49 | + $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." ); |
|
| 50 | + |
|
| 51 | + return; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Only process valid post types |
|
| 56 | + * |
|
| 57 | + * @since 3.25.6 |
|
| 58 | + * |
|
| 59 | + */ |
|
| 60 | + $supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 61 | + |
|
| 62 | + // Bail out if it's not a valid entity. |
|
| 63 | + if ( ! in_array( $post_type, $supported_types ) ) { |
|
| 64 | + $log->debug( "Skipping $post_id, because $post_type is not a valid entity." ); |
|
| 65 | + |
|
| 66 | + return; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + // Unhook this function so it doesn't loop infinitely. |
|
| 70 | + remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 71 | + |
|
| 72 | + // raise the *wl_linked_data_save_post* event. |
|
| 73 | + do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 74 | + |
|
| 75 | + // Re-hook this function. |
|
| 76 | + add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | add_action( 'save_post', 'wl_linked_data_save_post' ); |
@@ -88,176 +88,176 @@ discard block |
||
| 88 | 88 | */ |
| 89 | 89 | function wl_linked_data_save_post_and_related_entities( $post_id ) { |
| 90 | 90 | |
| 91 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 91 | + $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 92 | 92 | |
| 93 | - $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 93 | + $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 94 | 94 | |
| 95 | - // Ignore auto-saves |
|
| 96 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 97 | - $log->trace( 'Doing autosave, skipping...' ); |
|
| 95 | + // Ignore auto-saves |
|
| 96 | + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 97 | + $log->trace( 'Doing autosave, skipping...' ); |
|
| 98 | 98 | |
| 99 | - return; |
|
| 100 | - } |
|
| 99 | + return; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - // get the current post. |
|
| 103 | - $post = get_post( $post_id ); |
|
| 102 | + // get the current post. |
|
| 103 | + $post = get_post( $post_id ); |
|
| 104 | 104 | |
| 105 | - remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 105 | + remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 106 | 106 | |
| 107 | - // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
|
| 107 | + // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
|
| 108 | 108 | |
| 109 | - // Get the entity service instance. |
|
| 110 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 109 | + // Get the entity service instance. |
|
| 110 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 111 | 111 | |
| 112 | - // Store mapping between tmp new entities uris and real new entities uri |
|
| 113 | - $entities_uri_mapping = array(); |
|
| 112 | + // Store mapping between tmp new entities uris and real new entities uri |
|
| 113 | + $entities_uri_mapping = array(); |
|
| 114 | 114 | |
| 115 | - // Save the entities coming with POST data. |
|
| 116 | - if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 117 | - |
|
| 118 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 119 | - wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 120 | - wl_write_log( "]" ); |
|
| 121 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 122 | - wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 123 | - wl_write_log( "]" ); |
|
| 115 | + // Save the entities coming with POST data. |
|
| 116 | + if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 117 | + |
|
| 118 | + wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 119 | + wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 120 | + wl_write_log( "]" ); |
|
| 121 | + wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 122 | + wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 123 | + wl_write_log( "]" ); |
|
| 124 | 124 | |
| 125 | - $entities_via_post = $_POST['wl_entities']; |
|
| 126 | - |
|
| 127 | - foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 125 | + $entities_via_post = $_POST['wl_entities']; |
|
| 126 | + |
|
| 127 | + foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 128 | 128 | |
| 129 | - // Only if the current entity is created from scratch let's avoid to |
|
| 130 | - // create more than one entity with same label & entity type. |
|
| 131 | - $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 132 | - $entity['main_type'] : null; |
|
| 133 | - |
|
| 134 | - // Look if current entity uri matches an internal existing entity, meaning: |
|
| 135 | - // 1. when $entity_uri is an internal uri |
|
| 136 | - // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
| 137 | - $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 129 | + // Only if the current entity is created from scratch let's avoid to |
|
| 130 | + // create more than one entity with same label & entity type. |
|
| 131 | + $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 132 | + $entity['main_type'] : null; |
|
| 133 | + |
|
| 134 | + // Look if current entity uri matches an internal existing entity, meaning: |
|
| 135 | + // 1. when $entity_uri is an internal uri |
|
| 136 | + // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
| 137 | + $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 138 | 138 | |
| 139 | - // Dont save the entities which are not found, but also local. |
|
| 140 | - if ( $ie === null && Wordlift_Entity_Uri_Service::get_instance()->is_internal( $entity_uri ) ) { |
|
| 141 | - continue; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - // Detect the uri depending if is an existing or a new entity |
|
| 145 | - $uri = ( null === $ie ) ? |
|
| 146 | - Wordlift_Uri_Service::get_instance()->build_uri( |
|
| 147 | - $entity['label'], |
|
| 148 | - Wordlift_Entity_Service::TYPE_NAME, |
|
| 149 | - $entity_type |
|
| 150 | - ) : wl_get_entity_uri( $ie->ID ); |
|
| 151 | - |
|
| 152 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
| 153 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 154 | - |
|
| 155 | - // Local entities have a tmp uri with 'local-entity-' prefix |
|
| 156 | - // These uris need to be rewritten here and replaced in the content |
|
| 157 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 158 | - // Override the entity obj |
|
| 159 | - $entity['uri'] = $uri; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - // Update entity data with related post |
|
| 163 | - $entity['related_post_id'] = $post_id; |
|
| 164 | - |
|
| 165 | - // Save the entity if is a new entity |
|
| 166 | - if ( null === $ie ) { |
|
| 167 | - wl_save_entity( $entity ); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - // Replace tmp uris in content post if needed |
|
| 175 | - $updated_post_content = $post->post_content; |
|
| 176 | - |
|
| 177 | - // Update the post content if we found mappings of new entities. |
|
| 178 | - if ( ! empty( $entities_uri_mapping ) ) { |
|
| 179 | - // Save each entity and store the post id. |
|
| 180 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 181 | - if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) { |
|
| 182 | - continue; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - // Update the post content. |
|
| 189 | - /** |
|
| 190 | - * Note: wp_update_post do stripslashes before saving the |
|
| 191 | - * content, so add the slashes to prevent back slash getting |
|
| 192 | - * removed. |
|
| 193 | - */ |
|
| 194 | - wp_update_post( array( |
|
| 195 | - 'ID' => $post->ID, |
|
| 196 | - 'post_content' => addslashes( $updated_post_content ), |
|
| 197 | - ) ); |
|
| 198 | - } |
|
| 199 | - // Reset previously saved instances. |
|
| 200 | - wl_core_delete_relation_instances( $post_id ); |
|
| 201 | - |
|
| 202 | - |
|
| 203 | - $relations = Object_Relation_Service::get_instance() |
|
| 204 | - ->get_relations_from_content( $updated_post_content, Object_Type_Enum::POST ); |
|
| 205 | - |
|
| 206 | - // Save relation instances |
|
| 207 | - foreach ( $relations as $relation ) { |
|
| 208 | - |
|
| 209 | - wl_core_add_relation_instance( |
|
| 210 | - // subject id. |
|
| 211 | - $post_id, |
|
| 212 | - // what, where, when, who |
|
| 213 | - $relation->get_relation_type(), |
|
| 214 | - // object id. |
|
| 215 | - $relation->get_object_id(), |
|
| 216 | - // Subject type. |
|
| 217 | - $relation->get_subject_type(), |
|
| 218 | - // Object type. |
|
| 219 | - $relation->get_object_type() |
|
| 220 | - ); |
|
| 221 | - |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - if ( isset( $_POST['wl_entities'] ) ) { |
|
| 225 | - // Save post metadata if available |
|
| 226 | - $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? |
|
| 227 | - $_POST['wl_metadata'] : array(); |
|
| 228 | - |
|
| 229 | - $fields = array( |
|
| 230 | - Wordlift_Schema_Service::FIELD_LOCATION_CREATED, |
|
| 231 | - Wordlift_Schema_Service::FIELD_TOPIC, |
|
| 232 | - ); |
|
| 233 | - |
|
| 234 | - // Unlink topic taxonomy terms |
|
| 235 | - Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 236 | - |
|
| 237 | - foreach ( $fields as $field ) { |
|
| 238 | - |
|
| 239 | - // Delete current values |
|
| 240 | - delete_post_meta( $post->ID, $field ); |
|
| 241 | - // Retrieve the entity uri |
|
| 242 | - $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 243 | - stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 244 | - |
|
| 245 | - $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
| 246 | - |
|
| 247 | - if ( $entity ) { |
|
| 248 | - add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 249 | - // Set also the topic taxonomy |
|
| 250 | - if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) { |
|
| 251 | - Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - // Push the post to Redlink. |
|
| 258 | - do_action( 'wl_legacy_linked_data__push', $post->ID ); |
|
| 259 | - |
|
| 260 | - add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 139 | + // Dont save the entities which are not found, but also local. |
|
| 140 | + if ( $ie === null && Wordlift_Entity_Uri_Service::get_instance()->is_internal( $entity_uri ) ) { |
|
| 141 | + continue; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + // Detect the uri depending if is an existing or a new entity |
|
| 145 | + $uri = ( null === $ie ) ? |
|
| 146 | + Wordlift_Uri_Service::get_instance()->build_uri( |
|
| 147 | + $entity['label'], |
|
| 148 | + Wordlift_Entity_Service::TYPE_NAME, |
|
| 149 | + $entity_type |
|
| 150 | + ) : wl_get_entity_uri( $ie->ID ); |
|
| 151 | + |
|
| 152 | + wl_write_log( "Map $entity_uri on $uri" ); |
|
| 153 | + $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 154 | + |
|
| 155 | + // Local entities have a tmp uri with 'local-entity-' prefix |
|
| 156 | + // These uris need to be rewritten here and replaced in the content |
|
| 157 | + if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 158 | + // Override the entity obj |
|
| 159 | + $entity['uri'] = $uri; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + // Update entity data with related post |
|
| 163 | + $entity['related_post_id'] = $post_id; |
|
| 164 | + |
|
| 165 | + // Save the entity if is a new entity |
|
| 166 | + if ( null === $ie ) { |
|
| 167 | + wl_save_entity( $entity ); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + // Replace tmp uris in content post if needed |
|
| 175 | + $updated_post_content = $post->post_content; |
|
| 176 | + |
|
| 177 | + // Update the post content if we found mappings of new entities. |
|
| 178 | + if ( ! empty( $entities_uri_mapping ) ) { |
|
| 179 | + // Save each entity and store the post id. |
|
| 180 | + foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 181 | + if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) { |
|
| 182 | + continue; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + // Update the post content. |
|
| 189 | + /** |
|
| 190 | + * Note: wp_update_post do stripslashes before saving the |
|
| 191 | + * content, so add the slashes to prevent back slash getting |
|
| 192 | + * removed. |
|
| 193 | + */ |
|
| 194 | + wp_update_post( array( |
|
| 195 | + 'ID' => $post->ID, |
|
| 196 | + 'post_content' => addslashes( $updated_post_content ), |
|
| 197 | + ) ); |
|
| 198 | + } |
|
| 199 | + // Reset previously saved instances. |
|
| 200 | + wl_core_delete_relation_instances( $post_id ); |
|
| 201 | + |
|
| 202 | + |
|
| 203 | + $relations = Object_Relation_Service::get_instance() |
|
| 204 | + ->get_relations_from_content( $updated_post_content, Object_Type_Enum::POST ); |
|
| 205 | + |
|
| 206 | + // Save relation instances |
|
| 207 | + foreach ( $relations as $relation ) { |
|
| 208 | + |
|
| 209 | + wl_core_add_relation_instance( |
|
| 210 | + // subject id. |
|
| 211 | + $post_id, |
|
| 212 | + // what, where, when, who |
|
| 213 | + $relation->get_relation_type(), |
|
| 214 | + // object id. |
|
| 215 | + $relation->get_object_id(), |
|
| 216 | + // Subject type. |
|
| 217 | + $relation->get_subject_type(), |
|
| 218 | + // Object type. |
|
| 219 | + $relation->get_object_type() |
|
| 220 | + ); |
|
| 221 | + |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + if ( isset( $_POST['wl_entities'] ) ) { |
|
| 225 | + // Save post metadata if available |
|
| 226 | + $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? |
|
| 227 | + $_POST['wl_metadata'] : array(); |
|
| 228 | + |
|
| 229 | + $fields = array( |
|
| 230 | + Wordlift_Schema_Service::FIELD_LOCATION_CREATED, |
|
| 231 | + Wordlift_Schema_Service::FIELD_TOPIC, |
|
| 232 | + ); |
|
| 233 | + |
|
| 234 | + // Unlink topic taxonomy terms |
|
| 235 | + Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 236 | + |
|
| 237 | + foreach ( $fields as $field ) { |
|
| 238 | + |
|
| 239 | + // Delete current values |
|
| 240 | + delete_post_meta( $post->ID, $field ); |
|
| 241 | + // Retrieve the entity uri |
|
| 242 | + $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 243 | + stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 244 | + |
|
| 245 | + $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
| 246 | + |
|
| 247 | + if ( $entity ) { |
|
| 248 | + add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 249 | + // Set also the topic taxonomy |
|
| 250 | + if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) { |
|
| 251 | + Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + // Push the post to Redlink. |
|
| 258 | + do_action( 'wl_legacy_linked_data__push', $post->ID ); |
|
| 259 | + |
|
| 260 | + add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
@@ -280,203 +280,203 @@ discard block |
||
| 280 | 280 | */ |
| 281 | 281 | function wl_save_entity( $entity_data ) { |
| 282 | 282 | |
| 283 | - // Required for REST API calls |
|
| 284 | - if ( ! function_exists( 'wp_crop_image' ) ) { |
|
| 285 | - require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
|
| 286 | - } |
|
| 283 | + // Required for REST API calls |
|
| 284 | + if ( ! function_exists( 'wp_crop_image' ) ) { |
|
| 285 | + require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
|
| 286 | + } |
|
| 287 | 287 | |
| 288 | - $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
| 288 | + $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
| 289 | 289 | |
| 290 | - $uri = $entity_data['uri']; |
|
| 291 | - /* |
|
| 290 | + $uri = $entity_data['uri']; |
|
| 291 | + /* |
|
| 292 | 292 | * Data is coming from a $_POST, sanitize it. |
| 293 | 293 | * |
| 294 | 294 | * @since 3.19.4 |
| 295 | 295 | * |
| 296 | 296 | * @see https://github.com/insideout10/wordlift-plugin/issues/841 |
| 297 | 297 | */ |
| 298 | - $label = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) ); |
|
| 299 | - $type_uri = $entity_data['main_type']; |
|
| 300 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 301 | - $description = $entity_data['description']; |
|
| 302 | - $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
| 303 | - $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
| 304 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 305 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 306 | - |
|
| 307 | - // Get the synonyms. |
|
| 308 | - $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
| 309 | - |
|
| 310 | - // Check whether an entity already exists with the provided URI. |
|
| 311 | - if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 312 | - $log->debug( "Post already exists for URI $uri." ); |
|
| 313 | - |
|
| 314 | - return $post; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - // Prepare properties of the new entity. |
|
| 318 | - $params = array( |
|
| 319 | - 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 320 | - 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 321 | - 'post_title' => $label, |
|
| 322 | - 'post_content' => $description, |
|
| 323 | - 'post_excerpt' => '', |
|
| 324 | - // Ensure we're using a valid slug. We're not overwriting an existing |
|
| 325 | - // entity with a post_name already set, since this call is made only for |
|
| 326 | - // new entities. |
|
| 327 | - // |
|
| 328 | - // See https://github.com/insideout10/wordlift-plugin/issues/282 |
|
| 329 | - 'post_name' => sanitize_title( $label ), |
|
| 330 | - ); |
|
| 331 | - |
|
| 332 | - // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
| 333 | - // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 334 | - // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
| 335 | - // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
| 336 | - // is created when saving a post. |
|
| 337 | - global $wpseo_metabox, $seo_ultimate; |
|
| 338 | - if ( isset( $wpseo_metabox ) ) { |
|
| 339 | - remove_action( 'wp_insert_post', array( |
|
| 340 | - $wpseo_metabox, |
|
| 341 | - 'save_postdata', |
|
| 342 | - ) ); |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - if ( isset( $seo_ultimate ) ) { |
|
| 346 | - remove_action( 'save_post', array( |
|
| 347 | - $seo_ultimate, |
|
| 348 | - 'save_postmeta_box', |
|
| 349 | - ) ); |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
| 353 | - // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
| 354 | - // to the save_post and restore them after we saved the entity. |
|
| 355 | - // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
| 356 | - // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
| 357 | - // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
| 358 | - global $wp_filter; |
|
| 359 | - $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 360 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 361 | - |
|
| 362 | - |
|
| 363 | - $log->trace( 'Going to insert new post...' ); |
|
| 364 | - |
|
| 365 | - // create or update the post. |
|
| 366 | - $post_id = wp_insert_post( $params, true ); |
|
| 367 | - |
|
| 368 | - // Setting the alternative labels for this entity. |
|
| 369 | - Wordlift_Entity_Service::get_instance() |
|
| 370 | - ->set_alternative_labels( $post_id, $synonyms ); |
|
| 371 | - |
|
| 372 | - // Restore all the existing filters. |
|
| 373 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 374 | - |
|
| 375 | - // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 376 | - if ( isset( $wpseo_metabox ) ) { |
|
| 377 | - add_action( 'wp_insert_post', array( |
|
| 378 | - $wpseo_metabox, |
|
| 379 | - 'save_postdata', |
|
| 380 | - ) ); |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
| 384 | - if ( isset( $seo_ultimate ) ) { |
|
| 385 | - add_action( 'save_post', array( |
|
| 386 | - $seo_ultimate, |
|
| 387 | - 'save_postmeta_box', |
|
| 388 | - ), 10, 2 ); |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - // TODO: handle errors. |
|
| 392 | - if ( is_wp_error( $post_id ) ) { |
|
| 393 | - $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
| 394 | - |
|
| 395 | - // inform an error occurred. |
|
| 396 | - return null; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 400 | - |
|
| 401 | - // Save the entity types. |
|
| 402 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 403 | - |
|
| 404 | - // Get a dataset URI for the entity. |
|
| 405 | - $wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id ); |
|
| 406 | - |
|
| 407 | - // Add the uri to the sameAs data if it's not a local URI. |
|
| 408 | - if ( $wl_uri !== $uri ) { |
|
| 409 | - array_push( $same_as, $uri ); |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - // Save the sameAs data for the entity. |
|
| 413 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 414 | - |
|
| 415 | - // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
| 416 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
| 417 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - // Call hooks. |
|
| 421 | - do_action( 'wl_save_entity', $post_id ); |
|
| 422 | - |
|
| 423 | - foreach ( $images as $image_remote_url ) { |
|
| 424 | - |
|
| 425 | - // Check if image is already present in local DB |
|
| 426 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 427 | - // Do nothing. |
|
| 428 | - continue; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - // Check if there is an existing attachment for this post ID and source URL. |
|
| 432 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 433 | - |
|
| 434 | - // Skip if an existing image is found. |
|
| 435 | - if ( null !== $existing_image ) { |
|
| 436 | - continue; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - // Save the image and get the local path. |
|
| 440 | - $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
| 441 | - |
|
| 442 | - if ( false === $image || is_wp_error( $image ) ) { |
|
| 443 | - continue; |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - // Get the local URL. |
|
| 447 | - $filename = $image['path']; |
|
| 448 | - $url = $image['url']; |
|
| 449 | - $content_type = $image['content_type']; |
|
| 450 | - |
|
| 451 | - $attachment = array( |
|
| 452 | - 'guid' => $url, |
|
| 453 | - // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
| 454 | - 'post_title' => $label, |
|
| 455 | - // Set the title to the post title. |
|
| 456 | - 'post_content' => '', |
|
| 457 | - 'post_status' => 'inherit', |
|
| 458 | - 'post_mime_type' => $content_type, |
|
| 459 | - ); |
|
| 298 | + $label = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) ); |
|
| 299 | + $type_uri = $entity_data['main_type']; |
|
| 300 | + $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 301 | + $description = $entity_data['description']; |
|
| 302 | + $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
| 303 | + $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
| 304 | + $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 305 | + $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 306 | + |
|
| 307 | + // Get the synonyms. |
|
| 308 | + $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
| 309 | + |
|
| 310 | + // Check whether an entity already exists with the provided URI. |
|
| 311 | + if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 312 | + $log->debug( "Post already exists for URI $uri." ); |
|
| 313 | + |
|
| 314 | + return $post; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + // Prepare properties of the new entity. |
|
| 318 | + $params = array( |
|
| 319 | + 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 320 | + 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 321 | + 'post_title' => $label, |
|
| 322 | + 'post_content' => $description, |
|
| 323 | + 'post_excerpt' => '', |
|
| 324 | + // Ensure we're using a valid slug. We're not overwriting an existing |
|
| 325 | + // entity with a post_name already set, since this call is made only for |
|
| 326 | + // new entities. |
|
| 327 | + // |
|
| 328 | + // See https://github.com/insideout10/wordlift-plugin/issues/282 |
|
| 329 | + 'post_name' => sanitize_title( $label ), |
|
| 330 | + ); |
|
| 331 | + |
|
| 332 | + // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
| 333 | + // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 334 | + // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
| 335 | + // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
| 336 | + // is created when saving a post. |
|
| 337 | + global $wpseo_metabox, $seo_ultimate; |
|
| 338 | + if ( isset( $wpseo_metabox ) ) { |
|
| 339 | + remove_action( 'wp_insert_post', array( |
|
| 340 | + $wpseo_metabox, |
|
| 341 | + 'save_postdata', |
|
| 342 | + ) ); |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + if ( isset( $seo_ultimate ) ) { |
|
| 346 | + remove_action( 'save_post', array( |
|
| 347 | + $seo_ultimate, |
|
| 348 | + 'save_postmeta_box', |
|
| 349 | + ) ); |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
| 353 | + // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
| 354 | + // to the save_post and restore them after we saved the entity. |
|
| 355 | + // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
| 356 | + // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
| 357 | + // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
| 358 | + global $wp_filter; |
|
| 359 | + $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 360 | + is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 361 | + |
|
| 362 | + |
|
| 363 | + $log->trace( 'Going to insert new post...' ); |
|
| 364 | + |
|
| 365 | + // create or update the post. |
|
| 366 | + $post_id = wp_insert_post( $params, true ); |
|
| 367 | + |
|
| 368 | + // Setting the alternative labels for this entity. |
|
| 369 | + Wordlift_Entity_Service::get_instance() |
|
| 370 | + ->set_alternative_labels( $post_id, $synonyms ); |
|
| 371 | + |
|
| 372 | + // Restore all the existing filters. |
|
| 373 | + is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 374 | + |
|
| 375 | + // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 376 | + if ( isset( $wpseo_metabox ) ) { |
|
| 377 | + add_action( 'wp_insert_post', array( |
|
| 378 | + $wpseo_metabox, |
|
| 379 | + 'save_postdata', |
|
| 380 | + ) ); |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
| 384 | + if ( isset( $seo_ultimate ) ) { |
|
| 385 | + add_action( 'save_post', array( |
|
| 386 | + $seo_ultimate, |
|
| 387 | + 'save_postmeta_box', |
|
| 388 | + ), 10, 2 ); |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + // TODO: handle errors. |
|
| 392 | + if ( is_wp_error( $post_id ) ) { |
|
| 393 | + $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
| 394 | + |
|
| 395 | + // inform an error occurred. |
|
| 396 | + return null; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 400 | + |
|
| 401 | + // Save the entity types. |
|
| 402 | + wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 403 | + |
|
| 404 | + // Get a dataset URI for the entity. |
|
| 405 | + $wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id ); |
|
| 406 | + |
|
| 407 | + // Add the uri to the sameAs data if it's not a local URI. |
|
| 408 | + if ( $wl_uri !== $uri ) { |
|
| 409 | + array_push( $same_as, $uri ); |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + // Save the sameAs data for the entity. |
|
| 413 | + wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 414 | + |
|
| 415 | + // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
| 416 | + foreach ( $other_properties as $property_name => $property_value ) { |
|
| 417 | + wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + // Call hooks. |
|
| 421 | + do_action( 'wl_save_entity', $post_id ); |
|
| 422 | + |
|
| 423 | + foreach ( $images as $image_remote_url ) { |
|
| 424 | + |
|
| 425 | + // Check if image is already present in local DB |
|
| 426 | + if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 427 | + // Do nothing. |
|
| 428 | + continue; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + // Check if there is an existing attachment for this post ID and source URL. |
|
| 432 | + $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 433 | + |
|
| 434 | + // Skip if an existing image is found. |
|
| 435 | + if ( null !== $existing_image ) { |
|
| 436 | + continue; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + // Save the image and get the local path. |
|
| 440 | + $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
| 441 | + |
|
| 442 | + if ( false === $image || is_wp_error( $image ) ) { |
|
| 443 | + continue; |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + // Get the local URL. |
|
| 447 | + $filename = $image['path']; |
|
| 448 | + $url = $image['url']; |
|
| 449 | + $content_type = $image['content_type']; |
|
| 450 | + |
|
| 451 | + $attachment = array( |
|
| 452 | + 'guid' => $url, |
|
| 453 | + // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
| 454 | + 'post_title' => $label, |
|
| 455 | + // Set the title to the post title. |
|
| 456 | + 'post_content' => '', |
|
| 457 | + 'post_status' => 'inherit', |
|
| 458 | + 'post_mime_type' => $content_type, |
|
| 459 | + ); |
|
| 460 | 460 | |
| 461 | - // Create the attachment in WordPress and generate the related metadata. |
|
| 462 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 463 | - |
|
| 464 | - // Set the source URL for the image. |
|
| 465 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 466 | - |
|
| 467 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 468 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 469 | - |
|
| 470 | - // Set it as the featured image. |
|
| 471 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - // The entity is pushed to Redlink on save by the function hooked to save_post. |
|
| 475 | - // save the entity in the triple store. |
|
| 476 | - do_action( 'wl_legacy_linked_data__push', $post_id ); |
|
| 477 | - |
|
| 478 | - // finally return the entity post. |
|
| 479 | - return get_post( $post_id ); |
|
| 461 | + // Create the attachment in WordPress and generate the related metadata. |
|
| 462 | + $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 463 | + |
|
| 464 | + // Set the source URL for the image. |
|
| 465 | + wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 466 | + |
|
| 467 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 468 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 469 | + |
|
| 470 | + // Set it as the featured image. |
|
| 471 | + set_post_thumbnail( $post_id, $attachment_id ); |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + // The entity is pushed to Redlink on save by the function hooked to save_post. |
|
| 475 | + // save the entity in the triple store. |
|
| 476 | + do_action( 'wl_legacy_linked_data__push', $post_id ); |
|
| 477 | + |
|
| 478 | + // finally return the entity post. |
|
| 479 | + return get_post( $post_id ); |
|
| 480 | 480 | } |
| 481 | 481 | |
| 482 | 482 | /** |
@@ -490,41 +490,41 @@ discard block |
||
| 490 | 490 | */ |
| 491 | 491 | function wl_linked_data_content_get_embedded_entities( $content ) { |
| 492 | 492 | |
| 493 | - // Remove quote escapes. |
|
| 494 | - $content = str_replace( '\\"', '"', $content ); |
|
| 493 | + // Remove quote escapes. |
|
| 494 | + $content = str_replace( '\\"', '"', $content ); |
|
| 495 | 495 | |
| 496 | - // Match all itemid attributes. |
|
| 497 | - $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
|
| 496 | + // Match all itemid attributes. |
|
| 497 | + $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
|
| 498 | 498 | |
| 499 | - // wl_write_log( "Getting entities embedded into content [ pattern :: $pattern ]" ); |
|
| 499 | + // wl_write_log( "Getting entities embedded into content [ pattern :: $pattern ]" ); |
|
| 500 | 500 | |
| 501 | - // Remove the pattern while it is found (match nested annotations). |
|
| 502 | - $matches = array(); |
|
| 501 | + // Remove the pattern while it is found (match nested annotations). |
|
| 502 | + $matches = array(); |
|
| 503 | 503 | |
| 504 | - // In case of errors, return an empty array. |
|
| 505 | - if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 506 | - wl_write_log( "Found no entities embedded in content" ); |
|
| 504 | + // In case of errors, return an empty array. |
|
| 505 | + if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 506 | + wl_write_log( "Found no entities embedded in content" ); |
|
| 507 | 507 | |
| 508 | - return array(); |
|
| 509 | - } |
|
| 508 | + return array(); |
|
| 509 | + } |
|
| 510 | 510 | |
| 511 | 511 | // wl_write_log("wl_update_related_entities [ content :: $content ][ data :: " . var_export($data, true). " ][ matches :: " . var_export($matches, true) . " ]"); |
| 512 | 512 | |
| 513 | - $uris = $matches[1]; |
|
| 514 | - // Collect the entities. |
|
| 515 | - $entities = array(); |
|
| 516 | - foreach ( $uris as $uri ) { |
|
| 517 | - $uri_d = html_entity_decode( $uri ); |
|
| 513 | + $uris = $matches[1]; |
|
| 514 | + // Collect the entities. |
|
| 515 | + $entities = array(); |
|
| 516 | + foreach ( $uris as $uri ) { |
|
| 517 | + $uri_d = html_entity_decode( $uri ); |
|
| 518 | 518 | |
| 519 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 519 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 520 | 520 | |
| 521 | - if ( null !== $entity ) { |
|
| 522 | - array_push( $entities, $entity->ID ); |
|
| 523 | - } |
|
| 524 | - } |
|
| 521 | + if ( null !== $entity ) { |
|
| 522 | + array_push( $entities, $entity->ID ); |
|
| 523 | + } |
|
| 524 | + } |
|
| 525 | 525 | |
| 526 | - // $count = sizeof( $entities ); |
|
| 527 | - // wl_write_log( "Found $count entities embedded in content" ); |
|
| 526 | + // $count = sizeof( $entities ); |
|
| 527 | + // wl_write_log( "Found $count entities embedded in content" ); |
|
| 528 | 528 | |
| 529 | - return $entities; |
|
| 529 | + return $entities; |
|
| 530 | 530 | } |
| 531 | 531 | \ No newline at end of file |
@@ -18,15 +18,15 @@ discard block |
||
| 18 | 18 | * @since 3.0.0 |
| 19 | 19 | * |
| 20 | 20 | */ |
| 21 | -function wl_linked_data_save_post( $post_id ) { |
|
| 21 | +function wl_linked_data_save_post($post_id) { |
|
| 22 | 22 | |
| 23 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' ); |
|
| 23 | + $log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post'); |
|
| 24 | 24 | |
| 25 | - $log->trace( "Saving post $post_id to Linked Data..." ); |
|
| 25 | + $log->trace("Saving post $post_id to Linked Data..."); |
|
| 26 | 26 | |
| 27 | 27 | // If it's not numeric exit from here. |
| 28 | - if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
| 29 | - $log->debug( "Skipping $post_id, because id is not numeric or is a post revision." ); |
|
| 28 | + if ( ! is_numeric($post_id) || is_numeric(wp_is_post_revision($post_id))) { |
|
| 29 | + $log->debug("Skipping $post_id, because id is not numeric or is a post revision."); |
|
| 30 | 30 | |
| 31 | 31 | return; |
| 32 | 32 | } |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | // Get the post type and check whether it supports the editor. |
| 35 | 35 | // |
| 36 | 36 | // @see https://github.com/insideout10/wordlift-plugin/issues/659. |
| 37 | - $post_type = get_post_type( $post_id ); |
|
| 37 | + $post_type = get_post_type($post_id); |
|
| 38 | 38 | /** |
| 39 | 39 | * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting. |
| 40 | 40 | * |
@@ -42,11 +42,11 @@ discard block |
||
| 42 | 42 | * |
| 43 | 43 | * @see https://github.com/insideout10/wordlift-plugin/issues/847. |
| 44 | 44 | */ |
| 45 | - $is_editor_supported = wl_post_type_supports_editor( $post_type ); |
|
| 45 | + $is_editor_supported = wl_post_type_supports_editor($post_type); |
|
| 46 | 46 | |
| 47 | 47 | // Bail out if it's not an entity. |
| 48 | - if ( ! $is_editor_supported ) { |
|
| 49 | - $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." ); |
|
| 48 | + if ( ! $is_editor_supported) { |
|
| 49 | + $log->debug("Skipping $post_id, because $post_type doesn't support the editor (content)."); |
|
| 50 | 50 | |
| 51 | 51 | return; |
| 52 | 52 | } |
@@ -60,23 +60,23 @@ discard block |
||
| 60 | 60 | $supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
| 61 | 61 | |
| 62 | 62 | // Bail out if it's not a valid entity. |
| 63 | - if ( ! in_array( $post_type, $supported_types ) ) { |
|
| 64 | - $log->debug( "Skipping $post_id, because $post_type is not a valid entity." ); |
|
| 63 | + if ( ! in_array($post_type, $supported_types)) { |
|
| 64 | + $log->debug("Skipping $post_id, because $post_type is not a valid entity."); |
|
| 65 | 65 | |
| 66 | 66 | return; |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | // Unhook this function so it doesn't loop infinitely. |
| 70 | - remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 70 | + remove_action('save_post', 'wl_linked_data_save_post'); |
|
| 71 | 71 | |
| 72 | 72 | // raise the *wl_linked_data_save_post* event. |
| 73 | - do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 73 | + do_action('wl_linked_data_save_post', $post_id); |
|
| 74 | 74 | |
| 75 | 75 | // Re-hook this function. |
| 76 | - add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 76 | + add_action('save_post', 'wl_linked_data_save_post'); |
|
| 77 | 77 | } |
| 78 | 78 | |
| 79 | -add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 79 | +add_action('save_post', 'wl_linked_data_save_post'); |
|
| 80 | 80 | |
| 81 | 81 | /** |
| 82 | 82 | * Save the post to the triple store. Also saves the entities locally and on the triple store. |
@@ -86,23 +86,23 @@ discard block |
||
| 86 | 86 | * @since 3.0.0 |
| 87 | 87 | * |
| 88 | 88 | */ |
| 89 | -function wl_linked_data_save_post_and_related_entities( $post_id ) { |
|
| 89 | +function wl_linked_data_save_post_and_related_entities($post_id) { |
|
| 90 | 90 | |
| 91 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
| 91 | + $log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post_and_related_entities'); |
|
| 92 | 92 | |
| 93 | - $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
| 93 | + $log->trace("Saving $post_id to Linked Data along with related entities..."); |
|
| 94 | 94 | |
| 95 | 95 | // Ignore auto-saves |
| 96 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 97 | - $log->trace( 'Doing autosave, skipping...' ); |
|
| 96 | + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
|
| 97 | + $log->trace('Doing autosave, skipping...'); |
|
| 98 | 98 | |
| 99 | 99 | return; |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | // get the current post. |
| 103 | - $post = get_post( $post_id ); |
|
| 103 | + $post = get_post($post_id); |
|
| 104 | 104 | |
| 105 | - remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 105 | + remove_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 106 | 106 | |
| 107 | 107 | // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
| 108 | 108 | |
@@ -113,48 +113,48 @@ discard block |
||
| 113 | 113 | $entities_uri_mapping = array(); |
| 114 | 114 | |
| 115 | 115 | // Save the entities coming with POST data. |
| 116 | - if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 116 | + if (isset($_POST['wl_entities']) && isset($_POST['wl_boxes'])) { |
|
| 117 | 117 | |
| 118 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 119 | - wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 120 | - wl_write_log( "]" ); |
|
| 121 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 122 | - wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 123 | - wl_write_log( "]" ); |
|
| 118 | + wl_write_log("[ post id :: $post_id ][ POST(wl_entities) :: "); |
|
| 119 | + wl_write_log(json_encode($_POST['wl_entities'])); |
|
| 120 | + wl_write_log("]"); |
|
| 121 | + wl_write_log("[ post id :: $post_id ][ POST(wl_boxes) :: "); |
|
| 122 | + wl_write_log(json_encode($_POST['wl_boxes'], true)); |
|
| 123 | + wl_write_log("]"); |
|
| 124 | 124 | |
| 125 | 125 | $entities_via_post = $_POST['wl_entities']; |
| 126 | 126 | |
| 127 | - foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 127 | + foreach ($entities_via_post as $entity_uri => $entity) { |
|
| 128 | 128 | |
| 129 | 129 | // Only if the current entity is created from scratch let's avoid to |
| 130 | 130 | // create more than one entity with same label & entity type. |
| 131 | - $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 131 | + $entity_type = (preg_match('/^local-entity-.+/', $entity_uri) > 0) ? |
|
| 132 | 132 | $entity['main_type'] : null; |
| 133 | 133 | |
| 134 | 134 | // Look if current entity uri matches an internal existing entity, meaning: |
| 135 | 135 | // 1. when $entity_uri is an internal uri |
| 136 | 136 | // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
| 137 | - $ie = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
| 137 | + $ie = $entity_service->get_entity_post_by_uri($entity_uri); |
|
| 138 | 138 | |
| 139 | 139 | // Dont save the entities which are not found, but also local. |
| 140 | - if ( $ie === null && Wordlift_Entity_Uri_Service::get_instance()->is_internal( $entity_uri ) ) { |
|
| 140 | + if ($ie === null && Wordlift_Entity_Uri_Service::get_instance()->is_internal($entity_uri)) { |
|
| 141 | 141 | continue; |
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | // Detect the uri depending if is an existing or a new entity |
| 145 | - $uri = ( null === $ie ) ? |
|
| 145 | + $uri = (null === $ie) ? |
|
| 146 | 146 | Wordlift_Uri_Service::get_instance()->build_uri( |
| 147 | 147 | $entity['label'], |
| 148 | 148 | Wordlift_Entity_Service::TYPE_NAME, |
| 149 | 149 | $entity_type |
| 150 | - ) : wl_get_entity_uri( $ie->ID ); |
|
| 150 | + ) : wl_get_entity_uri($ie->ID); |
|
| 151 | 151 | |
| 152 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
| 153 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 152 | + wl_write_log("Map $entity_uri on $uri"); |
|
| 153 | + $entities_uri_mapping[$entity_uri] = $uri; |
|
| 154 | 154 | |
| 155 | 155 | // Local entities have a tmp uri with 'local-entity-' prefix |
| 156 | 156 | // These uris need to be rewritten here and replaced in the content |
| 157 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 157 | + if (preg_match('/^local-entity-.+/', $entity_uri) > 0) { |
|
| 158 | 158 | // Override the entity obj |
| 159 | 159 | $entity['uri'] = $uri; |
| 160 | 160 | } |
@@ -163,8 +163,8 @@ discard block |
||
| 163 | 163 | $entity['related_post_id'] = $post_id; |
| 164 | 164 | |
| 165 | 165 | // Save the entity if is a new entity |
| 166 | - if ( null === $ie ) { |
|
| 167 | - wl_save_entity( $entity ); |
|
| 166 | + if (null === $ie) { |
|
| 167 | + wl_save_entity($entity); |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | } |
@@ -175,14 +175,14 @@ discard block |
||
| 175 | 175 | $updated_post_content = $post->post_content; |
| 176 | 176 | |
| 177 | 177 | // Update the post content if we found mappings of new entities. |
| 178 | - if ( ! empty( $entities_uri_mapping ) ) { |
|
| 178 | + if ( ! empty($entities_uri_mapping)) { |
|
| 179 | 179 | // Save each entity and store the post id. |
| 180 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 181 | - if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) { |
|
| 180 | + foreach ($entities_uri_mapping as $tmp_uri => $uri) { |
|
| 181 | + if (1 !== preg_match('@^(https?://|local-entity-)@', $tmp_uri)) { |
|
| 182 | 182 | continue; |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 185 | + $updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | // Update the post content. |
@@ -191,20 +191,20 @@ discard block |
||
| 191 | 191 | * content, so add the slashes to prevent back slash getting |
| 192 | 192 | * removed. |
| 193 | 193 | */ |
| 194 | - wp_update_post( array( |
|
| 194 | + wp_update_post(array( |
|
| 195 | 195 | 'ID' => $post->ID, |
| 196 | - 'post_content' => addslashes( $updated_post_content ), |
|
| 197 | - ) ); |
|
| 196 | + 'post_content' => addslashes($updated_post_content), |
|
| 197 | + )); |
|
| 198 | 198 | } |
| 199 | 199 | // Reset previously saved instances. |
| 200 | - wl_core_delete_relation_instances( $post_id ); |
|
| 200 | + wl_core_delete_relation_instances($post_id); |
|
| 201 | 201 | |
| 202 | 202 | |
| 203 | 203 | $relations = Object_Relation_Service::get_instance() |
| 204 | - ->get_relations_from_content( $updated_post_content, Object_Type_Enum::POST ); |
|
| 204 | + ->get_relations_from_content($updated_post_content, Object_Type_Enum::POST); |
|
| 205 | 205 | |
| 206 | 206 | // Save relation instances |
| 207 | - foreach ( $relations as $relation ) { |
|
| 207 | + foreach ($relations as $relation) { |
|
| 208 | 208 | |
| 209 | 209 | wl_core_add_relation_instance( |
| 210 | 210 | // subject id. |
@@ -221,9 +221,9 @@ discard block |
||
| 221 | 221 | |
| 222 | 222 | } |
| 223 | 223 | |
| 224 | - if ( isset( $_POST['wl_entities'] ) ) { |
|
| 224 | + if (isset($_POST['wl_entities'])) { |
|
| 225 | 225 | // Save post metadata if available |
| 226 | - $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? |
|
| 226 | + $metadata_via_post = (isset($_POST['wl_metadata'])) ? |
|
| 227 | 227 | $_POST['wl_metadata'] : array(); |
| 228 | 228 | |
| 229 | 229 | $fields = array( |
@@ -232,35 +232,35 @@ discard block |
||
| 232 | 232 | ); |
| 233 | 233 | |
| 234 | 234 | // Unlink topic taxonomy terms |
| 235 | - Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 235 | + Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for($post->ID); |
|
| 236 | 236 | |
| 237 | - foreach ( $fields as $field ) { |
|
| 237 | + foreach ($fields as $field) { |
|
| 238 | 238 | |
| 239 | 239 | // Delete current values |
| 240 | - delete_post_meta( $post->ID, $field ); |
|
| 240 | + delete_post_meta($post->ID, $field); |
|
| 241 | 241 | // Retrieve the entity uri |
| 242 | - $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 243 | - stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 242 | + $uri = (isset($metadata_via_post[$field])) ? |
|
| 243 | + stripslashes($metadata_via_post[$field]) : ''; |
|
| 244 | 244 | |
| 245 | - $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
| 245 | + $entity = $entity_service->get_entity_post_by_uri($uri); |
|
| 246 | 246 | |
| 247 | - if ( $entity ) { |
|
| 248 | - add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 247 | + if ($entity) { |
|
| 248 | + add_post_meta($post->ID, $field, $entity->ID, true); |
|
| 249 | 249 | // Set also the topic taxonomy |
| 250 | - if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) { |
|
| 251 | - Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 250 | + if ($field === Wordlift_Schema_Service::FIELD_TOPIC) { |
|
| 251 | + Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for($post->ID, $entity); |
|
| 252 | 252 | } |
| 253 | 253 | } |
| 254 | 254 | } |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | // Push the post to Redlink. |
| 258 | - do_action( 'wl_legacy_linked_data__push', $post->ID ); |
|
| 258 | + do_action('wl_legacy_linked_data__push', $post->ID); |
|
| 259 | 259 | |
| 260 | - add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 260 | + add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | -add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 263 | +add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 264 | 264 | |
| 265 | 265 | /** |
| 266 | 266 | * Save the specified data as an entity in WordPress. This method only create new entities. When an existing entity is |
@@ -278,14 +278,14 @@ discard block |
||
| 278 | 278 | * |
| 279 | 279 | * @return null|WP_Post A post instance or null in case of failure. |
| 280 | 280 | */ |
| 281 | -function wl_save_entity( $entity_data ) { |
|
| 281 | +function wl_save_entity($entity_data) { |
|
| 282 | 282 | |
| 283 | 283 | // Required for REST API calls |
| 284 | - if ( ! function_exists( 'wp_crop_image' ) ) { |
|
| 285 | - require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
|
| 284 | + if ( ! function_exists('wp_crop_image')) { |
|
| 285 | + require_once(ABSPATH.'wp-admin/includes/image.php'); |
|
| 286 | 286 | } |
| 287 | 287 | |
| 288 | - $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
| 288 | + $log = Wordlift_Log_Service::get_logger('wl_save_entity'); |
|
| 289 | 289 | |
| 290 | 290 | $uri = $entity_data['uri']; |
| 291 | 291 | /* |
@@ -295,28 +295,28 @@ discard block |
||
| 295 | 295 | * |
| 296 | 296 | * @see https://github.com/insideout10/wordlift-plugin/issues/841 |
| 297 | 297 | */ |
| 298 | - $label = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) ); |
|
| 298 | + $label = preg_replace('/\xEF\xBB\xBF/', '', sanitize_text_field($entity_data['label'])); |
|
| 299 | 299 | $type_uri = $entity_data['main_type']; |
| 300 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 300 | + $entity_types = isset($entity_data['type']) ? $entity_data['type'] : array(); |
|
| 301 | 301 | $description = $entity_data['description']; |
| 302 | - $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
| 303 | - $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
| 304 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 305 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 302 | + $images = isset($entity_data['image']) ? (array) $entity_data['image'] : array(); |
|
| 303 | + $same_as = isset($entity_data['sameas']) ? (array) $entity_data['sameas'] : array(); |
|
| 304 | + $related_post_id = isset($entity_data['related_post_id']) ? $entity_data['related_post_id'] : null; |
|
| 305 | + $other_properties = isset($entity_data['properties']) ? $entity_data['properties'] : array(); |
|
| 306 | 306 | |
| 307 | 307 | // Get the synonyms. |
| 308 | - $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
| 308 | + $synonyms = isset($entity_data['synonym']) ? $entity_data['synonym'] : array(); |
|
| 309 | 309 | |
| 310 | 310 | // Check whether an entity already exists with the provided URI. |
| 311 | - if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 312 | - $log->debug( "Post already exists for URI $uri." ); |
|
| 311 | + if (null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri)) { |
|
| 312 | + $log->debug("Post already exists for URI $uri."); |
|
| 313 | 313 | |
| 314 | 314 | return $post; |
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | // Prepare properties of the new entity. |
| 318 | 318 | $params = array( |
| 319 | - 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 319 | + 'post_status' => (is_numeric($related_post_id) ? get_post_status($related_post_id) : 'draft'), |
|
| 320 | 320 | 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
| 321 | 321 | 'post_title' => $label, |
| 322 | 322 | 'post_content' => $description, |
@@ -326,7 +326,7 @@ discard block |
||
| 326 | 326 | // new entities. |
| 327 | 327 | // |
| 328 | 328 | // See https://github.com/insideout10/wordlift-plugin/issues/282 |
| 329 | - 'post_name' => sanitize_title( $label ), |
|
| 329 | + 'post_name' => sanitize_title($label), |
|
| 330 | 330 | ); |
| 331 | 331 | |
| 332 | 332 | // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
@@ -335,18 +335,18 @@ discard block |
||
| 335 | 335 | // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
| 336 | 336 | // is created when saving a post. |
| 337 | 337 | global $wpseo_metabox, $seo_ultimate; |
| 338 | - if ( isset( $wpseo_metabox ) ) { |
|
| 339 | - remove_action( 'wp_insert_post', array( |
|
| 338 | + if (isset($wpseo_metabox)) { |
|
| 339 | + remove_action('wp_insert_post', array( |
|
| 340 | 340 | $wpseo_metabox, |
| 341 | 341 | 'save_postdata', |
| 342 | - ) ); |
|
| 342 | + )); |
|
| 343 | 343 | } |
| 344 | 344 | |
| 345 | - if ( isset( $seo_ultimate ) ) { |
|
| 346 | - remove_action( 'save_post', array( |
|
| 345 | + if (isset($seo_ultimate)) { |
|
| 346 | + remove_action('save_post', array( |
|
| 347 | 347 | $seo_ultimate, |
| 348 | 348 | 'save_postmeta_box', |
| 349 | - ) ); |
|
| 349 | + )); |
|
| 350 | 350 | } |
| 351 | 351 | |
| 352 | 352 | // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
@@ -356,90 +356,90 @@ discard block |
||
| 356 | 356 | // see https://github.com/insideout10/wordlift-plugin/issues/156 |
| 357 | 357 | // see https://github.com/insideout10/wordlift-plugin/issues/148 |
| 358 | 358 | global $wp_filter; |
| 359 | - $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 360 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 359 | + $save_post_filters = is_array($wp_filter['save_post']) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 360 | + is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 361 | 361 | |
| 362 | 362 | |
| 363 | - $log->trace( 'Going to insert new post...' ); |
|
| 363 | + $log->trace('Going to insert new post...'); |
|
| 364 | 364 | |
| 365 | 365 | // create or update the post. |
| 366 | - $post_id = wp_insert_post( $params, true ); |
|
| 366 | + $post_id = wp_insert_post($params, true); |
|
| 367 | 367 | |
| 368 | 368 | // Setting the alternative labels for this entity. |
| 369 | 369 | Wordlift_Entity_Service::get_instance() |
| 370 | - ->set_alternative_labels( $post_id, $synonyms ); |
|
| 370 | + ->set_alternative_labels($post_id, $synonyms); |
|
| 371 | 371 | |
| 372 | 372 | // Restore all the existing filters. |
| 373 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 373 | + is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 374 | 374 | |
| 375 | 375 | // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
| 376 | - if ( isset( $wpseo_metabox ) ) { |
|
| 377 | - add_action( 'wp_insert_post', array( |
|
| 376 | + if (isset($wpseo_metabox)) { |
|
| 377 | + add_action('wp_insert_post', array( |
|
| 378 | 378 | $wpseo_metabox, |
| 379 | 379 | 'save_postdata', |
| 380 | - ) ); |
|
| 380 | + )); |
|
| 381 | 381 | } |
| 382 | 382 | |
| 383 | 383 | // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
| 384 | - if ( isset( $seo_ultimate ) ) { |
|
| 385 | - add_action( 'save_post', array( |
|
| 384 | + if (isset($seo_ultimate)) { |
|
| 385 | + add_action('save_post', array( |
|
| 386 | 386 | $seo_ultimate, |
| 387 | 387 | 'save_postmeta_box', |
| 388 | - ), 10, 2 ); |
|
| 388 | + ), 10, 2); |
|
| 389 | 389 | } |
| 390 | 390 | |
| 391 | 391 | // TODO: handle errors. |
| 392 | - if ( is_wp_error( $post_id ) ) { |
|
| 393 | - $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
| 392 | + if (is_wp_error($post_id)) { |
|
| 393 | + $log->error('An error occurred: '.$post_id->get_error_message()); |
|
| 394 | 394 | |
| 395 | 395 | // inform an error occurred. |
| 396 | 396 | return null; |
| 397 | 397 | } |
| 398 | 398 | |
| 399 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 399 | + wl_set_entity_main_type($post_id, $type_uri); |
|
| 400 | 400 | |
| 401 | 401 | // Save the entity types. |
| 402 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 402 | + wl_set_entity_rdf_types($post_id, $entity_types); |
|
| 403 | 403 | |
| 404 | 404 | // Get a dataset URI for the entity. |
| 405 | - $wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id ); |
|
| 405 | + $wl_uri = Wordlift_Entity_Service::get_instance()->get_uri($post_id); |
|
| 406 | 406 | |
| 407 | 407 | // Add the uri to the sameAs data if it's not a local URI. |
| 408 | - if ( $wl_uri !== $uri ) { |
|
| 409 | - array_push( $same_as, $uri ); |
|
| 408 | + if ($wl_uri !== $uri) { |
|
| 409 | + array_push($same_as, $uri); |
|
| 410 | 410 | } |
| 411 | 411 | |
| 412 | 412 | // Save the sameAs data for the entity. |
| 413 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 413 | + wl_schema_set_value($post_id, 'sameAs', $same_as); |
|
| 414 | 414 | |
| 415 | 415 | // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
| 416 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
| 417 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 416 | + foreach ($other_properties as $property_name => $property_value) { |
|
| 417 | + wl_schema_set_value($post_id, $property_name, $property_value); |
|
| 418 | 418 | } |
| 419 | 419 | |
| 420 | 420 | // Call hooks. |
| 421 | - do_action( 'wl_save_entity', $post_id ); |
|
| 421 | + do_action('wl_save_entity', $post_id); |
|
| 422 | 422 | |
| 423 | - foreach ( $images as $image_remote_url ) { |
|
| 423 | + foreach ($images as $image_remote_url) { |
|
| 424 | 424 | |
| 425 | 425 | // Check if image is already present in local DB |
| 426 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 426 | + if (strpos($image_remote_url, site_url()) !== false) { |
|
| 427 | 427 | // Do nothing. |
| 428 | 428 | continue; |
| 429 | 429 | } |
| 430 | 430 | |
| 431 | 431 | // Check if there is an existing attachment for this post ID and source URL. |
| 432 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 432 | + $existing_image = wl_get_attachment_for_source_url($post_id, $image_remote_url); |
|
| 433 | 433 | |
| 434 | 434 | // Skip if an existing image is found. |
| 435 | - if ( null !== $existing_image ) { |
|
| 435 | + if (null !== $existing_image) { |
|
| 436 | 436 | continue; |
| 437 | 437 | } |
| 438 | 438 | |
| 439 | 439 | // Save the image and get the local path. |
| 440 | - $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
| 440 | + $image = Wordlift_Remote_Image_Service::save_from_url($image_remote_url); |
|
| 441 | 441 | |
| 442 | - if ( false === $image || is_wp_error( $image ) ) { |
|
| 442 | + if (false === $image || is_wp_error($image)) { |
|
| 443 | 443 | continue; |
| 444 | 444 | } |
| 445 | 445 | |
@@ -459,24 +459,24 @@ discard block |
||
| 459 | 459 | ); |
| 460 | 460 | |
| 461 | 461 | // Create the attachment in WordPress and generate the related metadata. |
| 462 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 462 | + $attachment_id = wp_insert_attachment($attachment, $filename, $post_id); |
|
| 463 | 463 | |
| 464 | 464 | // Set the source URL for the image. |
| 465 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 465 | + wl_set_source_url($attachment_id, $image_remote_url); |
|
| 466 | 466 | |
| 467 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 468 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 467 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename); |
|
| 468 | + wp_update_attachment_metadata($attachment_id, $attachment_data); |
|
| 469 | 469 | |
| 470 | 470 | // Set it as the featured image. |
| 471 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 471 | + set_post_thumbnail($post_id, $attachment_id); |
|
| 472 | 472 | } |
| 473 | 473 | |
| 474 | 474 | // The entity is pushed to Redlink on save by the function hooked to save_post. |
| 475 | 475 | // save the entity in the triple store. |
| 476 | - do_action( 'wl_legacy_linked_data__push', $post_id ); |
|
| 476 | + do_action('wl_legacy_linked_data__push', $post_id); |
|
| 477 | 477 | |
| 478 | 478 | // finally return the entity post. |
| 479 | - return get_post( $post_id ); |
|
| 479 | + return get_post($post_id); |
|
| 480 | 480 | } |
| 481 | 481 | |
| 482 | 482 | /** |
@@ -488,10 +488,10 @@ discard block |
||
| 488 | 488 | * @since 3.0.0 |
| 489 | 489 | * |
| 490 | 490 | */ |
| 491 | -function wl_linked_data_content_get_embedded_entities( $content ) { |
|
| 491 | +function wl_linked_data_content_get_embedded_entities($content) { |
|
| 492 | 492 | |
| 493 | 493 | // Remove quote escapes. |
| 494 | - $content = str_replace( '\\"', '"', $content ); |
|
| 494 | + $content = str_replace('\\"', '"', $content); |
|
| 495 | 495 | |
| 496 | 496 | // Match all itemid attributes. |
| 497 | 497 | $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
@@ -502,24 +502,24 @@ discard block |
||
| 502 | 502 | $matches = array(); |
| 503 | 503 | |
| 504 | 504 | // In case of errors, return an empty array. |
| 505 | - if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 506 | - wl_write_log( "Found no entities embedded in content" ); |
|
| 505 | + if (false === preg_match_all($pattern, $content, $matches)) { |
|
| 506 | + wl_write_log("Found no entities embedded in content"); |
|
| 507 | 507 | |
| 508 | 508 | return array(); |
| 509 | 509 | } |
| 510 | 510 | |
| 511 | 511 | // wl_write_log("wl_update_related_entities [ content :: $content ][ data :: " . var_export($data, true). " ][ matches :: " . var_export($matches, true) . " ]"); |
| 512 | 512 | |
| 513 | - $uris = $matches[1]; |
|
| 513 | + $uris = $matches[1]; |
|
| 514 | 514 | // Collect the entities. |
| 515 | 515 | $entities = array(); |
| 516 | - foreach ( $uris as $uri ) { |
|
| 517 | - $uri_d = html_entity_decode( $uri ); |
|
| 516 | + foreach ($uris as $uri) { |
|
| 517 | + $uri_d = html_entity_decode($uri); |
|
| 518 | 518 | |
| 519 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 519 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri_d); |
|
| 520 | 520 | |
| 521 | - if ( null !== $entity ) { |
|
| 522 | - array_push( $entities, $entity->ID ); |
|
| 521 | + if (null !== $entity) { |
|
| 522 | + array_push($entities, $entity->ID); |
|
| 523 | 523 | } |
| 524 | 524 | } |
| 525 | 525 | |
@@ -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; |