@@ -15,57 +15,57 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Sync_Hooks_Entity_Relation { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var \Wordlift_Entity_Service |
|
| 20 | - */ |
|
| 21 | - private $entity_service; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Entity_Dct_Relation constructor. |
|
| 25 | - * |
|
| 26 | - * @param $entity_service \Wordlift_Entity_Service |
|
| 27 | - */ |
|
| 28 | - public function __construct( $entity_service ) { |
|
| 29 | - $this->entity_service = $entity_service; |
|
| 30 | - |
|
| 31 | - add_filter( 'wl_dataset__sync_service__sync_item__jsonld', array( $this, 'jsonld' ), 10, 3 ); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - public function jsonld( $jsonld, $type, $post_id ) { |
|
| 35 | - |
|
| 36 | - // @@todo add support anything that isn't a POST. |
|
| 37 | - if ( Object_Type_Enum::POST !== $type ) { |
|
| 38 | - return $jsonld; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - // Choose the dcterm property according to the post type. |
|
| 42 | - $property = $this->entity_service->is_entity( $post_id ) |
|
| 43 | - ? 'http://purl.org/dc/terms/relation' |
|
| 44 | - : 'http://purl.org/dc/terms/references'; |
|
| 45 | - |
|
| 46 | - $references = array_unique( $this->entity_service->get_related_entities( $post_id ) ); |
|
| 47 | - |
|
| 48 | - // Bail out if there are no references. |
|
| 49 | - if ( empty( $references ) ) { |
|
| 50 | - return $jsonld; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - if ( ! isset( $jsonld[0][ $property ] ) ) { |
|
| 54 | - $jsonld[0][ $property ] = array(); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - if ( ! is_array( $jsonld[0][ $property ] ) ) { |
|
| 58 | - $jsonld[0][ $property ] = array( $jsonld[0][ $property ] ); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - $that = $this; |
|
| 62 | - $references_array = array_values( array_map( function ( $item ) use ( $that ) { |
|
| 63 | - return array( '@id' => $that->entity_service->get_uri( $item ) ); |
|
| 64 | - }, $references ) ); |
|
| 65 | - |
|
| 66 | - $jsonld[0][ $property ] = array_merge( $jsonld[0][ $property ], $references_array ); |
|
| 67 | - |
|
| 68 | - return $jsonld; |
|
| 69 | - } |
|
| 18 | + /** |
|
| 19 | + * @var \Wordlift_Entity_Service |
|
| 20 | + */ |
|
| 21 | + private $entity_service; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Entity_Dct_Relation constructor. |
|
| 25 | + * |
|
| 26 | + * @param $entity_service \Wordlift_Entity_Service |
|
| 27 | + */ |
|
| 28 | + public function __construct( $entity_service ) { |
|
| 29 | + $this->entity_service = $entity_service; |
|
| 30 | + |
|
| 31 | + add_filter( 'wl_dataset__sync_service__sync_item__jsonld', array( $this, 'jsonld' ), 10, 3 ); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + public function jsonld( $jsonld, $type, $post_id ) { |
|
| 35 | + |
|
| 36 | + // @@todo add support anything that isn't a POST. |
|
| 37 | + if ( Object_Type_Enum::POST !== $type ) { |
|
| 38 | + return $jsonld; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + // Choose the dcterm property according to the post type. |
|
| 42 | + $property = $this->entity_service->is_entity( $post_id ) |
|
| 43 | + ? 'http://purl.org/dc/terms/relation' |
|
| 44 | + : 'http://purl.org/dc/terms/references'; |
|
| 45 | + |
|
| 46 | + $references = array_unique( $this->entity_service->get_related_entities( $post_id ) ); |
|
| 47 | + |
|
| 48 | + // Bail out if there are no references. |
|
| 49 | + if ( empty( $references ) ) { |
|
| 50 | + return $jsonld; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + if ( ! isset( $jsonld[0][ $property ] ) ) { |
|
| 54 | + $jsonld[0][ $property ] = array(); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + if ( ! is_array( $jsonld[0][ $property ] ) ) { |
|
| 58 | + $jsonld[0][ $property ] = array( $jsonld[0][ $property ] ); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + $that = $this; |
|
| 62 | + $references_array = array_values( array_map( function ( $item ) use ( $that ) { |
|
| 63 | + return array( '@id' => $that->entity_service->get_uri( $item ) ); |
|
| 64 | + }, $references ) ); |
|
| 65 | + |
|
| 66 | + $jsonld[0][ $property ] = array_merge( $jsonld[0][ $property ], $references_array ); |
|
| 67 | + |
|
| 68 | + return $jsonld; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | 71 | } |
@@ -25,45 +25,45 @@ |
||
| 25 | 25 | * |
| 26 | 26 | * @param $entity_service \Wordlift_Entity_Service |
| 27 | 27 | */ |
| 28 | - public function __construct( $entity_service ) { |
|
| 28 | + public function __construct($entity_service) { |
|
| 29 | 29 | $this->entity_service = $entity_service; |
| 30 | 30 | |
| 31 | - add_filter( 'wl_dataset__sync_service__sync_item__jsonld', array( $this, 'jsonld' ), 10, 3 ); |
|
| 31 | + add_filter('wl_dataset__sync_service__sync_item__jsonld', array($this, 'jsonld'), 10, 3); |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - public function jsonld( $jsonld, $type, $post_id ) { |
|
| 34 | + public function jsonld($jsonld, $type, $post_id) { |
|
| 35 | 35 | |
| 36 | 36 | // @@todo add support anything that isn't a POST. |
| 37 | - if ( Object_Type_Enum::POST !== $type ) { |
|
| 37 | + if (Object_Type_Enum::POST !== $type) { |
|
| 38 | 38 | return $jsonld; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | // Choose the dcterm property according to the post type. |
| 42 | - $property = $this->entity_service->is_entity( $post_id ) |
|
| 42 | + $property = $this->entity_service->is_entity($post_id) |
|
| 43 | 43 | ? 'http://purl.org/dc/terms/relation' |
| 44 | 44 | : 'http://purl.org/dc/terms/references'; |
| 45 | 45 | |
| 46 | - $references = array_unique( $this->entity_service->get_related_entities( $post_id ) ); |
|
| 46 | + $references = array_unique($this->entity_service->get_related_entities($post_id)); |
|
| 47 | 47 | |
| 48 | 48 | // Bail out if there are no references. |
| 49 | - if ( empty( $references ) ) { |
|
| 49 | + if (empty($references)) { |
|
| 50 | 50 | return $jsonld; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - if ( ! isset( $jsonld[0][ $property ] ) ) { |
|
| 54 | - $jsonld[0][ $property ] = array(); |
|
| 53 | + if ( ! isset($jsonld[0][$property])) { |
|
| 54 | + $jsonld[0][$property] = array(); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - if ( ! is_array( $jsonld[0][ $property ] ) ) { |
|
| 58 | - $jsonld[0][ $property ] = array( $jsonld[0][ $property ] ); |
|
| 57 | + if ( ! is_array($jsonld[0][$property])) { |
|
| 58 | + $jsonld[0][$property] = array($jsonld[0][$property]); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | $that = $this; |
| 62 | - $references_array = array_values( array_map( function ( $item ) use ( $that ) { |
|
| 63 | - return array( '@id' => $that->entity_service->get_uri( $item ) ); |
|
| 64 | - }, $references ) ); |
|
| 62 | + $references_array = array_values(array_map(function($item) use ($that) { |
|
| 63 | + return array('@id' => $that->entity_service->get_uri($item)); |
|
| 64 | + }, $references)); |
|
| 65 | 65 | |
| 66 | - $jsonld[0][ $property ] = array_merge( $jsonld[0][ $property ], $references_array ); |
|
| 66 | + $jsonld[0][$property] = array_merge($jsonld[0][$property], $references_array); |
|
| 67 | 67 | |
| 68 | 68 | return $jsonld; |
| 69 | 69 | } |
@@ -6,76 +6,76 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Jsonld_User_Service { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * @var \Wordlift_User_Service $user_service |
|
| 11 | - */ |
|
| 12 | - private $user_service; |
|
| 13 | - |
|
| 14 | - /** |
|
| 15 | - * @var Jsonld_Service |
|
| 16 | - */ |
|
| 17 | - private $jsonld_service; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Jsonld_User_Service constructor. |
|
| 21 | - * |
|
| 22 | - * @param \Wordlift_User_Service $user_service |
|
| 23 | - */ |
|
| 24 | - function __construct( $user_service ) { |
|
| 25 | - $this->user_service = $user_service; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @param Jsonld_Service $jsonld_service |
|
| 30 | - */ |
|
| 31 | - function set_jsonld_service( $jsonld_service ) { |
|
| 32 | - $this->jsonld_service = $jsonld_service; |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - function get( $user_id ) { |
|
| 36 | - $userdata = get_userdata( $user_id ); |
|
| 37 | - |
|
| 38 | - // Bail out if user not found. |
|
| 39 | - if ( ! ( $userdata instanceof \WP_User ) ) { |
|
| 40 | - return array(); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - // Return the post JSON-LD if a post has been bound to this user. |
|
| 44 | - $post_id = $this->user_service->get_entity( $user_id ); |
|
| 45 | - if ( ! empty( $post_id ) && isset( $this->jsonld_service ) ) { |
|
| 46 | - return $this->jsonld_service->get( Object_Type_Enum::POST, $post_id ); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - // Bail out if the user doesn't have a valid URI. |
|
| 50 | - $uri = $this->user_service->get_uri( $user_id ); |
|
| 51 | - if ( empty( $uri ) ) { |
|
| 52 | - return array(); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - // Finally return the user's JSON-LD. |
|
| 56 | - $data = array( |
|
| 57 | - '@context' => 'http://schema.org', |
|
| 58 | - '@id' => $uri, |
|
| 59 | - '@type' => 'Person', |
|
| 60 | - ); |
|
| 61 | - |
|
| 62 | - if ( ! empty( $userdata->display_name ) ) { |
|
| 63 | - $data['name'] = $userdata->display_name; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - if ( ! empty( $userdata->first_name ) ) { |
|
| 67 | - $data['givenName'] = $userdata->first_name; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - if ( ! empty( $userdata->last_name ) ) { |
|
| 71 | - $data['familyName'] = $userdata->last_name; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - if ( ! empty( $userdata->user_url ) ) { |
|
| 75 | - $data['url'] = $userdata->user_url; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - return array( $data ); |
|
| 79 | - } |
|
| 9 | + /** |
|
| 10 | + * @var \Wordlift_User_Service $user_service |
|
| 11 | + */ |
|
| 12 | + private $user_service; |
|
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * @var Jsonld_Service |
|
| 16 | + */ |
|
| 17 | + private $jsonld_service; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Jsonld_User_Service constructor. |
|
| 21 | + * |
|
| 22 | + * @param \Wordlift_User_Service $user_service |
|
| 23 | + */ |
|
| 24 | + function __construct( $user_service ) { |
|
| 25 | + $this->user_service = $user_service; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @param Jsonld_Service $jsonld_service |
|
| 30 | + */ |
|
| 31 | + function set_jsonld_service( $jsonld_service ) { |
|
| 32 | + $this->jsonld_service = $jsonld_service; |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + function get( $user_id ) { |
|
| 36 | + $userdata = get_userdata( $user_id ); |
|
| 37 | + |
|
| 38 | + // Bail out if user not found. |
|
| 39 | + if ( ! ( $userdata instanceof \WP_User ) ) { |
|
| 40 | + return array(); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + // Return the post JSON-LD if a post has been bound to this user. |
|
| 44 | + $post_id = $this->user_service->get_entity( $user_id ); |
|
| 45 | + if ( ! empty( $post_id ) && isset( $this->jsonld_service ) ) { |
|
| 46 | + return $this->jsonld_service->get( Object_Type_Enum::POST, $post_id ); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + // Bail out if the user doesn't have a valid URI. |
|
| 50 | + $uri = $this->user_service->get_uri( $user_id ); |
|
| 51 | + if ( empty( $uri ) ) { |
|
| 52 | + return array(); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + // Finally return the user's JSON-LD. |
|
| 56 | + $data = array( |
|
| 57 | + '@context' => 'http://schema.org', |
|
| 58 | + '@id' => $uri, |
|
| 59 | + '@type' => 'Person', |
|
| 60 | + ); |
|
| 61 | + |
|
| 62 | + if ( ! empty( $userdata->display_name ) ) { |
|
| 63 | + $data['name'] = $userdata->display_name; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + if ( ! empty( $userdata->first_name ) ) { |
|
| 67 | + $data['givenName'] = $userdata->first_name; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + if ( ! empty( $userdata->last_name ) ) { |
|
| 71 | + $data['familyName'] = $userdata->last_name; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + if ( ! empty( $userdata->user_url ) ) { |
|
| 75 | + $data['url'] = $userdata->user_url; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + return array( $data ); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | 81 | } |
@@ -21,34 +21,34 @@ discard block |
||
| 21 | 21 | * |
| 22 | 22 | * @param \Wordlift_User_Service $user_service |
| 23 | 23 | */ |
| 24 | - function __construct( $user_service ) { |
|
| 24 | + function __construct($user_service) { |
|
| 25 | 25 | $this->user_service = $user_service; |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * @param Jsonld_Service $jsonld_service |
| 30 | 30 | */ |
| 31 | - function set_jsonld_service( $jsonld_service ) { |
|
| 31 | + function set_jsonld_service($jsonld_service) { |
|
| 32 | 32 | $this->jsonld_service = $jsonld_service; |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | - function get( $user_id ) { |
|
| 36 | - $userdata = get_userdata( $user_id ); |
|
| 35 | + function get($user_id) { |
|
| 36 | + $userdata = get_userdata($user_id); |
|
| 37 | 37 | |
| 38 | 38 | // Bail out if user not found. |
| 39 | - if ( ! ( $userdata instanceof \WP_User ) ) { |
|
| 39 | + if ( ! ($userdata instanceof \WP_User)) { |
|
| 40 | 40 | return array(); |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | // Return the post JSON-LD if a post has been bound to this user. |
| 44 | - $post_id = $this->user_service->get_entity( $user_id ); |
|
| 45 | - if ( ! empty( $post_id ) && isset( $this->jsonld_service ) ) { |
|
| 46 | - return $this->jsonld_service->get( Object_Type_Enum::POST, $post_id ); |
|
| 44 | + $post_id = $this->user_service->get_entity($user_id); |
|
| 45 | + if ( ! empty($post_id) && isset($this->jsonld_service)) { |
|
| 46 | + return $this->jsonld_service->get(Object_Type_Enum::POST, $post_id); |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | // Bail out if the user doesn't have a valid URI. |
| 50 | - $uri = $this->user_service->get_uri( $user_id ); |
|
| 51 | - if ( empty( $uri ) ) { |
|
| 50 | + $uri = $this->user_service->get_uri($user_id); |
|
| 51 | + if (empty($uri)) { |
|
| 52 | 52 | return array(); |
| 53 | 53 | } |
| 54 | 54 | |
@@ -59,23 +59,23 @@ discard block |
||
| 59 | 59 | '@type' => 'Person', |
| 60 | 60 | ); |
| 61 | 61 | |
| 62 | - if ( ! empty( $userdata->display_name ) ) { |
|
| 62 | + if ( ! empty($userdata->display_name)) { |
|
| 63 | 63 | $data['name'] = $userdata->display_name; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - if ( ! empty( $userdata->first_name ) ) { |
|
| 66 | + if ( ! empty($userdata->first_name)) { |
|
| 67 | 67 | $data['givenName'] = $userdata->first_name; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - if ( ! empty( $userdata->last_name ) ) { |
|
| 70 | + if ( ! empty($userdata->last_name)) { |
|
| 71 | 71 | $data['familyName'] = $userdata->last_name; |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - if ( ! empty( $userdata->user_url ) ) { |
|
| 74 | + if ( ! empty($userdata->user_url)) { |
|
| 75 | 75 | $data['url'] = $userdata->user_url; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - return array( $data ); |
|
| 78 | + return array($data); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | } |
@@ -15,62 +15,62 @@ |
||
| 15 | 15 | |
| 16 | 16 | class Local_Autocomplete_Service extends Abstract_Autocomplete_Service { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @inheritDoc |
|
| 20 | - */ |
|
| 21 | - public function query( $query, $scope, $excludes ) { |
|
| 22 | - global $wpdb; |
|
| 18 | + /** |
|
| 19 | + * @inheritDoc |
|
| 20 | + */ |
|
| 21 | + public function query( $query, $scope, $excludes ) { |
|
| 22 | + global $wpdb; |
|
| 23 | 23 | |
| 24 | - $posts = $wpdb->get_results( $wpdb->prepare( |
|
| 25 | - "SELECT * FROM {$wpdb->posts} p" |
|
| 26 | - . " INNER JOIN {$wpdb->term_relationships} tr" |
|
| 27 | - . " ON tr.object_id = p.ID" |
|
| 28 | - . " INNER JOIN {$wpdb->term_taxonomy} tt" |
|
| 29 | - . " ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 30 | - . " INNER JOIN {$wpdb->terms} t" |
|
| 31 | - . " ON t.term_id = tt.term_id AND t.name != %s" |
|
| 32 | - . " LEFT OUTER JOIN {$wpdb->postmeta} pm" |
|
| 33 | - . " ON pm.meta_key = %s AND pm.post_id = p.ID" |
|
| 34 | - . " WHERE p.post_type IN ( '" . implode( "', '", array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) ) . "' )" |
|
| 35 | - . " AND p.post_status IN ( 'publish', 'draft', 'private', 'future' ) " |
|
| 36 | - . " AND ( p.post_title LIKE %s OR pm.meta_value LIKE %s )" |
|
| 37 | - . " LIMIT %d", |
|
| 38 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 39 | - 'article', |
|
| 40 | - Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, |
|
| 41 | - // `prepare` doesn't support argument number, hence we must repeat the query. |
|
| 42 | - '%' . $wpdb->esc_like( $query ) . '%', |
|
| 43 | - '%' . $wpdb->esc_like( $query ) . '%', |
|
| 44 | - 50 |
|
| 45 | - ) ); |
|
| 24 | + $posts = $wpdb->get_results( $wpdb->prepare( |
|
| 25 | + "SELECT * FROM {$wpdb->posts} p" |
|
| 26 | + . " INNER JOIN {$wpdb->term_relationships} tr" |
|
| 27 | + . " ON tr.object_id = p.ID" |
|
| 28 | + . " INNER JOIN {$wpdb->term_taxonomy} tt" |
|
| 29 | + . " ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 30 | + . " INNER JOIN {$wpdb->terms} t" |
|
| 31 | + . " ON t.term_id = tt.term_id AND t.name != %s" |
|
| 32 | + . " LEFT OUTER JOIN {$wpdb->postmeta} pm" |
|
| 33 | + . " ON pm.meta_key = %s AND pm.post_id = p.ID" |
|
| 34 | + . " WHERE p.post_type IN ( '" . implode( "', '", array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) ) . "' )" |
|
| 35 | + . " AND p.post_status IN ( 'publish', 'draft', 'private', 'future' ) " |
|
| 36 | + . " AND ( p.post_title LIKE %s OR pm.meta_value LIKE %s )" |
|
| 37 | + . " LIMIT %d", |
|
| 38 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 39 | + 'article', |
|
| 40 | + Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, |
|
| 41 | + // `prepare` doesn't support argument number, hence we must repeat the query. |
|
| 42 | + '%' . $wpdb->esc_like( $query ) . '%', |
|
| 43 | + '%' . $wpdb->esc_like( $query ) . '%', |
|
| 44 | + 50 |
|
| 45 | + ) ); |
|
| 46 | 46 | |
| 47 | - $results = array_map( function ( $item ) { |
|
| 47 | + $results = array_map( function ( $item ) { |
|
| 48 | 48 | |
| 49 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 50 | - $uri = $entity_service->get_uri( $item->ID ); |
|
| 49 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 50 | + $uri = $entity_service->get_uri( $item->ID ); |
|
| 51 | 51 | |
| 52 | - return array( |
|
| 53 | - // see #1074: The value property is needed for autocomplete in category page |
|
| 54 | - // to function correctly, if value is not provided, then the entity |
|
| 55 | - // wont be correctly saved. |
|
| 56 | - 'value' => $uri, |
|
| 57 | - 'id' => $uri, |
|
| 58 | - 'label' => array( $item->post_title ), |
|
| 59 | - 'labels' => $entity_service->get_alternative_labels( $item->ID ), |
|
| 60 | - 'descriptions' => array( Wordlift_Post_Excerpt_Helper::get_text_excerpt( $item ) ), |
|
| 61 | - 'scope' => 'local', |
|
| 62 | - 'sameAss' => get_post_meta( $item->ID, \Wordlift_Schema_Service::FIELD_SAME_AS ), |
|
| 63 | - // The following properties are less relevant because we're linking entities that exist already in the |
|
| 64 | - // vocabulary. That's why we don't make an effort to load the real data. |
|
| 65 | - 'types' => array( 'http://schema.org/Thing' ), |
|
| 66 | - 'urls' => array(), |
|
| 67 | - 'images' => array(), |
|
| 68 | - ); |
|
| 69 | - }, $posts ); |
|
| 52 | + return array( |
|
| 53 | + // see #1074: The value property is needed for autocomplete in category page |
|
| 54 | + // to function correctly, if value is not provided, then the entity |
|
| 55 | + // wont be correctly saved. |
|
| 56 | + 'value' => $uri, |
|
| 57 | + 'id' => $uri, |
|
| 58 | + 'label' => array( $item->post_title ), |
|
| 59 | + 'labels' => $entity_service->get_alternative_labels( $item->ID ), |
|
| 60 | + 'descriptions' => array( Wordlift_Post_Excerpt_Helper::get_text_excerpt( $item ) ), |
|
| 61 | + 'scope' => 'local', |
|
| 62 | + 'sameAss' => get_post_meta( $item->ID, \Wordlift_Schema_Service::FIELD_SAME_AS ), |
|
| 63 | + // The following properties are less relevant because we're linking entities that exist already in the |
|
| 64 | + // vocabulary. That's why we don't make an effort to load the real data. |
|
| 65 | + 'types' => array( 'http://schema.org/Thing' ), |
|
| 66 | + 'urls' => array(), |
|
| 67 | + 'images' => array(), |
|
| 68 | + ); |
|
| 69 | + }, $posts ); |
|
| 70 | 70 | |
| 71 | - $results = array_unique( $results, SORT_REGULAR ); |
|
| 71 | + $results = array_unique( $results, SORT_REGULAR ); |
|
| 72 | 72 | |
| 73 | - return $this->filter( $results, $excludes ); |
|
| 74 | - } |
|
| 73 | + return $this->filter( $results, $excludes ); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | 76 | } |
@@ -18,10 +18,10 @@ discard block |
||
| 18 | 18 | /** |
| 19 | 19 | * @inheritDoc |
| 20 | 20 | */ |
| 21 | - public function query( $query, $scope, $excludes ) { |
|
| 21 | + public function query($query, $scope, $excludes) { |
|
| 22 | 22 | global $wpdb; |
| 23 | 23 | |
| 24 | - $posts = $wpdb->get_results( $wpdb->prepare( |
|
| 24 | + $posts = $wpdb->get_results($wpdb->prepare( |
|
| 25 | 25 | "SELECT * FROM {$wpdb->posts} p" |
| 26 | 26 | . " INNER JOIN {$wpdb->term_relationships} tr" |
| 27 | 27 | . " ON tr.object_id = p.ID" |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | . " ON t.term_id = tt.term_id AND t.name != %s" |
| 32 | 32 | . " LEFT OUTER JOIN {$wpdb->postmeta} pm" |
| 33 | 33 | . " ON pm.meta_key = %s AND pm.post_id = p.ID" |
| 34 | - . " WHERE p.post_type IN ( '" . implode( "', '", array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) ) . "' )" |
|
| 34 | + . " WHERE p.post_type IN ( '".implode("', '", array_map('esc_sql', Wordlift_Entity_Service::valid_entity_post_types()))."' )" |
|
| 35 | 35 | . " AND p.post_status IN ( 'publish', 'draft', 'private', 'future' ) " |
| 36 | 36 | . " AND ( p.post_title LIKE %s OR pm.meta_value LIKE %s )" |
| 37 | 37 | . " LIMIT %d", |
@@ -39,15 +39,15 @@ discard block |
||
| 39 | 39 | 'article', |
| 40 | 40 | Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, |
| 41 | 41 | // `prepare` doesn't support argument number, hence we must repeat the query. |
| 42 | - '%' . $wpdb->esc_like( $query ) . '%', |
|
| 43 | - '%' . $wpdb->esc_like( $query ) . '%', |
|
| 42 | + '%'.$wpdb->esc_like($query).'%', |
|
| 43 | + '%'.$wpdb->esc_like($query).'%', |
|
| 44 | 44 | 50 |
| 45 | - ) ); |
|
| 45 | + )); |
|
| 46 | 46 | |
| 47 | - $results = array_map( function ( $item ) { |
|
| 47 | + $results = array_map(function($item) { |
|
| 48 | 48 | |
| 49 | 49 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 50 | - $uri = $entity_service->get_uri( $item->ID ); |
|
| 50 | + $uri = $entity_service->get_uri($item->ID); |
|
| 51 | 51 | |
| 52 | 52 | return array( |
| 53 | 53 | // see #1074: The value property is needed for autocomplete in category page |
@@ -55,22 +55,22 @@ discard block |
||
| 55 | 55 | // wont be correctly saved. |
| 56 | 56 | 'value' => $uri, |
| 57 | 57 | 'id' => $uri, |
| 58 | - 'label' => array( $item->post_title ), |
|
| 59 | - 'labels' => $entity_service->get_alternative_labels( $item->ID ), |
|
| 60 | - 'descriptions' => array( Wordlift_Post_Excerpt_Helper::get_text_excerpt( $item ) ), |
|
| 58 | + 'label' => array($item->post_title), |
|
| 59 | + 'labels' => $entity_service->get_alternative_labels($item->ID), |
|
| 60 | + 'descriptions' => array(Wordlift_Post_Excerpt_Helper::get_text_excerpt($item)), |
|
| 61 | 61 | 'scope' => 'local', |
| 62 | - 'sameAss' => get_post_meta( $item->ID, \Wordlift_Schema_Service::FIELD_SAME_AS ), |
|
| 62 | + 'sameAss' => get_post_meta($item->ID, \Wordlift_Schema_Service::FIELD_SAME_AS), |
|
| 63 | 63 | // The following properties are less relevant because we're linking entities that exist already in the |
| 64 | 64 | // vocabulary. That's why we don't make an effort to load the real data. |
| 65 | - 'types' => array( 'http://schema.org/Thing' ), |
|
| 65 | + 'types' => array('http://schema.org/Thing'), |
|
| 66 | 66 | 'urls' => array(), |
| 67 | 67 | 'images' => array(), |
| 68 | 68 | ); |
| 69 | - }, $posts ); |
|
| 69 | + }, $posts); |
|
| 70 | 70 | |
| 71 | - $results = array_unique( $results, SORT_REGULAR ); |
|
| 71 | + $results = array_unique($results, SORT_REGULAR); |
|
| 72 | 72 | |
| 73 | - return $this->filter( $results, $excludes ); |
|
| 73 | + return $this->filter($results, $excludes); |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | } |
@@ -22,29 +22,29 @@ discard block |
||
| 22 | 22 | * |
| 23 | 23 | * @param $shortcode_instance \Wordlift_Shortcode |
| 24 | 24 | */ |
| 25 | - public function __construct( $shortcode_instance ) { |
|
| 26 | - $this->rest_url_name = $this->get_widget_name( $shortcode_instance ); |
|
| 27 | - $this->filter_name = str_replace('-', '_', $this->rest_url_name ); |
|
| 28 | - add_action( 'rest_api_init', array( $this, 'register_template_route' ) ); |
|
| 25 | + public function __construct($shortcode_instance) { |
|
| 26 | + $this->rest_url_name = $this->get_widget_name($shortcode_instance); |
|
| 27 | + $this->filter_name = str_replace('-', '_', $this->rest_url_name); |
|
| 28 | + add_action('rest_api_init', array($this, 'register_template_route')); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | public function register_template_route() { |
| 32 | 32 | |
| 33 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, "/{$this->rest_url_name}/template/", array( |
|
| 33 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, "/{$this->rest_url_name}/template/", array( |
|
| 34 | 34 | 'methods' => \WP_REST_Server::CREATABLE, |
| 35 | - 'callback' => array( $this, 'get_template' ), |
|
| 35 | + 'callback' => array($this, 'get_template'), |
|
| 36 | 36 | /** |
| 37 | 37 | * We want this endpoint to be publicly accessible |
| 38 | 38 | */ |
| 39 | 39 | 'permission_callback' => '__return_true', |
| 40 | 40 | 'args' => array( |
| 41 | 41 | 'template_id' => array( |
| 42 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 43 | - return is_string( $param ) && $param; |
|
| 42 | + 'validate_callback' => function($param, $request, $key) { |
|
| 43 | + return is_string($param) && $param; |
|
| 44 | 44 | }, |
| 45 | 45 | ), |
| 46 | 46 | ) |
| 47 | - ) ); |
|
| 47 | + )); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | /** |
@@ -56,13 +56,13 @@ discard block |
||
| 56 | 56 | * |
| 57 | 57 | * @return string Returns the template string. |
| 58 | 58 | */ |
| 59 | - public function get_template( $request ) { |
|
| 59 | + public function get_template($request) { |
|
| 60 | 60 | $data = $request->get_params(); |
| 61 | 61 | $template_id = (string) $data['template_id']; |
| 62 | - $templates = apply_filters( "wordlift_{$this->filter_name}_templates", array() ); |
|
| 63 | - $template = array_key_exists( $template_id, $templates ) ? $templates[ $template_id ] : ''; |
|
| 62 | + $templates = apply_filters("wordlift_{$this->filter_name}_templates", array()); |
|
| 63 | + $template = array_key_exists($template_id, $templates) ? $templates[$template_id] : ''; |
|
| 64 | 64 | |
| 65 | - return array( 'template' => $template ); |
|
| 65 | + return array('template' => $template); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
@@ -70,9 +70,9 @@ discard block |
||
| 70 | 70 | * |
| 71 | 71 | * @return string |
| 72 | 72 | */ |
| 73 | - private static function get_widget_name( $shortcode_instance ) { |
|
| 74 | - $name = str_replace( 'wl_', '', $shortcode_instance::SHORTCODE ); |
|
| 75 | - return str_replace( '_', '-', $name ); |
|
| 73 | + private static function get_widget_name($shortcode_instance) { |
|
| 74 | + $name = str_replace('wl_', '', $shortcode_instance::SHORTCODE); |
|
| 75 | + return str_replace('_', '-', $name); |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | } |
| 79 | 79 | \ No newline at end of file |
@@ -7,72 +7,72 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Async_Template_Decorator { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * @var $rest_url_name string |
|
| 12 | - */ |
|
| 13 | - private $rest_url_name; |
|
| 10 | + /** |
|
| 11 | + * @var $rest_url_name string |
|
| 12 | + */ |
|
| 13 | + private $rest_url_name; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @var $filter_name string |
|
| 17 | - */ |
|
| 18 | - private $filter_name; |
|
| 15 | + /** |
|
| 16 | + * @var $filter_name string |
|
| 17 | + */ |
|
| 18 | + private $filter_name; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Async_Template_Decorator constructor. |
|
| 22 | - * |
|
| 23 | - * @param $shortcode_instance \Wordlift_Shortcode |
|
| 24 | - */ |
|
| 25 | - public function __construct( $shortcode_instance ) { |
|
| 26 | - $this->rest_url_name = $this->get_widget_name( $shortcode_instance ); |
|
| 27 | - $this->filter_name = str_replace('-', '_', $this->rest_url_name ); |
|
| 28 | - add_action( 'rest_api_init', array( $this, 'register_template_route' ) ); |
|
| 29 | - } |
|
| 20 | + /** |
|
| 21 | + * Async_Template_Decorator constructor. |
|
| 22 | + * |
|
| 23 | + * @param $shortcode_instance \Wordlift_Shortcode |
|
| 24 | + */ |
|
| 25 | + public function __construct( $shortcode_instance ) { |
|
| 26 | + $this->rest_url_name = $this->get_widget_name( $shortcode_instance ); |
|
| 27 | + $this->filter_name = str_replace('-', '_', $this->rest_url_name ); |
|
| 28 | + add_action( 'rest_api_init', array( $this, 'register_template_route' ) ); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - public function register_template_route() { |
|
| 31 | + public function register_template_route() { |
|
| 32 | 32 | |
| 33 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, "/{$this->rest_url_name}/template/", array( |
|
| 34 | - 'methods' => \WP_REST_Server::CREATABLE, |
|
| 35 | - 'callback' => array( $this, 'get_template' ), |
|
| 36 | - /** |
|
| 37 | - * We want this endpoint to be publicly accessible |
|
| 38 | - */ |
|
| 39 | - 'permission_callback' => '__return_true', |
|
| 40 | - 'args' => array( |
|
| 41 | - 'template_id' => array( |
|
| 42 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 43 | - return is_string( $param ) && $param; |
|
| 44 | - }, |
|
| 45 | - ), |
|
| 46 | - ) |
|
| 47 | - ) ); |
|
| 48 | - } |
|
| 33 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, "/{$this->rest_url_name}/template/", array( |
|
| 34 | + 'methods' => \WP_REST_Server::CREATABLE, |
|
| 35 | + 'callback' => array( $this, 'get_template' ), |
|
| 36 | + /** |
|
| 37 | + * We want this endpoint to be publicly accessible |
|
| 38 | + */ |
|
| 39 | + 'permission_callback' => '__return_true', |
|
| 40 | + 'args' => array( |
|
| 41 | + 'template_id' => array( |
|
| 42 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
| 43 | + return is_string( $param ) && $param; |
|
| 44 | + }, |
|
| 45 | + ), |
|
| 46 | + ) |
|
| 47 | + ) ); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Shortcode widget makes call to this endpoint to get the template. |
|
| 52 | - * Takes the request, checks if template id is registered via filter, |
|
| 53 | - * if not it returns empty. |
|
| 54 | - * |
|
| 55 | - * @param $request \WP_REST_Request |
|
| 56 | - * |
|
| 57 | - * @return string Returns the template string. |
|
| 58 | - */ |
|
| 59 | - public function get_template( $request ) { |
|
| 60 | - $data = $request->get_params(); |
|
| 61 | - $template_id = (string) $data['template_id']; |
|
| 62 | - $templates = apply_filters( "wordlift_{$this->filter_name}_templates", array() ); |
|
| 63 | - $template = array_key_exists( $template_id, $templates ) ? $templates[ $template_id ] : ''; |
|
| 50 | + /** |
|
| 51 | + * Shortcode widget makes call to this endpoint to get the template. |
|
| 52 | + * Takes the request, checks if template id is registered via filter, |
|
| 53 | + * if not it returns empty. |
|
| 54 | + * |
|
| 55 | + * @param $request \WP_REST_Request |
|
| 56 | + * |
|
| 57 | + * @return string Returns the template string. |
|
| 58 | + */ |
|
| 59 | + public function get_template( $request ) { |
|
| 60 | + $data = $request->get_params(); |
|
| 61 | + $template_id = (string) $data['template_id']; |
|
| 62 | + $templates = apply_filters( "wordlift_{$this->filter_name}_templates", array() ); |
|
| 63 | + $template = array_key_exists( $template_id, $templates ) ? $templates[ $template_id ] : ''; |
|
| 64 | 64 | |
| 65 | - return array( 'template' => $template ); |
|
| 66 | - } |
|
| 65 | + return array( 'template' => $template ); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * @param $shortcode_instance \Wordlift_Shortcode |
|
| 70 | - * |
|
| 71 | - * @return string |
|
| 72 | - */ |
|
| 73 | - private static function get_widget_name( $shortcode_instance ) { |
|
| 74 | - $name = str_replace( 'wl_', '', $shortcode_instance::SHORTCODE ); |
|
| 75 | - return str_replace( '_', '-', $name ); |
|
| 76 | - } |
|
| 68 | + /** |
|
| 69 | + * @param $shortcode_instance \Wordlift_Shortcode |
|
| 70 | + * |
|
| 71 | + * @return string |
|
| 72 | + */ |
|
| 73 | + private static function get_widget_name( $shortcode_instance ) { |
|
| 74 | + $name = str_replace( 'wl_', '', $shortcode_instance::SHORTCODE ); |
|
| 75 | + return str_replace( '_', '-', $name ); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | 78 | } |
| 79 | 79 | \ No newline at end of file |
@@ -7,39 +7,39 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | class Navigator_Data { |
| 9 | 9 | |
| 10 | - public static function get_post_types_as_string( $post_types ) { |
|
| 11 | - if ( $post_types === array() ) { |
|
| 12 | - $post_types = get_post_types(); |
|
| 13 | - } |
|
| 14 | - $post_types = array_map( function ( $post_type ) { |
|
| 15 | - return "'" . esc_sql( $post_type ) . "'"; |
|
| 16 | - }, $post_types ); |
|
| 17 | - |
|
| 18 | - return implode( ',', $post_types ); |
|
| 19 | - } |
|
| 20 | - |
|
| 21 | - public static function post_navigator_get_results( |
|
| 22 | - $post_id, $fields = array( |
|
| 23 | - 'ID', |
|
| 24 | - 'post_title', |
|
| 25 | - ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
|
| 26 | - ) { |
|
| 27 | - |
|
| 28 | - $post_types = self::get_post_types_as_string( $post_types ); |
|
| 29 | - global $wpdb; |
|
| 30 | - |
|
| 31 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 32 | - return "p.$item AS $item"; |
|
| 33 | - }, (array) $fields ) ); |
|
| 34 | - |
|
| 35 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 36 | - return "p.$item"; |
|
| 37 | - }, (array) $order_by ) ); |
|
| 38 | - |
|
| 39 | - |
|
| 40 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 41 | - return $wpdb->get_results( |
|
| 42 | - $wpdb->prepare( <<<EOF |
|
| 10 | + public static function get_post_types_as_string( $post_types ) { |
|
| 11 | + if ( $post_types === array() ) { |
|
| 12 | + $post_types = get_post_types(); |
|
| 13 | + } |
|
| 14 | + $post_types = array_map( function ( $post_type ) { |
|
| 15 | + return "'" . esc_sql( $post_type ) . "'"; |
|
| 16 | + }, $post_types ); |
|
| 17 | + |
|
| 18 | + return implode( ',', $post_types ); |
|
| 19 | + } |
|
| 20 | + |
|
| 21 | + public static function post_navigator_get_results( |
|
| 22 | + $post_id, $fields = array( |
|
| 23 | + 'ID', |
|
| 24 | + 'post_title', |
|
| 25 | + ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
|
| 26 | + ) { |
|
| 27 | + |
|
| 28 | + $post_types = self::get_post_types_as_string( $post_types ); |
|
| 29 | + global $wpdb; |
|
| 30 | + |
|
| 31 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 32 | + return "p.$item AS $item"; |
|
| 33 | + }, (array) $fields ) ); |
|
| 34 | + |
|
| 35 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 36 | + return "p.$item"; |
|
| 37 | + }, (array) $order_by ) ); |
|
| 38 | + |
|
| 39 | + |
|
| 40 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 41 | + return $wpdb->get_results( |
|
| 42 | + $wpdb->prepare( <<<EOF |
|
| 43 | 43 | SELECT %4\$s, p2.ID as entity_id |
| 44 | 44 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 45 | 45 | INNER JOIN {$wpdb->prefix}wl_relation_instances r2 |
@@ -73,33 +73,33 @@ discard block |
||
| 73 | 73 | LIMIT %2\$d |
| 74 | 74 | OFFSET %3\$d |
| 75 | 75 | EOF |
| 76 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 77 | - ); |
|
| 76 | + , $post_id, $limit, $offset, $select, $order_by ) |
|
| 77 | + ); |
|
| 78 | 78 | |
| 79 | - } |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | 81 | |
| 82 | - public static function entity_navigator_get_results( |
|
| 83 | - $post_id, $fields = array( |
|
| 84 | - 'ID', |
|
| 85 | - 'post_title', |
|
| 86 | - ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
|
| 87 | - ) { |
|
| 88 | - global $wpdb; |
|
| 82 | + public static function entity_navigator_get_results( |
|
| 83 | + $post_id, $fields = array( |
|
| 84 | + 'ID', |
|
| 85 | + 'post_title', |
|
| 86 | + ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
|
| 87 | + ) { |
|
| 88 | + global $wpdb; |
|
| 89 | 89 | |
| 90 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 91 | - return "p.$item AS $item"; |
|
| 92 | - }, (array) $fields ) ); |
|
| 90 | + $select = implode( ', ', array_map( function ( $item ) { |
|
| 91 | + return "p.$item AS $item"; |
|
| 92 | + }, (array) $fields ) ); |
|
| 93 | 93 | |
| 94 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 95 | - return "p.$item"; |
|
| 96 | - }, (array) $order_by ) ); |
|
| 94 | + $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 95 | + return "p.$item"; |
|
| 96 | + }, (array) $order_by ) ); |
|
| 97 | 97 | |
| 98 | - $post_types = self::get_post_types_as_string( $post_types ); |
|
| 98 | + $post_types = self::get_post_types_as_string( $post_types ); |
|
| 99 | 99 | |
| 100 | - /** @noinspection SqlNoDataSourceInspection */ |
|
| 101 | - return $wpdb->get_results( |
|
| 102 | - $wpdb->prepare( <<<EOF |
|
| 100 | + /** @noinspection SqlNoDataSourceInspection */ |
|
| 101 | + return $wpdb->get_results( |
|
| 102 | + $wpdb->prepare( <<<EOF |
|
| 103 | 103 | SELECT %4\$s, p2.ID as entity_id |
| 104 | 104 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 105 | 105 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -130,9 +130,9 @@ discard block |
||
| 130 | 130 | LIMIT %2\$d |
| 131 | 131 | OFFSET %3\$d |
| 132 | 132 | EOF |
| 133 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 134 | - ); |
|
| 135 | - } |
|
| 133 | + , $post_id, $limit, $offset, $select, $order_by ) |
|
| 134 | + ); |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | 137 | |
| 138 | 138 | } |
@@ -7,15 +7,15 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | class Navigator_Data { |
| 9 | 9 | |
| 10 | - public static function get_post_types_as_string( $post_types ) { |
|
| 11 | - if ( $post_types === array() ) { |
|
| 10 | + public static function get_post_types_as_string($post_types) { |
|
| 11 | + if ($post_types === array()) { |
|
| 12 | 12 | $post_types = get_post_types(); |
| 13 | 13 | } |
| 14 | - $post_types = array_map( function ( $post_type ) { |
|
| 15 | - return "'" . esc_sql( $post_type ) . "'"; |
|
| 16 | - }, $post_types ); |
|
| 14 | + $post_types = array_map(function($post_type) { |
|
| 15 | + return "'".esc_sql($post_type)."'"; |
|
| 16 | + }, $post_types); |
|
| 17 | 17 | |
| 18 | - return implode( ',', $post_types ); |
|
| 18 | + return implode(',', $post_types); |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | public static function post_navigator_get_results( |
@@ -25,21 +25,21 @@ discard block |
||
| 25 | 25 | ), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array() |
| 26 | 26 | ) { |
| 27 | 27 | |
| 28 | - $post_types = self::get_post_types_as_string( $post_types ); |
|
| 28 | + $post_types = self::get_post_types_as_string($post_types); |
|
| 29 | 29 | global $wpdb; |
| 30 | 30 | |
| 31 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 31 | + $select = implode(', ', array_map(function($item) { |
|
| 32 | 32 | return "p.$item AS $item"; |
| 33 | - }, (array) $fields ) ); |
|
| 33 | + }, (array) $fields)); |
|
| 34 | 34 | |
| 35 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 35 | + $order_by = implode(', ', array_map(function($item) { |
|
| 36 | 36 | return "p.$item"; |
| 37 | - }, (array) $order_by ) ); |
|
| 37 | + }, (array) $order_by)); |
|
| 38 | 38 | |
| 39 | 39 | |
| 40 | 40 | /** @noinspection SqlNoDataSourceInspection */ |
| 41 | 41 | return $wpdb->get_results( |
| 42 | - $wpdb->prepare( <<<EOF |
|
| 42 | + $wpdb->prepare(<<<EOF |
|
| 43 | 43 | SELECT %4\$s, p2.ID as entity_id |
| 44 | 44 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 45 | 45 | INNER JOIN {$wpdb->prefix}wl_relation_instances r2 |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | LIMIT %2\$d |
| 74 | 74 | OFFSET %3\$d |
| 75 | 75 | EOF |
| 76 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 76 | + , $post_id, $limit, $offset, $select, $order_by) |
|
| 77 | 77 | ); |
| 78 | 78 | |
| 79 | 79 | } |
@@ -87,19 +87,19 @@ discard block |
||
| 87 | 87 | ) { |
| 88 | 88 | global $wpdb; |
| 89 | 89 | |
| 90 | - $select = implode( ', ', array_map( function ( $item ) { |
|
| 90 | + $select = implode(', ', array_map(function($item) { |
|
| 91 | 91 | return "p.$item AS $item"; |
| 92 | - }, (array) $fields ) ); |
|
| 92 | + }, (array) $fields)); |
|
| 93 | 93 | |
| 94 | - $order_by = implode( ', ', array_map( function ( $item ) { |
|
| 94 | + $order_by = implode(', ', array_map(function($item) { |
|
| 95 | 95 | return "p.$item"; |
| 96 | - }, (array) $order_by ) ); |
|
| 96 | + }, (array) $order_by)); |
|
| 97 | 97 | |
| 98 | - $post_types = self::get_post_types_as_string( $post_types ); |
|
| 98 | + $post_types = self::get_post_types_as_string($post_types); |
|
| 99 | 99 | |
| 100 | 100 | /** @noinspection SqlNoDataSourceInspection */ |
| 101 | 101 | return $wpdb->get_results( |
| 102 | - $wpdb->prepare( <<<EOF |
|
| 102 | + $wpdb->prepare(<<<EOF |
|
| 103 | 103 | SELECT %4\$s, p2.ID as entity_id |
| 104 | 104 | FROM {$wpdb->prefix}wl_relation_instances r1 |
| 105 | 105 | -- get the ID of the post entity in common between the object and the subject 2. |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | LIMIT %2\$d |
| 131 | 131 | OFFSET %3\$d |
| 132 | 132 | EOF |
| 133 | - , $post_id, $limit, $offset, $select, $order_by ) |
|
| 133 | + , $post_id, $limit, $offset, $select, $order_by) |
|
| 134 | 134 | ); |
| 135 | 135 | } |
| 136 | 136 | |
@@ -17,158 +17,158 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | class Wordlift_Products_Navigator_Shortcode extends Wordlift_Shortcode { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - const SHORTCODE = 'wl_products_navigator'; |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + const SHORTCODE = 'wl_products_navigator'; |
|
| 24 | 24 | |
| 25 | - public function __construct() { |
|
| 26 | - parent::__construct(); |
|
| 27 | - $this->register_block_type(); |
|
| 28 | - } |
|
| 25 | + public function __construct() { |
|
| 26 | + parent::__construct(); |
|
| 27 | + $this->register_block_type(); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * {@inheritdoc} |
|
| 32 | - */ |
|
| 33 | - public function render( $atts ) { |
|
| 30 | + /** |
|
| 31 | + * {@inheritdoc} |
|
| 32 | + */ |
|
| 33 | + public function render( $atts ) { |
|
| 34 | 34 | |
| 35 | - return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 36 | - : $this->web_shortcode( $atts ); |
|
| 37 | - } |
|
| 35 | + return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 36 | + : $this->web_shortcode( $atts ); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - private function register_block_type() { |
|
| 39 | + private function register_block_type() { |
|
| 40 | 40 | |
| 41 | - $scope = $this; |
|
| 41 | + $scope = $this; |
|
| 42 | 42 | |
| 43 | - add_action( 'init', function () use ( $scope ) { |
|
| 44 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 45 | - // Gutenberg is not active. |
|
| 46 | - return; |
|
| 47 | - } |
|
| 43 | + add_action( 'init', function () use ( $scope ) { |
|
| 44 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 45 | + // Gutenberg is not active. |
|
| 46 | + return; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - register_block_type( 'wordlift/products-navigator', array( |
|
| 50 | - 'editor_script' => 'wl-block-editor', |
|
| 51 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 52 | - $attr_code = ''; |
|
| 53 | - foreach ( $attributes as $key => $value ) { |
|
| 54 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 55 | - } |
|
| 49 | + register_block_type( 'wordlift/products-navigator', array( |
|
| 50 | + 'editor_script' => 'wl-block-editor', |
|
| 51 | + 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 52 | + $attr_code = ''; |
|
| 53 | + foreach ( $attributes as $key => $value ) { |
|
| 54 | + $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 58 | - }, |
|
| 59 | - 'attributes' => array( |
|
| 60 | - 'title' => array( |
|
| 61 | - 'type' => 'string', |
|
| 62 | - 'default' => __( 'Related products', 'wordlift' ), |
|
| 63 | - ), |
|
| 64 | - 'limit' => array( |
|
| 65 | - 'type' => 'number', |
|
| 66 | - 'default' => 4, |
|
| 67 | - ), |
|
| 68 | - 'template_id' => array( |
|
| 69 | - 'type' => 'string', |
|
| 70 | - 'default' => '', |
|
| 71 | - ), |
|
| 72 | - 'post_id' => array( |
|
| 73 | - 'type' => 'number', |
|
| 74 | - 'default' => '', |
|
| 75 | - ), |
|
| 76 | - 'offset' => array( |
|
| 77 | - 'type' => 'number', |
|
| 78 | - 'default' => 0, |
|
| 79 | - ), |
|
| 80 | - 'uniqid' => array( |
|
| 81 | - 'type' => 'string', |
|
| 82 | - 'default' => '', |
|
| 83 | - ), |
|
| 84 | - 'order_by' => array( |
|
| 85 | - 'type' => 'string', |
|
| 86 | - 'default' => 'ID DESC', |
|
| 87 | - ), |
|
| 88 | - 'preview' => array( |
|
| 89 | - 'type' => 'boolean', |
|
| 90 | - 'default' => false, |
|
| 91 | - ), |
|
| 92 | - 'preview_src' => array( |
|
| 93 | - 'type' => 'string', |
|
| 94 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/products-navigator.png', |
|
| 95 | - ), |
|
| 96 | - ), |
|
| 97 | - ) ); |
|
| 98 | - } ); |
|
| 99 | - } |
|
| 57 | + return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 58 | + }, |
|
| 59 | + 'attributes' => array( |
|
| 60 | + 'title' => array( |
|
| 61 | + 'type' => 'string', |
|
| 62 | + 'default' => __( 'Related products', 'wordlift' ), |
|
| 63 | + ), |
|
| 64 | + 'limit' => array( |
|
| 65 | + 'type' => 'number', |
|
| 66 | + 'default' => 4, |
|
| 67 | + ), |
|
| 68 | + 'template_id' => array( |
|
| 69 | + 'type' => 'string', |
|
| 70 | + 'default' => '', |
|
| 71 | + ), |
|
| 72 | + 'post_id' => array( |
|
| 73 | + 'type' => 'number', |
|
| 74 | + 'default' => '', |
|
| 75 | + ), |
|
| 76 | + 'offset' => array( |
|
| 77 | + 'type' => 'number', |
|
| 78 | + 'default' => 0, |
|
| 79 | + ), |
|
| 80 | + 'uniqid' => array( |
|
| 81 | + 'type' => 'string', |
|
| 82 | + 'default' => '', |
|
| 83 | + ), |
|
| 84 | + 'order_by' => array( |
|
| 85 | + 'type' => 'string', |
|
| 86 | + 'default' => 'ID DESC', |
|
| 87 | + ), |
|
| 88 | + 'preview' => array( |
|
| 89 | + 'type' => 'boolean', |
|
| 90 | + 'default' => false, |
|
| 91 | + ), |
|
| 92 | + 'preview_src' => array( |
|
| 93 | + 'type' => 'string', |
|
| 94 | + 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/products-navigator.png', |
|
| 95 | + ), |
|
| 96 | + ), |
|
| 97 | + ) ); |
|
| 98 | + } ); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * Shared function used by web_shortcode and amp_shortcode |
|
| 103 | - * Bootstrap logic for attributes extraction and boolean filtering |
|
| 104 | - * |
|
| 105 | - * @param array $atts Shortcode attributes. |
|
| 106 | - * |
|
| 107 | - * @return array $shortcode_atts |
|
| 108 | - * @since 3.27.0 |
|
| 109 | - * |
|
| 110 | - */ |
|
| 111 | - private function make_shortcode_atts( $atts ) { |
|
| 101 | + /** |
|
| 102 | + * Shared function used by web_shortcode and amp_shortcode |
|
| 103 | + * Bootstrap logic for attributes extraction and boolean filtering |
|
| 104 | + * |
|
| 105 | + * @param array $atts Shortcode attributes. |
|
| 106 | + * |
|
| 107 | + * @return array $shortcode_atts |
|
| 108 | + * @since 3.27.0 |
|
| 109 | + * |
|
| 110 | + */ |
|
| 111 | + private function make_shortcode_atts( $atts ) { |
|
| 112 | 112 | |
| 113 | - // Extract attributes and set default values. |
|
| 114 | - $shortcode_atts = shortcode_atts( array( |
|
| 115 | - 'title' => __( 'Related products', 'wordlift' ), |
|
| 116 | - 'limit' => 4, |
|
| 117 | - 'offset' => 0, |
|
| 118 | - 'template_id' => '', |
|
| 119 | - 'post_id' => '', |
|
| 120 | - 'uniqid' => uniqid( 'wl-products-navigator-widget-' ), |
|
| 121 | - 'order_by' => 'ID DESC' |
|
| 122 | - ), $atts ); |
|
| 113 | + // Extract attributes and set default values. |
|
| 114 | + $shortcode_atts = shortcode_atts( array( |
|
| 115 | + 'title' => __( 'Related products', 'wordlift' ), |
|
| 116 | + 'limit' => 4, |
|
| 117 | + 'offset' => 0, |
|
| 118 | + 'template_id' => '', |
|
| 119 | + 'post_id' => '', |
|
| 120 | + 'uniqid' => uniqid( 'wl-products-navigator-widget-' ), |
|
| 121 | + 'order_by' => 'ID DESC' |
|
| 122 | + ), $atts ); |
|
| 123 | 123 | |
| 124 | - return $shortcode_atts; |
|
| 125 | - } |
|
| 124 | + return $shortcode_atts; |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * Function in charge of diplaying the [wl_products_navigator] in web mode. |
|
| 129 | - * |
|
| 130 | - * @param array $atts Shortcode attributes. |
|
| 131 | - * |
|
| 132 | - * @return string Shortcode HTML for web |
|
| 133 | - * @since 3.20.0 |
|
| 134 | - * |
|
| 135 | - */ |
|
| 136 | - private function web_shortcode( $atts ) { |
|
| 127 | + /** |
|
| 128 | + * Function in charge of diplaying the [wl_products_navigator] in web mode. |
|
| 129 | + * |
|
| 130 | + * @param array $atts Shortcode attributes. |
|
| 131 | + * |
|
| 132 | + * @return string Shortcode HTML for web |
|
| 133 | + * @since 3.20.0 |
|
| 134 | + * |
|
| 135 | + */ |
|
| 136 | + private function web_shortcode( $atts ) { |
|
| 137 | 137 | |
| 138 | - // attributes extraction and boolean filtering |
|
| 139 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 138 | + // attributes extraction and boolean filtering |
|
| 139 | + $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 140 | 140 | |
| 141 | - // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 142 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 143 | - return; |
|
| 144 | - } |
|
| 141 | + // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 142 | + if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 143 | + return; |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 147 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 148 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 149 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 150 | - $offset = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) ); |
|
| 151 | - $sort = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) ); |
|
| 152 | - $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-products-navigator-widget-' ); |
|
| 146 | + $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 147 | + $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 148 | + $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 149 | + $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 150 | + $offset = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) ); |
|
| 151 | + $sort = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) ); |
|
| 152 | + $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-products-navigator-widget-' ); |
|
| 153 | 153 | |
| 154 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 155 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 156 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/products-navigator' . $delimiter . build_query( array( |
|
| 157 | - 'uniqid' => $navigator_id, |
|
| 158 | - 'post_id' => $post->ID, |
|
| 159 | - 'limit' => $limit, |
|
| 160 | - 'offset' => $offset, |
|
| 161 | - 'sort' => $sort |
|
| 162 | - ) ) ) : false; |
|
| 154 | + $permalink_structure = get_option( 'permalink_structure' ); |
|
| 155 | + $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 156 | + $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/products-navigator' . $delimiter . build_query( array( |
|
| 157 | + 'uniqid' => $navigator_id, |
|
| 158 | + 'post_id' => $post->ID, |
|
| 159 | + 'limit' => $limit, |
|
| 160 | + 'offset' => $offset, |
|
| 161 | + 'sort' => $sort |
|
| 162 | + ) ) ) : false; |
|
| 163 | 163 | |
| 164 | - // avoid building the widget when no valid $rest_url |
|
| 165 | - if ( ! $rest_url ) { |
|
| 166 | - return; |
|
| 167 | - } |
|
| 164 | + // avoid building the widget when no valid $rest_url |
|
| 165 | + if ( ! $rest_url ) { |
|
| 166 | + return; |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 169 | + wp_enqueue_script( 'wordlift-cloud' ); |
|
| 170 | 170 | |
| 171 | - return <<<HTML |
|
| 171 | + return <<<HTML |
|
| 172 | 172 | <!-- Products Navigator {$navigator_id} --> |
| 173 | 173 | <div id="{$navigator_id}" |
| 174 | 174 | class="wl-products-navigator" |
@@ -178,61 +178,61 @@ discard block |
||
| 178 | 178 | data-limit="{$limit}"></div> |
| 179 | 179 | <!-- /Products Navigator {$navigator_id} --> |
| 180 | 180 | HTML; |
| 181 | - } |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - /** |
|
| 184 | - * Function in charge of diplaying the [wl_products_navigator] in amp mode. |
|
| 185 | - * |
|
| 186 | - * @param array $atts Shortcode attributes. |
|
| 187 | - * |
|
| 188 | - * @return string Shortcode HTML for amp |
|
| 189 | - * @since 3.20.0 |
|
| 190 | - * |
|
| 191 | - */ |
|
| 192 | - private function amp_shortcode( $atts ) { |
|
| 183 | + /** |
|
| 184 | + * Function in charge of diplaying the [wl_products_navigator] in amp mode. |
|
| 185 | + * |
|
| 186 | + * @param array $atts Shortcode attributes. |
|
| 187 | + * |
|
| 188 | + * @return string Shortcode HTML for amp |
|
| 189 | + * @since 3.20.0 |
|
| 190 | + * |
|
| 191 | + */ |
|
| 192 | + private function amp_shortcode( $atts ) { |
|
| 193 | 193 | |
| 194 | - // attributes extraction and boolean filtering |
|
| 195 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 194 | + // attributes extraction and boolean filtering |
|
| 195 | + $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 196 | 196 | |
| 197 | - // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 198 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 199 | - return; |
|
| 200 | - } |
|
| 197 | + // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 198 | + if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 199 | + return; |
|
| 200 | + } |
|
| 201 | 201 | |
| 202 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 203 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 204 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 205 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 206 | - $offset = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) ); |
|
| 207 | - $sort = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) ); |
|
| 208 | - $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-products-navigator-widget-' ); |
|
| 202 | + $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 203 | + $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 204 | + $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 205 | + $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 206 | + $offset = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) ); |
|
| 207 | + $sort = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) ); |
|
| 208 | + $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-products-navigator-widget-' ); |
|
| 209 | 209 | |
| 210 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 211 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 212 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/products-navigator' . $delimiter . build_query( array( |
|
| 213 | - 'amp' => true, |
|
| 214 | - 'uniqid' => $navigator_id, |
|
| 215 | - 'post_id' => $post->ID, |
|
| 216 | - 'limit' => $limit, |
|
| 217 | - 'offset' => $offset, |
|
| 218 | - 'sort' => $sort |
|
| 219 | - ) ) ) : false; |
|
| 210 | + $permalink_structure = get_option( 'permalink_structure' ); |
|
| 211 | + $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 212 | + $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/products-navigator' . $delimiter . build_query( array( |
|
| 213 | + 'amp' => true, |
|
| 214 | + 'uniqid' => $navigator_id, |
|
| 215 | + 'post_id' => $post->ID, |
|
| 216 | + 'limit' => $limit, |
|
| 217 | + 'offset' => $offset, |
|
| 218 | + 'sort' => $sort |
|
| 219 | + ) ) ) : false; |
|
| 220 | 220 | |
| 221 | - // avoid building the widget when no valid $rest_url |
|
| 222 | - if ( ! $rest_url ) { |
|
| 223 | - return; |
|
| 224 | - } |
|
| 221 | + // avoid building the widget when no valid $rest_url |
|
| 222 | + if ( ! $rest_url ) { |
|
| 223 | + return; |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | - // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
|
| 227 | - // This is a hackish way, but this works for http and https URLs |
|
| 228 | - $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 226 | + // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
|
| 227 | + // This is a hackish way, but this works for http and https URLs |
|
| 228 | + $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 229 | 229 | |
| 230 | - if ( empty( $template_id ) ) { |
|
| 231 | - $template_id = "template-" . $navigator_id; |
|
| 232 | - wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 233 | - } |
|
| 230 | + if ( empty( $template_id ) ) { |
|
| 231 | + $template_id = "template-" . $navigator_id; |
|
| 232 | + wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 233 | + } |
|
| 234 | 234 | |
| 235 | - return <<<HTML |
|
| 235 | + return <<<HTML |
|
| 236 | 236 | <div id="{$navigator_id}" class="wl-amp-products-navigator"> |
| 237 | 237 | <h3 class="wl-headline">{$title}</h3> |
| 238 | 238 | <section class="cards"> |
@@ -334,6 +334,6 @@ discard block |
||
| 334 | 334 | </section> |
| 335 | 335 | </div> |
| 336 | 336 | HTML; |
| 337 | - } |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | 339 | } |
@@ -30,36 +30,36 @@ discard block |
||
| 30 | 30 | /** |
| 31 | 31 | * {@inheritdoc} |
| 32 | 32 | */ |
| 33 | - public function render( $atts ) { |
|
| 33 | + public function render($atts) { |
|
| 34 | 34 | |
| 35 | - return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 36 | - : $this->web_shortcode( $atts ); |
|
| 35 | + return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode($atts) |
|
| 36 | + : $this->web_shortcode($atts); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | private function register_block_type() { |
| 40 | 40 | |
| 41 | 41 | $scope = $this; |
| 42 | 42 | |
| 43 | - add_action( 'init', function () use ( $scope ) { |
|
| 44 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 43 | + add_action('init', function() use ($scope) { |
|
| 44 | + if ( ! function_exists('register_block_type')) { |
|
| 45 | 45 | // Gutenberg is not active. |
| 46 | 46 | return; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | - register_block_type( 'wordlift/products-navigator', array( |
|
| 49 | + register_block_type('wordlift/products-navigator', array( |
|
| 50 | 50 | 'editor_script' => 'wl-block-editor', |
| 51 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 51 | + 'render_callback' => function($attributes) use ($scope) { |
|
| 52 | 52 | $attr_code = ''; |
| 53 | - foreach ( $attributes as $key => $value ) { |
|
| 54 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 53 | + foreach ($attributes as $key => $value) { |
|
| 54 | + $attr_code .= $key.'="'.htmlentities($value).'" '; |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 57 | + return '['.$scope::SHORTCODE.' '.$attr_code.']'; |
|
| 58 | 58 | }, |
| 59 | 59 | 'attributes' => array( |
| 60 | 60 | 'title' => array( |
| 61 | 61 | 'type' => 'string', |
| 62 | - 'default' => __( 'Related products', 'wordlift' ), |
|
| 62 | + 'default' => __('Related products', 'wordlift'), |
|
| 63 | 63 | ), |
| 64 | 64 | 'limit' => array( |
| 65 | 65 | 'type' => 'number', |
@@ -91,10 +91,10 @@ discard block |
||
| 91 | 91 | ), |
| 92 | 92 | 'preview_src' => array( |
| 93 | 93 | 'type' => 'string', |
| 94 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/products-navigator.png', |
|
| 94 | + 'default' => WP_CONTENT_URL.'/plugins/wordlift/images/block-previews/products-navigator.png', |
|
| 95 | 95 | ), |
| 96 | 96 | ), |
| 97 | - ) ); |
|
| 97 | + )); |
|
| 98 | 98 | } ); |
| 99 | 99 | } |
| 100 | 100 | |
@@ -108,18 +108,18 @@ discard block |
||
| 108 | 108 | * @since 3.27.0 |
| 109 | 109 | * |
| 110 | 110 | */ |
| 111 | - private function make_shortcode_atts( $atts ) { |
|
| 111 | + private function make_shortcode_atts($atts) { |
|
| 112 | 112 | |
| 113 | 113 | // Extract attributes and set default values. |
| 114 | - $shortcode_atts = shortcode_atts( array( |
|
| 115 | - 'title' => __( 'Related products', 'wordlift' ), |
|
| 114 | + $shortcode_atts = shortcode_atts(array( |
|
| 115 | + 'title' => __('Related products', 'wordlift'), |
|
| 116 | 116 | 'limit' => 4, |
| 117 | 117 | 'offset' => 0, |
| 118 | 118 | 'template_id' => '', |
| 119 | 119 | 'post_id' => '', |
| 120 | - 'uniqid' => uniqid( 'wl-products-navigator-widget-' ), |
|
| 120 | + 'uniqid' => uniqid('wl-products-navigator-widget-'), |
|
| 121 | 121 | 'order_by' => 'ID DESC' |
| 122 | - ), $atts ); |
|
| 122 | + ), $atts); |
|
| 123 | 123 | |
| 124 | 124 | return $shortcode_atts; |
| 125 | 125 | } |
@@ -133,40 +133,40 @@ discard block |
||
| 133 | 133 | * @since 3.20.0 |
| 134 | 134 | * |
| 135 | 135 | */ |
| 136 | - private function web_shortcode( $atts ) { |
|
| 136 | + private function web_shortcode($atts) { |
|
| 137 | 137 | |
| 138 | 138 | // attributes extraction and boolean filtering |
| 139 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 139 | + $shortcode_atts = $this->make_shortcode_atts($atts); |
|
| 140 | 140 | |
| 141 | 141 | // avoid building the widget when no post_id is specified and there is a list of posts. |
| 142 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 142 | + if (empty($shortcode_atts['post_id']) && ! is_singular()) { |
|
| 143 | 143 | return; |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 147 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 148 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 149 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 150 | - $offset = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) ); |
|
| 151 | - $sort = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) ); |
|
| 152 | - $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-products-navigator-widget-' ); |
|
| 146 | + $post = ! empty($shortcode_atts['post_id']) ? get_post(intval(sanitize_text_field($shortcode_atts['post_id']))) : get_post(); |
|
| 147 | + $title = esc_attr(sanitize_text_field($shortcode_atts['title'])); |
|
| 148 | + $template_id = esc_attr(sanitize_text_field($shortcode_atts['template_id'])); |
|
| 149 | + $limit = esc_attr(sanitize_text_field($shortcode_atts['limit'])); |
|
| 150 | + $offset = esc_attr(sanitize_text_field($shortcode_atts['offset'])); |
|
| 151 | + $sort = esc_attr(sanitize_sql_orderby(sanitize_text_field($shortcode_atts['order_by']))); |
|
| 152 | + $navigator_id = ! empty($shortcode_atts['uniqid']) ? esc_attr(sanitize_text_field($shortcode_atts['uniqid'])) : uniqid('wl-products-navigator-widget-'); |
|
| 153 | 153 | |
| 154 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 155 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 156 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/products-navigator' . $delimiter . build_query( array( |
|
| 154 | + $permalink_structure = get_option('permalink_structure'); |
|
| 155 | + $delimiter = empty($permalink_structure) ? '&' : '?'; |
|
| 156 | + $rest_url = $post ? rest_url(WL_REST_ROUTE_DEFAULT_NAMESPACE.'/products-navigator'.$delimiter.build_query(array( |
|
| 157 | 157 | 'uniqid' => $navigator_id, |
| 158 | 158 | 'post_id' => $post->ID, |
| 159 | 159 | 'limit' => $limit, |
| 160 | 160 | 'offset' => $offset, |
| 161 | 161 | 'sort' => $sort |
| 162 | - ) ) ) : false; |
|
| 162 | + ))) : false; |
|
| 163 | 163 | |
| 164 | 164 | // avoid building the widget when no valid $rest_url |
| 165 | - if ( ! $rest_url ) { |
|
| 165 | + if ( ! $rest_url) { |
|
| 166 | 166 | return; |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 169 | + wp_enqueue_script('wordlift-cloud'); |
|
| 170 | 170 | |
| 171 | 171 | return <<<HTML |
| 172 | 172 | <!-- Products Navigator {$navigator_id} --> |
@@ -189,47 +189,47 @@ discard block |
||
| 189 | 189 | * @since 3.20.0 |
| 190 | 190 | * |
| 191 | 191 | */ |
| 192 | - private function amp_shortcode( $atts ) { |
|
| 192 | + private function amp_shortcode($atts) { |
|
| 193 | 193 | |
| 194 | 194 | // attributes extraction and boolean filtering |
| 195 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 195 | + $shortcode_atts = $this->make_shortcode_atts($atts); |
|
| 196 | 196 | |
| 197 | 197 | // avoid building the widget when no post_id is specified and there is a list of posts. |
| 198 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 198 | + if (empty($shortcode_atts['post_id']) && ! is_singular()) { |
|
| 199 | 199 | return; |
| 200 | 200 | } |
| 201 | 201 | |
| 202 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( sanitize_text_field( $shortcode_atts['post_id'] ) ) ) : get_post(); |
|
| 203 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 204 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 205 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 206 | - $offset = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) ); |
|
| 207 | - $sort = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) ); |
|
| 208 | - $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-products-navigator-widget-' ); |
|
| 202 | + $post = ! empty($shortcode_atts['post_id']) ? get_post(intval(sanitize_text_field($shortcode_atts['post_id']))) : get_post(); |
|
| 203 | + $title = esc_attr(sanitize_text_field($shortcode_atts['title'])); |
|
| 204 | + $template_id = esc_attr(sanitize_text_field($shortcode_atts['template_id'])); |
|
| 205 | + $limit = esc_attr(sanitize_text_field($shortcode_atts['limit'])); |
|
| 206 | + $offset = esc_attr(sanitize_text_field($shortcode_atts['offset'])); |
|
| 207 | + $sort = esc_attr(sanitize_sql_orderby(sanitize_text_field($shortcode_atts['order_by']))); |
|
| 208 | + $navigator_id = ! empty($shortcode_atts['uniqid']) ? esc_attr(sanitize_text_field($shortcode_atts['uniqid'])) : uniqid('wl-products-navigator-widget-'); |
|
| 209 | 209 | |
| 210 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 211 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 212 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/products-navigator' . $delimiter . build_query( array( |
|
| 210 | + $permalink_structure = get_option('permalink_structure'); |
|
| 211 | + $delimiter = empty($permalink_structure) ? '&' : '?'; |
|
| 212 | + $rest_url = $post ? rest_url(WL_REST_ROUTE_DEFAULT_NAMESPACE.'/products-navigator'.$delimiter.build_query(array( |
|
| 213 | 213 | 'amp' => true, |
| 214 | 214 | 'uniqid' => $navigator_id, |
| 215 | 215 | 'post_id' => $post->ID, |
| 216 | 216 | 'limit' => $limit, |
| 217 | 217 | 'offset' => $offset, |
| 218 | 218 | 'sort' => $sort |
| 219 | - ) ) ) : false; |
|
| 219 | + ))) : false; |
|
| 220 | 220 | |
| 221 | 221 | // avoid building the widget when no valid $rest_url |
| 222 | - if ( ! $rest_url ) { |
|
| 222 | + if ( ! $rest_url) { |
|
| 223 | 223 | return; |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
| 227 | 227 | // This is a hackish way, but this works for http and https URLs |
| 228 | - $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 228 | + $rest_url = str_replace(array('http:', 'https:'), '', $rest_url); |
|
| 229 | 229 | |
| 230 | - if ( empty( $template_id ) ) { |
|
| 231 | - $template_id = "template-" . $navigator_id; |
|
| 232 | - wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 230 | + if (empty($template_id)) { |
|
| 231 | + $template_id = "template-".$navigator_id; |
|
| 232 | + wp_enqueue_style('wordlift-amp-custom', plugin_dir_url(dirname(__FILE__)).'/css/wordlift-amp-custom.min.css'); |
|
| 233 | 233 | } |
| 234 | 234 | |
| 235 | 235 | return <<<HTML |
@@ -6,33 +6,33 @@ |
||
| 6 | 6 | */ |
| 7 | 7 | class Wordlift_Install_3_28_0 extends Wordlift_Install { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * {@inheritdoc} |
|
| 11 | - */ |
|
| 12 | - protected static $version = '3.28.0'; |
|
| 9 | + /** |
|
| 10 | + * {@inheritdoc} |
|
| 11 | + */ |
|
| 12 | + protected static $version = '3.28.0'; |
|
| 13 | 13 | |
| 14 | - public function install() { |
|
| 14 | + public function install() { |
|
| 15 | 15 | |
| 16 | - global $wpdb; |
|
| 16 | + global $wpdb; |
|
| 17 | 17 | |
| 18 | - $dataset_uri = Wordlift_Configuration_Service::get_instance()->get_dataset_uri(); |
|
| 18 | + $dataset_uri = Wordlift_Configuration_Service::get_instance()->get_dataset_uri(); |
|
| 19 | 19 | |
| 20 | - if ( ! $dataset_uri ) { |
|
| 21 | - // Dont run the query, running it would delete all the values. |
|
| 22 | - return; |
|
| 23 | - } |
|
| 24 | - $post_meta_table_name = $wpdb->postmeta; |
|
| 25 | - $meta_key = Wordlift_Schema_Service::FIELD_SAME_AS; |
|
| 20 | + if ( ! $dataset_uri ) { |
|
| 21 | + // Dont run the query, running it would delete all the values. |
|
| 22 | + return; |
|
| 23 | + } |
|
| 24 | + $post_meta_table_name = $wpdb->postmeta; |
|
| 25 | + $meta_key = Wordlift_Schema_Service::FIELD_SAME_AS; |
|
| 26 | 26 | |
| 27 | - $http = $wpdb->esc_like( 'http://' ) . '%'; |
|
| 28 | - $https = $wpdb->esc_like( 'https://' ) . '%'; |
|
| 29 | - $dataset_uri = $wpdb->esc_like( $dataset_uri ) . '%'; |
|
| 30 | - $sql = $wpdb->prepare( "DELETE FROM $post_meta_table_name WHERE meta_key='$meta_key' |
|
| 27 | + $http = $wpdb->esc_like( 'http://' ) . '%'; |
|
| 28 | + $https = $wpdb->esc_like( 'https://' ) . '%'; |
|
| 29 | + $dataset_uri = $wpdb->esc_like( $dataset_uri ) . '%'; |
|
| 30 | + $sql = $wpdb->prepare( "DELETE FROM $post_meta_table_name WHERE meta_key='$meta_key' |
|
| 31 | 31 | AND ( ( meta_value NOT LIKE %s AND meta_value NOT LIKE %s ) |
| 32 | 32 | OR meta_value LIKE %s )", $http, $https, $dataset_uri ); |
| 33 | - $wpdb->query( $sql ); |
|
| 33 | + $wpdb->query( $sql ); |
|
| 34 | 34 | |
| 35 | - } |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | 37 | |
| 38 | 38 | } |
@@ -17,20 +17,20 @@ |
||
| 17 | 17 | |
| 18 | 18 | $dataset_uri = Wordlift_Configuration_Service::get_instance()->get_dataset_uri(); |
| 19 | 19 | |
| 20 | - if ( ! $dataset_uri ) { |
|
| 20 | + if ( ! $dataset_uri) { |
|
| 21 | 21 | // Dont run the query, running it would delete all the values. |
| 22 | 22 | return; |
| 23 | 23 | } |
| 24 | 24 | $post_meta_table_name = $wpdb->postmeta; |
| 25 | 25 | $meta_key = Wordlift_Schema_Service::FIELD_SAME_AS; |
| 26 | 26 | |
| 27 | - $http = $wpdb->esc_like( 'http://' ) . '%'; |
|
| 28 | - $https = $wpdb->esc_like( 'https://' ) . '%'; |
|
| 29 | - $dataset_uri = $wpdb->esc_like( $dataset_uri ) . '%'; |
|
| 30 | - $sql = $wpdb->prepare( "DELETE FROM $post_meta_table_name WHERE meta_key='$meta_key' |
|
| 27 | + $http = $wpdb->esc_like('http://').'%'; |
|
| 28 | + $https = $wpdb->esc_like('https://').'%'; |
|
| 29 | + $dataset_uri = $wpdb->esc_like($dataset_uri).'%'; |
|
| 30 | + $sql = $wpdb->prepare("DELETE FROM $post_meta_table_name WHERE meta_key='$meta_key' |
|
| 31 | 31 | AND ( ( meta_value NOT LIKE %s AND meta_value NOT LIKE %s ) |
| 32 | -OR meta_value LIKE %s )", $http, $https, $dataset_uri ); |
|
| 33 | - $wpdb->query( $sql ); |
|
| 32 | +OR meta_value LIKE %s )", $http, $https, $dataset_uri); |
|
| 33 | + $wpdb->query($sql); |
|
| 34 | 34 | |
| 35 | 35 | } |
| 36 | 36 | |
@@ -19,122 +19,122 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | class Wordlift_Navigator_Shortcode extends Wordlift_Shortcode { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * {@inheritdoc} |
|
| 24 | - */ |
|
| 25 | - const SHORTCODE = 'wl_navigator'; |
|
| 22 | + /** |
|
| 23 | + * {@inheritdoc} |
|
| 24 | + */ |
|
| 25 | + const SHORTCODE = 'wl_navigator'; |
|
| 26 | 26 | |
| 27 | - public function __construct() { |
|
| 28 | - parent::__construct(); |
|
| 29 | - $this->register_block_type(); |
|
| 30 | - } |
|
| 27 | + public function __construct() { |
|
| 28 | + parent::__construct(); |
|
| 29 | + $this->register_block_type(); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * {@inheritdoc} |
|
| 34 | - */ |
|
| 35 | - public function render( $atts ) { |
|
| 36 | - return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 37 | - : $this->web_shortcode( $atts ); |
|
| 38 | - } |
|
| 32 | + /** |
|
| 33 | + * {@inheritdoc} |
|
| 34 | + */ |
|
| 35 | + public function render( $atts ) { |
|
| 36 | + return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 37 | + : $this->web_shortcode( $atts ); |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - private function register_block_type() { |
|
| 40 | + private function register_block_type() { |
|
| 41 | 41 | |
| 42 | - $scope = $this; |
|
| 42 | + $scope = $this; |
|
| 43 | 43 | |
| 44 | - add_action( 'init', function () use ( $scope ) { |
|
| 45 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 46 | - // Gutenberg is not active. |
|
| 47 | - return; |
|
| 48 | - } |
|
| 44 | + add_action( 'init', function () use ( $scope ) { |
|
| 45 | + if ( ! function_exists( 'register_block_type' ) ) { |
|
| 46 | + // Gutenberg is not active. |
|
| 47 | + return; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - register_block_type( 'wordlift/navigator', array( |
|
| 51 | - 'editor_script' => 'wl-block-editor', |
|
| 52 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 53 | - $attr_code = ''; |
|
| 54 | - foreach ( $attributes as $key => $value ) { |
|
| 55 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 56 | - } |
|
| 50 | + register_block_type( 'wordlift/navigator', array( |
|
| 51 | + 'editor_script' => 'wl-block-editor', |
|
| 52 | + 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 53 | + $attr_code = ''; |
|
| 54 | + foreach ( $attributes as $key => $value ) { |
|
| 55 | + $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 59 | - }, |
|
| 60 | - 'attributes' => $scope->get_navigator_block_attributes(), |
|
| 61 | - ) ); |
|
| 62 | - } ); |
|
| 63 | - } |
|
| 58 | + return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 59 | + }, |
|
| 60 | + 'attributes' => $scope->get_navigator_block_attributes(), |
|
| 61 | + ) ); |
|
| 62 | + } ); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Shared function used by web_shortcode and amp_shortcode |
|
| 67 | - * Bootstrap logic for attributes extraction and boolean filtering |
|
| 68 | - * |
|
| 69 | - * @param array $atts Shortcode attributes. |
|
| 70 | - * |
|
| 71 | - * @return array $shortcode_atts |
|
| 72 | - * @since 3.20.0 |
|
| 73 | - * |
|
| 74 | - */ |
|
| 75 | - private function make_shortcode_atts( $atts ) { |
|
| 65 | + /** |
|
| 66 | + * Shared function used by web_shortcode and amp_shortcode |
|
| 67 | + * Bootstrap logic for attributes extraction and boolean filtering |
|
| 68 | + * |
|
| 69 | + * @param array $atts Shortcode attributes. |
|
| 70 | + * |
|
| 71 | + * @return array $shortcode_atts |
|
| 72 | + * @since 3.20.0 |
|
| 73 | + * |
|
| 74 | + */ |
|
| 75 | + private function make_shortcode_atts( $atts ) { |
|
| 76 | 76 | |
| 77 | - // Extract attributes and set default values. |
|
| 78 | - $shortcode_atts = shortcode_atts( array( |
|
| 79 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 80 | - 'limit' => 4, |
|
| 81 | - 'offset' => 0, |
|
| 82 | - 'template_id' => '', |
|
| 83 | - 'post_id' => '', |
|
| 84 | - 'uniqid' => uniqid( 'wl-navigator-widget-' ), |
|
| 85 | - 'order_by' => 'ID DESC', |
|
| 86 | - 'post_types' => '', |
|
| 87 | - ), $atts ); |
|
| 77 | + // Extract attributes and set default values. |
|
| 78 | + $shortcode_atts = shortcode_atts( array( |
|
| 79 | + 'title' => __( 'Related articles', 'wordlift' ), |
|
| 80 | + 'limit' => 4, |
|
| 81 | + 'offset' => 0, |
|
| 82 | + 'template_id' => '', |
|
| 83 | + 'post_id' => '', |
|
| 84 | + 'uniqid' => uniqid( 'wl-navigator-widget-' ), |
|
| 85 | + 'order_by' => 'ID DESC', |
|
| 86 | + 'post_types' => '', |
|
| 87 | + ), $atts ); |
|
| 88 | 88 | |
| 89 | - return $shortcode_atts; |
|
| 90 | - } |
|
| 89 | + return $shortcode_atts; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Function in charge of displaying the [wl-navigator] in web mode. |
|
| 94 | - * |
|
| 95 | - * @param array $atts Shortcode attributes. |
|
| 96 | - * |
|
| 97 | - * @return string Shortcode HTML for web |
|
| 98 | - * @since 3.20.0 |
|
| 99 | - * |
|
| 100 | - */ |
|
| 101 | - private function web_shortcode( $atts ) { |
|
| 92 | + /** |
|
| 93 | + * Function in charge of displaying the [wl-navigator] in web mode. |
|
| 94 | + * |
|
| 95 | + * @param array $atts Shortcode attributes. |
|
| 96 | + * |
|
| 97 | + * @return string Shortcode HTML for web |
|
| 98 | + * @since 3.20.0 |
|
| 99 | + * |
|
| 100 | + */ |
|
| 101 | + private function web_shortcode( $atts ) { |
|
| 102 | 102 | |
| 103 | - // attributes extraction and boolean filtering |
|
| 104 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 103 | + // attributes extraction and boolean filtering |
|
| 104 | + $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 105 | 105 | |
| 106 | - // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 107 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 108 | - return; |
|
| 109 | - } |
|
| 106 | + // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 107 | + if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 108 | + return; |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( $shortcode_atts['post_id'] ) ) : get_post(); |
|
| 112 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 113 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 114 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 115 | - $offset = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) ); |
|
| 116 | - $sort = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) ); |
|
| 117 | - $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-navigator-widget-' ); |
|
| 111 | + $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( $shortcode_atts['post_id'] ) ) : get_post(); |
|
| 112 | + $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 113 | + $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 114 | + $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 115 | + $offset = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) ); |
|
| 116 | + $sort = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) ); |
|
| 117 | + $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-navigator-widget-' ); |
|
| 118 | 118 | |
| 119 | - $rest_url = $post ? admin_url( 'admin-ajax.php?' . build_query( array( |
|
| 120 | - 'action' => 'wl_navigator', |
|
| 121 | - 'uniqid' => $navigator_id, |
|
| 122 | - 'post_id' => $post->ID, |
|
| 123 | - 'limit' => $limit, |
|
| 124 | - 'offset' => $offset, |
|
| 125 | - 'sort' => $sort, |
|
| 126 | - 'post_types' => $shortcode_atts['post_types'] |
|
| 127 | - ) ) ) : false; |
|
| 119 | + $rest_url = $post ? admin_url( 'admin-ajax.php?' . build_query( array( |
|
| 120 | + 'action' => 'wl_navigator', |
|
| 121 | + 'uniqid' => $navigator_id, |
|
| 122 | + 'post_id' => $post->ID, |
|
| 123 | + 'limit' => $limit, |
|
| 124 | + 'offset' => $offset, |
|
| 125 | + 'sort' => $sort, |
|
| 126 | + 'post_types' => $shortcode_atts['post_types'] |
|
| 127 | + ) ) ) : false; |
|
| 128 | 128 | |
| 129 | - // avoid building the widget when no valid $rest_url |
|
| 130 | - if ( ! $rest_url ) { |
|
| 131 | - return; |
|
| 132 | - } |
|
| 129 | + // avoid building the widget when no valid $rest_url |
|
| 130 | + if ( ! $rest_url ) { |
|
| 131 | + return; |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 135 | - $template_url = get_rest_url( null, '/wordlift/v1/navigator/template' ); |
|
| 134 | + wp_enqueue_script( 'wordlift-cloud' ); |
|
| 135 | + $template_url = get_rest_url( null, '/wordlift/v1/navigator/template' ); |
|
| 136 | 136 | |
| 137 | - return <<<HTML |
|
| 137 | + return <<<HTML |
|
| 138 | 138 | <!-- Navigator {$navigator_id} --> |
| 139 | 139 | <div id="{$navigator_id}" |
| 140 | 140 | class="wl-navigator" |
@@ -145,59 +145,59 @@ discard block |
||
| 145 | 145 | data-limit="{$limit}"></div> |
| 146 | 146 | <!-- /Navigator {$navigator_id} --> |
| 147 | 147 | HTML; |
| 148 | - } |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * Function in charge of diplaying the [wl-faceted-search] in amp mode. |
|
| 152 | - * |
|
| 153 | - * @param array $atts Shortcode attributes. |
|
| 154 | - * |
|
| 155 | - * @return string Shortcode HTML for amp |
|
| 156 | - * @since 3.20.0 |
|
| 157 | - * |
|
| 158 | - */ |
|
| 159 | - private function amp_shortcode( $atts ) { |
|
| 160 | - // attributes extraction and boolean filtering |
|
| 161 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 150 | + /** |
|
| 151 | + * Function in charge of diplaying the [wl-faceted-search] in amp mode. |
|
| 152 | + * |
|
| 153 | + * @param array $atts Shortcode attributes. |
|
| 154 | + * |
|
| 155 | + * @return string Shortcode HTML for amp |
|
| 156 | + * @since 3.20.0 |
|
| 157 | + * |
|
| 158 | + */ |
|
| 159 | + private function amp_shortcode( $atts ) { |
|
| 160 | + // attributes extraction and boolean filtering |
|
| 161 | + $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 162 | 162 | |
| 163 | - // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 164 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 165 | - return; |
|
| 166 | - } |
|
| 163 | + // avoid building the widget when no post_id is specified and there is a list of posts. |
|
| 164 | + if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 165 | + return; |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( $shortcode_atts['post_id'] ) ) : get_post(); |
|
| 169 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 170 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 171 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 172 | - $offset = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) ); |
|
| 173 | - $sort = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) ); |
|
| 174 | - $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-navigator-widget-' ); |
|
| 168 | + $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( $shortcode_atts['post_id'] ) ) : get_post(); |
|
| 169 | + $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 170 | + $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 171 | + $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 172 | + $offset = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) ); |
|
| 173 | + $sort = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) ); |
|
| 174 | + $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-navigator-widget-' ); |
|
| 175 | 175 | |
| 176 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 177 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 178 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/navigator' . $delimiter . build_query( array( |
|
| 179 | - 'uniqid' => $navigator_id, |
|
| 180 | - 'post_id' => $post->ID, |
|
| 181 | - 'limit' => $limit, |
|
| 182 | - 'offset' => $offset, |
|
| 183 | - 'sort' => $sort |
|
| 184 | - ) ) ) : false; |
|
| 176 | + $permalink_structure = get_option( 'permalink_structure' ); |
|
| 177 | + $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 178 | + $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/navigator' . $delimiter . build_query( array( |
|
| 179 | + 'uniqid' => $navigator_id, |
|
| 180 | + 'post_id' => $post->ID, |
|
| 181 | + 'limit' => $limit, |
|
| 182 | + 'offset' => $offset, |
|
| 183 | + 'sort' => $sort |
|
| 184 | + ) ) ) : false; |
|
| 185 | 185 | |
| 186 | - // avoid building the widget when no valid $rest_url |
|
| 187 | - if ( ! $rest_url ) { |
|
| 188 | - return; |
|
| 189 | - } |
|
| 186 | + // avoid building the widget when no valid $rest_url |
|
| 187 | + if ( ! $rest_url ) { |
|
| 188 | + return; |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | - // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
|
| 192 | - // This is a hackish way, but this works for http and https URLs |
|
| 193 | - $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 191 | + // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
|
| 192 | + // This is a hackish way, but this works for http and https URLs |
|
| 193 | + $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 194 | 194 | |
| 195 | - if ( empty( $template_id ) ) { |
|
| 196 | - $template_id = "template-" . $navigator_id; |
|
| 197 | - wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 198 | - } |
|
| 195 | + if ( empty( $template_id ) ) { |
|
| 196 | + $template_id = "template-" . $navigator_id; |
|
| 197 | + wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - return <<<HTML |
|
| 200 | + return <<<HTML |
|
| 201 | 201 | <div id="{$navigator_id}" class="wl-amp-navigator" style="width: 100%"> |
| 202 | 202 | <h3 class="wl-headline">{$title}</h3> |
| 203 | 203 | <amp-list |
@@ -230,54 +230,54 @@ discard block |
||
| 230 | 230 | </div> |
| 231 | 231 | </template> |
| 232 | 232 | HTML; |
| 233 | - } |
|
| 233 | + } |
|
| 234 | 234 | |
| 235 | - /** |
|
| 236 | - * @return array |
|
| 237 | - */ |
|
| 238 | - public function get_navigator_block_attributes() { |
|
| 239 | - return array( |
|
| 240 | - 'title' => array( |
|
| 241 | - 'type' => 'string', |
|
| 242 | - 'default' => __( 'Related articles', 'wordlift' ), |
|
| 243 | - ), |
|
| 244 | - 'limit' => array( |
|
| 245 | - 'type' => 'number', |
|
| 246 | - 'default' => 4, |
|
| 247 | - ), |
|
| 248 | - 'template_id' => array( |
|
| 249 | - 'type' => 'string', |
|
| 250 | - 'default' => '', |
|
| 251 | - ), |
|
| 252 | - 'post_id' => array( |
|
| 253 | - 'type' => 'number', |
|
| 254 | - 'default' => '', |
|
| 255 | - ), |
|
| 256 | - 'offset' => array( |
|
| 257 | - 'type' => 'number', |
|
| 258 | - 'default' => 0, |
|
| 259 | - ), |
|
| 260 | - 'uniqid' => array( |
|
| 261 | - 'type' => 'string', |
|
| 262 | - 'default' => '', |
|
| 263 | - ), |
|
| 264 | - 'order_by' => array( |
|
| 265 | - 'type' => 'string', |
|
| 266 | - 'default' => 'ID DESC', |
|
| 267 | - ), |
|
| 268 | - 'preview' => array( |
|
| 269 | - 'type' => 'boolean', |
|
| 270 | - 'default' => false, |
|
| 271 | - ), |
|
| 272 | - 'preview_src' => array( |
|
| 273 | - 'type' => 'string', |
|
| 274 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/navigator.png', |
|
| 275 | - ), |
|
| 276 | - 'post_types' => array( |
|
| 277 | - 'type' => 'string', |
|
| 278 | - 'default' => '', |
|
| 279 | - ) |
|
| 280 | - ); |
|
| 281 | - } |
|
| 235 | + /** |
|
| 236 | + * @return array |
|
| 237 | + */ |
|
| 238 | + public function get_navigator_block_attributes() { |
|
| 239 | + return array( |
|
| 240 | + 'title' => array( |
|
| 241 | + 'type' => 'string', |
|
| 242 | + 'default' => __( 'Related articles', 'wordlift' ), |
|
| 243 | + ), |
|
| 244 | + 'limit' => array( |
|
| 245 | + 'type' => 'number', |
|
| 246 | + 'default' => 4, |
|
| 247 | + ), |
|
| 248 | + 'template_id' => array( |
|
| 249 | + 'type' => 'string', |
|
| 250 | + 'default' => '', |
|
| 251 | + ), |
|
| 252 | + 'post_id' => array( |
|
| 253 | + 'type' => 'number', |
|
| 254 | + 'default' => '', |
|
| 255 | + ), |
|
| 256 | + 'offset' => array( |
|
| 257 | + 'type' => 'number', |
|
| 258 | + 'default' => 0, |
|
| 259 | + ), |
|
| 260 | + 'uniqid' => array( |
|
| 261 | + 'type' => 'string', |
|
| 262 | + 'default' => '', |
|
| 263 | + ), |
|
| 264 | + 'order_by' => array( |
|
| 265 | + 'type' => 'string', |
|
| 266 | + 'default' => 'ID DESC', |
|
| 267 | + ), |
|
| 268 | + 'preview' => array( |
|
| 269 | + 'type' => 'boolean', |
|
| 270 | + 'default' => false, |
|
| 271 | + ), |
|
| 272 | + 'preview_src' => array( |
|
| 273 | + 'type' => 'string', |
|
| 274 | + 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/navigator.png', |
|
| 275 | + ), |
|
| 276 | + 'post_types' => array( |
|
| 277 | + 'type' => 'string', |
|
| 278 | + 'default' => '', |
|
| 279 | + ) |
|
| 280 | + ); |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | 283 | } |
@@ -32,33 +32,33 @@ discard block |
||
| 32 | 32 | /** |
| 33 | 33 | * {@inheritdoc} |
| 34 | 34 | */ |
| 35 | - public function render( $atts ) { |
|
| 36 | - return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode( $atts ) |
|
| 37 | - : $this->web_shortcode( $atts ); |
|
| 35 | + public function render($atts) { |
|
| 36 | + return Wordlift_AMP_Service::is_amp_endpoint() ? $this->amp_shortcode($atts) |
|
| 37 | + : $this->web_shortcode($atts); |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | private function register_block_type() { |
| 41 | 41 | |
| 42 | 42 | $scope = $this; |
| 43 | 43 | |
| 44 | - add_action( 'init', function () use ( $scope ) { |
|
| 45 | - if ( ! function_exists( 'register_block_type' ) ) { |
|
| 44 | + add_action('init', function() use ($scope) { |
|
| 45 | + if ( ! function_exists('register_block_type')) { |
|
| 46 | 46 | // Gutenberg is not active. |
| 47 | 47 | return; |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | - register_block_type( 'wordlift/navigator', array( |
|
| 50 | + register_block_type('wordlift/navigator', array( |
|
| 51 | 51 | 'editor_script' => 'wl-block-editor', |
| 52 | - 'render_callback' => function ( $attributes ) use ( $scope ) { |
|
| 52 | + 'render_callback' => function($attributes) use ($scope) { |
|
| 53 | 53 | $attr_code = ''; |
| 54 | - foreach ( $attributes as $key => $value ) { |
|
| 55 | - $attr_code .= $key . '="' . htmlentities( $value ) . '" '; |
|
| 54 | + foreach ($attributes as $key => $value) { |
|
| 55 | + $attr_code .= $key.'="'.htmlentities($value).'" '; |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - return '[' . $scope::SHORTCODE . ' ' . $attr_code . ']'; |
|
| 58 | + return '['.$scope::SHORTCODE.' '.$attr_code.']'; |
|
| 59 | 59 | }, |
| 60 | 60 | 'attributes' => $scope->get_navigator_block_attributes(), |
| 61 | - ) ); |
|
| 61 | + )); |
|
| 62 | 62 | } ); |
| 63 | 63 | } |
| 64 | 64 | |
@@ -72,19 +72,19 @@ discard block |
||
| 72 | 72 | * @since 3.20.0 |
| 73 | 73 | * |
| 74 | 74 | */ |
| 75 | - private function make_shortcode_atts( $atts ) { |
|
| 75 | + private function make_shortcode_atts($atts) { |
|
| 76 | 76 | |
| 77 | 77 | // Extract attributes and set default values. |
| 78 | - $shortcode_atts = shortcode_atts( array( |
|
| 79 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 78 | + $shortcode_atts = shortcode_atts(array( |
|
| 79 | + 'title' => __('Related articles', 'wordlift'), |
|
| 80 | 80 | 'limit' => 4, |
| 81 | 81 | 'offset' => 0, |
| 82 | 82 | 'template_id' => '', |
| 83 | 83 | 'post_id' => '', |
| 84 | - 'uniqid' => uniqid( 'wl-navigator-widget-' ), |
|
| 84 | + 'uniqid' => uniqid('wl-navigator-widget-'), |
|
| 85 | 85 | 'order_by' => 'ID DESC', |
| 86 | 86 | 'post_types' => '', |
| 87 | - ), $atts ); |
|
| 87 | + ), $atts); |
|
| 88 | 88 | |
| 89 | 89 | return $shortcode_atts; |
| 90 | 90 | } |
@@ -98,25 +98,25 @@ discard block |
||
| 98 | 98 | * @since 3.20.0 |
| 99 | 99 | * |
| 100 | 100 | */ |
| 101 | - private function web_shortcode( $atts ) { |
|
| 101 | + private function web_shortcode($atts) { |
|
| 102 | 102 | |
| 103 | 103 | // attributes extraction and boolean filtering |
| 104 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 104 | + $shortcode_atts = $this->make_shortcode_atts($atts); |
|
| 105 | 105 | |
| 106 | 106 | // avoid building the widget when no post_id is specified and there is a list of posts. |
| 107 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 107 | + if (empty($shortcode_atts['post_id']) && ! is_singular()) { |
|
| 108 | 108 | return; |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( $shortcode_atts['post_id'] ) ) : get_post(); |
|
| 112 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 113 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 114 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 115 | - $offset = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) ); |
|
| 116 | - $sort = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) ); |
|
| 117 | - $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-navigator-widget-' ); |
|
| 111 | + $post = ! empty($shortcode_atts['post_id']) ? get_post(intval($shortcode_atts['post_id'])) : get_post(); |
|
| 112 | + $title = esc_attr(sanitize_text_field($shortcode_atts['title'])); |
|
| 113 | + $template_id = esc_attr(sanitize_text_field($shortcode_atts['template_id'])); |
|
| 114 | + $limit = esc_attr(sanitize_text_field($shortcode_atts['limit'])); |
|
| 115 | + $offset = esc_attr(sanitize_text_field($shortcode_atts['offset'])); |
|
| 116 | + $sort = esc_attr(sanitize_sql_orderby(sanitize_text_field($shortcode_atts['order_by']))); |
|
| 117 | + $navigator_id = ! empty($shortcode_atts['uniqid']) ? esc_attr(sanitize_text_field($shortcode_atts['uniqid'])) : uniqid('wl-navigator-widget-'); |
|
| 118 | 118 | |
| 119 | - $rest_url = $post ? admin_url( 'admin-ajax.php?' . build_query( array( |
|
| 119 | + $rest_url = $post ? admin_url('admin-ajax.php?'.build_query(array( |
|
| 120 | 120 | 'action' => 'wl_navigator', |
| 121 | 121 | 'uniqid' => $navigator_id, |
| 122 | 122 | 'post_id' => $post->ID, |
@@ -124,15 +124,15 @@ discard block |
||
| 124 | 124 | 'offset' => $offset, |
| 125 | 125 | 'sort' => $sort, |
| 126 | 126 | 'post_types' => $shortcode_atts['post_types'] |
| 127 | - ) ) ) : false; |
|
| 127 | + ))) : false; |
|
| 128 | 128 | |
| 129 | 129 | // avoid building the widget when no valid $rest_url |
| 130 | - if ( ! $rest_url ) { |
|
| 130 | + if ( ! $rest_url) { |
|
| 131 | 131 | return; |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 135 | - $template_url = get_rest_url( null, '/wordlift/v1/navigator/template' ); |
|
| 134 | + wp_enqueue_script('wordlift-cloud'); |
|
| 135 | + $template_url = get_rest_url(null, '/wordlift/v1/navigator/template'); |
|
| 136 | 136 | |
| 137 | 137 | return <<<HTML |
| 138 | 138 | <!-- Navigator {$navigator_id} --> |
@@ -156,45 +156,45 @@ discard block |
||
| 156 | 156 | * @since 3.20.0 |
| 157 | 157 | * |
| 158 | 158 | */ |
| 159 | - private function amp_shortcode( $atts ) { |
|
| 159 | + private function amp_shortcode($atts) { |
|
| 160 | 160 | // attributes extraction and boolean filtering |
| 161 | - $shortcode_atts = $this->make_shortcode_atts( $atts ); |
|
| 161 | + $shortcode_atts = $this->make_shortcode_atts($atts); |
|
| 162 | 162 | |
| 163 | 163 | // avoid building the widget when no post_id is specified and there is a list of posts. |
| 164 | - if ( empty( $shortcode_atts['post_id'] ) && ! is_singular() ) { |
|
| 164 | + if (empty($shortcode_atts['post_id']) && ! is_singular()) { |
|
| 165 | 165 | return; |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | - $post = ! empty( $shortcode_atts['post_id'] ) ? get_post( intval( $shortcode_atts['post_id'] ) ) : get_post(); |
|
| 169 | - $title = esc_attr( sanitize_text_field( $shortcode_atts['title'] ) ); |
|
| 170 | - $template_id = esc_attr( sanitize_text_field( $shortcode_atts['template_id'] ) ); |
|
| 171 | - $limit = esc_attr( sanitize_text_field( $shortcode_atts['limit'] ) ); |
|
| 172 | - $offset = esc_attr( sanitize_text_field( $shortcode_atts['offset'] ) ); |
|
| 173 | - $sort = esc_attr( sanitize_sql_orderby( sanitize_text_field( $shortcode_atts['order_by'] ) ) ); |
|
| 174 | - $navigator_id = ! empty( $shortcode_atts['uniqid'] ) ? esc_attr( sanitize_text_field( $shortcode_atts['uniqid'] ) ) : uniqid( 'wl-navigator-widget-' ); |
|
| 168 | + $post = ! empty($shortcode_atts['post_id']) ? get_post(intval($shortcode_atts['post_id'])) : get_post(); |
|
| 169 | + $title = esc_attr(sanitize_text_field($shortcode_atts['title'])); |
|
| 170 | + $template_id = esc_attr(sanitize_text_field($shortcode_atts['template_id'])); |
|
| 171 | + $limit = esc_attr(sanitize_text_field($shortcode_atts['limit'])); |
|
| 172 | + $offset = esc_attr(sanitize_text_field($shortcode_atts['offset'])); |
|
| 173 | + $sort = esc_attr(sanitize_sql_orderby(sanitize_text_field($shortcode_atts['order_by']))); |
|
| 174 | + $navigator_id = ! empty($shortcode_atts['uniqid']) ? esc_attr(sanitize_text_field($shortcode_atts['uniqid'])) : uniqid('wl-navigator-widget-'); |
|
| 175 | 175 | |
| 176 | - $permalink_structure = get_option( 'permalink_structure' ); |
|
| 177 | - $delimiter = empty( $permalink_structure ) ? '&' : '?'; |
|
| 178 | - $rest_url = $post ? rest_url( WL_REST_ROUTE_DEFAULT_NAMESPACE . '/navigator' . $delimiter . build_query( array( |
|
| 176 | + $permalink_structure = get_option('permalink_structure'); |
|
| 177 | + $delimiter = empty($permalink_structure) ? '&' : '?'; |
|
| 178 | + $rest_url = $post ? rest_url(WL_REST_ROUTE_DEFAULT_NAMESPACE.'/navigator'.$delimiter.build_query(array( |
|
| 179 | 179 | 'uniqid' => $navigator_id, |
| 180 | 180 | 'post_id' => $post->ID, |
| 181 | 181 | 'limit' => $limit, |
| 182 | 182 | 'offset' => $offset, |
| 183 | 183 | 'sort' => $sort |
| 184 | - ) ) ) : false; |
|
| 184 | + ))) : false; |
|
| 185 | 185 | |
| 186 | 186 | // avoid building the widget when no valid $rest_url |
| 187 | - if ( ! $rest_url ) { |
|
| 187 | + if ( ! $rest_url) { |
|
| 188 | 188 | return; |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | // Use a protocol-relative URL as amp-list spec says that URL's protocol must be HTTPS. |
| 192 | 192 | // This is a hackish way, but this works for http and https URLs |
| 193 | - $rest_url = str_replace( array( 'http:', 'https:' ), '', $rest_url ); |
|
| 193 | + $rest_url = str_replace(array('http:', 'https:'), '', $rest_url); |
|
| 194 | 194 | |
| 195 | - if ( empty( $template_id ) ) { |
|
| 196 | - $template_id = "template-" . $navigator_id; |
|
| 197 | - wp_enqueue_style( 'wordlift-amp-custom', plugin_dir_url( dirname( __FILE__ ) ) . '/css/wordlift-amp-custom.min.css' ); |
|
| 195 | + if (empty($template_id)) { |
|
| 196 | + $template_id = "template-".$navigator_id; |
|
| 197 | + wp_enqueue_style('wordlift-amp-custom', plugin_dir_url(dirname(__FILE__)).'/css/wordlift-amp-custom.min.css'); |
|
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | return <<<HTML |
@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | return array( |
| 240 | 240 | 'title' => array( |
| 241 | 241 | 'type' => 'string', |
| 242 | - 'default' => __( 'Related articles', 'wordlift' ), |
|
| 242 | + 'default' => __('Related articles', 'wordlift'), |
|
| 243 | 243 | ), |
| 244 | 244 | 'limit' => array( |
| 245 | 245 | 'type' => 'number', |
@@ -271,7 +271,7 @@ discard block |
||
| 271 | 271 | ), |
| 272 | 272 | 'preview_src' => array( |
| 273 | 273 | 'type' => 'string', |
| 274 | - 'default' => WP_CONTENT_URL . '/plugins/wordlift/images/block-previews/navigator.png', |
|
| 274 | + 'default' => WP_CONTENT_URL.'/plugins/wordlift/images/block-previews/navigator.png', |
|
| 275 | 275 | ), |
| 276 | 276 | 'post_types' => array( |
| 277 | 277 | 'type' => 'string', |
@@ -7,53 +7,53 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Srcset_Util { |
| 9 | 9 | |
| 10 | - const NAVIGATOR_WIDGET = 'navigator'; |
|
| 10 | + const NAVIGATOR_WIDGET = 'navigator'; |
|
| 11 | 11 | |
| 12 | - const FACETED_SEARCH_WIDGET = 'faceted_search'; |
|
| 12 | + const FACETED_SEARCH_WIDGET = 'faceted_search'; |
|
| 13 | 13 | |
| 14 | - public static function get_srcset( $post_id, $widget_name ) { |
|
| 14 | + public static function get_srcset( $post_id, $widget_name ) { |
|
| 15 | 15 | |
| 16 | - $srcset = array(); |
|
| 17 | - $medium = get_the_post_thumbnail_url( $post_id, 'medium' ); |
|
| 18 | - $large = get_the_post_thumbnail_url( $post_id, 'large' ); |
|
| 16 | + $srcset = array(); |
|
| 17 | + $medium = get_the_post_thumbnail_url( $post_id, 'medium' ); |
|
| 18 | + $large = get_the_post_thumbnail_url( $post_id, 'large' ); |
|
| 19 | 19 | |
| 20 | - if ( $medium && $width = self::get_image_width( $post_id, 'medium' ) ) { |
|
| 21 | - $srcset[] = $medium . ' ' . $width . 'w'; |
|
| 22 | - } |
|
| 20 | + if ( $medium && $width = self::get_image_width( $post_id, 'medium' ) ) { |
|
| 21 | + $srcset[] = $medium . ' ' . $width . 'w'; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - if ( $large && $width = self::get_image_width( $post_id, 'large' ) ) { |
|
| 25 | - $srcset[] = $large . ' ' . $width . 'w'; |
|
| 26 | - } |
|
| 24 | + if ( $large && $width = self::get_image_width( $post_id, 'large' ) ) { |
|
| 25 | + $srcset[] = $large . ' ' . $width . 'w'; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - $srcset_string = join( ",", $srcset ); |
|
| 28 | + $srcset_string = join( ",", $srcset ); |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Filter name: wordlift_${widget_name}_thumbnail_srcset |
|
| 32 | - * Filters the srcset string supplied to widgets for each post. |
|
| 33 | - * |
|
| 34 | - * @param $srcset_string string The srcset string |
|
| 35 | - * |
|
| 36 | - * @since 3.28.0 |
|
| 37 | - */ |
|
| 38 | - $srcset_string = apply_filters( "wordlift_${widget_name}_thumbnail_srcset", $srcset_string ); |
|
| 30 | + /** |
|
| 31 | + * Filter name: wordlift_${widget_name}_thumbnail_srcset |
|
| 32 | + * Filters the srcset string supplied to widgets for each post. |
|
| 33 | + * |
|
| 34 | + * @param $srcset_string string The srcset string |
|
| 35 | + * |
|
| 36 | + * @since 3.28.0 |
|
| 37 | + */ |
|
| 38 | + $srcset_string = apply_filters( "wordlift_${widget_name}_thumbnail_srcset", $srcset_string ); |
|
| 39 | 39 | |
| 40 | - return $srcset_string; |
|
| 40 | + return $srcset_string; |
|
| 41 | 41 | |
| 42 | - } |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | 44 | |
| 45 | - private static function get_image_width( $post_id, $size ) { |
|
| 46 | - $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 47 | - if ( ! $thumbnail_id ) { |
|
| 48 | - return false; |
|
| 49 | - } |
|
| 50 | - $data = wp_get_attachment_image_src( $thumbnail_id, $size ); |
|
| 51 | - if ( ! $data ) { |
|
| 52 | - return false; |
|
| 53 | - } |
|
| 45 | + private static function get_image_width( $post_id, $size ) { |
|
| 46 | + $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 47 | + if ( ! $thumbnail_id ) { |
|
| 48 | + return false; |
|
| 49 | + } |
|
| 50 | + $data = wp_get_attachment_image_src( $thumbnail_id, $size ); |
|
| 51 | + if ( ! $data ) { |
|
| 52 | + return false; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - return array_key_exists( 2, $data ) ? $data[2] : false; |
|
| 56 | - } |
|
| 55 | + return array_key_exists( 2, $data ) ? $data[2] : false; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | 58 | |
| 59 | 59 | } |
| 60 | 60 | \ No newline at end of file |
@@ -11,21 +11,21 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | const FACETED_SEARCH_WIDGET = 'faceted_search'; |
| 13 | 13 | |
| 14 | - public static function get_srcset( $post_id, $widget_name ) { |
|
| 14 | + public static function get_srcset($post_id, $widget_name) { |
|
| 15 | 15 | |
| 16 | 16 | $srcset = array(); |
| 17 | - $medium = get_the_post_thumbnail_url( $post_id, 'medium' ); |
|
| 18 | - $large = get_the_post_thumbnail_url( $post_id, 'large' ); |
|
| 17 | + $medium = get_the_post_thumbnail_url($post_id, 'medium'); |
|
| 18 | + $large = get_the_post_thumbnail_url($post_id, 'large'); |
|
| 19 | 19 | |
| 20 | - if ( $medium && $width = self::get_image_width( $post_id, 'medium' ) ) { |
|
| 21 | - $srcset[] = $medium . ' ' . $width . 'w'; |
|
| 20 | + if ($medium && $width = self::get_image_width($post_id, 'medium')) { |
|
| 21 | + $srcset[] = $medium.' '.$width.'w'; |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | - if ( $large && $width = self::get_image_width( $post_id, 'large' ) ) { |
|
| 25 | - $srcset[] = $large . ' ' . $width . 'w'; |
|
| 24 | + if ($large && $width = self::get_image_width($post_id, 'large')) { |
|
| 25 | + $srcset[] = $large.' '.$width.'w'; |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | - $srcset_string = join( ",", $srcset ); |
|
| 28 | + $srcset_string = join(",", $srcset); |
|
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * Filter name: wordlift_${widget_name}_thumbnail_srcset |
@@ -35,24 +35,24 @@ discard block |
||
| 35 | 35 | * |
| 36 | 36 | * @since 3.28.0 |
| 37 | 37 | */ |
| 38 | - $srcset_string = apply_filters( "wordlift_${widget_name}_thumbnail_srcset", $srcset_string ); |
|
| 38 | + $srcset_string = apply_filters("wordlift_${widget_name}_thumbnail_srcset", $srcset_string); |
|
| 39 | 39 | |
| 40 | 40 | return $srcset_string; |
| 41 | 41 | |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | |
| 45 | - private static function get_image_width( $post_id, $size ) { |
|
| 46 | - $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 47 | - if ( ! $thumbnail_id ) { |
|
| 45 | + private static function get_image_width($post_id, $size) { |
|
| 46 | + $thumbnail_id = get_post_thumbnail_id($post_id); |
|
| 47 | + if ( ! $thumbnail_id) { |
|
| 48 | 48 | return false; |
| 49 | 49 | } |
| 50 | - $data = wp_get_attachment_image_src( $thumbnail_id, $size ); |
|
| 51 | - if ( ! $data ) { |
|
| 50 | + $data = wp_get_attachment_image_src($thumbnail_id, $size); |
|
| 51 | + if ( ! $data) { |
|
| 52 | 52 | return false; |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | - return array_key_exists( 2, $data ) ? $data[2] : false; |
|
| 55 | + return array_key_exists(2, $data) ? $data[2] : false; |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | |