@@ -18,406 +18,406 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Relation_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The relation table name in MySQL, set during instantiation. |
|
| 23 | - * |
|
| 24 | - * @since 3.15.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var string $relation_table The relation table name. |
|
| 27 | - */ |
|
| 28 | - private $relation_table; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * A {@link Wordlift_Log_Service} instance. |
|
| 32 | - * |
|
| 33 | - * @since 3.15.3 |
|
| 34 | - * |
|
| 35 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 36 | - */ |
|
| 37 | - private static $log; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Create a {@link Wordlift_Relation_Service} instance. |
|
| 41 | - * |
|
| 42 | - * @since 3.15.0 |
|
| 43 | - */ |
|
| 44 | - protected function __construct() { |
|
| 45 | - global $wpdb; |
|
| 46 | - |
|
| 47 | - self::$log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 48 | - |
|
| 49 | - // The relations table. |
|
| 50 | - $this->relation_table = "{$wpdb->prefix}wl_relation_instances"; |
|
| 51 | - |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * The singleton instance. |
|
| 56 | - * |
|
| 57 | - * @since 3.15.0 |
|
| 58 | - * @access private |
|
| 59 | - * @var Wordlift_Relation_Service $instance The singleton instance. |
|
| 60 | - */ |
|
| 61 | - private static $instance = null; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Get the singleton instance. |
|
| 65 | - * |
|
| 66 | - * @return Wordlift_Relation_Service The {@link Wordlift_Relation_Service} singleton instance. |
|
| 67 | - * @since 3.15.0 |
|
| 68 | - * @access public |
|
| 69 | - */ |
|
| 70 | - public static function get_instance() { |
|
| 71 | - |
|
| 72 | - if ( ! isset( self::$instance ) ) { |
|
| 73 | - self::$instance = new self(); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - return self::$instance; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Get the articles referencing the specified entity {@link WP_Post}. |
|
| 81 | - * |
|
| 82 | - * @param int|array $object_id The entity {@link WP_Post}'s id. |
|
| 83 | - * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 84 | - * '*' to return all fields, by default '*'. |
|
| 85 | - * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 86 | - * @param null|string $status The status, by default all. |
|
| 87 | - * @param array $excludes An array of ids to exclude from the results. |
|
| 88 | - * @param null|int $limit The maximum number of results, by default |
|
| 89 | - * no limit. |
|
| 90 | - * @param null|array $include The {@link WP_Post}s' ids to include. |
|
| 91 | - * |
|
| 92 | - * @param null | string $order_by |
|
| 93 | - * |
|
| 94 | - * @param array $post_types |
|
| 95 | - * |
|
| 96 | - * @return array|object|null Database query results |
|
| 97 | - * @since 3.15.0 |
|
| 98 | - */ |
|
| 99 | - public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null, $order_by = null, $post_types = array(), $offset = null ) { |
|
| 100 | - global $wpdb; |
|
| 101 | - |
|
| 102 | - // The output fields. |
|
| 103 | - $actual_fields = self::fields( $fields ); |
|
| 104 | - |
|
| 105 | - self::$log->trace( 'Getting article subjects for object ' . implode( ', ', (array) $object_id ) . '...' ); |
|
| 106 | - |
|
| 107 | - $objects = $this->article_id_to_entity_id( $object_id ); |
|
| 108 | - |
|
| 109 | - // If there are no related objects, return an empty array. |
|
| 110 | - if ( empty( $objects ) ) { |
|
| 111 | - self::$log->debug( 'No entities found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 112 | - |
|
| 113 | - return array(); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - self::$log->debug( count( $objects ) . ' entity id(s) found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 117 | - |
|
| 118 | - $sql = |
|
| 119 | - " |
|
| 21 | + /** |
|
| 22 | + * The relation table name in MySQL, set during instantiation. |
|
| 23 | + * |
|
| 24 | + * @since 3.15.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var string $relation_table The relation table name. |
|
| 27 | + */ |
|
| 28 | + private $relation_table; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * A {@link Wordlift_Log_Service} instance. |
|
| 32 | + * |
|
| 33 | + * @since 3.15.3 |
|
| 34 | + * |
|
| 35 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 36 | + */ |
|
| 37 | + private static $log; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Create a {@link Wordlift_Relation_Service} instance. |
|
| 41 | + * |
|
| 42 | + * @since 3.15.0 |
|
| 43 | + */ |
|
| 44 | + protected function __construct() { |
|
| 45 | + global $wpdb; |
|
| 46 | + |
|
| 47 | + self::$log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 48 | + |
|
| 49 | + // The relations table. |
|
| 50 | + $this->relation_table = "{$wpdb->prefix}wl_relation_instances"; |
|
| 51 | + |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * The singleton instance. |
|
| 56 | + * |
|
| 57 | + * @since 3.15.0 |
|
| 58 | + * @access private |
|
| 59 | + * @var Wordlift_Relation_Service $instance The singleton instance. |
|
| 60 | + */ |
|
| 61 | + private static $instance = null; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Get the singleton instance. |
|
| 65 | + * |
|
| 66 | + * @return Wordlift_Relation_Service The {@link Wordlift_Relation_Service} singleton instance. |
|
| 67 | + * @since 3.15.0 |
|
| 68 | + * @access public |
|
| 69 | + */ |
|
| 70 | + public static function get_instance() { |
|
| 71 | + |
|
| 72 | + if ( ! isset( self::$instance ) ) { |
|
| 73 | + self::$instance = new self(); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + return self::$instance; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Get the articles referencing the specified entity {@link WP_Post}. |
|
| 81 | + * |
|
| 82 | + * @param int|array $object_id The entity {@link WP_Post}'s id. |
|
| 83 | + * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 84 | + * '*' to return all fields, by default '*'. |
|
| 85 | + * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 86 | + * @param null|string $status The status, by default all. |
|
| 87 | + * @param array $excludes An array of ids to exclude from the results. |
|
| 88 | + * @param null|int $limit The maximum number of results, by default |
|
| 89 | + * no limit. |
|
| 90 | + * @param null|array $include The {@link WP_Post}s' ids to include. |
|
| 91 | + * |
|
| 92 | + * @param null | string $order_by |
|
| 93 | + * |
|
| 94 | + * @param array $post_types |
|
| 95 | + * |
|
| 96 | + * @return array|object|null Database query results |
|
| 97 | + * @since 3.15.0 |
|
| 98 | + */ |
|
| 99 | + public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null, $order_by = null, $post_types = array(), $offset = null ) { |
|
| 100 | + global $wpdb; |
|
| 101 | + |
|
| 102 | + // The output fields. |
|
| 103 | + $actual_fields = self::fields( $fields ); |
|
| 104 | + |
|
| 105 | + self::$log->trace( 'Getting article subjects for object ' . implode( ', ', (array) $object_id ) . '...' ); |
|
| 106 | + |
|
| 107 | + $objects = $this->article_id_to_entity_id( $object_id ); |
|
| 108 | + |
|
| 109 | + // If there are no related objects, return an empty array. |
|
| 110 | + if ( empty( $objects ) ) { |
|
| 111 | + self::$log->debug( 'No entities found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 112 | + |
|
| 113 | + return array(); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + self::$log->debug( count( $objects ) . ' entity id(s) found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 117 | + |
|
| 118 | + $sql = |
|
| 119 | + " |
|
| 120 | 120 | SELECT DISTINCT p.$actual_fields |
| 121 | 121 | FROM {$this->relation_table} r |
| 122 | 122 | INNER JOIN $wpdb->posts p |
| 123 | 123 | ON p.id = r.subject_id |
| 124 | 124 | " |
| 125 | - // Add the status clause. |
|
| 126 | - . self::and_status( $status ) |
|
| 127 | - . self::inner_join_is_article() |
|
| 128 | - . self::where_object_id( $objects ) |
|
| 129 | - // Since `object_id` can be an article ID we need to exclude it from |
|
| 130 | - // the results. |
|
| 131 | - . self::and_article_not_in( array_merge( $excludes, (array) $object_id ) ) |
|
| 132 | - . self::and_article_in( $include ) |
|
| 133 | - . self::and_post_type_in( $post_types ) |
|
| 134 | - . self::and_predicate( $predicate ) |
|
| 135 | - . self::order_by( $order_by ) |
|
| 136 | - . self::limit( $limit ) |
|
| 137 | - . self::offset( $offset ); |
|
| 138 | - |
|
| 139 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * The `post_type IN` clause. |
|
| 144 | - * |
|
| 145 | - * @param array $post_types If the post type is not provided then the valid |
|
| 146 | - * entity post types are used. |
|
| 147 | - * |
|
| 148 | - * @return string The `post_type IN` clause. |
|
| 149 | - * @since 3.15.3 |
|
| 150 | - */ |
|
| 151 | - private static function and_post_type_in( $post_types = array() ) { |
|
| 152 | - |
|
| 153 | - if ( array() === $post_types ) { |
|
| 154 | - $post_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return " AND p.post_type IN ( '" |
|
| 158 | - . implode( |
|
| 159 | - "','", |
|
| 160 | - array_map( 'esc_sql', $post_types ) |
|
| 161 | - ) |
|
| 162 | - . "' )"; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Add the limit clause if specified. |
|
| 167 | - * |
|
| 168 | - * @param null|int $limit The maximum number of results. |
|
| 169 | - * |
|
| 170 | - * @return string The limit clause (empty if no limit has been specified). |
|
| 171 | - * @since 3.15.0 |
|
| 172 | - */ |
|
| 173 | - private static function limit( $limit = null ) { |
|
| 174 | - |
|
| 175 | - if ( null === $limit ) { |
|
| 176 | - return ''; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - return "LIMIT $limit"; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Add the OFFSET clause if specified. |
|
| 184 | - * |
|
| 185 | - * @param null|int $offset The number of results to skip. |
|
| 186 | - * |
|
| 187 | - * @return string The offset clause (empty if no offset has been specified). |
|
| 188 | - * @since 3.35.11 |
|
| 189 | - */ |
|
| 190 | - private static function offset( $offset = null ) { |
|
| 191 | - |
|
| 192 | - if ( null === $offset || ! is_numeric( $offset ) ) { |
|
| 193 | - return ''; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - return " OFFSET $offset"; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * @param $order_by string | null |
|
| 201 | - * |
|
| 202 | - * @return string |
|
| 203 | - */ |
|
| 204 | - private static function order_by( $order_by ) { |
|
| 205 | - if ( ! $order_by ) { |
|
| 206 | - return ''; |
|
| 207 | - } |
|
| 208 | - $order_by = (string) $order_by; |
|
| 209 | - $order_by_clauses = array( 'DESC', 'ASC' ); |
|
| 210 | - |
|
| 211 | - if ( in_array( $order_by, $order_by_clauses, true ) ) { |
|
| 212 | - return " ORDER BY p.post_modified {$order_by} "; |
|
| 213 | - } else { |
|
| 214 | - return ' ORDER BY p.post_modified DESC '; |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Map the provided ids into entities (i.e. return the id if it's an entity |
|
| 220 | - * or get the entities if it's a post). |
|
| 221 | - * |
|
| 222 | - * @param int|array $object_id An array of posts/entities' ids. |
|
| 223 | - * |
|
| 224 | - * @return array An array of entities' ids. |
|
| 225 | - * @since 3.15.0 |
|
| 226 | - */ |
|
| 227 | - private function article_id_to_entity_id( $object_id ) { |
|
| 228 | - |
|
| 229 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 230 | - |
|
| 231 | - $relation_service = $this; |
|
| 232 | - |
|
| 233 | - return array_reduce( |
|
| 234 | - (array) $object_id, |
|
| 235 | - function ( $carry, $item ) use ( $entity_service, $relation_service ) { |
|
| 236 | - if ( $entity_service->is_entity( $item ) ) { |
|
| 237 | - return array_merge( $carry, (array) $item ); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) ); |
|
| 241 | - }, |
|
| 242 | - array() |
|
| 243 | - ); |
|
| 244 | - |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Add the WHERE clause. |
|
| 249 | - * |
|
| 250 | - * @param int|array $object_id An array of {@link WP_Post}s' ids. |
|
| 251 | - * |
|
| 252 | - * @return string The WHERE clause. |
|
| 253 | - * @since 3.15.0 |
|
| 254 | - */ |
|
| 255 | - private static function where_object_id( $object_id ) { |
|
| 256 | - |
|
| 257 | - if ( empty( $object_id ) ) { |
|
| 258 | - // self::$log->warn( sprintf( "%s `where_object_id` called with empty `object_id`.", var_export( debug_backtrace( false, 3 ), true ) ) ); |
|
| 259 | - |
|
| 260 | - return ' WHERE 1 = 1'; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )'; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Add the exclude clause. |
|
| 268 | - * |
|
| 269 | - * @param int|array $exclude An array of {@link WP_Post}s' ids to exclude. |
|
| 270 | - * |
|
| 271 | - * @return string The exclude clause. |
|
| 272 | - * @since 3.15.0 |
|
| 273 | - */ |
|
| 274 | - private static function and_article_not_in( $exclude ) { |
|
| 275 | - |
|
| 276 | - return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )'; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Add the include clause. |
|
| 281 | - * |
|
| 282 | - * @param null|int|array $include An array of {@link WP_Post}s' ids. |
|
| 283 | - * |
|
| 284 | - * @return string An empty string if $include is null otherwise the include |
|
| 285 | - * clause. |
|
| 286 | - * @since 3.15.0 |
|
| 287 | - */ |
|
| 288 | - private static function and_article_in( $include = null ) { |
|
| 289 | - |
|
| 290 | - if ( null === $include ) { |
|
| 291 | - return ''; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )'; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * Get the entities' {@link WP_Post}s' ids referencing the specified {@link WP_Post}. |
|
| 299 | - * |
|
| 300 | - * @param int $object_id The object {@link WP_Post}'s id. |
|
| 301 | - * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 302 | - * '*' to return all fields, by default '*'. |
|
| 303 | - * @param null|string $status The status, by default all. |
|
| 304 | - * |
|
| 305 | - * @return array|object|null Database query results |
|
| 306 | - * @since 3.15.0 |
|
| 307 | - */ |
|
| 308 | - public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) { |
|
| 309 | - global $wpdb; |
|
| 310 | - |
|
| 311 | - // The output fields. |
|
| 312 | - $actual_fields = self::fields( $fields ); |
|
| 313 | - |
|
| 314 | - $sql = $wpdb->prepare( |
|
| 315 | - "SELECT p.$actual_fields FROM {$wpdb->prefix}wl_relation_instances r INNER JOIN $wpdb->posts p ON p.id = r.subject_id" // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
| 316 | - // Add the status clause. |
|
| 317 | - . self::and_status( $status ) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 318 | - . self::inner_join_is_not_article() // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 319 | - . ' WHERE r.object_id = %d ' |
|
| 320 | - . self::and_post_type_in(), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 321 | - $object_id |
|
| 322 | - ); |
|
| 323 | - |
|
| 324 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * Get the entities referenced by the specified {@link WP_Post}. |
|
| 329 | - * |
|
| 330 | - * @param int $subject_id The {@link WP_Post}'s id. |
|
| 331 | - * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 332 | - * '*' to return all fields, by default '*'. |
|
| 333 | - * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 334 | - * @param null|string $status The status, by default all. |
|
| 335 | - * |
|
| 336 | - * @return array|object|null Database query results |
|
| 337 | - * |
|
| 338 | - * @deprecated since it doesn't handle the subject_type nor the object_type |
|
| 339 | - * @since 3.15.0 |
|
| 340 | - */ |
|
| 341 | - public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) { |
|
| 342 | - global $wpdb; |
|
| 343 | - |
|
| 344 | - // The output fields. |
|
| 345 | - $actual_fields = self::fields( $fields ); |
|
| 346 | - |
|
| 347 | - $sql = $wpdb->prepare( |
|
| 348 | - "SELECT p.$actual_fields FROM {$this->relation_table} r INNER JOIN $wpdb->posts p ON p.id = r.object_id" // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
| 349 | - // Add the status clause. |
|
| 350 | - . self::and_status( $status ) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 351 | - . self::inner_join_is_not_article() // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 352 | - . ' WHERE r.subject_id = %d ' |
|
| 353 | - . self::and_post_type_in() // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 354 | - . self::and_predicate( $predicate ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 355 | - $subject_id |
|
| 356 | - ); |
|
| 357 | - |
|
| 358 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Add the `post_status` clause. |
|
| 363 | - * |
|
| 364 | - * @param null|string|array $status The status values. |
|
| 365 | - * |
|
| 366 | - * @return string An empty string if $status is null, otherwise the status clause. |
|
| 367 | - * @since 3.15.0 |
|
| 368 | - */ |
|
| 369 | - private static function and_status( $status = null ) { |
|
| 370 | - |
|
| 371 | - if ( null === $status ) { |
|
| 372 | - return ''; |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')"; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * Add the `predicate` clause. |
|
| 380 | - * |
|
| 381 | - * @param null|string|array $predicate An array of predicates. |
|
| 382 | - * |
|
| 383 | - * @return string An empty string if $predicate is null otherwise the predicate |
|
| 384 | - * clause. |
|
| 385 | - * @since 3.15.0 |
|
| 386 | - */ |
|
| 387 | - private static function and_predicate( $predicate = null ) { |
|
| 388 | - |
|
| 389 | - if ( null === $predicate ) { |
|
| 390 | - return ''; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')"; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * The select fields. |
|
| 398 | - * |
|
| 399 | - * @param string $fields Either 'ids' or '*', by default '*'. |
|
| 400 | - * |
|
| 401 | - * @return string The `id` field if `ids` otherwise `*`. |
|
| 402 | - * @since 3.15.0 |
|
| 403 | - */ |
|
| 404 | - private static function fields( $fields = '*' ) { |
|
| 405 | - |
|
| 406 | - // The output fields. |
|
| 407 | - return 'ids' === $fields ? 'id' : '*'; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * The inner join clause for articles. |
|
| 412 | - * |
|
| 413 | - * @return string The articles inner join clause. |
|
| 414 | - * @since 3.15.0 |
|
| 415 | - */ |
|
| 416 | - private static function inner_join_is_article() { |
|
| 417 | - global $wpdb; |
|
| 418 | - |
|
| 419 | - return $wpdb->prepare( |
|
| 420 | - " |
|
| 125 | + // Add the status clause. |
|
| 126 | + . self::and_status( $status ) |
|
| 127 | + . self::inner_join_is_article() |
|
| 128 | + . self::where_object_id( $objects ) |
|
| 129 | + // Since `object_id` can be an article ID we need to exclude it from |
|
| 130 | + // the results. |
|
| 131 | + . self::and_article_not_in( array_merge( $excludes, (array) $object_id ) ) |
|
| 132 | + . self::and_article_in( $include ) |
|
| 133 | + . self::and_post_type_in( $post_types ) |
|
| 134 | + . self::and_predicate( $predicate ) |
|
| 135 | + . self::order_by( $order_by ) |
|
| 136 | + . self::limit( $limit ) |
|
| 137 | + . self::offset( $offset ); |
|
| 138 | + |
|
| 139 | + return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * The `post_type IN` clause. |
|
| 144 | + * |
|
| 145 | + * @param array $post_types If the post type is not provided then the valid |
|
| 146 | + * entity post types are used. |
|
| 147 | + * |
|
| 148 | + * @return string The `post_type IN` clause. |
|
| 149 | + * @since 3.15.3 |
|
| 150 | + */ |
|
| 151 | + private static function and_post_type_in( $post_types = array() ) { |
|
| 152 | + |
|
| 153 | + if ( array() === $post_types ) { |
|
| 154 | + $post_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return " AND p.post_type IN ( '" |
|
| 158 | + . implode( |
|
| 159 | + "','", |
|
| 160 | + array_map( 'esc_sql', $post_types ) |
|
| 161 | + ) |
|
| 162 | + . "' )"; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Add the limit clause if specified. |
|
| 167 | + * |
|
| 168 | + * @param null|int $limit The maximum number of results. |
|
| 169 | + * |
|
| 170 | + * @return string The limit clause (empty if no limit has been specified). |
|
| 171 | + * @since 3.15.0 |
|
| 172 | + */ |
|
| 173 | + private static function limit( $limit = null ) { |
|
| 174 | + |
|
| 175 | + if ( null === $limit ) { |
|
| 176 | + return ''; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + return "LIMIT $limit"; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Add the OFFSET clause if specified. |
|
| 184 | + * |
|
| 185 | + * @param null|int $offset The number of results to skip. |
|
| 186 | + * |
|
| 187 | + * @return string The offset clause (empty if no offset has been specified). |
|
| 188 | + * @since 3.35.11 |
|
| 189 | + */ |
|
| 190 | + private static function offset( $offset = null ) { |
|
| 191 | + |
|
| 192 | + if ( null === $offset || ! is_numeric( $offset ) ) { |
|
| 193 | + return ''; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + return " OFFSET $offset"; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * @param $order_by string | null |
|
| 201 | + * |
|
| 202 | + * @return string |
|
| 203 | + */ |
|
| 204 | + private static function order_by( $order_by ) { |
|
| 205 | + if ( ! $order_by ) { |
|
| 206 | + return ''; |
|
| 207 | + } |
|
| 208 | + $order_by = (string) $order_by; |
|
| 209 | + $order_by_clauses = array( 'DESC', 'ASC' ); |
|
| 210 | + |
|
| 211 | + if ( in_array( $order_by, $order_by_clauses, true ) ) { |
|
| 212 | + return " ORDER BY p.post_modified {$order_by} "; |
|
| 213 | + } else { |
|
| 214 | + return ' ORDER BY p.post_modified DESC '; |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Map the provided ids into entities (i.e. return the id if it's an entity |
|
| 220 | + * or get the entities if it's a post). |
|
| 221 | + * |
|
| 222 | + * @param int|array $object_id An array of posts/entities' ids. |
|
| 223 | + * |
|
| 224 | + * @return array An array of entities' ids. |
|
| 225 | + * @since 3.15.0 |
|
| 226 | + */ |
|
| 227 | + private function article_id_to_entity_id( $object_id ) { |
|
| 228 | + |
|
| 229 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 230 | + |
|
| 231 | + $relation_service = $this; |
|
| 232 | + |
|
| 233 | + return array_reduce( |
|
| 234 | + (array) $object_id, |
|
| 235 | + function ( $carry, $item ) use ( $entity_service, $relation_service ) { |
|
| 236 | + if ( $entity_service->is_entity( $item ) ) { |
|
| 237 | + return array_merge( $carry, (array) $item ); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) ); |
|
| 241 | + }, |
|
| 242 | + array() |
|
| 243 | + ); |
|
| 244 | + |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Add the WHERE clause. |
|
| 249 | + * |
|
| 250 | + * @param int|array $object_id An array of {@link WP_Post}s' ids. |
|
| 251 | + * |
|
| 252 | + * @return string The WHERE clause. |
|
| 253 | + * @since 3.15.0 |
|
| 254 | + */ |
|
| 255 | + private static function where_object_id( $object_id ) { |
|
| 256 | + |
|
| 257 | + if ( empty( $object_id ) ) { |
|
| 258 | + // self::$log->warn( sprintf( "%s `where_object_id` called with empty `object_id`.", var_export( debug_backtrace( false, 3 ), true ) ) ); |
|
| 259 | + |
|
| 260 | + return ' WHERE 1 = 1'; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )'; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Add the exclude clause. |
|
| 268 | + * |
|
| 269 | + * @param int|array $exclude An array of {@link WP_Post}s' ids to exclude. |
|
| 270 | + * |
|
| 271 | + * @return string The exclude clause. |
|
| 272 | + * @since 3.15.0 |
|
| 273 | + */ |
|
| 274 | + private static function and_article_not_in( $exclude ) { |
|
| 275 | + |
|
| 276 | + return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )'; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * Add the include clause. |
|
| 281 | + * |
|
| 282 | + * @param null|int|array $include An array of {@link WP_Post}s' ids. |
|
| 283 | + * |
|
| 284 | + * @return string An empty string if $include is null otherwise the include |
|
| 285 | + * clause. |
|
| 286 | + * @since 3.15.0 |
|
| 287 | + */ |
|
| 288 | + private static function and_article_in( $include = null ) { |
|
| 289 | + |
|
| 290 | + if ( null === $include ) { |
|
| 291 | + return ''; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )'; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * Get the entities' {@link WP_Post}s' ids referencing the specified {@link WP_Post}. |
|
| 299 | + * |
|
| 300 | + * @param int $object_id The object {@link WP_Post}'s id. |
|
| 301 | + * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 302 | + * '*' to return all fields, by default '*'. |
|
| 303 | + * @param null|string $status The status, by default all. |
|
| 304 | + * |
|
| 305 | + * @return array|object|null Database query results |
|
| 306 | + * @since 3.15.0 |
|
| 307 | + */ |
|
| 308 | + public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) { |
|
| 309 | + global $wpdb; |
|
| 310 | + |
|
| 311 | + // The output fields. |
|
| 312 | + $actual_fields = self::fields( $fields ); |
|
| 313 | + |
|
| 314 | + $sql = $wpdb->prepare( |
|
| 315 | + "SELECT p.$actual_fields FROM {$wpdb->prefix}wl_relation_instances r INNER JOIN $wpdb->posts p ON p.id = r.subject_id" // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
| 316 | + // Add the status clause. |
|
| 317 | + . self::and_status( $status ) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 318 | + . self::inner_join_is_not_article() // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 319 | + . ' WHERE r.object_id = %d ' |
|
| 320 | + . self::and_post_type_in(), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 321 | + $object_id |
|
| 322 | + ); |
|
| 323 | + |
|
| 324 | + return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * Get the entities referenced by the specified {@link WP_Post}. |
|
| 329 | + * |
|
| 330 | + * @param int $subject_id The {@link WP_Post}'s id. |
|
| 331 | + * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 332 | + * '*' to return all fields, by default '*'. |
|
| 333 | + * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 334 | + * @param null|string $status The status, by default all. |
|
| 335 | + * |
|
| 336 | + * @return array|object|null Database query results |
|
| 337 | + * |
|
| 338 | + * @deprecated since it doesn't handle the subject_type nor the object_type |
|
| 339 | + * @since 3.15.0 |
|
| 340 | + */ |
|
| 341 | + public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) { |
|
| 342 | + global $wpdb; |
|
| 343 | + |
|
| 344 | + // The output fields. |
|
| 345 | + $actual_fields = self::fields( $fields ); |
|
| 346 | + |
|
| 347 | + $sql = $wpdb->prepare( |
|
| 348 | + "SELECT p.$actual_fields FROM {$this->relation_table} r INNER JOIN $wpdb->posts p ON p.id = r.object_id" // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
| 349 | + // Add the status clause. |
|
| 350 | + . self::and_status( $status ) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 351 | + . self::inner_join_is_not_article() // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 352 | + . ' WHERE r.subject_id = %d ' |
|
| 353 | + . self::and_post_type_in() // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 354 | + . self::and_predicate( $predicate ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 355 | + $subject_id |
|
| 356 | + ); |
|
| 357 | + |
|
| 358 | + return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Add the `post_status` clause. |
|
| 363 | + * |
|
| 364 | + * @param null|string|array $status The status values. |
|
| 365 | + * |
|
| 366 | + * @return string An empty string if $status is null, otherwise the status clause. |
|
| 367 | + * @since 3.15.0 |
|
| 368 | + */ |
|
| 369 | + private static function and_status( $status = null ) { |
|
| 370 | + |
|
| 371 | + if ( null === $status ) { |
|
| 372 | + return ''; |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')"; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * Add the `predicate` clause. |
|
| 380 | + * |
|
| 381 | + * @param null|string|array $predicate An array of predicates. |
|
| 382 | + * |
|
| 383 | + * @return string An empty string if $predicate is null otherwise the predicate |
|
| 384 | + * clause. |
|
| 385 | + * @since 3.15.0 |
|
| 386 | + */ |
|
| 387 | + private static function and_predicate( $predicate = null ) { |
|
| 388 | + |
|
| 389 | + if ( null === $predicate ) { |
|
| 390 | + return ''; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')"; |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * The select fields. |
|
| 398 | + * |
|
| 399 | + * @param string $fields Either 'ids' or '*', by default '*'. |
|
| 400 | + * |
|
| 401 | + * @return string The `id` field if `ids` otherwise `*`. |
|
| 402 | + * @since 3.15.0 |
|
| 403 | + */ |
|
| 404 | + private static function fields( $fields = '*' ) { |
|
| 405 | + |
|
| 406 | + // The output fields. |
|
| 407 | + return 'ids' === $fields ? 'id' : '*'; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * The inner join clause for articles. |
|
| 412 | + * |
|
| 413 | + * @return string The articles inner join clause. |
|
| 414 | + * @since 3.15.0 |
|
| 415 | + */ |
|
| 416 | + private static function inner_join_is_article() { |
|
| 417 | + global $wpdb; |
|
| 418 | + |
|
| 419 | + return $wpdb->prepare( |
|
| 420 | + " |
|
| 421 | 421 | INNER JOIN $wpdb->term_relationships tr |
| 422 | 422 | ON p.id = tr.object_id |
| 423 | 423 | INNER JOIN $wpdb->term_taxonomy tt |
@@ -427,22 +427,22 @@ discard block |
||
| 427 | 427 | ON t.term_id = tt.term_id |
| 428 | 428 | AND t.slug = %s |
| 429 | 429 | ", |
| 430 | - 'wl_entity_type', |
|
| 431 | - 'article' |
|
| 432 | - ); |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * The inner join clause for non-articles. |
|
| 437 | - * |
|
| 438 | - * @return string The non-articles inner join clause. |
|
| 439 | - * @since 3.15.0 |
|
| 440 | - */ |
|
| 441 | - private static function inner_join_is_not_article() { |
|
| 442 | - global $wpdb; |
|
| 443 | - |
|
| 444 | - return $wpdb->prepare( |
|
| 445 | - " |
|
| 430 | + 'wl_entity_type', |
|
| 431 | + 'article' |
|
| 432 | + ); |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * The inner join clause for non-articles. |
|
| 437 | + * |
|
| 438 | + * @return string The non-articles inner join clause. |
|
| 439 | + * @since 3.15.0 |
|
| 440 | + */ |
|
| 441 | + private static function inner_join_is_not_article() { |
|
| 442 | + global $wpdb; |
|
| 443 | + |
|
| 444 | + return $wpdb->prepare( |
|
| 445 | + " |
|
| 446 | 446 | INNER JOIN $wpdb->term_relationships tr |
| 447 | 447 | ON p.id = tr.object_id |
| 448 | 448 | INNER JOIN $wpdb->term_taxonomy tt |
@@ -452,29 +452,29 @@ discard block |
||
| 452 | 452 | ON t.term_id = tt.term_id |
| 453 | 453 | AND NOT t.slug = %s |
| 454 | 454 | ", |
| 455 | - 'wl_entity_type', |
|
| 456 | - 'article' |
|
| 457 | - ); |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - /** |
|
| 461 | - * Find all the subject IDs and their referenced/related object IDs. The |
|
| 462 | - * object IDs are returned as comma separated IDs in the `object_ids` key. |
|
| 463 | - * |
|
| 464 | - * @return mixed Database query results |
|
| 465 | - * @since 3.18.0 |
|
| 466 | - */ |
|
| 467 | - public function find_all_grouped_by_subject_id() { |
|
| 468 | - global $wpdb; |
|
| 469 | - |
|
| 470 | - return $wpdb->get_results( |
|
| 471 | - " |
|
| 455 | + 'wl_entity_type', |
|
| 456 | + 'article' |
|
| 457 | + ); |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + /** |
|
| 461 | + * Find all the subject IDs and their referenced/related object IDs. The |
|
| 462 | + * object IDs are returned as comma separated IDs in the `object_ids` key. |
|
| 463 | + * |
|
| 464 | + * @return mixed Database query results |
|
| 465 | + * @since 3.18.0 |
|
| 466 | + */ |
|
| 467 | + public function find_all_grouped_by_subject_id() { |
|
| 468 | + global $wpdb; |
|
| 469 | + |
|
| 470 | + return $wpdb->get_results( |
|
| 471 | + " |
|
| 472 | 472 | SELECT subject_id, GROUP_CONCAT( DISTINCT object_id ORDER BY object_id SEPARATOR ',' ) AS object_ids |
| 473 | 473 | FROM {$wpdb->prefix}wl_relation_instances |
| 474 | 474 | GROUP BY subject_id |
| 475 | 475 | " |
| 476 | - ); |
|
| 476 | + ); |
|
| 477 | 477 | |
| 478 | - } |
|
| 478 | + } |
|
| 479 | 479 | |
| 480 | 480 | } |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | protected function __construct() { |
| 45 | 45 | global $wpdb; |
| 46 | 46 | |
| 47 | - self::$log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 47 | + self::$log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 48 | 48 | |
| 49 | 49 | // The relations table. |
| 50 | 50 | $this->relation_table = "{$wpdb->prefix}wl_relation_instances"; |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | */ |
| 70 | 70 | public static function get_instance() { |
| 71 | 71 | |
| 72 | - if ( ! isset( self::$instance ) ) { |
|
| 72 | + if ( ! isset(self::$instance)) { |
|
| 73 | 73 | self::$instance = new self(); |
| 74 | 74 | } |
| 75 | 75 | |
@@ -96,24 +96,24 @@ discard block |
||
| 96 | 96 | * @return array|object|null Database query results |
| 97 | 97 | * @since 3.15.0 |
| 98 | 98 | */ |
| 99 | - public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null, $order_by = null, $post_types = array(), $offset = null ) { |
|
| 99 | + public function get_article_subjects($object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null, $order_by = null, $post_types = array(), $offset = null) { |
|
| 100 | 100 | global $wpdb; |
| 101 | 101 | |
| 102 | 102 | // The output fields. |
| 103 | - $actual_fields = self::fields( $fields ); |
|
| 103 | + $actual_fields = self::fields($fields); |
|
| 104 | 104 | |
| 105 | - self::$log->trace( 'Getting article subjects for object ' . implode( ', ', (array) $object_id ) . '...' ); |
|
| 105 | + self::$log->trace('Getting article subjects for object '.implode(', ', (array) $object_id).'...'); |
|
| 106 | 106 | |
| 107 | - $objects = $this->article_id_to_entity_id( $object_id ); |
|
| 107 | + $objects = $this->article_id_to_entity_id($object_id); |
|
| 108 | 108 | |
| 109 | 109 | // If there are no related objects, return an empty array. |
| 110 | - if ( empty( $objects ) ) { |
|
| 111 | - self::$log->debug( 'No entities found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 110 | + if (empty($objects)) { |
|
| 111 | + self::$log->debug('No entities found for object '.implode(', ', (array) $object_id).'.'); |
|
| 112 | 112 | |
| 113 | 113 | return array(); |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - self::$log->debug( count( $objects ) . ' entity id(s) found for object ' . implode( ', ', (array) $object_id ) . '.' ); |
|
| 116 | + self::$log->debug(count($objects).' entity id(s) found for object '.implode(', ', (array) $object_id).'.'); |
|
| 117 | 117 | |
| 118 | 118 | $sql = |
| 119 | 119 | " |
@@ -123,20 +123,20 @@ discard block |
||
| 123 | 123 | ON p.id = r.subject_id |
| 124 | 124 | " |
| 125 | 125 | // Add the status clause. |
| 126 | - . self::and_status( $status ) |
|
| 126 | + . self::and_status($status) |
|
| 127 | 127 | . self::inner_join_is_article() |
| 128 | - . self::where_object_id( $objects ) |
|
| 128 | + . self::where_object_id($objects) |
|
| 129 | 129 | // Since `object_id` can be an article ID we need to exclude it from |
| 130 | 130 | // the results. |
| 131 | - . self::and_article_not_in( array_merge( $excludes, (array) $object_id ) ) |
|
| 132 | - . self::and_article_in( $include ) |
|
| 133 | - . self::and_post_type_in( $post_types ) |
|
| 134 | - . self::and_predicate( $predicate ) |
|
| 135 | - . self::order_by( $order_by ) |
|
| 136 | - . self::limit( $limit ) |
|
| 137 | - . self::offset( $offset ); |
|
| 138 | - |
|
| 139 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 131 | + . self::and_article_not_in(array_merge($excludes, (array) $object_id)) |
|
| 132 | + . self::and_article_in($include) |
|
| 133 | + . self::and_post_type_in($post_types) |
|
| 134 | + . self::and_predicate($predicate) |
|
| 135 | + . self::order_by($order_by) |
|
| 136 | + . self::limit($limit) |
|
| 137 | + . self::offset($offset); |
|
| 138 | + |
|
| 139 | + return '*' === $actual_fields ? $wpdb->get_results($sql) : $wpdb->get_col($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | /** |
@@ -148,16 +148,16 @@ discard block |
||
| 148 | 148 | * @return string The `post_type IN` clause. |
| 149 | 149 | * @since 3.15.3 |
| 150 | 150 | */ |
| 151 | - private static function and_post_type_in( $post_types = array() ) { |
|
| 151 | + private static function and_post_type_in($post_types = array()) { |
|
| 152 | 152 | |
| 153 | - if ( array() === $post_types ) { |
|
| 153 | + if (array() === $post_types) { |
|
| 154 | 154 | $post_types = Wordlift_Entity_Service::valid_entity_post_types(); |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | return " AND p.post_type IN ( '" |
| 158 | 158 | . implode( |
| 159 | 159 | "','", |
| 160 | - array_map( 'esc_sql', $post_types ) |
|
| 160 | + array_map('esc_sql', $post_types) |
|
| 161 | 161 | ) |
| 162 | 162 | . "' )"; |
| 163 | 163 | } |
@@ -170,9 +170,9 @@ discard block |
||
| 170 | 170 | * @return string The limit clause (empty if no limit has been specified). |
| 171 | 171 | * @since 3.15.0 |
| 172 | 172 | */ |
| 173 | - private static function limit( $limit = null ) { |
|
| 173 | + private static function limit($limit = null) { |
|
| 174 | 174 | |
| 175 | - if ( null === $limit ) { |
|
| 175 | + if (null === $limit) { |
|
| 176 | 176 | return ''; |
| 177 | 177 | } |
| 178 | 178 | |
@@ -187,9 +187,9 @@ discard block |
||
| 187 | 187 | * @return string The offset clause (empty if no offset has been specified). |
| 188 | 188 | * @since 3.35.11 |
| 189 | 189 | */ |
| 190 | - private static function offset( $offset = null ) { |
|
| 190 | + private static function offset($offset = null) { |
|
| 191 | 191 | |
| 192 | - if ( null === $offset || ! is_numeric( $offset ) ) { |
|
| 192 | + if (null === $offset || ! is_numeric($offset)) { |
|
| 193 | 193 | return ''; |
| 194 | 194 | } |
| 195 | 195 | |
@@ -201,14 +201,14 @@ discard block |
||
| 201 | 201 | * |
| 202 | 202 | * @return string |
| 203 | 203 | */ |
| 204 | - private static function order_by( $order_by ) { |
|
| 205 | - if ( ! $order_by ) { |
|
| 204 | + private static function order_by($order_by) { |
|
| 205 | + if ( ! $order_by) { |
|
| 206 | 206 | return ''; |
| 207 | 207 | } |
| 208 | 208 | $order_by = (string) $order_by; |
| 209 | - $order_by_clauses = array( 'DESC', 'ASC' ); |
|
| 209 | + $order_by_clauses = array('DESC', 'ASC'); |
|
| 210 | 210 | |
| 211 | - if ( in_array( $order_by, $order_by_clauses, true ) ) { |
|
| 211 | + if (in_array($order_by, $order_by_clauses, true)) { |
|
| 212 | 212 | return " ORDER BY p.post_modified {$order_by} "; |
| 213 | 213 | } else { |
| 214 | 214 | return ' ORDER BY p.post_modified DESC '; |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | * @return array An array of entities' ids. |
| 225 | 225 | * @since 3.15.0 |
| 226 | 226 | */ |
| 227 | - private function article_id_to_entity_id( $object_id ) { |
|
| 227 | + private function article_id_to_entity_id($object_id) { |
|
| 228 | 228 | |
| 229 | 229 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 230 | 230 | |
@@ -232,12 +232,12 @@ discard block |
||
| 232 | 232 | |
| 233 | 233 | return array_reduce( |
| 234 | 234 | (array) $object_id, |
| 235 | - function ( $carry, $item ) use ( $entity_service, $relation_service ) { |
|
| 236 | - if ( $entity_service->is_entity( $item ) ) { |
|
| 237 | - return array_merge( $carry, (array) $item ); |
|
| 235 | + function($carry, $item) use ($entity_service, $relation_service) { |
|
| 236 | + if ($entity_service->is_entity($item)) { |
|
| 237 | + return array_merge($carry, (array) $item); |
|
| 238 | 238 | } |
| 239 | 239 | |
| 240 | - return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) ); |
|
| 240 | + return array_merge($carry, $relation_service->get_objects($item, 'ids')); |
|
| 241 | 241 | }, |
| 242 | 242 | array() |
| 243 | 243 | ); |
@@ -252,15 +252,15 @@ discard block |
||
| 252 | 252 | * @return string The WHERE clause. |
| 253 | 253 | * @since 3.15.0 |
| 254 | 254 | */ |
| 255 | - private static function where_object_id( $object_id ) { |
|
| 255 | + private static function where_object_id($object_id) { |
|
| 256 | 256 | |
| 257 | - if ( empty( $object_id ) ) { |
|
| 257 | + if (empty($object_id)) { |
|
| 258 | 258 | // self::$log->warn( sprintf( "%s `where_object_id` called with empty `object_id`.", var_export( debug_backtrace( false, 3 ), true ) ) ); |
| 259 | 259 | |
| 260 | 260 | return ' WHERE 1 = 1'; |
| 261 | 261 | } |
| 262 | 262 | |
| 263 | - return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )'; |
|
| 263 | + return ' WHERE r.object_id IN ( '.implode(',', wp_parse_id_list((array) $object_id)).' )'; |
|
| 264 | 264 | } |
| 265 | 265 | |
| 266 | 266 | /** |
@@ -271,9 +271,9 @@ discard block |
||
| 271 | 271 | * @return string The exclude clause. |
| 272 | 272 | * @since 3.15.0 |
| 273 | 273 | */ |
| 274 | - private static function and_article_not_in( $exclude ) { |
|
| 274 | + private static function and_article_not_in($exclude) { |
|
| 275 | 275 | |
| 276 | - return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )'; |
|
| 276 | + return ' AND NOT p.ID IN ( '.implode(',', wp_parse_id_list((array) $exclude)).' )'; |
|
| 277 | 277 | } |
| 278 | 278 | |
| 279 | 279 | /** |
@@ -285,13 +285,13 @@ discard block |
||
| 285 | 285 | * clause. |
| 286 | 286 | * @since 3.15.0 |
| 287 | 287 | */ |
| 288 | - private static function and_article_in( $include = null ) { |
|
| 288 | + private static function and_article_in($include = null) { |
|
| 289 | 289 | |
| 290 | - if ( null === $include ) { |
|
| 290 | + if (null === $include) { |
|
| 291 | 291 | return ''; |
| 292 | 292 | } |
| 293 | 293 | |
| 294 | - return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )'; |
|
| 294 | + return ' AND p.ID IN ( '.implode(',', wp_parse_id_list((array) $include)).' )'; |
|
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | /** |
@@ -305,23 +305,23 @@ discard block |
||
| 305 | 305 | * @return array|object|null Database query results |
| 306 | 306 | * @since 3.15.0 |
| 307 | 307 | */ |
| 308 | - public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) { |
|
| 308 | + public function get_non_article_subjects($object_id, $fields = '*', $status = null) { |
|
| 309 | 309 | global $wpdb; |
| 310 | 310 | |
| 311 | 311 | // The output fields. |
| 312 | - $actual_fields = self::fields( $fields ); |
|
| 312 | + $actual_fields = self::fields($fields); |
|
| 313 | 313 | |
| 314 | 314 | $sql = $wpdb->prepare( |
| 315 | 315 | "SELECT p.$actual_fields FROM {$wpdb->prefix}wl_relation_instances r INNER JOIN $wpdb->posts p ON p.id = r.subject_id" // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 316 | 316 | // Add the status clause. |
| 317 | - . self::and_status( $status ) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 317 | + . self::and_status($status) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 318 | 318 | . self::inner_join_is_not_article() // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 319 | 319 | . ' WHERE r.object_id = %d ' |
| 320 | 320 | . self::and_post_type_in(), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 321 | 321 | $object_id |
| 322 | 322 | ); |
| 323 | 323 | |
| 324 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 324 | + return '*' === $actual_fields ? $wpdb->get_results($sql) : $wpdb->get_col($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 325 | 325 | } |
| 326 | 326 | |
| 327 | 327 | /** |
@@ -338,24 +338,24 @@ discard block |
||
| 338 | 338 | * @deprecated since it doesn't handle the subject_type nor the object_type |
| 339 | 339 | * @since 3.15.0 |
| 340 | 340 | */ |
| 341 | - public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) { |
|
| 341 | + public function get_objects($subject_id, $fields = '*', $predicate = null, $status = null) { |
|
| 342 | 342 | global $wpdb; |
| 343 | 343 | |
| 344 | 344 | // The output fields. |
| 345 | - $actual_fields = self::fields( $fields ); |
|
| 345 | + $actual_fields = self::fields($fields); |
|
| 346 | 346 | |
| 347 | 347 | $sql = $wpdb->prepare( |
| 348 | 348 | "SELECT p.$actual_fields FROM {$this->relation_table} r INNER JOIN $wpdb->posts p ON p.id = r.object_id" // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 349 | 349 | // Add the status clause. |
| 350 | - . self::and_status( $status ) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 350 | + . self::and_status($status) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 351 | 351 | . self::inner_join_is_not_article() // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 352 | 352 | . ' WHERE r.subject_id = %d ' |
| 353 | 353 | . self::and_post_type_in() // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 354 | - . self::and_predicate( $predicate ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 354 | + . self::and_predicate($predicate), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 355 | 355 | $subject_id |
| 356 | 356 | ); |
| 357 | 357 | |
| 358 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 358 | + return '*' === $actual_fields ? $wpdb->get_results($sql) : $wpdb->get_col($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 359 | 359 | } |
| 360 | 360 | |
| 361 | 361 | /** |
@@ -366,13 +366,13 @@ discard block |
||
| 366 | 366 | * @return string An empty string if $status is null, otherwise the status clause. |
| 367 | 367 | * @since 3.15.0 |
| 368 | 368 | */ |
| 369 | - private static function and_status( $status = null ) { |
|
| 369 | + private static function and_status($status = null) { |
|
| 370 | 370 | |
| 371 | - if ( null === $status ) { |
|
| 371 | + if (null === $status) { |
|
| 372 | 372 | return ''; |
| 373 | 373 | } |
| 374 | 374 | |
| 375 | - return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')"; |
|
| 375 | + return " AND p.post_status IN ('".implode("', '", array_map('esc_sql', (array) $status))."')"; |
|
| 376 | 376 | } |
| 377 | 377 | |
| 378 | 378 | /** |
@@ -384,13 +384,13 @@ discard block |
||
| 384 | 384 | * clause. |
| 385 | 385 | * @since 3.15.0 |
| 386 | 386 | */ |
| 387 | - private static function and_predicate( $predicate = null ) { |
|
| 387 | + private static function and_predicate($predicate = null) { |
|
| 388 | 388 | |
| 389 | - if ( null === $predicate ) { |
|
| 389 | + if (null === $predicate) { |
|
| 390 | 390 | return ''; |
| 391 | 391 | } |
| 392 | 392 | |
| 393 | - return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')"; |
|
| 393 | + return " AND r.predicate IN ('".implode("', '", array_map('esc_sql', (array) $predicate))."')"; |
|
| 394 | 394 | } |
| 395 | 395 | |
| 396 | 396 | /** |
@@ -401,7 +401,7 @@ discard block |
||
| 401 | 401 | * @return string The `id` field if `ids` otherwise `*`. |
| 402 | 402 | * @since 3.15.0 |
| 403 | 403 | */ |
| 404 | - private static function fields( $fields = '*' ) { |
|
| 404 | + private static function fields($fields = '*') { |
|
| 405 | 405 | |
| 406 | 406 | // The output fields. |
| 407 | 407 | return 'ids' === $fields ? 'id' : '*'; |
@@ -10,141 +10,141 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | abstract class Wordlift_Shortcode_REST { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * The cache_ttl, set by extending classes. |
|
| 15 | - */ |
|
| 16 | - const CACHE_TTL = 86400; // 24 hours |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * @var $endpoint string The endpoint. |
|
| 20 | - */ |
|
| 21 | - private $endpoint; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * @var $args array The args. |
|
| 25 | - */ |
|
| 26 | - private $args; |
|
| 27 | - |
|
| 28 | - public function __construct( $endpoint, $args ) { |
|
| 29 | - |
|
| 30 | - $scope = $this; |
|
| 31 | - $this->endpoint = $endpoint; |
|
| 32 | - $this->args = $args; |
|
| 33 | - |
|
| 34 | - // Register rest route with callback |
|
| 35 | - add_action( |
|
| 36 | - 'rest_api_init', |
|
| 37 | - function () use ( $scope ) { |
|
| 38 | - register_rest_route( |
|
| 39 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 40 | - $scope->endpoint, |
|
| 41 | - array( |
|
| 42 | - 'methods' => WP_REST_Server::READABLE, |
|
| 43 | - 'permission_callback' => '__return_true', |
|
| 44 | - 'callback' => array( $scope, 'rest_callback' ), |
|
| 45 | - 'args' => $scope->args, |
|
| 46 | - ) |
|
| 47 | - ); |
|
| 48 | - } |
|
| 49 | - ); |
|
| 50 | - |
|
| 51 | - // Optimizations: disable unneeded plugins on this specific REST call. WPSeo is slowing down the responses quite a bit. |
|
| 52 | - add_action( |
|
| 53 | - 'plugins_loaded', |
|
| 54 | - function () use ( $scope ) { |
|
| 55 | - |
|
| 56 | - if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) { |
|
| 57 | - return; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 61 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 62 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 63 | - }, |
|
| 64 | - 0 |
|
| 65 | - ); |
|
| 66 | - |
|
| 67 | - add_action( |
|
| 68 | - 'init', |
|
| 69 | - function () use ( $scope ) { |
|
| 70 | - |
|
| 71 | - if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) { |
|
| 72 | - return; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 76 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 77 | - }, |
|
| 78 | - 0 |
|
| 79 | - ); |
|
| 80 | - |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - abstract public function get_data( $request ); |
|
| 84 | - |
|
| 85 | - public function rest_callback( WP_REST_Request $request ) { |
|
| 86 | - |
|
| 87 | - // Respond from origin if TTL is 0 |
|
| 88 | - if ( static::CACHE_TTL === 0 ) { |
|
| 89 | - |
|
| 90 | - $data = $this->get_data( $request ); |
|
| 91 | - $response = rest_ensure_response( $data ); |
|
| 92 | - if ( is_wp_error( $data ) ) { |
|
| 93 | - return $response; |
|
| 94 | - } |
|
| 95 | - $response->header( 'Access-Control-Allow-Origin', '*' ); |
|
| 96 | - $response->header( 'X-WordLift-Cache', 'MISS' ); |
|
| 97 | - |
|
| 98 | - return $response; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - // Create the cache key. |
|
| 102 | - $cache_key_params = $request->get_params(); |
|
| 103 | - unset( $cache_key_params['uniqid'] ); |
|
| 104 | - unset( $cache_key_params['rest_route'] ); |
|
| 105 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 106 | - |
|
| 107 | - // Create the TTL cache and try to get the results. |
|
| 108 | - $cache = new Ttl_Cache( $this->endpoint, static::CACHE_TTL ); |
|
| 109 | - $cache_results = $cache->get( $cache_key ); |
|
| 110 | - |
|
| 111 | - if ( isset( $cache_results ) ) { |
|
| 112 | - |
|
| 113 | - $response = rest_ensure_response( $cache_results ); |
|
| 114 | - $response->header( 'Access-Control-Allow-Origin', '*' ); |
|
| 115 | - $response->header( 'X-WordLift-Cache', 'HIT' ); |
|
| 116 | - |
|
| 117 | - return $response; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - $data = $this->get_data( $request ); |
|
| 121 | - $response = rest_ensure_response( $data ); |
|
| 122 | - if ( is_wp_error( $data ) ) { |
|
| 123 | - return $response; |
|
| 124 | - } |
|
| 125 | - $response->header( 'Access-Control-Allow-Origin', '*' ); |
|
| 126 | - $response->header( 'X-WordLift-Cache', 'MISS' ); |
|
| 127 | - |
|
| 128 | - // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 129 | - $cache->put( $cache_key, $data ); |
|
| 130 | - |
|
| 131 | - return $response; |
|
| 132 | - |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - private function is_endpoint() { |
|
| 136 | - $compare_route = WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint; |
|
| 137 | - |
|
| 138 | - // Directly accessing $_SERVER['REQUEST_URI'] or $_GET['rest_route'] here as it's too early to use global $wp reliably |
|
| 139 | - |
|
| 140 | - if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( esc_url_raw( wp_unslash( (string) $_SERVER['REQUEST_URI'] ) ), $compare_route ) ) { |
|
| 141 | - return true; |
|
| 142 | - } |
|
| 143 | - if ( ! empty( $_GET['rest_route'] ) && strpos( esc_url_raw( wp_unslash( (string) $_GET['rest_route'] ) ), $compare_route ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 144 | - return true; |
|
| 145 | - } |
|
| 13 | + /** |
|
| 14 | + * The cache_ttl, set by extending classes. |
|
| 15 | + */ |
|
| 16 | + const CACHE_TTL = 86400; // 24 hours |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * @var $endpoint string The endpoint. |
|
| 20 | + */ |
|
| 21 | + private $endpoint; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * @var $args array The args. |
|
| 25 | + */ |
|
| 26 | + private $args; |
|
| 27 | + |
|
| 28 | + public function __construct( $endpoint, $args ) { |
|
| 29 | + |
|
| 30 | + $scope = $this; |
|
| 31 | + $this->endpoint = $endpoint; |
|
| 32 | + $this->args = $args; |
|
| 33 | + |
|
| 34 | + // Register rest route with callback |
|
| 35 | + add_action( |
|
| 36 | + 'rest_api_init', |
|
| 37 | + function () use ( $scope ) { |
|
| 38 | + register_rest_route( |
|
| 39 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 40 | + $scope->endpoint, |
|
| 41 | + array( |
|
| 42 | + 'methods' => WP_REST_Server::READABLE, |
|
| 43 | + 'permission_callback' => '__return_true', |
|
| 44 | + 'callback' => array( $scope, 'rest_callback' ), |
|
| 45 | + 'args' => $scope->args, |
|
| 46 | + ) |
|
| 47 | + ); |
|
| 48 | + } |
|
| 49 | + ); |
|
| 50 | + |
|
| 51 | + // Optimizations: disable unneeded plugins on this specific REST call. WPSeo is slowing down the responses quite a bit. |
|
| 52 | + add_action( |
|
| 53 | + 'plugins_loaded', |
|
| 54 | + function () use ( $scope ) { |
|
| 55 | + |
|
| 56 | + if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) { |
|
| 57 | + return; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 61 | + remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 62 | + remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 63 | + }, |
|
| 64 | + 0 |
|
| 65 | + ); |
|
| 66 | + |
|
| 67 | + add_action( |
|
| 68 | + 'init', |
|
| 69 | + function () use ( $scope ) { |
|
| 70 | + |
|
| 71 | + if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) { |
|
| 72 | + return; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 76 | + remove_action( 'init', 'gglcptch_init' ); |
|
| 77 | + }, |
|
| 78 | + 0 |
|
| 79 | + ); |
|
| 80 | + |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + abstract public function get_data( $request ); |
|
| 84 | + |
|
| 85 | + public function rest_callback( WP_REST_Request $request ) { |
|
| 86 | + |
|
| 87 | + // Respond from origin if TTL is 0 |
|
| 88 | + if ( static::CACHE_TTL === 0 ) { |
|
| 89 | + |
|
| 90 | + $data = $this->get_data( $request ); |
|
| 91 | + $response = rest_ensure_response( $data ); |
|
| 92 | + if ( is_wp_error( $data ) ) { |
|
| 93 | + return $response; |
|
| 94 | + } |
|
| 95 | + $response->header( 'Access-Control-Allow-Origin', '*' ); |
|
| 96 | + $response->header( 'X-WordLift-Cache', 'MISS' ); |
|
| 97 | + |
|
| 98 | + return $response; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + // Create the cache key. |
|
| 102 | + $cache_key_params = $request->get_params(); |
|
| 103 | + unset( $cache_key_params['uniqid'] ); |
|
| 104 | + unset( $cache_key_params['rest_route'] ); |
|
| 105 | + $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 106 | + |
|
| 107 | + // Create the TTL cache and try to get the results. |
|
| 108 | + $cache = new Ttl_Cache( $this->endpoint, static::CACHE_TTL ); |
|
| 109 | + $cache_results = $cache->get( $cache_key ); |
|
| 110 | + |
|
| 111 | + if ( isset( $cache_results ) ) { |
|
| 112 | + |
|
| 113 | + $response = rest_ensure_response( $cache_results ); |
|
| 114 | + $response->header( 'Access-Control-Allow-Origin', '*' ); |
|
| 115 | + $response->header( 'X-WordLift-Cache', 'HIT' ); |
|
| 116 | + |
|
| 117 | + return $response; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + $data = $this->get_data( $request ); |
|
| 121 | + $response = rest_ensure_response( $data ); |
|
| 122 | + if ( is_wp_error( $data ) ) { |
|
| 123 | + return $response; |
|
| 124 | + } |
|
| 125 | + $response->header( 'Access-Control-Allow-Origin', '*' ); |
|
| 126 | + $response->header( 'X-WordLift-Cache', 'MISS' ); |
|
| 127 | + |
|
| 128 | + // Put the result before sending the json to the client, since sending the json will terminate us. |
|
| 129 | + $cache->put( $cache_key, $data ); |
|
| 130 | + |
|
| 131 | + return $response; |
|
| 132 | + |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + private function is_endpoint() { |
|
| 136 | + $compare_route = WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint; |
|
| 137 | + |
|
| 138 | + // Directly accessing $_SERVER['REQUEST_URI'] or $_GET['rest_route'] here as it's too early to use global $wp reliably |
|
| 139 | + |
|
| 140 | + if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( esc_url_raw( wp_unslash( (string) $_SERVER['REQUEST_URI'] ) ), $compare_route ) ) { |
|
| 141 | + return true; |
|
| 142 | + } |
|
| 143 | + if ( ! empty( $_GET['rest_route'] ) && strpos( esc_url_raw( wp_unslash( (string) $_GET['rest_route'] ) ), $compare_route ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 144 | + return true; |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - return false; |
|
| 148 | - } |
|
| 147 | + return false; |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | 150 | } |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | */ |
| 26 | 26 | private $args; |
| 27 | 27 | |
| 28 | - public function __construct( $endpoint, $args ) { |
|
| 28 | + public function __construct($endpoint, $args) { |
|
| 29 | 29 | |
| 30 | 30 | $scope = $this; |
| 31 | 31 | $this->endpoint = $endpoint; |
@@ -34,14 +34,14 @@ discard block |
||
| 34 | 34 | // Register rest route with callback |
| 35 | 35 | add_action( |
| 36 | 36 | 'rest_api_init', |
| 37 | - function () use ( $scope ) { |
|
| 37 | + function() use ($scope) { |
|
| 38 | 38 | register_rest_route( |
| 39 | 39 | WL_REST_ROUTE_DEFAULT_NAMESPACE, |
| 40 | 40 | $scope->endpoint, |
| 41 | 41 | array( |
| 42 | 42 | 'methods' => WP_REST_Server::READABLE, |
| 43 | 43 | 'permission_callback' => '__return_true', |
| 44 | - 'callback' => array( $scope, 'rest_callback' ), |
|
| 44 | + 'callback' => array($scope, 'rest_callback'), |
|
| 45 | 45 | 'args' => $scope->args, |
| 46 | 46 | ) |
| 47 | 47 | ); |
@@ -51,96 +51,96 @@ discard block |
||
| 51 | 51 | // Optimizations: disable unneeded plugins on this specific REST call. WPSeo is slowing down the responses quite a bit. |
| 52 | 52 | add_action( |
| 53 | 53 | 'plugins_loaded', |
| 54 | - function () use ( $scope ) { |
|
| 54 | + function() use ($scope) { |
|
| 55 | 55 | |
| 56 | - if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) { |
|
| 56 | + if ( ! defined('REST_REQUEST') || ! REST_REQUEST || ! $scope->is_endpoint()) { |
|
| 57 | 57 | return; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - remove_action( 'plugins_loaded', 'rocket_init' ); |
|
| 61 | - remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 ); |
|
| 62 | - remove_action( 'plugins_loaded', 'wpseo_init', 14 ); |
|
| 60 | + remove_action('plugins_loaded', 'rocket_init'); |
|
| 61 | + remove_action('plugins_loaded', 'wpseo_premium_init', 14); |
|
| 62 | + remove_action('plugins_loaded', 'wpseo_init', 14); |
|
| 63 | 63 | }, |
| 64 | 64 | 0 |
| 65 | 65 | ); |
| 66 | 66 | |
| 67 | 67 | add_action( |
| 68 | 68 | 'init', |
| 69 | - function () use ( $scope ) { |
|
| 69 | + function() use ($scope) { |
|
| 70 | 70 | |
| 71 | - if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) { |
|
| 71 | + if ( ! defined('REST_REQUEST') || ! REST_REQUEST || ! $scope->is_endpoint()) { |
|
| 72 | 72 | return; |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - remove_action( 'init', 'wp_widgets_init', 1 ); |
|
| 76 | - remove_action( 'init', 'gglcptch_init' ); |
|
| 75 | + remove_action('init', 'wp_widgets_init', 1); |
|
| 76 | + remove_action('init', 'gglcptch_init'); |
|
| 77 | 77 | }, |
| 78 | 78 | 0 |
| 79 | 79 | ); |
| 80 | 80 | |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | - abstract public function get_data( $request ); |
|
| 83 | + abstract public function get_data($request); |
|
| 84 | 84 | |
| 85 | - public function rest_callback( WP_REST_Request $request ) { |
|
| 85 | + public function rest_callback(WP_REST_Request $request) { |
|
| 86 | 86 | |
| 87 | 87 | // Respond from origin if TTL is 0 |
| 88 | - if ( static::CACHE_TTL === 0 ) { |
|
| 88 | + if (static::CACHE_TTL === 0) { |
|
| 89 | 89 | |
| 90 | - $data = $this->get_data( $request ); |
|
| 91 | - $response = rest_ensure_response( $data ); |
|
| 92 | - if ( is_wp_error( $data ) ) { |
|
| 90 | + $data = $this->get_data($request); |
|
| 91 | + $response = rest_ensure_response($data); |
|
| 92 | + if (is_wp_error($data)) { |
|
| 93 | 93 | return $response; |
| 94 | 94 | } |
| 95 | - $response->header( 'Access-Control-Allow-Origin', '*' ); |
|
| 96 | - $response->header( 'X-WordLift-Cache', 'MISS' ); |
|
| 95 | + $response->header('Access-Control-Allow-Origin', '*'); |
|
| 96 | + $response->header('X-WordLift-Cache', 'MISS'); |
|
| 97 | 97 | |
| 98 | 98 | return $response; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | // Create the cache key. |
| 102 | 102 | $cache_key_params = $request->get_params(); |
| 103 | - unset( $cache_key_params['uniqid'] ); |
|
| 104 | - unset( $cache_key_params['rest_route'] ); |
|
| 105 | - $cache_key = array( 'request_params' => $cache_key_params ); |
|
| 103 | + unset($cache_key_params['uniqid']); |
|
| 104 | + unset($cache_key_params['rest_route']); |
|
| 105 | + $cache_key = array('request_params' => $cache_key_params); |
|
| 106 | 106 | |
| 107 | 107 | // Create the TTL cache and try to get the results. |
| 108 | - $cache = new Ttl_Cache( $this->endpoint, static::CACHE_TTL ); |
|
| 109 | - $cache_results = $cache->get( $cache_key ); |
|
| 108 | + $cache = new Ttl_Cache($this->endpoint, static::CACHE_TTL); |
|
| 109 | + $cache_results = $cache->get($cache_key); |
|
| 110 | 110 | |
| 111 | - if ( isset( $cache_results ) ) { |
|
| 111 | + if (isset($cache_results)) { |
|
| 112 | 112 | |
| 113 | - $response = rest_ensure_response( $cache_results ); |
|
| 114 | - $response->header( 'Access-Control-Allow-Origin', '*' ); |
|
| 115 | - $response->header( 'X-WordLift-Cache', 'HIT' ); |
|
| 113 | + $response = rest_ensure_response($cache_results); |
|
| 114 | + $response->header('Access-Control-Allow-Origin', '*'); |
|
| 115 | + $response->header('X-WordLift-Cache', 'HIT'); |
|
| 116 | 116 | |
| 117 | 117 | return $response; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - $data = $this->get_data( $request ); |
|
| 121 | - $response = rest_ensure_response( $data ); |
|
| 122 | - if ( is_wp_error( $data ) ) { |
|
| 120 | + $data = $this->get_data($request); |
|
| 121 | + $response = rest_ensure_response($data); |
|
| 122 | + if (is_wp_error($data)) { |
|
| 123 | 123 | return $response; |
| 124 | 124 | } |
| 125 | - $response->header( 'Access-Control-Allow-Origin', '*' ); |
|
| 126 | - $response->header( 'X-WordLift-Cache', 'MISS' ); |
|
| 125 | + $response->header('Access-Control-Allow-Origin', '*'); |
|
| 126 | + $response->header('X-WordLift-Cache', 'MISS'); |
|
| 127 | 127 | |
| 128 | 128 | // Put the result before sending the json to the client, since sending the json will terminate us. |
| 129 | - $cache->put( $cache_key, $data ); |
|
| 129 | + $cache->put($cache_key, $data); |
|
| 130 | 130 | |
| 131 | 131 | return $response; |
| 132 | 132 | |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | private function is_endpoint() { |
| 136 | - $compare_route = WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint; |
|
| 136 | + $compare_route = WL_REST_ROUTE_DEFAULT_NAMESPACE.$this->endpoint; |
|
| 137 | 137 | |
| 138 | 138 | // Directly accessing $_SERVER['REQUEST_URI'] or $_GET['rest_route'] here as it's too early to use global $wp reliably |
| 139 | 139 | |
| 140 | - if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( esc_url_raw( wp_unslash( (string) $_SERVER['REQUEST_URI'] ) ), $compare_route ) ) { |
|
| 140 | + if (isset($_SERVER['REQUEST_URI']) && strpos(esc_url_raw(wp_unslash((string) $_SERVER['REQUEST_URI'])), $compare_route)) { |
|
| 141 | 141 | return true; |
| 142 | 142 | } |
| 143 | - if ( ! empty( $_GET['rest_route'] ) && strpos( esc_url_raw( wp_unslash( (string) $_GET['rest_route'] ) ), $compare_route ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 143 | + if ( ! empty($_GET['rest_route']) && strpos(esc_url_raw(wp_unslash((string) $_GET['rest_route'])), $compare_route)) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 144 | 144 | return true; |
| 145 | 145 | } |
| 146 | 146 | |
@@ -4,54 +4,54 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Features_Registry { |
| 6 | 6 | |
| 7 | - /** |
|
| 8 | - * @var array<Feature> |
|
| 9 | - */ |
|
| 10 | - private $features = array(); |
|
| 11 | - |
|
| 12 | - private static $instance = null; |
|
| 13 | - |
|
| 14 | - public static function get_instance() { |
|
| 15 | - if ( null === self::$instance ) { |
|
| 16 | - self::$instance = new Features_Registry(); |
|
| 17 | - } |
|
| 18 | - |
|
| 19 | - return self::$instance; |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @param $feature Feature |
|
| 24 | - */ |
|
| 25 | - public function register_feature( $feature ) { |
|
| 26 | - $this->features[] = $feature; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @param $feature_slug string |
|
| 31 | - * @param $default_value bool |
|
| 32 | - * @param $callback callable |
|
| 33 | - */ |
|
| 34 | - public function register_feature_from_slug( $feature_slug, $default_value, $callback ) { |
|
| 35 | - $this->features[] = new Feature( |
|
| 36 | - $feature_slug, |
|
| 37 | - $default_value, |
|
| 38 | - $callback |
|
| 39 | - ); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - public function initialize_all_features() { |
|
| 43 | - foreach ( $this->features as $feature ) { |
|
| 44 | - /** |
|
| 45 | - * @var $feature Feature |
|
| 46 | - */ |
|
| 47 | - $feature_slug = $feature->feature_slug; |
|
| 48 | - if ( apply_filters( "wl_feature__enable__{$feature_slug}", $feature->default_value ) ) { |
|
| 49 | - call_user_func( $feature->callback ); |
|
| 50 | - } |
|
| 51 | - } |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - public function clear_all() { |
|
| 55 | - $this->features = array(); |
|
| 56 | - } |
|
| 7 | + /** |
|
| 8 | + * @var array<Feature> |
|
| 9 | + */ |
|
| 10 | + private $features = array(); |
|
| 11 | + |
|
| 12 | + private static $instance = null; |
|
| 13 | + |
|
| 14 | + public static function get_instance() { |
|
| 15 | + if ( null === self::$instance ) { |
|
| 16 | + self::$instance = new Features_Registry(); |
|
| 17 | + } |
|
| 18 | + |
|
| 19 | + return self::$instance; |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @param $feature Feature |
|
| 24 | + */ |
|
| 25 | + public function register_feature( $feature ) { |
|
| 26 | + $this->features[] = $feature; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @param $feature_slug string |
|
| 31 | + * @param $default_value bool |
|
| 32 | + * @param $callback callable |
|
| 33 | + */ |
|
| 34 | + public function register_feature_from_slug( $feature_slug, $default_value, $callback ) { |
|
| 35 | + $this->features[] = new Feature( |
|
| 36 | + $feature_slug, |
|
| 37 | + $default_value, |
|
| 38 | + $callback |
|
| 39 | + ); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + public function initialize_all_features() { |
|
| 43 | + foreach ( $this->features as $feature ) { |
|
| 44 | + /** |
|
| 45 | + * @var $feature Feature |
|
| 46 | + */ |
|
| 47 | + $feature_slug = $feature->feature_slug; |
|
| 48 | + if ( apply_filters( "wl_feature__enable__{$feature_slug}", $feature->default_value ) ) { |
|
| 49 | + call_user_func( $feature->callback ); |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + public function clear_all() { |
|
| 55 | + $this->features = array(); |
|
| 56 | + } |
|
| 57 | 57 | } |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | private static $instance = null; |
| 13 | 13 | |
| 14 | 14 | public static function get_instance() { |
| 15 | - if ( null === self::$instance ) { |
|
| 15 | + if (null === self::$instance) { |
|
| 16 | 16 | self::$instance = new Features_Registry(); |
| 17 | 17 | } |
| 18 | 18 | |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | /** |
| 23 | 23 | * @param $feature Feature |
| 24 | 24 | */ |
| 25 | - public function register_feature( $feature ) { |
|
| 25 | + public function register_feature($feature) { |
|
| 26 | 26 | $this->features[] = $feature; |
| 27 | 27 | } |
| 28 | 28 | |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * @param $default_value bool |
| 32 | 32 | * @param $callback callable |
| 33 | 33 | */ |
| 34 | - public function register_feature_from_slug( $feature_slug, $default_value, $callback ) { |
|
| 34 | + public function register_feature_from_slug($feature_slug, $default_value, $callback) { |
|
| 35 | 35 | $this->features[] = new Feature( |
| 36 | 36 | $feature_slug, |
| 37 | 37 | $default_value, |
@@ -40,13 +40,13 @@ discard block |
||
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | public function initialize_all_features() { |
| 43 | - foreach ( $this->features as $feature ) { |
|
| 43 | + foreach ($this->features as $feature) { |
|
| 44 | 44 | /** |
| 45 | 45 | * @var $feature Feature |
| 46 | 46 | */ |
| 47 | 47 | $feature_slug = $feature->feature_slug; |
| 48 | - if ( apply_filters( "wl_feature__enable__{$feature_slug}", $feature->default_value ) ) { |
|
| 49 | - call_user_func( $feature->callback ); |
|
| 48 | + if (apply_filters("wl_feature__enable__{$feature_slug}", $feature->default_value)) { |
|
| 49 | + call_user_func($feature->callback); |
|
| 50 | 50 | } |
| 51 | 51 | } |
| 52 | 52 | } |
@@ -3,108 +3,108 @@ |
||
| 3 | 3 | namespace Wordlift\Features; |
| 4 | 4 | |
| 5 | 5 | class Response_Adapter { |
| 6 | - const WL_FEATURES = '_wl_features'; |
|
| 7 | - const WL_1 = 'wl1'; |
|
| 8 | - |
|
| 9 | - public function __construct() { |
|
| 10 | - |
|
| 11 | - // Filter responses from the API calls to update the enabled features. |
|
| 12 | - add_filter( 'wl_api_service__response', array( $this, 'response' ), 10, 1 ); |
|
| 13 | - |
|
| 14 | - // Initialize from `$_ENV`: this is currently required for Unit Tests, since `tests/bootstrap.php` loads WordLift |
|
| 15 | - // before it can actually query the enabled features via HTTP (mock), which would prevent files from being included. |
|
| 16 | - // $this->init_from_env(); |
|
| 17 | - |
|
| 18 | - // Register the `wl_features__enable__{feature-name}` filters. |
|
| 19 | - $this->register_filters(); |
|
| 20 | - |
|
| 21 | - // Hook to the updates to the features setting to refresh the features' filters. |
|
| 22 | - add_action( 'update_option_' . self::WL_FEATURES, array( $this, 'register_filters' ), 10, 0 ); |
|
| 23 | - |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - public function response( $response ) { |
|
| 27 | - |
|
| 28 | - $headers = wp_remote_retrieve_headers( $response ); |
|
| 29 | - |
|
| 30 | - // Bail out if the `wl1` header isn't defined. |
|
| 31 | - if ( ! isset( $headers[ self::WL_1 ] ) ) { |
|
| 32 | - return $response; |
|
| 33 | - } |
|
| 34 | - $wl1_as_base64_string = $headers[ self::WL_1 ]; |
|
| 35 | - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
|
| 36 | - $wl1 = json_decode( base64_decode( $wl1_as_base64_string ), true ); |
|
| 37 | - |
|
| 38 | - $updated_features = $wl1['features']; |
|
| 39 | - |
|
| 40 | - $existing_features = get_option( self::WL_FEATURES, array() ); |
|
| 41 | - |
|
| 42 | - // Loop through updated features. |
|
| 43 | - foreach ( $updated_features as $feature_slug => $new_value ) { |
|
| 44 | - |
|
| 45 | - // We cant pass false because that indicates if the feature is active or not, null is used to represent the features which are |
|
| 46 | - // not set before. |
|
| 47 | - $old_value = array_key_exists( $feature_slug, $existing_features ) ? $existing_features[ $feature_slug ] : null; |
|
| 48 | - |
|
| 49 | - if ( $old_value !== $new_value ) { |
|
| 50 | - /** |
|
| 51 | - * @param $feature_slug string The feature slug. |
|
| 52 | - * @param $old_value null | boolean Null represents the feature flag was not set before. |
|
| 53 | - * @param $new_value boolean True or false. |
|
| 54 | - * |
|
| 55 | - * @since 3.32.1 |
|
| 56 | - * Hook : `wl_feature__change__{feature_slug}` |
|
| 57 | - * Action hook to be fired when there is a change in feature state. |
|
| 58 | - */ |
|
| 59 | - do_action( "wl_feature__change__$feature_slug", $new_value, $old_value, $feature_slug ); |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - if ( isset( $updated_features ) ) { |
|
| 64 | - |
|
| 65 | - if ( update_option( self::WL_FEATURES, (array) $updated_features, true ) ) { |
|
| 66 | - $this->register_filters(); |
|
| 67 | - } |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - return $response; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Registers the feature filters. |
|
| 75 | - */ |
|
| 76 | - public function register_filters() { |
|
| 77 | - |
|
| 78 | - foreach ( (array) get_option( self::WL_FEATURES, array() ) as $name => $enabled ) { |
|
| 79 | - // Remove previous filters. |
|
| 80 | - remove_filter( "wl_feature__enable__{$name}", '__return_true' ); |
|
| 81 | - remove_filter( "wl_feature__enable__{$name}", '__return_false' ); |
|
| 82 | - |
|
| 83 | - $callback = ( $enabled ? '__return_true' : '__return_false' ); |
|
| 84 | - add_filter( "wl_feature__enable__{$name}", $callback ); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - private function init_from_env() { |
|
| 90 | - $features = array_reduce( |
|
| 91 | - array_filter( |
|
| 92 | - array_keys( $_ENV ), |
|
| 93 | - function ( $key ) { |
|
| 94 | - return preg_match( '~^WL_FEATURES__.*~', $key ); |
|
| 95 | - } |
|
| 96 | - ), |
|
| 97 | - function ( $features, $env ) { |
|
| 98 | - $name = strtolower( str_replace( '_', '-', substr( $env, strlen( 'WL_FEATURES__' ) ) ) ); |
|
| 99 | - $value = wp_validate_boolean( getenv( $env ) ); |
|
| 100 | - $features[ $name ] = $value; |
|
| 101 | - |
|
| 102 | - return $features; |
|
| 103 | - }, |
|
| 104 | - array() |
|
| 105 | - ); |
|
| 106 | - |
|
| 107 | - update_option( self::WL_FEATURES, (array) $features, true ); |
|
| 108 | - } |
|
| 6 | + const WL_FEATURES = '_wl_features'; |
|
| 7 | + const WL_1 = 'wl1'; |
|
| 8 | + |
|
| 9 | + public function __construct() { |
|
| 10 | + |
|
| 11 | + // Filter responses from the API calls to update the enabled features. |
|
| 12 | + add_filter( 'wl_api_service__response', array( $this, 'response' ), 10, 1 ); |
|
| 13 | + |
|
| 14 | + // Initialize from `$_ENV`: this is currently required for Unit Tests, since `tests/bootstrap.php` loads WordLift |
|
| 15 | + // before it can actually query the enabled features via HTTP (mock), which would prevent files from being included. |
|
| 16 | + // $this->init_from_env(); |
|
| 17 | + |
|
| 18 | + // Register the `wl_features__enable__{feature-name}` filters. |
|
| 19 | + $this->register_filters(); |
|
| 20 | + |
|
| 21 | + // Hook to the updates to the features setting to refresh the features' filters. |
|
| 22 | + add_action( 'update_option_' . self::WL_FEATURES, array( $this, 'register_filters' ), 10, 0 ); |
|
| 23 | + |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + public function response( $response ) { |
|
| 27 | + |
|
| 28 | + $headers = wp_remote_retrieve_headers( $response ); |
|
| 29 | + |
|
| 30 | + // Bail out if the `wl1` header isn't defined. |
|
| 31 | + if ( ! isset( $headers[ self::WL_1 ] ) ) { |
|
| 32 | + return $response; |
|
| 33 | + } |
|
| 34 | + $wl1_as_base64_string = $headers[ self::WL_1 ]; |
|
| 35 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
|
| 36 | + $wl1 = json_decode( base64_decode( $wl1_as_base64_string ), true ); |
|
| 37 | + |
|
| 38 | + $updated_features = $wl1['features']; |
|
| 39 | + |
|
| 40 | + $existing_features = get_option( self::WL_FEATURES, array() ); |
|
| 41 | + |
|
| 42 | + // Loop through updated features. |
|
| 43 | + foreach ( $updated_features as $feature_slug => $new_value ) { |
|
| 44 | + |
|
| 45 | + // We cant pass false because that indicates if the feature is active or not, null is used to represent the features which are |
|
| 46 | + // not set before. |
|
| 47 | + $old_value = array_key_exists( $feature_slug, $existing_features ) ? $existing_features[ $feature_slug ] : null; |
|
| 48 | + |
|
| 49 | + if ( $old_value !== $new_value ) { |
|
| 50 | + /** |
|
| 51 | + * @param $feature_slug string The feature slug. |
|
| 52 | + * @param $old_value null | boolean Null represents the feature flag was not set before. |
|
| 53 | + * @param $new_value boolean True or false. |
|
| 54 | + * |
|
| 55 | + * @since 3.32.1 |
|
| 56 | + * Hook : `wl_feature__change__{feature_slug}` |
|
| 57 | + * Action hook to be fired when there is a change in feature state. |
|
| 58 | + */ |
|
| 59 | + do_action( "wl_feature__change__$feature_slug", $new_value, $old_value, $feature_slug ); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + if ( isset( $updated_features ) ) { |
|
| 64 | + |
|
| 65 | + if ( update_option( self::WL_FEATURES, (array) $updated_features, true ) ) { |
|
| 66 | + $this->register_filters(); |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + return $response; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Registers the feature filters. |
|
| 75 | + */ |
|
| 76 | + public function register_filters() { |
|
| 77 | + |
|
| 78 | + foreach ( (array) get_option( self::WL_FEATURES, array() ) as $name => $enabled ) { |
|
| 79 | + // Remove previous filters. |
|
| 80 | + remove_filter( "wl_feature__enable__{$name}", '__return_true' ); |
|
| 81 | + remove_filter( "wl_feature__enable__{$name}", '__return_false' ); |
|
| 82 | + |
|
| 83 | + $callback = ( $enabled ? '__return_true' : '__return_false' ); |
|
| 84 | + add_filter( "wl_feature__enable__{$name}", $callback ); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + private function init_from_env() { |
|
| 90 | + $features = array_reduce( |
|
| 91 | + array_filter( |
|
| 92 | + array_keys( $_ENV ), |
|
| 93 | + function ( $key ) { |
|
| 94 | + return preg_match( '~^WL_FEATURES__.*~', $key ); |
|
| 95 | + } |
|
| 96 | + ), |
|
| 97 | + function ( $features, $env ) { |
|
| 98 | + $name = strtolower( str_replace( '_', '-', substr( $env, strlen( 'WL_FEATURES__' ) ) ) ); |
|
| 99 | + $value = wp_validate_boolean( getenv( $env ) ); |
|
| 100 | + $features[ $name ] = $value; |
|
| 101 | + |
|
| 102 | + return $features; |
|
| 103 | + }, |
|
| 104 | + array() |
|
| 105 | + ); |
|
| 106 | + |
|
| 107 | + update_option( self::WL_FEATURES, (array) $features, true ); |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | 110 | } |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | public function __construct() { |
| 10 | 10 | |
| 11 | 11 | // Filter responses from the API calls to update the enabled features. |
| 12 | - add_filter( 'wl_api_service__response', array( $this, 'response' ), 10, 1 ); |
|
| 12 | + add_filter('wl_api_service__response', array($this, 'response'), 10, 1); |
|
| 13 | 13 | |
| 14 | 14 | // Initialize from `$_ENV`: this is currently required for Unit Tests, since `tests/bootstrap.php` loads WordLift |
| 15 | 15 | // before it can actually query the enabled features via HTTP (mock), which would prevent files from being included. |
@@ -19,34 +19,34 @@ discard block |
||
| 19 | 19 | $this->register_filters(); |
| 20 | 20 | |
| 21 | 21 | // Hook to the updates to the features setting to refresh the features' filters. |
| 22 | - add_action( 'update_option_' . self::WL_FEATURES, array( $this, 'register_filters' ), 10, 0 ); |
|
| 22 | + add_action('update_option_'.self::WL_FEATURES, array($this, 'register_filters'), 10, 0); |
|
| 23 | 23 | |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | - public function response( $response ) { |
|
| 26 | + public function response($response) { |
|
| 27 | 27 | |
| 28 | - $headers = wp_remote_retrieve_headers( $response ); |
|
| 28 | + $headers = wp_remote_retrieve_headers($response); |
|
| 29 | 29 | |
| 30 | 30 | // Bail out if the `wl1` header isn't defined. |
| 31 | - if ( ! isset( $headers[ self::WL_1 ] ) ) { |
|
| 31 | + if ( ! isset($headers[self::WL_1])) { |
|
| 32 | 32 | return $response; |
| 33 | 33 | } |
| 34 | - $wl1_as_base64_string = $headers[ self::WL_1 ]; |
|
| 34 | + $wl1_as_base64_string = $headers[self::WL_1]; |
|
| 35 | 35 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
| 36 | - $wl1 = json_decode( base64_decode( $wl1_as_base64_string ), true ); |
|
| 36 | + $wl1 = json_decode(base64_decode($wl1_as_base64_string), true); |
|
| 37 | 37 | |
| 38 | 38 | $updated_features = $wl1['features']; |
| 39 | 39 | |
| 40 | - $existing_features = get_option( self::WL_FEATURES, array() ); |
|
| 40 | + $existing_features = get_option(self::WL_FEATURES, array()); |
|
| 41 | 41 | |
| 42 | 42 | // Loop through updated features. |
| 43 | - foreach ( $updated_features as $feature_slug => $new_value ) { |
|
| 43 | + foreach ($updated_features as $feature_slug => $new_value) { |
|
| 44 | 44 | |
| 45 | 45 | // We cant pass false because that indicates if the feature is active or not, null is used to represent the features which are |
| 46 | 46 | // not set before. |
| 47 | - $old_value = array_key_exists( $feature_slug, $existing_features ) ? $existing_features[ $feature_slug ] : null; |
|
| 47 | + $old_value = array_key_exists($feature_slug, $existing_features) ? $existing_features[$feature_slug] : null; |
|
| 48 | 48 | |
| 49 | - if ( $old_value !== $new_value ) { |
|
| 49 | + if ($old_value !== $new_value) { |
|
| 50 | 50 | /** |
| 51 | 51 | * @param $feature_slug string The feature slug. |
| 52 | 52 | * @param $old_value null | boolean Null represents the feature flag was not set before. |
@@ -56,13 +56,13 @@ discard block |
||
| 56 | 56 | * Hook : `wl_feature__change__{feature_slug}` |
| 57 | 57 | * Action hook to be fired when there is a change in feature state. |
| 58 | 58 | */ |
| 59 | - do_action( "wl_feature__change__$feature_slug", $new_value, $old_value, $feature_slug ); |
|
| 59 | + do_action("wl_feature__change__$feature_slug", $new_value, $old_value, $feature_slug); |
|
| 60 | 60 | } |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - if ( isset( $updated_features ) ) { |
|
| 63 | + if (isset($updated_features)) { |
|
| 64 | 64 | |
| 65 | - if ( update_option( self::WL_FEATURES, (array) $updated_features, true ) ) { |
|
| 65 | + if (update_option(self::WL_FEATURES, (array) $updated_features, true)) { |
|
| 66 | 66 | $this->register_filters(); |
| 67 | 67 | } |
| 68 | 68 | } |
@@ -75,13 +75,13 @@ discard block |
||
| 75 | 75 | */ |
| 76 | 76 | public function register_filters() { |
| 77 | 77 | |
| 78 | - foreach ( (array) get_option( self::WL_FEATURES, array() ) as $name => $enabled ) { |
|
| 78 | + foreach ((array) get_option(self::WL_FEATURES, array()) as $name => $enabled) { |
|
| 79 | 79 | // Remove previous filters. |
| 80 | - remove_filter( "wl_feature__enable__{$name}", '__return_true' ); |
|
| 81 | - remove_filter( "wl_feature__enable__{$name}", '__return_false' ); |
|
| 80 | + remove_filter("wl_feature__enable__{$name}", '__return_true'); |
|
| 81 | + remove_filter("wl_feature__enable__{$name}", '__return_false'); |
|
| 82 | 82 | |
| 83 | - $callback = ( $enabled ? '__return_true' : '__return_false' ); |
|
| 84 | - add_filter( "wl_feature__enable__{$name}", $callback ); |
|
| 83 | + $callback = ($enabled ? '__return_true' : '__return_false'); |
|
| 84 | + add_filter("wl_feature__enable__{$name}", $callback); |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | } |
@@ -89,22 +89,22 @@ discard block |
||
| 89 | 89 | private function init_from_env() { |
| 90 | 90 | $features = array_reduce( |
| 91 | 91 | array_filter( |
| 92 | - array_keys( $_ENV ), |
|
| 93 | - function ( $key ) { |
|
| 94 | - return preg_match( '~^WL_FEATURES__.*~', $key ); |
|
| 92 | + array_keys($_ENV), |
|
| 93 | + function($key) { |
|
| 94 | + return preg_match('~^WL_FEATURES__.*~', $key); |
|
| 95 | 95 | } |
| 96 | 96 | ), |
| 97 | - function ( $features, $env ) { |
|
| 98 | - $name = strtolower( str_replace( '_', '-', substr( $env, strlen( 'WL_FEATURES__' ) ) ) ); |
|
| 99 | - $value = wp_validate_boolean( getenv( $env ) ); |
|
| 100 | - $features[ $name ] = $value; |
|
| 97 | + function($features, $env) { |
|
| 98 | + $name = strtolower(str_replace('_', '-', substr($env, strlen('WL_FEATURES__')))); |
|
| 99 | + $value = wp_validate_boolean(getenv($env)); |
|
| 100 | + $features[$name] = $value; |
|
| 101 | 101 | |
| 102 | 102 | return $features; |
| 103 | 103 | }, |
| 104 | 104 | array() |
| 105 | 105 | ); |
| 106 | 106 | |
| 107 | - update_option( self::WL_FEATURES, (array) $features, true ); |
|
| 107 | + update_option(self::WL_FEATURES, (array) $features, true); |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | } |
@@ -4,50 +4,50 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Configuration { |
| 6 | 6 | |
| 7 | - private static $instance; |
|
| 8 | - |
|
| 9 | - private $type; |
|
| 10 | - private $urls; |
|
| 11 | - |
|
| 12 | - protected function __construct() { |
|
| 13 | - $include_exclude_data = get_option( 'wl_exclude_include_urls_settings', array() ); |
|
| 14 | - $include_exclude = isset( $include_exclude_data['include_exclude'] ) ? $include_exclude_data['include_exclude'] : 'exclude'; |
|
| 15 | - |
|
| 16 | - $this->type = in_array( |
|
| 17 | - $include_exclude, |
|
| 18 | - array( |
|
| 19 | - 'include', |
|
| 20 | - 'exclude', |
|
| 21 | - ), |
|
| 22 | - true |
|
| 23 | - ) |
|
| 24 | - ? $include_exclude : 'exclude'; |
|
| 25 | - $this->urls = isset( $include_exclude_data['urls'] ) ? $include_exclude_data['urls'] : ''; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - public static function get_instance() { |
|
| 29 | - if ( ! isset( self::$instance ) ) { |
|
| 30 | - self::$instance = new self(); |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - return self::$instance; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - public function get_type() { |
|
| 37 | - return $this->type; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Gets the default action, which is the inverse of the configuration type. |
|
| 42 | - * |
|
| 43 | - * @return string |
|
| 44 | - */ |
|
| 45 | - public function get_default() { |
|
| 46 | - return ( $this->type === 'exclude' ? 'include' : 'exclude' ); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - public function get_urls() { |
|
| 50 | - return $this->urls; |
|
| 51 | - } |
|
| 7 | + private static $instance; |
|
| 8 | + |
|
| 9 | + private $type; |
|
| 10 | + private $urls; |
|
| 11 | + |
|
| 12 | + protected function __construct() { |
|
| 13 | + $include_exclude_data = get_option( 'wl_exclude_include_urls_settings', array() ); |
|
| 14 | + $include_exclude = isset( $include_exclude_data['include_exclude'] ) ? $include_exclude_data['include_exclude'] : 'exclude'; |
|
| 15 | + |
|
| 16 | + $this->type = in_array( |
|
| 17 | + $include_exclude, |
|
| 18 | + array( |
|
| 19 | + 'include', |
|
| 20 | + 'exclude', |
|
| 21 | + ), |
|
| 22 | + true |
|
| 23 | + ) |
|
| 24 | + ? $include_exclude : 'exclude'; |
|
| 25 | + $this->urls = isset( $include_exclude_data['urls'] ) ? $include_exclude_data['urls'] : ''; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + public static function get_instance() { |
|
| 29 | + if ( ! isset( self::$instance ) ) { |
|
| 30 | + self::$instance = new self(); |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + return self::$instance; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + public function get_type() { |
|
| 37 | + return $this->type; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Gets the default action, which is the inverse of the configuration type. |
|
| 42 | + * |
|
| 43 | + * @return string |
|
| 44 | + */ |
|
| 45 | + public function get_default() { |
|
| 46 | + return ( $this->type === 'exclude' ? 'include' : 'exclude' ); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + public function get_urls() { |
|
| 50 | + return $this->urls; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | 53 | } |
@@ -10,8 +10,8 @@ discard block |
||
| 10 | 10 | private $urls; |
| 11 | 11 | |
| 12 | 12 | protected function __construct() { |
| 13 | - $include_exclude_data = get_option( 'wl_exclude_include_urls_settings', array() ); |
|
| 14 | - $include_exclude = isset( $include_exclude_data['include_exclude'] ) ? $include_exclude_data['include_exclude'] : 'exclude'; |
|
| 13 | + $include_exclude_data = get_option('wl_exclude_include_urls_settings', array()); |
|
| 14 | + $include_exclude = isset($include_exclude_data['include_exclude']) ? $include_exclude_data['include_exclude'] : 'exclude'; |
|
| 15 | 15 | |
| 16 | 16 | $this->type = in_array( |
| 17 | 17 | $include_exclude, |
@@ -22,11 +22,11 @@ discard block |
||
| 22 | 22 | true |
| 23 | 23 | ) |
| 24 | 24 | ? $include_exclude : 'exclude'; |
| 25 | - $this->urls = isset( $include_exclude_data['urls'] ) ? $include_exclude_data['urls'] : ''; |
|
| 25 | + $this->urls = isset($include_exclude_data['urls']) ? $include_exclude_data['urls'] : ''; |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | public static function get_instance() { |
| 29 | - if ( ! isset( self::$instance ) ) { |
|
| 29 | + if ( ! isset(self::$instance)) { |
|
| 30 | 30 | self::$instance = new self(); |
| 31 | 31 | } |
| 32 | 32 | |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | * @return string |
| 44 | 44 | */ |
| 45 | 45 | public function get_default() { |
| 46 | - return ( $this->type === 'exclude' ? 'include' : 'exclude' ); |
|
| 46 | + return ($this->type === 'exclude' ? 'include' : 'exclude'); |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | public function get_urls() { |
@@ -4,44 +4,44 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Jsonld_Interceptor { |
| 6 | 6 | |
| 7 | - /** @var Plugin_Enabled $plugin_enabled */ |
|
| 8 | - private $plugin_enabled; |
|
| 7 | + /** @var Plugin_Enabled $plugin_enabled */ |
|
| 8 | + private $plugin_enabled; |
|
| 9 | 9 | |
| 10 | - public function __construct( $plugin_enabled ) { |
|
| 11 | - $this->plugin_enabled = $plugin_enabled; |
|
| 12 | - } |
|
| 10 | + public function __construct( $plugin_enabled ) { |
|
| 11 | + $this->plugin_enabled = $plugin_enabled; |
|
| 12 | + } |
|
| 13 | 13 | |
| 14 | - public function register_hooks() { |
|
| 15 | - add_filter( 'wl_after_get_jsonld', array( $this, 'after_get_jsonld' ) ); |
|
| 16 | - } |
|
| 14 | + public function register_hooks() { |
|
| 15 | + add_filter( 'wl_after_get_jsonld', array( $this, 'after_get_jsonld' ) ); |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - public function after_get_jsonld( $jsonld_arr ) { |
|
| 19 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 20 | - @header( 'X-Wordlift-IncludeExclude-Stage-0: Filter Called with default ' . $this->plugin_enabled->get_configuration()->get_default() ); |
|
| 21 | - if ( ! is_array( $jsonld_arr ) || empty( $jsonld_arr ) || ! isset( $jsonld_arr[0]['url'] ) || null !== filter_input( INPUT_SERVER, 'HTTP_X_WORDLIFT_BYPASS_INCLUDE_EXCLUDE' ) ) { |
|
| 22 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 23 | - @header( 'X-Wordlift-IncludeExclude-Stage-1: Condition Not Matched' ); |
|
| 18 | + public function after_get_jsonld( $jsonld_arr ) { |
|
| 19 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 20 | + @header( 'X-Wordlift-IncludeExclude-Stage-0: Filter Called with default ' . $this->plugin_enabled->get_configuration()->get_default() ); |
|
| 21 | + if ( ! is_array( $jsonld_arr ) || empty( $jsonld_arr ) || ! isset( $jsonld_arr[0]['url'] ) || null !== filter_input( INPUT_SERVER, 'HTTP_X_WORDLIFT_BYPASS_INCLUDE_EXCLUDE' ) ) { |
|
| 22 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 23 | + @header( 'X-Wordlift-IncludeExclude-Stage-1: Condition Not Matched' ); |
|
| 24 | 24 | |
| 25 | - return $jsonld_arr; |
|
| 26 | - } |
|
| 25 | + return $jsonld_arr; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 29 | - @header( 'X-Wordlift-IncludeExclude-Stage-1: Condition Matched for ' . $jsonld_arr[0]['url'] ); |
|
| 30 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 31 | - @header( 'X-Wordlift-IncludeExclude-Note: To bypass the Include/Exclude filter add a `x-wordlift-bypass-include-exclude` HTTP request header with any value.' ); |
|
| 28 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 29 | + @header( 'X-Wordlift-IncludeExclude-Stage-1: Condition Matched for ' . $jsonld_arr[0]['url'] ); |
|
| 30 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 31 | + @header( 'X-Wordlift-IncludeExclude-Note: To bypass the Include/Exclude filter add a `x-wordlift-bypass-include-exclude` HTTP request header with any value.' ); |
|
| 32 | 32 | |
| 33 | - // If the URLs are included then publish them. |
|
| 34 | - if ( $this->plugin_enabled->are_urls_included( $jsonld_arr[0]['url'] ) ) { |
|
| 35 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 36 | - @header( 'X-Wordlift-IncludeExclude-Stage-2: URL Included' ); |
|
| 33 | + // If the URLs are included then publish them. |
|
| 34 | + if ( $this->plugin_enabled->are_urls_included( $jsonld_arr[0]['url'] ) ) { |
|
| 35 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 36 | + @header( 'X-Wordlift-IncludeExclude-Stage-2: URL Included' ); |
|
| 37 | 37 | |
| 38 | - return $jsonld_arr; |
|
| 39 | - } |
|
| 38 | + return $jsonld_arr; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 42 | - @header( 'X-Wordlift-IncludeExclude-Stage-2: URL Excluded' ); |
|
| 41 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 42 | + @header( 'X-Wordlift-IncludeExclude-Stage-2: URL Excluded' ); |
|
| 43 | 43 | |
| 44 | - return array(); |
|
| 45 | - } |
|
| 44 | + return array(); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | 47 | } |
@@ -7,39 +7,39 @@ |
||
| 7 | 7 | /** @var Plugin_Enabled $plugin_enabled */ |
| 8 | 8 | private $plugin_enabled; |
| 9 | 9 | |
| 10 | - public function __construct( $plugin_enabled ) { |
|
| 10 | + public function __construct($plugin_enabled) { |
|
| 11 | 11 | $this->plugin_enabled = $plugin_enabled; |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | public function register_hooks() { |
| 15 | - add_filter( 'wl_after_get_jsonld', array( $this, 'after_get_jsonld' ) ); |
|
| 15 | + add_filter('wl_after_get_jsonld', array($this, 'after_get_jsonld')); |
|
| 16 | 16 | } |
| 17 | 17 | |
| 18 | - public function after_get_jsonld( $jsonld_arr ) { |
|
| 18 | + public function after_get_jsonld($jsonld_arr) { |
|
| 19 | 19 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 20 | - @header( 'X-Wordlift-IncludeExclude-Stage-0: Filter Called with default ' . $this->plugin_enabled->get_configuration()->get_default() ); |
|
| 21 | - if ( ! is_array( $jsonld_arr ) || empty( $jsonld_arr ) || ! isset( $jsonld_arr[0]['url'] ) || null !== filter_input( INPUT_SERVER, 'HTTP_X_WORDLIFT_BYPASS_INCLUDE_EXCLUDE' ) ) { |
|
| 20 | + @header('X-Wordlift-IncludeExclude-Stage-0: Filter Called with default '.$this->plugin_enabled->get_configuration()->get_default()); |
|
| 21 | + if ( ! is_array($jsonld_arr) || empty($jsonld_arr) || ! isset($jsonld_arr[0]['url']) || null !== filter_input(INPUT_SERVER, 'HTTP_X_WORDLIFT_BYPASS_INCLUDE_EXCLUDE')) { |
|
| 22 | 22 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 23 | - @header( 'X-Wordlift-IncludeExclude-Stage-1: Condition Not Matched' ); |
|
| 23 | + @header('X-Wordlift-IncludeExclude-Stage-1: Condition Not Matched'); |
|
| 24 | 24 | |
| 25 | 25 | return $jsonld_arr; |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 29 | - @header( 'X-Wordlift-IncludeExclude-Stage-1: Condition Matched for ' . $jsonld_arr[0]['url'] ); |
|
| 29 | + @header('X-Wordlift-IncludeExclude-Stage-1: Condition Matched for '.$jsonld_arr[0]['url']); |
|
| 30 | 30 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 31 | - @header( 'X-Wordlift-IncludeExclude-Note: To bypass the Include/Exclude filter add a `x-wordlift-bypass-include-exclude` HTTP request header with any value.' ); |
|
| 31 | + @header('X-Wordlift-IncludeExclude-Note: To bypass the Include/Exclude filter add a `x-wordlift-bypass-include-exclude` HTTP request header with any value.'); |
|
| 32 | 32 | |
| 33 | 33 | // If the URLs are included then publish them. |
| 34 | - if ( $this->plugin_enabled->are_urls_included( $jsonld_arr[0]['url'] ) ) { |
|
| 34 | + if ($this->plugin_enabled->are_urls_included($jsonld_arr[0]['url'])) { |
|
| 35 | 35 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 36 | - @header( 'X-Wordlift-IncludeExclude-Stage-2: URL Included' ); |
|
| 36 | + @header('X-Wordlift-IncludeExclude-Stage-2: URL Included'); |
|
| 37 | 37 | |
| 38 | 38 | return $jsonld_arr; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 42 | - @header( 'X-Wordlift-IncludeExclude-Stage-2: URL Excluded' ); |
|
| 42 | + @header('X-Wordlift-IncludeExclude-Stage-2: URL Excluded'); |
|
| 43 | 43 | |
| 44 | 44 | return array(); |
| 45 | 45 | } |
@@ -16,98 +16,98 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Plugin_Enabled { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @var Configuration $configuration |
|
| 21 | - */ |
|
| 22 | - private $configuration; |
|
| 23 | - |
|
| 24 | - public function __construct( $configuration ) { |
|
| 25 | - $this->configuration = $configuration; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Register hooks. |
|
| 30 | - */ |
|
| 31 | - public function register_hooks() { |
|
| 32 | - add_filter( 'wl_is_enabled', array( $this, 'wl_is_enabled' ) ); |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Enable/Disable WordLift Plugin. |
|
| 37 | - * |
|
| 38 | - * @param $enabled |
|
| 39 | - * |
|
| 40 | - * @return bool|mixed |
|
| 41 | - */ |
|
| 42 | - public function wl_is_enabled( $enabled ) { |
|
| 43 | - |
|
| 44 | - // Always enable wordlift on admin and rest api pages. |
|
| 45 | - if ( is_admin() || $this->is_rest_request() ) { |
|
| 46 | - return $enabled; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - $path = strtok( (string) $_SERVER['REQUEST_URI'], '?' ); // phpcs:ignore |
|
| 50 | - $options = get_option( 'wl_exclude_include_urls_settings' ); |
|
| 51 | - |
|
| 52 | - // Bail out if URLs are not set. |
|
| 53 | - if ( empty( $options['urls'] ) ) { |
|
| 54 | - return $enabled; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - $current_url = trailingslashit( home_url( $path ) ); |
|
| 58 | - |
|
| 59 | - return $this->are_urls_included( $current_url ); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - public function are_urls_included( $urls ) { |
|
| 63 | - // Ensure we deal with an array. We `trailingslashit` all URLs to avoid issues with missing slashes. |
|
| 64 | - $urls = array_map( 'trailingslashit', (array) $urls ); |
|
| 65 | - |
|
| 66 | - // Set a default state. |
|
| 67 | - $include_by_default = ( $this->configuration->get_default() === 'include' ); |
|
| 68 | - |
|
| 69 | - // Get URLs into an array from settings, trim them and make absolute if needed. |
|
| 70 | - $configured_urls = array_map( |
|
| 71 | - function ( $url ) { |
|
| 72 | - $url = trim( $url ); |
|
| 73 | - if ( substr( $url, 0, 4 ) !== 'http' ) { |
|
| 74 | - return trailingslashit( home_url( $url ) ); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - // Add a trailing slash and return the url |
|
| 78 | - return trailingslashit( $url ); |
|
| 79 | - }, |
|
| 80 | - explode( PHP_EOL, $this->configuration->get_urls() ) |
|
| 81 | - ); |
|
| 82 | - |
|
| 83 | - // Check if any of the provided URLs is in the configured URLs. |
|
| 84 | - $intersection = array_intersect( $urls, $configured_urls ); |
|
| 85 | - if ( ! empty( $intersection ) ) { |
|
| 86 | - return ! $include_by_default; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - return $include_by_default; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - public function get_configuration() { |
|
| 93 | - return $this->configuration; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * We cant rely on WP_REST_REQUEST constant here since it is loaded after init hook |
|
| 98 | - * |
|
| 99 | - * @return bool |
|
| 100 | - */ |
|
| 101 | - protected function is_rest_request() { |
|
| 102 | - if ( empty( $_SERVER['REQUEST_URI'] ) ) { |
|
| 103 | - // Probably a CLI request. |
|
| 104 | - return false; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - $rest_prefix = trailingslashit( rest_get_url_prefix() ); |
|
| 108 | - $path = strtok( (string) $_SERVER['REQUEST_URI'], '?' ); // phpcs:ignore |
|
| 109 | - |
|
| 110 | - return strpos( $path, $rest_prefix ) !== false; |
|
| 111 | - } |
|
| 19 | + /** |
|
| 20 | + * @var Configuration $configuration |
|
| 21 | + */ |
|
| 22 | + private $configuration; |
|
| 23 | + |
|
| 24 | + public function __construct( $configuration ) { |
|
| 25 | + $this->configuration = $configuration; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Register hooks. |
|
| 30 | + */ |
|
| 31 | + public function register_hooks() { |
|
| 32 | + add_filter( 'wl_is_enabled', array( $this, 'wl_is_enabled' ) ); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Enable/Disable WordLift Plugin. |
|
| 37 | + * |
|
| 38 | + * @param $enabled |
|
| 39 | + * |
|
| 40 | + * @return bool|mixed |
|
| 41 | + */ |
|
| 42 | + public function wl_is_enabled( $enabled ) { |
|
| 43 | + |
|
| 44 | + // Always enable wordlift on admin and rest api pages. |
|
| 45 | + if ( is_admin() || $this->is_rest_request() ) { |
|
| 46 | + return $enabled; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + $path = strtok( (string) $_SERVER['REQUEST_URI'], '?' ); // phpcs:ignore |
|
| 50 | + $options = get_option( 'wl_exclude_include_urls_settings' ); |
|
| 51 | + |
|
| 52 | + // Bail out if URLs are not set. |
|
| 53 | + if ( empty( $options['urls'] ) ) { |
|
| 54 | + return $enabled; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + $current_url = trailingslashit( home_url( $path ) ); |
|
| 58 | + |
|
| 59 | + return $this->are_urls_included( $current_url ); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + public function are_urls_included( $urls ) { |
|
| 63 | + // Ensure we deal with an array. We `trailingslashit` all URLs to avoid issues with missing slashes. |
|
| 64 | + $urls = array_map( 'trailingslashit', (array) $urls ); |
|
| 65 | + |
|
| 66 | + // Set a default state. |
|
| 67 | + $include_by_default = ( $this->configuration->get_default() === 'include' ); |
|
| 68 | + |
|
| 69 | + // Get URLs into an array from settings, trim them and make absolute if needed. |
|
| 70 | + $configured_urls = array_map( |
|
| 71 | + function ( $url ) { |
|
| 72 | + $url = trim( $url ); |
|
| 73 | + if ( substr( $url, 0, 4 ) !== 'http' ) { |
|
| 74 | + return trailingslashit( home_url( $url ) ); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + // Add a trailing slash and return the url |
|
| 78 | + return trailingslashit( $url ); |
|
| 79 | + }, |
|
| 80 | + explode( PHP_EOL, $this->configuration->get_urls() ) |
|
| 81 | + ); |
|
| 82 | + |
|
| 83 | + // Check if any of the provided URLs is in the configured URLs. |
|
| 84 | + $intersection = array_intersect( $urls, $configured_urls ); |
|
| 85 | + if ( ! empty( $intersection ) ) { |
|
| 86 | + return ! $include_by_default; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + return $include_by_default; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + public function get_configuration() { |
|
| 93 | + return $this->configuration; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * We cant rely on WP_REST_REQUEST constant here since it is loaded after init hook |
|
| 98 | + * |
|
| 99 | + * @return bool |
|
| 100 | + */ |
|
| 101 | + protected function is_rest_request() { |
|
| 102 | + if ( empty( $_SERVER['REQUEST_URI'] ) ) { |
|
| 103 | + // Probably a CLI request. |
|
| 104 | + return false; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + $rest_prefix = trailingslashit( rest_get_url_prefix() ); |
|
| 108 | + $path = strtok( (string) $_SERVER['REQUEST_URI'], '?' ); // phpcs:ignore |
|
| 109 | + |
|
| 110 | + return strpos( $path, $rest_prefix ) !== false; |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | 113 | } |