| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Admin Refresh Resources class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Registers scripts, which run when a Refresh button is clicked, to refresh resources |
| 11 |
* asynchronously whilst editing a Page, Post or Category. |
| 12 |
* |
| 13 |
* @package ConvertKit |
| 14 |
* @author ConvertKit |
| 15 |
*/ |
| 16 |
class ConvertKit_Admin_Refresh_Resources { |
| 17 |
|
| 18 |
/** |
| 19 |
* Registers action and filter hooks. |
| 20 |
* |
| 21 |
* @since 1.9.8.0 |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
|
| 25 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 26 |
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 27 |
|
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Register REST API routes. |
| 32 |
* |
| 33 |
* @since 3.1.0 |
| 34 |
*/ |
| 35 |
public function register_routes() { |
| 36 |
|
| 37 |
// Register route to return all blocks registered by the Plugin. |
| 38 |
register_rest_route( |
| 39 |
'kit/v1', |
| 40 |
'/resources/refresh/(?P<resource>[a-zA-Z0-9-_]+)', |
| 41 |
array( |
| 42 |
'methods' => WP_REST_Server::CREATABLE, |
| 43 |
'args' => array( |
| 44 |
// Resource: Validate resource is included in the request, a valid resource |
| 45 |
// and sanitize the resource. |
| 46 |
'resource' => array( |
| 47 |
'required' => true, |
| 48 |
'validate_callback' => function ( $param ) { |
| 49 |
|
| 50 |
return is_string( $param ) && in_array( $param, array( 'forms', 'landing_pages', 'tags', 'posts', 'products', 'restrict_content' ), true ); |
| 51 |
|
| 52 |
}, |
| 53 |
'sanitize_callback' => 'sanitize_text_field', |
| 54 |
), |
| 55 |
), |
| 56 |
'callback' => array( $this, 'refresh_resources' ), |
| 57 |
|
| 58 |
// Only refresh resources for users who can edit posts. |
| 59 |
'permission_callback' => function () { |
| 60 |
return current_user_can( 'edit_posts' ); |
| 61 |
}, |
| 62 |
) |
| 63 |
); |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Refreshes resources (forms, landing pages or tags) from the API, returning them as a JSON string. |
| 69 |
* |
| 70 |
* @since 1.9.8.0 |
| 71 |
* |
| 72 |
* @param WP_REST_Request $request Request object. |
| 73 |
* @return WP_REST_Response|WP_Error Response object. |
| 74 |
*/ |
| 75 |
public function refresh_resources( $request ) { |
| 76 |
|
| 77 |
// Get resource type. |
| 78 |
$resource = $request->get_param( 'resource' ); |
| 79 |
|
| 80 |
// Fetch resources. |
| 81 |
switch ( $resource ) { |
| 82 |
case 'forms': |
| 83 |
$forms = new ConvertKit_Resource_Forms( 'user_refresh_resource' ); |
| 84 |
$results = $forms->refresh(); |
| 85 |
|
| 86 |
// Only return non-legacy forms. |
| 87 |
if ( ! is_wp_error( $results ) ) { |
| 88 |
$non_legacy = $forms->get_non_legacy(); |
| 89 |
$results = is_array( $non_legacy ) ? $non_legacy : array(); |
| 90 |
} |
| 91 |
break; |
| 92 |
|
| 93 |
case 'landing_pages': |
| 94 |
$landing_pages = new ConvertKit_Resource_Landing_Pages( 'user_refresh_resource' ); |
| 95 |
$results = $landing_pages->refresh(); |
| 96 |
|
| 97 |
// Only return non-legacy landing pages. |
| 98 |
if ( ! is_wp_error( $results ) ) { |
| 99 |
$non_legacy = $landing_pages->get_non_legacy(); |
| 100 |
$results = is_array( $non_legacy ) ? $non_legacy : array(); |
| 101 |
} |
| 102 |
break; |
| 103 |
|
| 104 |
case 'tags': |
| 105 |
$tags = new ConvertKit_Resource_Tags( 'user_refresh_resource' ); |
| 106 |
$results = $tags->refresh(); |
| 107 |
break; |
| 108 |
|
| 109 |
case 'posts': |
| 110 |
$posts = new ConvertKit_Resource_Posts( 'user_refresh_resource' ); |
| 111 |
$results = $posts->refresh(); |
| 112 |
break; |
| 113 |
|
| 114 |
case 'products': |
| 115 |
$products = new ConvertKit_Resource_Products( 'user_refresh_resource' ); |
| 116 |
$results = $products->refresh(); |
| 117 |
break; |
| 118 |
|
| 119 |
case 'restrict_content': |
| 120 |
// Fetch Forms. |
| 121 |
$forms = new ConvertKit_Resource_Forms( 'user_refresh_resource' ); |
| 122 |
$results_forms = $forms->refresh(); |
| 123 |
|
| 124 |
// Bail if an error occured. |
| 125 |
if ( is_wp_error( $results_forms ) ) { |
| 126 |
return rest_ensure_response( $results_forms ); |
| 127 |
} |
| 128 |
|
| 129 |
// Only return non-legacy forms. |
| 130 |
$non_legacy_forms = $forms->get_non_legacy(); |
| 131 |
$results_forms = is_array( $non_legacy_forms ) ? $non_legacy_forms : array(); |
| 132 |
|
| 133 |
// Fetch Tags. |
| 134 |
$tags = new ConvertKit_Resource_Tags( 'user_refresh_resource' ); |
| 135 |
$results_tags = $tags->refresh(); |
| 136 |
|
| 137 |
// Bail if an error occured. |
| 138 |
if ( is_wp_error( $results_tags ) ) { |
| 139 |
return rest_ensure_response( $results_tags ); |
| 140 |
} |
| 141 |
|
| 142 |
// Fetch Products. |
| 143 |
$products = new ConvertKit_Resource_Products( 'user_refresh_resource' ); |
| 144 |
$results_products = $products->refresh(); |
| 145 |
|
| 146 |
// Bail if an error occured. |
| 147 |
if ( is_wp_error( $results_products ) ) { |
| 148 |
return rest_ensure_response( $results_products ); |
| 149 |
} |
| 150 |
|
| 151 |
// Return resources. |
| 152 |
return rest_ensure_response( |
| 153 |
array( |
| 154 |
'forms' => array_values( $results_forms ), |
| 155 |
'tags' => array_values( $results_tags ), |
| 156 |
'products' => array_values( $results_products ), |
| 157 |
) |
| 158 |
); |
| 159 |
|
| 160 |
default: |
| 161 |
$results = new WP_Error( |
| 162 |
'convertkit_admin_refresh_resources_error', |
| 163 |
sprintf( |
| 164 |
'Resource type %s is not supported in ConvertKit_Admin_Refresh_Resources class.', |
| 165 |
$resource |
| 166 |
) |
| 167 |
); |
| 168 |
break; |
| 169 |
} |
| 170 |
|
| 171 |
// Bail if an error occured. |
| 172 |
if ( is_wp_error( $results ) ) { |
| 173 |
return rest_ensure_response( $results ); |
| 174 |
} |
| 175 |
|
| 176 |
// Return resources as a zero based sequential array, so that JS retains the order of resources. |
| 177 |
return rest_ensure_response( array_values( $results ) ); |
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Enqueue JavaScript when editing a Page, Post, Custom Post Type or Category. |
| 183 |
* |
| 184 |
* @since 1.9.8.0 |
| 185 |
* |
| 186 |
* @param string $hook Hook. |
| 187 |
*/ |
| 188 |
public function enqueue_scripts( $hook ) { |
| 189 |
|
| 190 |
// Bail if we are not on an Edit or Term screen. |
| 191 |
if ( ! in_array( $hook, array( 'edit.php', 'post-new.php', 'term.php', 'edit-tags.php', 'post.php' ), true ) ) { |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
// Get settings. |
| 196 |
$settings = new ConvertKit_Settings(); |
| 197 |
|
| 198 |
// Bail if no Access Token is defined. |
| 199 |
if ( ! $settings->has_access_and_refresh_token() ) { |
| 200 |
return; |
| 201 |
} |
| 202 |
|
| 203 |
// Enqueue JS to perform AJAX request to refresh resources. |
| 204 |
wp_enqueue_script( 'convertkit-admin-refresh-resources', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/refresh-resources.js', array(), CONVERTKIT_PLUGIN_VERSION, true ); |
| 205 |
wp_localize_script( |
| 206 |
'convertkit-admin-refresh-resources', |
| 207 |
'convertkit_admin_refresh_resources', |
| 208 |
array( |
| 209 |
'ajaxurl' => rest_url( 'kit/v1/resources/refresh/' ), |
| 210 |
'debug' => $settings->debug_enabled(), |
| 211 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 212 |
) |
| 213 |
); |
| 214 |
|
| 215 |
} |
| 216 |
|
| 217 |
} |
| 218 |
|