@@ -11,143 +11,143 @@ |
||
11 | 11 | // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledClassName |
12 | 12 | abstract class Abstract_Wordpress_Content_Service implements Content_Service { |
13 | 13 | |
14 | - protected function __construct() { |
|
14 | + protected function __construct() { |
|
15 | 15 | |
16 | - } |
|
16 | + } |
|
17 | 17 | |
18 | - protected function get_dataset_uri() { |
|
19 | - return trailingslashit( Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ); |
|
20 | - } |
|
18 | + protected function get_dataset_uri() { |
|
19 | + return trailingslashit( Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ); |
|
20 | + } |
|
21 | 21 | |
22 | - protected function is_absolute( $uri ) { |
|
23 | - return 1 === preg_match( '@^https?://@', $uri ); |
|
24 | - } |
|
22 | + protected function is_absolute( $uri ) { |
|
23 | + return 1 === preg_match( '@^https?://@', $uri ); |
|
24 | + } |
|
25 | 25 | |
26 | - protected function is_internal( $uri ) { |
|
27 | - $dataset_uri = $this->get_dataset_uri(); |
|
26 | + protected function is_internal( $uri ) { |
|
27 | + $dataset_uri = $this->get_dataset_uri(); |
|
28 | 28 | |
29 | - return ! empty( $dataset_uri ) && 0 === strpos( $uri, $dataset_uri ); |
|
30 | - } |
|
29 | + return ! empty( $dataset_uri ) && 0 === strpos( $uri, $dataset_uri ); |
|
30 | + } |
|
31 | 31 | |
32 | - protected function make_absolute( $uri ) { |
|
33 | - Assertions::not_empty( $this->get_dataset_uri(), '`dataset_uri` cannot be empty.' ); |
|
32 | + protected function make_absolute( $uri ) { |
|
33 | + Assertions::not_empty( $this->get_dataset_uri(), '`dataset_uri` cannot be empty.' ); |
|
34 | 34 | |
35 | - if ( 1 !== preg_match( '@^https?://@', $uri ) ) { |
|
36 | - return $this->get_dataset_uri() . $uri; |
|
37 | - } |
|
35 | + if ( 1 !== preg_match( '@^https?://@', $uri ) ) { |
|
36 | + return $this->get_dataset_uri() . $uri; |
|
37 | + } |
|
38 | 38 | |
39 | - return $uri; |
|
40 | - } |
|
39 | + return $uri; |
|
40 | + } |
|
41 | 41 | |
42 | - protected function make_relative( $uri ) { |
|
43 | - $dataset_uri = $this->get_dataset_uri(); |
|
44 | - if ( 0 === strpos( $uri, $dataset_uri ) ) { |
|
45 | - return substr( $uri, strlen( $dataset_uri ) ); |
|
46 | - } |
|
42 | + protected function make_relative( $uri ) { |
|
43 | + $dataset_uri = $this->get_dataset_uri(); |
|
44 | + if ( 0 === strpos( $uri, $dataset_uri ) ) { |
|
45 | + return substr( $uri, strlen( $dataset_uri ) ); |
|
46 | + } |
|
47 | 47 | |
48 | - return $uri; |
|
49 | - } |
|
48 | + return $uri; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @param Wordpress_Content_Id $content_id |
|
53 | - * |
|
54 | - * @return string|null |
|
55 | - */ |
|
56 | - public function get_about_jsonld( $content_id ) { |
|
57 | - global $wpdb; |
|
51 | + /** |
|
52 | + * @param Wordpress_Content_Id $content_id |
|
53 | + * |
|
54 | + * @return string|null |
|
55 | + */ |
|
56 | + public function get_about_jsonld( $content_id ) { |
|
57 | + global $wpdb; |
|
58 | 58 | |
59 | - return $wpdb->get_var( |
|
60 | - $wpdb->prepare( |
|
61 | - " |
|
59 | + return $wpdb->get_var( |
|
60 | + $wpdb->prepare( |
|
61 | + " |
|
62 | 62 | SELECT about_jsonld FROM {$wpdb->prefix}wl_entities |
63 | 63 | WHERE content_id = %d AND content_type = %d |
64 | 64 | ", |
65 | - $content_id->get_id(), |
|
66 | - $content_id->get_type() |
|
67 | - ) |
|
68 | - ); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @param Wordpress_Content_Id $content_id |
|
73 | - * @param string $value |
|
74 | - * |
|
75 | - * @throws Exception If the 'match_name' column does not exist in the database table. |
|
76 | - */ |
|
77 | - public function set_about_jsonld( $content_id, $value ) { |
|
78 | - global $wpdb; |
|
79 | - |
|
80 | - // Cleanup value. |
|
81 | - $value = ( is_string( $value ) && strlen( $value ) > 2 ) ? $value : null; |
|
82 | - $match_name = "NULL"; |
|
83 | - |
|
84 | - if ( $value ) { |
|
85 | - // Check if the 'match_name' column exists in the database table |
|
86 | - $columns = $wpdb->get_col_info( 'name', 0 ); |
|
87 | - if ( in_array( 'match_name', $columns ) ) { |
|
88 | - $match_name = $this->get_match_name( $value ); |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - // This `hack` is necessary to ensure the entity exists in the entities table, but we |
|
93 | - // should revise how this works really. |
|
94 | - // |
|
95 | - // This is currently needed because rel_uri is required in the table. |
|
96 | - switch ( $content_id->get_type() ) { |
|
97 | - case Object_Type_Enum::POST: |
|
98 | - Wordpress_Dataset_Content_Service_Hooks::insert_post( $content_id->get_id() ); |
|
99 | - break; |
|
100 | - case Object_Type_Enum::TERM: |
|
101 | - Wordpress_Dataset_Content_Service_Hooks::created_term( $content_id->get_id() ); |
|
102 | - break; |
|
103 | - case Object_Type_Enum::USER: |
|
104 | - Wordpress_Dataset_Content_Service_Hooks::user_register( $content_id->get_id() ); |
|
105 | - break; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * As of May 16 2023, $wpdb:prepare doesnt support null |
|
110 | - * values in about_jsonld, this results in NULL values being populated |
|
111 | - * as `null` if we directly pass it to the prepare function(). So its necessary |
|
112 | - * to make the query conditional based on the $value |
|
113 | - */ |
|
114 | - if ( null === $value ) { |
|
115 | - return $wpdb->query( |
|
116 | - $wpdb->prepare( |
|
117 | - "UPDATE {$wpdb->prefix}wl_entities |
|
65 | + $content_id->get_id(), |
|
66 | + $content_id->get_type() |
|
67 | + ) |
|
68 | + ); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @param Wordpress_Content_Id $content_id |
|
73 | + * @param string $value |
|
74 | + * |
|
75 | + * @throws Exception If the 'match_name' column does not exist in the database table. |
|
76 | + */ |
|
77 | + public function set_about_jsonld( $content_id, $value ) { |
|
78 | + global $wpdb; |
|
79 | + |
|
80 | + // Cleanup value. |
|
81 | + $value = ( is_string( $value ) && strlen( $value ) > 2 ) ? $value : null; |
|
82 | + $match_name = "NULL"; |
|
83 | + |
|
84 | + if ( $value ) { |
|
85 | + // Check if the 'match_name' column exists in the database table |
|
86 | + $columns = $wpdb->get_col_info( 'name', 0 ); |
|
87 | + if ( in_array( 'match_name', $columns ) ) { |
|
88 | + $match_name = $this->get_match_name( $value ); |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + // This `hack` is necessary to ensure the entity exists in the entities table, but we |
|
93 | + // should revise how this works really. |
|
94 | + // |
|
95 | + // This is currently needed because rel_uri is required in the table. |
|
96 | + switch ( $content_id->get_type() ) { |
|
97 | + case Object_Type_Enum::POST: |
|
98 | + Wordpress_Dataset_Content_Service_Hooks::insert_post( $content_id->get_id() ); |
|
99 | + break; |
|
100 | + case Object_Type_Enum::TERM: |
|
101 | + Wordpress_Dataset_Content_Service_Hooks::created_term( $content_id->get_id() ); |
|
102 | + break; |
|
103 | + case Object_Type_Enum::USER: |
|
104 | + Wordpress_Dataset_Content_Service_Hooks::user_register( $content_id->get_id() ); |
|
105 | + break; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * As of May 16 2023, $wpdb:prepare doesnt support null |
|
110 | + * values in about_jsonld, this results in NULL values being populated |
|
111 | + * as `null` if we directly pass it to the prepare function(). So its necessary |
|
112 | + * to make the query conditional based on the $value |
|
113 | + */ |
|
114 | + if ( null === $value ) { |
|
115 | + return $wpdb->query( |
|
116 | + $wpdb->prepare( |
|
117 | + "UPDATE {$wpdb->prefix}wl_entities |
|
118 | 118 | SET about_jsonld = NULL, match_name = %s |
119 | 119 | WHERE content_id = %d AND content_type = %d", |
120 | - $match_name, |
|
121 | - $content_id->get_id(), |
|
122 | - $content_id->get_type() |
|
123 | - ) |
|
124 | - ); |
|
125 | - } |
|
126 | - |
|
127 | - return $wpdb->query( |
|
128 | - $wpdb->prepare( |
|
129 | - "UPDATE {$wpdb->prefix}wl_entities |
|
120 | + $match_name, |
|
121 | + $content_id->get_id(), |
|
122 | + $content_id->get_type() |
|
123 | + ) |
|
124 | + ); |
|
125 | + } |
|
126 | + |
|
127 | + return $wpdb->query( |
|
128 | + $wpdb->prepare( |
|
129 | + "UPDATE {$wpdb->prefix}wl_entities |
|
130 | 130 | SET about_jsonld = %s, match_name = %s |
131 | 131 | WHERE content_id = %d AND content_type = %d", |
132 | - $value, |
|
133 | - $match_name, |
|
134 | - $content_id->get_id(), |
|
135 | - $content_id->get_type() |
|
136 | - ) |
|
137 | - ); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * @param $jsonld |
|
142 | - * |
|
143 | - * @return mixed|null |
|
144 | - */ |
|
145 | - public function get_match_name( $jsonld ) { |
|
146 | - $data = json_decode( $jsonld, true ); |
|
147 | - if ( ! $data || ! array_key_exists( 'name', $data ) ) { |
|
148 | - return "NULL"; |
|
149 | - } |
|
150 | - |
|
151 | - return $data['name']; |
|
152 | - } |
|
132 | + $value, |
|
133 | + $match_name, |
|
134 | + $content_id->get_id(), |
|
135 | + $content_id->get_type() |
|
136 | + ) |
|
137 | + ); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * @param $jsonld |
|
142 | + * |
|
143 | + * @return mixed|null |
|
144 | + */ |
|
145 | + public function get_match_name( $jsonld ) { |
|
146 | + $data = json_decode( $jsonld, true ); |
|
147 | + if ( ! $data || ! array_key_exists( 'name', $data ) ) { |
|
148 | + return "NULL"; |
|
149 | + } |
|
150 | + |
|
151 | + return $data['name']; |
|
152 | + } |
|
153 | 153 | } |
@@ -7,85 +7,85 @@ |
||
7 | 7 | */ |
8 | 8 | class Wordlift_Install_3_49_1 extends Wordlift_Install { |
9 | 9 | |
10 | - /** |
|
11 | - * {@inheritdoc} |
|
12 | - */ |
|
13 | - protected static $version = '3.49.1'; |
|
10 | + /** |
|
11 | + * {@inheritdoc} |
|
12 | + */ |
|
13 | + protected static $version = '3.49.1'; |
|
14 | 14 | |
15 | - /** |
|
16 | - * Is column exists |
|
17 | - * |
|
18 | - * @param $column_name |
|
19 | - * |
|
20 | - * @return mixed |
|
21 | - */ |
|
22 | - public static function is_column_exists( $column_name ) { |
|
23 | - global $wpdb; |
|
15 | + /** |
|
16 | + * Is column exists |
|
17 | + * |
|
18 | + * @param $column_name |
|
19 | + * |
|
20 | + * @return mixed |
|
21 | + */ |
|
22 | + public static function is_column_exists( $column_name ) { |
|
23 | + global $wpdb; |
|
24 | 24 | |
25 | - return $wpdb->get_results( |
|
26 | - $wpdb->prepare( |
|
27 | - "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name ='{$wpdb->prefix}wl_relation_instances' AND column_name = %s", |
|
28 | - $column_name |
|
29 | - ) |
|
30 | - ); |
|
31 | - } |
|
25 | + return $wpdb->get_results( |
|
26 | + $wpdb->prepare( |
|
27 | + "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name ='{$wpdb->prefix}wl_relation_instances' AND column_name = %s", |
|
28 | + $column_name |
|
29 | + ) |
|
30 | + ); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Install |
|
35 | - * |
|
36 | - * @return void |
|
37 | - */ |
|
38 | - public function install() { |
|
39 | - global $wpdb; |
|
33 | + /** |
|
34 | + * Install |
|
35 | + * |
|
36 | + * @return void |
|
37 | + */ |
|
38 | + public function install() { |
|
39 | + global $wpdb; |
|
40 | 40 | |
41 | - // Check if 'match_name' column exists |
|
42 | - if ( self::is_column_exists( 'match_name' ) ) { |
|
43 | - return; |
|
44 | - } |
|
41 | + // Check if 'match_name' column exists |
|
42 | + if ( self::is_column_exists( 'match_name' ) ) { |
|
43 | + return; |
|
44 | + } |
|
45 | 45 | |
46 | - // Add new 'match_name' column |
|
47 | - $wpdb->query( |
|
48 | - "ALTER TABLE {$wpdb->prefix}wl_relation_instances |
|
46 | + // Add new 'match_name' column |
|
47 | + $wpdb->query( |
|
48 | + "ALTER TABLE {$wpdb->prefix}wl_relation_instances |
|
49 | 49 | ADD match_name VARCHAR(255) AFTER about_jsonld;" |
50 | - ); |
|
50 | + ); |
|
51 | 51 | |
52 | - // Get all rows with 'about_jsonld' |
|
53 | - $results = $wpdb->get_results( |
|
54 | - "SELECT id, about_jsonld FROM {$wpdb->prefix}wl_relation_instances WHERE about_jsonld IS NOT NULL", |
|
55 | - ARRAY_A |
|
56 | - ); |
|
52 | + // Get all rows with 'about_jsonld' |
|
53 | + $results = $wpdb->get_results( |
|
54 | + "SELECT id, about_jsonld FROM {$wpdb->prefix}wl_relation_instances WHERE about_jsonld IS NOT NULL", |
|
55 | + ARRAY_A |
|
56 | + ); |
|
57 | 57 | |
58 | - // Update 'match_name' for each row |
|
59 | - foreach ( $results as $row ) { |
|
60 | - $match_name = $this->get_match_name( $row['about_jsonld'] ); |
|
58 | + // Update 'match_name' for each row |
|
59 | + foreach ( $results as $row ) { |
|
60 | + $match_name = $this->get_match_name( $row['about_jsonld'] ); |
|
61 | 61 | |
62 | - if ( is_null( $match_name ) ) { |
|
63 | - continue; |
|
64 | - } |
|
62 | + if ( is_null( $match_name ) ) { |
|
63 | + continue; |
|
64 | + } |
|
65 | 65 | |
66 | - $wpdb->update( |
|
67 | - "{$wpdb->prefix}wl_relation_instances", |
|
68 | - array( 'match_name' => $match_name ), |
|
69 | - array( 'id' => $row['id'] ) |
|
70 | - ); |
|
71 | - } |
|
66 | + $wpdb->update( |
|
67 | + "{$wpdb->prefix}wl_relation_instances", |
|
68 | + array( 'match_name' => $match_name ), |
|
69 | + array( 'id' => $row['id'] ) |
|
70 | + ); |
|
71 | + } |
|
72 | 72 | |
73 | - Ttl_Cache::flush_all(); |
|
74 | - } |
|
73 | + Ttl_Cache::flush_all(); |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * Get match name |
|
78 | - * |
|
79 | - * @param $jsonld |
|
80 | - * |
|
81 | - * @return mixed|null |
|
82 | - */ |
|
83 | - public function get_match_name( $jsonld ) { |
|
84 | - $data = json_decode( $jsonld, true ); |
|
85 | - if ( ! $data || ! array_key_exists( 'name', $data ) ) { |
|
86 | - return null; |
|
87 | - } |
|
76 | + /** |
|
77 | + * Get match name |
|
78 | + * |
|
79 | + * @param $jsonld |
|
80 | + * |
|
81 | + * @return mixed|null |
|
82 | + */ |
|
83 | + public function get_match_name( $jsonld ) { |
|
84 | + $data = json_decode( $jsonld, true ); |
|
85 | + if ( ! $data || ! array_key_exists( 'name', $data ) ) { |
|
86 | + return null; |
|
87 | + } |
|
88 | 88 | |
89 | - return $data['name']; |
|
90 | - } |
|
89 | + return $data['name']; |
|
90 | + } |
|
91 | 91 | } |
@@ -7,52 +7,52 @@ discard block |
||
7 | 7 | use WP_REST_Request; |
8 | 8 | |
9 | 9 | class Term_Query { |
10 | - /** |
|
11 | - * @var WP_REST_Request |
|
12 | - */ |
|
13 | - private $request; |
|
14 | - /** |
|
15 | - * @var mixed |
|
16 | - */ |
|
17 | - private $position; |
|
18 | - /** |
|
19 | - * @var mixed |
|
20 | - */ |
|
21 | - private $element; |
|
22 | - /** |
|
23 | - * @var mixed |
|
24 | - */ |
|
25 | - private $direction; |
|
26 | - |
|
27 | - private $sort; |
|
28 | - |
|
29 | - private $sortby; |
|
30 | - /** |
|
31 | - * @var mixed |
|
32 | - */ |
|
33 | - private $limit; |
|
34 | - |
|
35 | - /** @var Cursor_Sort $cursor_sort */ |
|
36 | - private $cursor_sort; |
|
37 | - |
|
38 | - /** |
|
39 | - * @param WP_REST_Request $request |
|
40 | - * @param Cursor $cursor |
|
41 | - */ |
|
42 | - public function __construct( $request, $cursor, $cursor_sort, $limit ) { |
|
43 | - global $wpdb; |
|
44 | - |
|
45 | - $this->request = $request; |
|
46 | - $this->position = $cursor->get_position(); |
|
47 | - $this->element = $cursor->get_element(); |
|
48 | - $this->direction = $cursor->get_direction(); |
|
49 | - $this->limit = $limit; |
|
50 | - $this->cursor_sort = $cursor_sort; |
|
51 | - |
|
52 | - $this->set_sort(); |
|
53 | - |
|
54 | - // the `term_name` is required for sort. |
|
55 | - $this->sql = " |
|
10 | + /** |
|
11 | + * @var WP_REST_Request |
|
12 | + */ |
|
13 | + private $request; |
|
14 | + /** |
|
15 | + * @var mixed |
|
16 | + */ |
|
17 | + private $position; |
|
18 | + /** |
|
19 | + * @var mixed |
|
20 | + */ |
|
21 | + private $element; |
|
22 | + /** |
|
23 | + * @var mixed |
|
24 | + */ |
|
25 | + private $direction; |
|
26 | + |
|
27 | + private $sort; |
|
28 | + |
|
29 | + private $sortby; |
|
30 | + /** |
|
31 | + * @var mixed |
|
32 | + */ |
|
33 | + private $limit; |
|
34 | + |
|
35 | + /** @var Cursor_Sort $cursor_sort */ |
|
36 | + private $cursor_sort; |
|
37 | + |
|
38 | + /** |
|
39 | + * @param WP_REST_Request $request |
|
40 | + * @param Cursor $cursor |
|
41 | + */ |
|
42 | + public function __construct( $request, $cursor, $cursor_sort, $limit ) { |
|
43 | + global $wpdb; |
|
44 | + |
|
45 | + $this->request = $request; |
|
46 | + $this->position = $cursor->get_position(); |
|
47 | + $this->element = $cursor->get_element(); |
|
48 | + $this->direction = $cursor->get_direction(); |
|
49 | + $this->limit = $limit; |
|
50 | + $this->cursor_sort = $cursor_sort; |
|
51 | + |
|
52 | + $this->set_sort(); |
|
53 | + |
|
54 | + // the `term_name` is required for sort. |
|
55 | + $this->sql = " |
|
56 | 56 | SELECT t.term_id as id, |
57 | 57 | e.about_jsonld as match_jsonld, |
58 | 58 | t.name, |
@@ -66,157 +66,157 @@ discard block |
||
66 | 66 | WHERE 1=1 |
67 | 67 | "; |
68 | 68 | |
69 | - $this->cursor(); |
|
70 | - $this->has_match(); |
|
71 | - $this->term_contains(); |
|
72 | - $this->taxonomies(); |
|
73 | - $this->sort(); |
|
74 | - $this->limit(); |
|
75 | - |
|
76 | - } |
|
77 | - |
|
78 | - public function get_results() { |
|
79 | - global $wpdb; |
|
80 | - |
|
81 | - // The `sql` is prepared in each delegated function in this class. |
|
82 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
83 | - $items = $wpdb->get_results( $this->sql ); |
|
84 | - |
|
85 | - // Mapping elements to add match_name |
|
86 | - $items = array_map( array( $this, 'map_item' ), $items ); |
|
87 | - |
|
88 | - $sort = ( $this->sort === 'ASC' ? SORT_ASC : SORT_DESC ); |
|
89 | - |
|
90 | - // Sorting functions for specific fields |
|
91 | - $sort_functions = array( |
|
92 | - 'match_name' => function ( $a, $b ) use ( $sort ) { |
|
93 | - return $sort === SORT_ASC ? strcmp( $a->match_name, $b->match_name ) : strcmp( $b->match_name, $a->match_name ); |
|
94 | - }, |
|
95 | - // Add more specific sorting functions for other fields here |
|
96 | - ); |
|
97 | - |
|
98 | - // Check if a specific sorting function exists for the sortby field, otherwise use the default sorting |
|
99 | - if ( array_key_exists( $this->sortby, $sort_functions ) ) { |
|
100 | - usort( $items, $sort_functions[ $this->sortby ] ); |
|
101 | - } else { |
|
102 | - // Use the original sorting method for other keys |
|
103 | - array_multisort( array_column( $items, $this->cursor_sort->get_sort_property() ), $sort, $items ); |
|
104 | - } |
|
105 | - |
|
106 | - return $items; |
|
107 | - } |
|
108 | - |
|
109 | - public function map_item( $item ) { |
|
110 | - $item->match_name = $this->get_match_name( $item->match_jsonld ); |
|
111 | - |
|
112 | - return $item; |
|
113 | - } |
|
114 | - |
|
115 | - private function get_match_name( $jsonld ) { |
|
116 | - $data = json_decode( $jsonld, true ); |
|
117 | - if ( ! $data || ! array_key_exists( 'name', $data ) ) { |
|
118 | - return null; |
|
119 | - } |
|
120 | - |
|
121 | - return $data['name']; |
|
122 | - } |
|
123 | - |
|
124 | - private function post_types() { |
|
125 | - $post_types = $this->request->has_param( 'post_types' ) |
|
126 | - ? (array) $this->request->get_param( 'post_types' ) |
|
127 | - : array( 'post', 'page' ); |
|
128 | - $value = array_map( 'esc_sql', $post_types ); |
|
129 | - $this->sql .= " AND p.post_type IN ( '" . implode( "', '", $value ) . "' )"; |
|
130 | - } |
|
131 | - |
|
132 | - private function limit() { |
|
133 | - $value = is_numeric( $this->limit ) ? $this->limit : 10; |
|
134 | - $this->sql .= ' LIMIT ' . esc_sql( $value ); |
|
135 | - } |
|
136 | - |
|
137 | - private function has_match() { |
|
138 | - if ( ! $this->request->has_param( 'has_match' ) ) { |
|
139 | - return; |
|
140 | - } |
|
141 | - |
|
142 | - $value = (bool) $this->request->get_param( 'has_match' ); |
|
143 | - |
|
144 | - if ( $value ) { |
|
145 | - $this->sql .= ' AND e.about_jsonld IS NOT NULL'; |
|
146 | - } else { |
|
147 | - $this->sql .= ' AND e.about_jsonld IS NULL'; |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - private function sort() { |
|
152 | - switch ( $this->direction . '$' . $this->sort ) { |
|
153 | - case 'ASCENDING$ASC': |
|
154 | - case 'DESCENDING$DESC': |
|
155 | - $sort = 'ASC'; |
|
156 | - break; |
|
157 | - case 'ASCENDING$DESC': |
|
158 | - case 'DESCENDING$ASC': |
|
159 | - $sort = 'DESC'; |
|
160 | - break; |
|
161 | - } |
|
162 | - |
|
163 | - $this->sql .= ' ORDER BY t.' . $this->sortby . ' ' . $sort; |
|
164 | - } |
|
165 | - |
|
166 | - private function cursor() { |
|
167 | - if ( ! isset( $this->position ) ) { |
|
168 | - return; |
|
169 | - } |
|
170 | - |
|
171 | - switch ( $this->direction . '$' . $this->sort ) { |
|
172 | - case 'ASCENDING$ASC': |
|
173 | - case 'DESCENDING$DESC': |
|
174 | - $condition = '>'; |
|
175 | - break; |
|
176 | - case 'ASCENDING$DESC': |
|
177 | - case 'DESCENDING$ASC': |
|
178 | - $condition = '<'; |
|
179 | - break; |
|
180 | - } |
|
181 | - |
|
182 | - $condition .= ( $this->element === 'INCLUDED' ? '=' : '' ); |
|
183 | - global $wpdb; |
|
184 | - // We control the vars in this method. |
|
185 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
186 | - $this->sql .= $wpdb->prepare( ' AND t.' . esc_sql( $this->sortby ) . ' ' . $condition . ' %s', $this->position ); |
|
187 | - } |
|
188 | - |
|
189 | - private function set_sort() { |
|
190 | - $sortby_to_col = array( |
|
191 | - // sort param col |
|
192 | - 'term_name' => 'name', |
|
193 | - 'match_name' => 'match_name', |
|
194 | - ); |
|
195 | - |
|
196 | - $value = $this->request->has_param( 'sort' ) |
|
197 | - ? $this->request->get_param( 'sort' ) |
|
198 | - : '+term_name'; |
|
199 | - |
|
200 | - $sortby = substr( $value, 1 ); |
|
201 | - $this->sortby = isset( $sortby_to_col[ $sortby ] ) ? $sortby_to_col[ $sortby ] : $sortby; |
|
202 | - $this->sort = substr( $value, 0, 1 ) === '+' ? 'ASC' : 'DESC'; |
|
203 | - } |
|
204 | - |
|
205 | - private function term_contains() { |
|
206 | - if ( ! $this->request->has_param( 'term_contains' ) ) { |
|
207 | - return; |
|
208 | - } |
|
209 | - |
|
210 | - global $wpdb; |
|
211 | - $value = $this->request->get_param( 'term_contains' ); |
|
212 | - $this->sql .= $wpdb->prepare( ' and t.name LIKE %s', '%' . esc_sql( $value ) . '%' ); |
|
213 | - } |
|
214 | - |
|
215 | - private function taxonomies() { |
|
216 | - $taxonomies = $this->request->has_param( 'taxonomies' ) |
|
217 | - ? (array) $this->request->get_param( 'taxonomies' ) |
|
218 | - : array( 'post_tag', 'category' ); |
|
219 | - $value = array_map( 'esc_sql', $taxonomies ); |
|
220 | - $this->sql .= " AND tt.taxonomy IN ( '" . implode( "', '", $value ) . "' )"; |
|
221 | - } |
|
69 | + $this->cursor(); |
|
70 | + $this->has_match(); |
|
71 | + $this->term_contains(); |
|
72 | + $this->taxonomies(); |
|
73 | + $this->sort(); |
|
74 | + $this->limit(); |
|
75 | + |
|
76 | + } |
|
77 | + |
|
78 | + public function get_results() { |
|
79 | + global $wpdb; |
|
80 | + |
|
81 | + // The `sql` is prepared in each delegated function in this class. |
|
82 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
83 | + $items = $wpdb->get_results( $this->sql ); |
|
84 | + |
|
85 | + // Mapping elements to add match_name |
|
86 | + $items = array_map( array( $this, 'map_item' ), $items ); |
|
87 | + |
|
88 | + $sort = ( $this->sort === 'ASC' ? SORT_ASC : SORT_DESC ); |
|
89 | + |
|
90 | + // Sorting functions for specific fields |
|
91 | + $sort_functions = array( |
|
92 | + 'match_name' => function ( $a, $b ) use ( $sort ) { |
|
93 | + return $sort === SORT_ASC ? strcmp( $a->match_name, $b->match_name ) : strcmp( $b->match_name, $a->match_name ); |
|
94 | + }, |
|
95 | + // Add more specific sorting functions for other fields here |
|
96 | + ); |
|
97 | + |
|
98 | + // Check if a specific sorting function exists for the sortby field, otherwise use the default sorting |
|
99 | + if ( array_key_exists( $this->sortby, $sort_functions ) ) { |
|
100 | + usort( $items, $sort_functions[ $this->sortby ] ); |
|
101 | + } else { |
|
102 | + // Use the original sorting method for other keys |
|
103 | + array_multisort( array_column( $items, $this->cursor_sort->get_sort_property() ), $sort, $items ); |
|
104 | + } |
|
105 | + |
|
106 | + return $items; |
|
107 | + } |
|
108 | + |
|
109 | + public function map_item( $item ) { |
|
110 | + $item->match_name = $this->get_match_name( $item->match_jsonld ); |
|
111 | + |
|
112 | + return $item; |
|
113 | + } |
|
114 | + |
|
115 | + private function get_match_name( $jsonld ) { |
|
116 | + $data = json_decode( $jsonld, true ); |
|
117 | + if ( ! $data || ! array_key_exists( 'name', $data ) ) { |
|
118 | + return null; |
|
119 | + } |
|
120 | + |
|
121 | + return $data['name']; |
|
122 | + } |
|
123 | + |
|
124 | + private function post_types() { |
|
125 | + $post_types = $this->request->has_param( 'post_types' ) |
|
126 | + ? (array) $this->request->get_param( 'post_types' ) |
|
127 | + : array( 'post', 'page' ); |
|
128 | + $value = array_map( 'esc_sql', $post_types ); |
|
129 | + $this->sql .= " AND p.post_type IN ( '" . implode( "', '", $value ) . "' )"; |
|
130 | + } |
|
131 | + |
|
132 | + private function limit() { |
|
133 | + $value = is_numeric( $this->limit ) ? $this->limit : 10; |
|
134 | + $this->sql .= ' LIMIT ' . esc_sql( $value ); |
|
135 | + } |
|
136 | + |
|
137 | + private function has_match() { |
|
138 | + if ( ! $this->request->has_param( 'has_match' ) ) { |
|
139 | + return; |
|
140 | + } |
|
141 | + |
|
142 | + $value = (bool) $this->request->get_param( 'has_match' ); |
|
143 | + |
|
144 | + if ( $value ) { |
|
145 | + $this->sql .= ' AND e.about_jsonld IS NOT NULL'; |
|
146 | + } else { |
|
147 | + $this->sql .= ' AND e.about_jsonld IS NULL'; |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + private function sort() { |
|
152 | + switch ( $this->direction . '$' . $this->sort ) { |
|
153 | + case 'ASCENDING$ASC': |
|
154 | + case 'DESCENDING$DESC': |
|
155 | + $sort = 'ASC'; |
|
156 | + break; |
|
157 | + case 'ASCENDING$DESC': |
|
158 | + case 'DESCENDING$ASC': |
|
159 | + $sort = 'DESC'; |
|
160 | + break; |
|
161 | + } |
|
162 | + |
|
163 | + $this->sql .= ' ORDER BY t.' . $this->sortby . ' ' . $sort; |
|
164 | + } |
|
165 | + |
|
166 | + private function cursor() { |
|
167 | + if ( ! isset( $this->position ) ) { |
|
168 | + return; |
|
169 | + } |
|
170 | + |
|
171 | + switch ( $this->direction . '$' . $this->sort ) { |
|
172 | + case 'ASCENDING$ASC': |
|
173 | + case 'DESCENDING$DESC': |
|
174 | + $condition = '>'; |
|
175 | + break; |
|
176 | + case 'ASCENDING$DESC': |
|
177 | + case 'DESCENDING$ASC': |
|
178 | + $condition = '<'; |
|
179 | + break; |
|
180 | + } |
|
181 | + |
|
182 | + $condition .= ( $this->element === 'INCLUDED' ? '=' : '' ); |
|
183 | + global $wpdb; |
|
184 | + // We control the vars in this method. |
|
185 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
186 | + $this->sql .= $wpdb->prepare( ' AND t.' . esc_sql( $this->sortby ) . ' ' . $condition . ' %s', $this->position ); |
|
187 | + } |
|
188 | + |
|
189 | + private function set_sort() { |
|
190 | + $sortby_to_col = array( |
|
191 | + // sort param col |
|
192 | + 'term_name' => 'name', |
|
193 | + 'match_name' => 'match_name', |
|
194 | + ); |
|
195 | + |
|
196 | + $value = $this->request->has_param( 'sort' ) |
|
197 | + ? $this->request->get_param( 'sort' ) |
|
198 | + : '+term_name'; |
|
199 | + |
|
200 | + $sortby = substr( $value, 1 ); |
|
201 | + $this->sortby = isset( $sortby_to_col[ $sortby ] ) ? $sortby_to_col[ $sortby ] : $sortby; |
|
202 | + $this->sort = substr( $value, 0, 1 ) === '+' ? 'ASC' : 'DESC'; |
|
203 | + } |
|
204 | + |
|
205 | + private function term_contains() { |
|
206 | + if ( ! $this->request->has_param( 'term_contains' ) ) { |
|
207 | + return; |
|
208 | + } |
|
209 | + |
|
210 | + global $wpdb; |
|
211 | + $value = $this->request->get_param( 'term_contains' ); |
|
212 | + $this->sql .= $wpdb->prepare( ' and t.name LIKE %s', '%' . esc_sql( $value ) . '%' ); |
|
213 | + } |
|
214 | + |
|
215 | + private function taxonomies() { |
|
216 | + $taxonomies = $this->request->has_param( 'taxonomies' ) |
|
217 | + ? (array) $this->request->get_param( 'taxonomies' ) |
|
218 | + : array( 'post_tag', 'category' ); |
|
219 | + $value = array_map( 'esc_sql', $taxonomies ); |
|
220 | + $this->sql .= " AND tt.taxonomy IN ( '" . implode( "', '", $value ) . "' )"; |
|
221 | + } |
|
222 | 222 | } |