| @@ -1,0 +1,433 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Admin Licenses Page | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Admin\Licenses | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Setup the default parameters to license meta boxes | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + * | |
| 17 | + * @param array $meta_boxes | |
| 18 | + * | |
| 19 | + * @return array | |
| 20 | + */ | |
| 21 | +function automatorwp_licenses_meta_boxes_params( $meta_boxes ) { | |
| 22 | + | |
| 23 | + // Loop settings section meta boxes | |
| 24 | + foreach( $meta_boxes as $meta_box_id => $meta_box ) { | |
| 25 | + | |
| 26 | + // Only add settings meta box if has fields | |
| 27 | + if( isset( $meta_box['fields'] ) && ! empty( $meta_box['fields'] ) ) { | |
| 28 | + | |
| 29 | + // Loop meta box fields | |
| 30 | + foreach( $meta_box['fields'] as $field_id => $field ) { | |
| 31 | + | |
| 32 | + // Update edd_license fields with default parameters to the AutomatorWP server | |
| 33 | + if( $field['type'] !== 'edd_license' ) { | |
| 34 | + continue; | |
| 35 | + } | |
| 36 | + | |
| 37 | + // if not server provider, then add AutomatorWP server | |
| 38 | + if( ! isset( $field['server'] ) ) { | |
| 39 | + $field['server'] = 'https://automatorwp.com/edd-sl-api'; | |
| 40 | + } | |
| 41 | + | |
| 42 | + // Check if is a AutomatorWP hosted plugin | |
| 43 | + if( $field['server'] !== 'https://automatorwp.com/edd-sl-api' ) { | |
| 44 | + continue; | |
| 45 | + } | |
| 46 | + | |
| 47 | + // Renew link | |
| 48 | + $field['renew_license_link'] = 'https://automatorwp.com/renew-a-license'; | |
| 49 | + $field['license_management_link'] = 'https://automatorwp.com/account'; | |
| 50 | + $field['contact_link'] = 'https://automatorwp.com/contact'; | |
| 51 | + | |
| 52 | + // Before field row hook to render some extra information | |
| 53 | + $field['before_row'] = 'automatorwp_license_field_before'; | |
| 54 | + | |
| 55 | + // Update the field definition | |
| 56 | + $meta_boxes[$meta_box_id]['fields'][$field_id] = $field; | |
| 57 | + $meta_boxes[$meta_box_id]['priority'] = 'high'; // Fixes issue with CMB2 2.9.0 | |
| 58 | + | |
| 59 | + } | |
| 60 | + | |
| 61 | + } | |
| 62 | + | |
| 63 | + } | |
| 64 | + | |
| 65 | + return $meta_boxes; | |
| 66 | + | |
| 67 | +} | |
| 68 | +add_filter( 'automatorwp_licenses_meta_boxes', 'automatorwp_licenses_meta_boxes_params', 9999 ); | |
| 69 | + | |
| 70 | +/** | |
| 71 | + * Setup the thumbnail to license meta boxes | |
| 72 | + * | |
| 73 | + * @since 1.0.0 | |
| 74 | + * | |
| 75 | + * @param array $meta_boxes | |
| 76 | + * | |
| 77 | + * @return array | |
| 78 | + */ | |
| 79 | +function automatorwp_licenses_meta_boxes_thumbnails( $meta_boxes ) { | |
| 80 | + | |
| 81 | + // Check if we are on the licenses page to prevent API calls outside this page | |
| 82 | + if( ! isset( $_GET['page'] ) ) { | |
| 83 | + return $meta_boxes; | |
| 84 | + } | |
| 85 | + | |
| 86 | + if( $_GET['page'] !== 'automatorwp_licenses' ) { | |
| 87 | + return $meta_boxes; | |
| 88 | + } | |
| 89 | + | |
| 90 | + // Get our add-ons | |
| 91 | + $plugins = automatorwp_plugins_api(); | |
| 92 | + | |
| 93 | + // Loop settings section meta boxes | |
| 94 | + foreach( $meta_boxes as $meta_box_id => $meta_box ) { | |
| 95 | + | |
| 96 | + // Only add settings meta box if has fields | |
| 97 | + if( isset( $meta_box['fields'] ) && ! empty( $meta_box['fields'] ) ) { | |
| 98 | + | |
| 99 | + // Loop meta box fields | |
| 100 | + foreach( $meta_box['fields'] as $field_id => $field ) { | |
| 101 | + | |
| 102 | + // Update edd_license fields with default parameters to the AutomatorWP server | |
| 103 | + if( $field['type'] !== 'edd_license' ) { | |
| 104 | + continue; | |
| 105 | + } | |
| 106 | + | |
| 107 | + // if not server provider, then add AutomatorWP server | |
| 108 | + if( ! isset( $field['server'] ) ) { | |
| 109 | + $field['server'] = 'https://automatorwp.com/edd-sl-api'; | |
| 110 | + } | |
| 111 | + | |
| 112 | + // Check if is an AutomatorWP hosted plugin | |
| 113 | + if( $field['server'] !== 'https://automatorwp.com/edd-sl-api' ) { | |
| 114 | + continue; | |
| 115 | + } | |
| 116 | + | |
| 117 | + // Try to find the plugin thumbnail from plugins API | |
| 118 | + if ( ! is_wp_error( $plugins ) && isset( $field['file'] ) && ! isset( $field['thumbnail'] ) ) { | |
| 119 | + | |
| 120 | + foreach ( $plugins as $plugin ) { | |
| 121 | + | |
| 122 | + $slug = basename( $field['file'], '.php' ); | |
| 123 | + | |
| 124 | + if( $slug === 'automatorwp-' . $plugin->info->slug ) { | |
| 125 | + $field['thumbnail'] = $plugin->info->thumbnail; | |
| 126 | + // Thumbnail found so exit loop | |
| 127 | + break; | |
| 128 | + } | |
| 129 | + | |
| 130 | + } | |
| 131 | + | |
| 132 | + } | |
| 133 | + | |
| 134 | + // Update the field definition | |
| 135 | + $meta_boxes[$meta_box_id]['fields'][$field_id] = $field; | |
| 136 | + | |
| 137 | + } | |
| 138 | + | |
| 139 | + } | |
| 140 | + | |
| 141 | + } | |
| 142 | + | |
| 143 | + return $meta_boxes; | |
| 144 | + | |
| 145 | +} | |
| 146 | +add_filter( 'automatorwp_licenses_meta_boxes', 'automatorwp_licenses_meta_boxes_thumbnails', 99999 ); | |
| 147 | + | |
| 148 | +/** | |
| 149 | + * Register licenses page. | |
| 150 | + * | |
| 151 | + * @since 1.0.0 | |
| 152 | + * | |
| 153 | + * @return void | |
| 154 | + */ | |
| 155 | +function automatorwp_register_licenses_page() { | |
| 156 | + | |
| 157 | + $tabs = array(); | |
| 158 | + $boxes = array(); | |
| 159 | + | |
| 160 | + $meta_boxes = array(); | |
| 161 | + | |
| 162 | + /** | |
| 163 | + * Filter: automatorwp_licenses_meta_boxes | |
| 164 | + * | |
| 165 | + * @param array $meta_boxes | |
| 166 | + * | |
| 167 | + * @return array | |
| 168 | + */ | |
| 169 | + $meta_boxes = apply_filters( "automatorwp_licenses_meta_boxes", $meta_boxes ); | |
| 170 | + | |
| 171 | + if( ! empty( $meta_boxes ) ) { | |
| 172 | + | |
| 173 | + // Loop licenses section meta boxes | |
| 174 | + foreach( $meta_boxes as $meta_box_id => $meta_box ) { | |
| 175 | + | |
| 176 | + // Check meta box tabs | |
| 177 | + if( isset( $meta_box['tabs'] ) && ! empty( $meta_box['tabs'] ) ) { | |
| 178 | + | |
| 179 | + // Loop meta box tabs | |
| 180 | + foreach( $meta_box['tabs'] as $tab_id => $tab ) { | |
| 181 | + | |
| 182 | + $tab['id'] = $tab_id; | |
| 183 | + | |
| 184 | + $meta_box['tabs'][$tab_id] = $tab; | |
| 185 | + | |
| 186 | + } | |
| 187 | + | |
| 188 | + } | |
| 189 | + | |
| 190 | + // Only add licenses meta box if has fields | |
| 191 | + if( isset( $meta_box['fields'] ) && ! empty( $meta_box['fields'] ) ) { | |
| 192 | + | |
| 193 | + // Loop meta box fields | |
| 194 | + foreach( $meta_box['fields'] as $field_id => $field ) { | |
| 195 | + | |
| 196 | + $field['id'] = $field_id; | |
| 197 | + | |
| 198 | + // If license field without name, set "License" as standard name | |
| 199 | + if( ! isset( $field['name'] ) && $field['type'] === 'edd_license' ) { | |
| 200 | + $field['name'] = __( 'License', 'automatorwp' ); | |
| 201 | + } | |
| 202 | + | |
| 203 | + // Support for group fields | |
| 204 | + if( isset( $field['fields'] ) && is_array( $field['fields'] ) ) { | |
| 205 | + | |
| 206 | + foreach( $field['fields'] as $group_field_id => $group_field ) { | |
| 207 | + | |
| 208 | + $field['fields'][$group_field_id]['id'] = $group_field_id; | |
| 209 | + | |
| 210 | + } | |
| 211 | + | |
| 212 | + } | |
| 213 | + | |
| 214 | + // Register custom update message on plugins menu | |
| 215 | + if( isset( $field['file'] ) && ! is_multisite() ) { | |
| 216 | + | |
| 217 | + $plugin_file = plugin_basename( $field['file'] ); | |
| 218 | + | |
| 219 | + // Register custom plugin row for licensed plugins to update package if an active license exists | |
| 220 | + add_action( "after_plugin_row_$plugin_file", 'automatorwp_license_plugin_update_row', 5, 2 ); | |
| 221 | + add_action( "in_plugin_update_message-$plugin_file", 'automatorwp_license_in_plugin_update_message', 10, 2 ); | |
| 222 | + | |
| 223 | + } | |
| 224 | + | |
| 225 | + $meta_box['fields'][$field_id] = $field; | |
| 226 | + | |
| 227 | + } | |
| 228 | + | |
| 229 | + $meta_box['id'] = $meta_box_id; | |
| 230 | + | |
| 231 | + $meta_box['display_cb'] = false; | |
| 232 | + $meta_box['admin_menu_hook'] = false; | |
| 233 | + | |
| 234 | + $meta_box['show_on'] = array( | |
| 235 | + 'key' => 'options-page', | |
| 236 | + 'value' => array( 'automatorwp_licenses' ), | |
| 237 | + 'option_key' => 'automatorwp_settings', | |
| 238 | + ); | |
| 239 | + | |
| 240 | + $box = new_cmb2_box( $meta_box ); | |
| 241 | + | |
| 242 | + $box->object_type( 'options-page' ); | |
| 243 | + | |
| 244 | + $boxes[] = $box; | |
| 245 | + | |
| 246 | + } | |
| 247 | + } | |
| 248 | + } | |
| 249 | + | |
| 250 | + try { | |
| 251 | + // Create the options page | |
| 252 | + new Cmb2_Metatabs_Options( array( | |
| 253 | + 'key' => 'automatorwp_settings', | |
| 254 | + 'class' => 'automatorwp-page', | |
| 255 | + 'title' => __( 'Licenses', 'automatorwp' ), | |
| 256 | + 'topmenu' => 'automatorwp', | |
| 257 | + 'view_capability' => 'manage_options', | |
| 258 | + 'cols' => 1, | |
| 259 | + 'boxes' => $boxes, | |
| 260 | + //'tabs' => $tabs, | |
| 261 | + 'menuargs' => array( | |
| 262 | + 'menu_title' => __( 'Licenses', 'automatorwp' ), | |
| 263 | + 'menu_slug' => 'automatorwp_licenses', | |
| 264 | + ), | |
| 265 | + 'savetxt' => __( 'Save Changes', 'automatorwp' ), | |
| 266 | + 'resettxt' => false, | |
| 267 | + ) ); | |
| 268 | + } catch ( Exception $e ) { | |
| 269 | + | |
| 270 | + } | |
| 271 | + | |
| 272 | +} | |
| 273 | +add_action( 'cmb2_admin_init', 'automatorwp_register_licenses_page' ); | |
| 274 | + | |
| 275 | +/** | |
| 276 | + * License field thumbnail. | |
| 277 | + * | |
| 278 | + * @since 1.0.0 | |
| 279 | + * | |
| 280 | + * @param array $field_args Current field args | |
| 281 | + * @param CMB2_Field $field Current field object | |
| 282 | + */ | |
| 283 | +function automatorwp_license_field_before( $field_args, $field ) { | |
| 284 | + | |
| 285 | + if( isset( $field_args['thumbnail'] ) && ! empty( $field_args['thumbnail'] ) ) : ?> | |
| 286 | + | |
| 287 | + <div class="automatorwp-license-thumbnail"> | |
| 288 | + <img src="<?php echo $field_args['thumbnail']; ?>" alt="<?php echo $field_args['item_name']; ?>"> | |
| 289 | + </div> | |
| 290 | + | |
| 291 | + <?php endif; | |
| 292 | + | |
| 293 | +} | |
| 294 | + | |
| 295 | +/** | |
| 296 | + * Force package and download link update for licensed plugins. | |
| 297 | + * | |
| 298 | + * @since 1.0.0 | |
| 299 | + * | |
| 300 | + * @param string $file Plugin file | |
| 301 | + * @param array $plugin_data An array of plugin data. | |
| 302 | + */ | |
| 303 | +function automatorwp_license_plugin_update_row( $file, $plugin_data ) { | |
| 304 | + | |
| 305 | + $update_cache = get_site_transient( 'update_plugins' ); | |
| 306 | + | |
| 307 | + if ( ! isset( $update_cache->response[ $file ] ) ) { | |
| 308 | + return; | |
| 309 | + } | |
| 310 | + | |
| 311 | + $response = $update_cache->response[ $file ]; | |
| 312 | + | |
| 313 | + // If there is not a package link, then try to update it | |
| 314 | + if ( empty( $response->package ) ) { | |
| 315 | + | |
| 316 | + // Turn plugin slug like 'plugin-slug' to 'plugin_slug' | |
| 317 | + $slug = str_replace( '-', '_', $response->slug ); | |
| 318 | + | |
| 319 | + // Get the stored license key | |
| 320 | + $license = automatorwp_get_option( $slug . '_license', '' ); | |
| 321 | + | |
| 322 | + // Check the license status | |
| 323 | + $license_status = rgc_cmb2_edd_license_status( $license ); | |
| 324 | + | |
| 325 | + if( $license_status === 'valid' ) { | |
| 326 | + | |
| 327 | + // Make a new request to the API to check package and download link | |
| 328 | + $api_params = array( | |
| 329 | + 'edd_action' => 'get_version', | |
| 330 | + 'license' => $license, | |
| 331 | + 'item_name' => $response->name, | |
| 332 | + 'slug' => $response->slug, | |
| 333 | + 'url' => home_url(), | |
| 334 | + ); | |
| 335 | + | |
| 336 | + $api_request = wp_remote_post( 'https://automatorwp.com/edd-sl-api', array( 'timeout' => 15, 'sslverify' => true, 'body' => $api_params ) ); | |
| 337 | + | |
| 338 | + if ( ! is_wp_error( $api_request ) ) { | |
| 339 | + | |
| 340 | + // Decode the API response | |
| 341 | + $version_info = json_decode( wp_remote_retrieve_body( $api_request ) ); | |
| 342 | + | |
| 343 | + // If package link provided, update it | |
| 344 | + if( ! empty( $version_info->package ) ) { | |
| 345 | + $update_cache->response[ $file ]->package = $version_info->package; | |
| 346 | + } | |
| 347 | + | |
| 348 | + // If download link provided, update it | |
| 349 | + if( ! empty( $version_info->download_link ) ) { | |
| 350 | + $update_cache->response[ $file ]->download_link = $version_info->download_link; | |
| 351 | + } | |
| 352 | + | |
| 353 | + // Update site transient with updated data | |
| 354 | + set_site_transient( 'update_plugins', $update_cache ); | |
| 355 | + | |
| 356 | + } | |
| 357 | + | |
| 358 | + } | |
| 359 | + | |
| 360 | + } | |
| 361 | + | |
| 362 | +} | |
| 363 | + | |
| 364 | +/** | |
| 365 | + * Advice to user about invalid license keys | |
| 366 | + * | |
| 367 | + * @param array $plugin_data | |
| 368 | + * @param array $response | |
| 369 | + */ | |
| 370 | +function automatorwp_license_in_plugin_update_message( $plugin_data, $response ) { | |
| 371 | + | |
| 372 | + // Turn plugin slug like 'plugin-slug' to 'plugin_slug' | |
| 373 | + $slug = str_replace( '-', '_', $response->slug ); | |
| 374 | + | |
| 375 | + // Get the stored license key | |
| 376 | + $license = automatorwp_get_option( $slug . '_license', '' ); | |
| 377 | + | |
| 378 | + // Check the license status | |
| 379 | + $license_status = rgc_cmb2_edd_license_status( $license ); | |
| 380 | + | |
| 381 | + if( $license_status !== 'valid' ) { | |
| 382 | + | |
| 383 | + echo ' <strong><a href="' . esc_url( admin_url( 'admin.php?page=automatorwp_licenses' ) ) . '">' . __( 'Enter valid license key for automatic updates.', 'automatorwp' ) . '</a></strong>'; | |
| 384 | + | |
| 385 | + } | |
| 386 | + | |
| 387 | +} | |
| 388 | + | |
| 389 | +/** | |
| 390 | + * Filter to set correct option key for our licenses | |
| 391 | + * | |
| 392 | + * @since 1.0.0 | |
| 393 | + * | |
| 394 | + * @param string $option_key | |
| 395 | + * @param CMB2 $cmb | |
| 396 | + * | |
| 397 | + * @return string | |
| 398 | + */ | |
| 399 | +function automatorwp_licenses_option_key( $option_key, $cmb ) { | |
| 400 | + | |
| 401 | + if( isset( $cmb->meta_box['show_on'] ) && isset( $cmb->meta_box['show_on']['option_key'] ) ) { | |
| 402 | + return $cmb->meta_box['show_on']['option_key']; | |
| 403 | + } | |
| 404 | + | |
| 405 | + return $option_key; | |
| 406 | + | |
| 407 | +} | |
| 408 | +add_filter( 'cmb2_edd_license_option_key', 'automatorwp_licenses_option_key', 10, 2 ); | |
| 409 | + | |
| 410 | +/** | |
| 411 | + * Before licenses form | |
| 412 | + * | |
| 413 | + * @since 1.0.0 | |
| 414 | + * | |
| 415 | + * @param string $filterable | |
| 416 | + * @param string $page | |
| 417 | + * | |
| 418 | + * @return string | |
| 419 | + */ | |
| 420 | +function automatorwp_licenses_before_form( $filterable, $page ) { | |
| 421 | + | |
| 422 | + if( $page !== 'automatorwp_licenses' ) { | |
| 423 | + return $filterable; | |
| 424 | + } | |
| 425 | + | |
| 426 | + $output = '<em class="automatorwp-licenses-intructions">' | |
| 427 | + . sprintf( __( 'Looking to install a pro add-on? Check the <a href="%s" target="_blank">installation instructions</a>.', 'automatorwp' ), 'https://automatorwp.com/docs/getting-started/installing-pro-add-ons/' ) | |
| 428 | + . '</em>'; | |
| 429 | + | |
| 430 | + return $filterable . $output; | |
| 431 | + | |
| 432 | +} | |
| 433 | +add_filter( 'cmb2metatabs_before_form', 'automatorwp_licenses_before_form', 10, 2 ); | |