| 1 |
<?php |
| 2 |
/** |
| 3 |
* BetterDocs controller class. |
| 4 |
* |
| 5 |
* @package ContentControl |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace ContentControl\Controllers\Compatibility; |
| 9 |
|
| 10 |
use ContentControl\Base\Controller; |
| 11 |
|
| 12 |
/** |
| 13 |
* BetterDocs controller class. |
| 14 |
*/ |
| 15 |
class BetterDocs extends Controller { |
| 16 |
|
| 17 |
/** |
| 18 |
* Initiate hooks & filter. |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public function init() { |
| 23 |
add_filter( 'content_control/get_rest_api_intent', [ $this, 'get_rest_api_intent' ], 10 ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Check if controller is enabled. |
| 28 |
* |
| 29 |
* @return bool |
| 30 |
*/ |
| 31 |
public function controller_enabled() { |
| 32 |
return defined( 'BETTERDOCS_PLUGIN_FILE' ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get intent for BetterDocs. |
| 37 |
* |
| 38 |
* @param array<string,mixed> $intent Intent. |
| 39 |
* |
| 40 |
* @return array<string,mixed> |
| 41 |
*/ |
| 42 |
public function get_rest_api_intent( $intent ) { |
| 43 |
global $wp; |
| 44 |
|
| 45 |
$rest_route = $wp->query_vars['rest_route']; |
| 46 |
$endpoint_parts = explode( '/', str_replace( '/wp/v2/', '', $rest_route ) ); |
| 47 |
|
| 48 |
// Set the custom search intent. |
| 49 |
if ( isset( $wp->query_vars['search'] ) ) { |
| 50 |
$intent['search'] = sanitize_title( $wp->query_vars['search'] ); |
| 51 |
} |
| 52 |
|
| 53 |
if ( 'unknown' === $intent['type'] && 'docs' === $intent['name'] ) { |
| 54 |
// If we have a post type or taxonomy, the name is the first part (posts, categories). |
| 55 |
$post_type = sanitize_key( $endpoint_parts[0] ); |
| 56 |
|
| 57 |
if ( 'docs' === $post_type ) { |
| 58 |
$intent['type'] = 'post_type'; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 63 |
if ( isset( $_REQUEST['post_type'] ) && is_string( $_REQUEST['post_type'] ) ) { |
| 64 |
$post_type = sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ); |
| 65 |
|
| 66 |
// Check if any ct_forced_* request aregs are set. If so we should use the post type intent. |
| 67 |
if ( strpos( $post_type, 'ct_forced_' ) !== false ) { |
| 68 |
$intent['type'] = 'post_type'; |
| 69 |
|
| 70 |
$post_type = str_replace( 'ct_forced_', '', $post_type ); |
| 71 |
|
| 72 |
$intent['name'] = array_values( array_filter( array_map( 'sanitize_key', explode( ':', $post_type ) ) ) ); |
| 73 |
} |
| 74 |
} |
| 75 |
// phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 76 |
|
| 77 |
return $intent; |
| 78 |
} |
| 79 |
} |
| 80 |
|