| 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( 'wp_ajax_convertkit_admin_refresh_resources', array( $this, 'refresh_resources' ) ); |
| 26 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 27 |
|
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Refreshes resources (forms, landing pages or tags) from the API, returning them as a JSON string. |
| 32 |
* |
| 33 |
* @since 1.9.8.0 |
| 34 |
*/ |
| 35 |
public function refresh_resources() { |
| 36 |
|
| 37 |
// Check nonce. |
| 38 |
check_ajax_referer( 'convertkit_admin_refresh_resources', 'nonce' ); |
| 39 |
|
| 40 |
// Get resource type. |
| 41 |
$resource = ( isset( $_REQUEST['resource'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['resource'] ) ) : '' ); |
| 42 |
|
| 43 |
// Fetch resources. |
| 44 |
switch ( $resource ) { |
| 45 |
case 'forms': |
| 46 |
$forms = new ConvertKit_Resource_Forms( 'user_refresh_resource' ); |
| 47 |
$results = $forms->refresh(); |
| 48 |
break; |
| 49 |
|
| 50 |
case 'landing_pages': |
| 51 |
$landing_pages = new ConvertKit_Resource_Landing_Pages( 'user_refresh_resource' ); |
| 52 |
$results = $landing_pages->refresh(); |
| 53 |
break; |
| 54 |
|
| 55 |
case 'tags': |
| 56 |
$tags = new ConvertKit_Resource_Tags( 'user_refresh_resource' ); |
| 57 |
$results = $tags->refresh(); |
| 58 |
break; |
| 59 |
|
| 60 |
case 'posts': |
| 61 |
$posts = new ConvertKit_Resource_Posts( 'user_refresh_resource' ); |
| 62 |
$results = $posts->refresh(); |
| 63 |
break; |
| 64 |
|
| 65 |
case 'products': |
| 66 |
$products = new ConvertKit_Resource_Products( 'user_refresh_resource' ); |
| 67 |
$results = $products->refresh(); |
| 68 |
break; |
| 69 |
|
| 70 |
case 'restrict_content': |
| 71 |
// Fetch Tags. |
| 72 |
$tags = new ConvertKit_Resource_Tags( 'user_refresh_resource' ); |
| 73 |
$results_tags = $tags->refresh(); |
| 74 |
|
| 75 |
// Bail if an error occured. |
| 76 |
if ( is_wp_error( $results_tags ) ) { |
| 77 |
wp_send_json_error( $results_tags->get_error_message() ); |
| 78 |
} |
| 79 |
|
| 80 |
// Fetch Products. |
| 81 |
$products = new ConvertKit_Resource_Products( 'user_refresh_resource' ); |
| 82 |
$results_products = $products->refresh(); |
| 83 |
|
| 84 |
// Bail if an error occured. |
| 85 |
if ( is_wp_error( $results_products ) ) { |
| 86 |
wp_send_json_error( $results_products->get_error_message() ); |
| 87 |
} |
| 88 |
|
| 89 |
// Return resources. |
| 90 |
wp_send_json_success( |
| 91 |
array( |
| 92 |
'tags' => array_values( $results_tags ), |
| 93 |
'products' => array_values( $results_products ), |
| 94 |
) |
| 95 |
); |
| 96 |
// no break as wp_send_json_success terminates. |
| 97 |
|
| 98 |
default: |
| 99 |
$results = new WP_Error( |
| 100 |
'convertkit_admin_refresh_resources_error', |
| 101 |
sprintf( |
| 102 |
'Resource type %s is not supported in ConvertKit_Admin_Refresh_Resources class.', |
| 103 |
$resource |
| 104 |
) |
| 105 |
); |
| 106 |
break; |
| 107 |
} |
| 108 |
|
| 109 |
// Bail if an error occured. |
| 110 |
if ( is_wp_error( $results ) ) { |
| 111 |
wp_send_json_error( $results->get_error_message() ); |
| 112 |
} |
| 113 |
|
| 114 |
// Return resources as a zero based sequential array, so that JS retains the order of resources. |
| 115 |
wp_send_json_success( array_values( $results ) ); |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Enqueue JavaScript when editing a Page, Post, Custom Post Type or Category. |
| 121 |
* |
| 122 |
* @since 1.9.8.0 |
| 123 |
* |
| 124 |
* @param string $hook Hook. |
| 125 |
*/ |
| 126 |
public function enqueue_scripts( $hook ) { |
| 127 |
|
| 128 |
// Bail if we are not on an Edit or Term screen. |
| 129 |
if ( ! in_array( $hook, array( 'edit.php', 'post-new.php', 'term.php', 'edit-tags.php', 'post.php' ), true ) ) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
// Get settings. |
| 134 |
$settings = new ConvertKit_Settings(); |
| 135 |
|
| 136 |
// Bail if no Access Token is defined. |
| 137 |
if ( ! $settings->has_access_and_refresh_token() ) { |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
// Enqueue JS to perform AJAX request to refresh resources. |
| 142 |
wp_enqueue_script( 'convertkit-admin-refresh-resources', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/refresh-resources.js', array(), CONVERTKIT_PLUGIN_VERSION, true ); |
| 143 |
wp_localize_script( |
| 144 |
'convertkit-admin-refresh-resources', |
| 145 |
'convertkit_admin_refresh_resources', |
| 146 |
array( |
| 147 |
'action' => 'convertkit_admin_refresh_resources', |
| 148 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 149 |
'debug' => $settings->debug_enabled(), |
| 150 |
'nonce' => wp_create_nonce( 'convertkit_admin_refresh_resources' ), |
| 151 |
) |
| 152 |
); |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
} |
| 157 |
|