| 1 |
<?php |
| 2 |
/** |
| 3 |
* RestAPI blocks setup. |
| 4 |
* |
| 5 |
* @copyright (c) 2021, Code Atlantic LLC. |
| 6 |
* @package ContentControl |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ContentControl\Controllers; |
| 10 |
|
| 11 |
defined( 'ABSPATH' ) || exit; |
| 12 |
|
| 13 |
use ContentControl\Base\Controller; |
| 14 |
|
| 15 |
use function ContentControl\check_referrer_is_admin; |
| 16 |
use function ContentControl\is_rest; |
| 17 |
|
| 18 |
/** |
| 19 |
* RestAPI function initialization. |
| 20 |
*/ |
| 21 |
class RestAPI extends Controller { |
| 22 |
/** |
| 23 |
* Initiate rest api integrations. |
| 24 |
*/ |
| 25 |
public function init() { |
| 26 |
add_action( 'rest_api_init', [ $this, 'register_routes' ] ); |
| 27 |
|
| 28 |
// Handle CPT & Taxonomy that are not registered with the `show_in_rest` arg when searching from our settings pages. |
| 29 |
add_filter( 'register_post_type_args', [ $this, 'modify_post_type_show_in_rest' ], 10, 2 ); |
| 30 |
add_filter( 'register_taxonomy_args', [ $this, 'modify_taxonomy_show_in_rest' ], 10, 2 ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Register Rest API routes. |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function register_routes() { |
| 39 |
( new \ContentControl\RestAPI\BlockTypes() )->register_routes(); |
| 40 |
( new \ContentControl\RestAPI\License() )->register_routes(); |
| 41 |
( new \ContentControl\RestAPI\ObjectSearch() )->register_routes(); |
| 42 |
( new \ContentControl\RestAPI\Settings() )->register_routes(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Modify show_in_rest for post types. |
| 47 |
* |
| 48 |
* @param array<string,mixed> $args Post type args. |
| 49 |
* |
| 50 |
* @return array<string,mixed> |
| 51 |
*/ |
| 52 |
public function modify_post_type_show_in_rest( $args ) { |
| 53 |
$include_private = $this->container->get_option( 'includePrivatePostTypes', true ); |
| 54 |
return $this->modify_type_force_show_in_rest( $args, $include_private ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Modify show_in_rest for taxonomies. |
| 59 |
* |
| 60 |
* @param array<string,mixed> $args Taxonomy args. |
| 61 |
* |
| 62 |
* @return array<string,mixed> |
| 63 |
*/ |
| 64 |
public function modify_taxonomy_show_in_rest( $args ) { |
| 65 |
$include_private = $this->container->get_option( 'includePrivateTaxonomies', true ); |
| 66 |
return $this->modify_type_force_show_in_rest( $args, $include_private ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Modify show_in_rest for post types. |
| 71 |
* |
| 72 |
* @param array<string,mixed> $args Post type args. |
| 73 |
* @param boolean $include_private Whether to include private post types. |
| 74 |
* |
| 75 |
* @return array<string,mixed> |
| 76 |
*/ |
| 77 |
public function modify_type_force_show_in_rest( $args, $include_private = false ) { |
| 78 |
if ( ! $this->force_show_in_rest() ) { |
| 79 |
return $args; |
| 80 |
} |
| 81 |
|
| 82 |
// If show_in_rest is already false, return early. |
| 83 |
if ( isset( $args['show_in_rest'] ) && $args['show_in_rest'] ) { |
| 84 |
return $args; |
| 85 |
} |
| 86 |
|
| 87 |
// Check if this is a private taxonomy. |
| 88 |
if ( true !== $args['public'] ) { |
| 89 |
if ( $include_private ) { |
| 90 |
$args['show_in_rest'] = true; // Enable REST API. |
| 91 |
} |
| 92 |
} else { |
| 93 |
$args['show_in_rest'] = true; // Enable REST API. |
| 94 |
} |
| 95 |
|
| 96 |
return $args; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Force show_in_rest to true. |
| 101 |
* |
| 102 |
* @return boolean |
| 103 |
*/ |
| 104 |
public function force_show_in_rest() { |
| 105 |
static $force_show_in_rest; |
| 106 |
|
| 107 |
if ( isset( $force_show_in_rest ) ) { |
| 108 |
return $force_show_in_rest; |
| 109 |
} |
| 110 |
|
| 111 |
$force_show_in_rest = false; |
| 112 |
|
| 113 |
if ( ! is_rest() ) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
// Return false if referrer is not our settings page. /wp-admin/options-general.php?page=content-control-settings. |
| 118 |
if ( ! isset( $_SERVER['HTTP_REFERER'] ) ) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
if ( ! check_referrer_is_admin() ) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
$referrer = sanitize_url( wp_unslash( $_SERVER['HTTP_REFERER'] ) ); |
| 127 |
|
| 128 |
// Check if referrer is our settings page and contains the page=content-control-settings query param. |
| 129 |
if ( strpos( $referrer, 'options-general.php' ) === false ) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
// Check if referrer is our settings page and contains the page=content-control-settings query param. |
| 134 |
if ( strpos( $referrer, 'page=content-control-settings' ) === false ) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
// Check if current user has permission to make plugin settings changes. |
| 139 |
if ( ! current_user_can( $this->container->get_permission( 'manage_settings' ) ) ) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
$force_show_in_rest = true; |
| 144 |
|
| 145 |
return true; |
| 146 |
} |
| 147 |
} |
| 148 |
|