From 617c6f2fb7e3e75db665aac71d8512eabd266c56 Mon Sep 17 00:00:00 2001 From: Ben Ritner Date: Wed, 15 Jan 2025 21:56:20 -0700 Subject: [PATCH] Template stuff --- .../class-kadence-blocks-prebuilt-library.php | 338 +++++++++--------- src/plugins/prebuilt-library/cloud-library.js | 1 + src/plugins/prebuilt-library/editor.scss | 6 +- .../single-template-library.js | 17 +- .../prebuilt-library/templates-library.js | 4 +- 5 files changed, 197 insertions(+), 169 deletions(-) diff --git a/includes/class-kadence-blocks-prebuilt-library.php b/includes/class-kadence-blocks-prebuilt-library.php index 4829e9c19..cc582987c 100644 --- a/includes/class-kadence-blocks-prebuilt-library.php +++ b/includes/class-kadence-blocks-prebuilt-library.php @@ -43,6 +43,13 @@ class Kadence_Blocks_Prebuilt_Library { */ private $product_id = ''; + /** + * API product for kadence + * + * @var string + */ + private $product_slug = ''; + /** * API email for kadence * @@ -179,30 +186,30 @@ public static function get_instance() { public function __construct() { if ( is_admin() ) { // Ajax Calls. - add_action( 'wp_ajax_kadence_import_get_prebuilt_data', array( $this, 'prebuilt_data_ajax_callback' ) ); - add_action( 'wp_ajax_kadence_import_reload_prebuilt_data', array( $this, 'prebuilt_data_reload_ajax_callback' ) ); - add_action( 'wp_ajax_kadence_import_get_new_connection_data', array( $this, 'prebuilt_connection_info_ajax_callback' ) ); - add_action( 'wp_ajax_kadence_import_get_prebuilt_templates_data', array( $this, 'prebuilt_templates_data_ajax_callback' ) ); - add_action( 'wp_ajax_kadence_import_reload_prebuilt_templates_data', array( $this, 'prebuilt_templates_data_reload_ajax_callback' ) ); - add_action( 'wp_ajax_kadence_import_get_prebuilt_pages_data', array( $this, 'prebuilt_pages_data_ajax_callback' ) ); - add_action( 'wp_ajax_kadence_import_reload_prebuilt_pages_data', array( $this, 'prebuilt_pages_data_reload_ajax_callback' ) ); - add_action( 'wp_ajax_kadence_import_process_data', array( $this, 'process_data_ajax_callback' ) ); - add_action( 'wp_ajax_kadence_import_process_image_data', array( $this, 'process_image_data_ajax_callback' ) ); - add_action( 'wp_ajax_kadence_import_process_pattern', array( $this, 'process_pattern_ajax_callback' ) ); - add_action( 'wp_ajax_kadence_subscribe_process_data', array( $this, 'process_subscribe_ajax_callback' ) ); + add_action( 'wp_ajax_kadence_import_get_prebuilt_data', [ $this, 'prebuilt_data_ajax_callback' ] ); + add_action( 'wp_ajax_kadence_import_reload_prebuilt_data', [ $this, 'prebuilt_data_reload_ajax_callback' ] ); + add_action( 'wp_ajax_kadence_import_get_new_connection_data', [ $this, 'prebuilt_connection_info_ajax_callback' ] ); + add_action( 'wp_ajax_kadence_import_get_prebuilt_templates_data', [ $this, 'prebuilt_templates_data_ajax_callback' ] ); + add_action( 'wp_ajax_kadence_import_reload_prebuilt_templates_data', [ $this, 'prebuilt_templates_data_reload_ajax_callback' ] ); + add_action( 'wp_ajax_kadence_import_get_prebuilt_pages_data', [ $this, 'prebuilt_pages_data_ajax_callback' ] ); + add_action( 'wp_ajax_kadence_import_reload_prebuilt_pages_data', [ $this, 'prebuilt_pages_data_reload_ajax_callback' ] ); + add_action( 'wp_ajax_kadence_import_process_data', [ $this, 'process_data_ajax_callback' ] ); + add_action( 'wp_ajax_kadence_import_process_image_data', [ $this, 'process_image_data_ajax_callback' ] ); + add_action( 'wp_ajax_kadence_import_process_pattern', [ $this, 'process_pattern_ajax_callback' ] ); + add_action( 'wp_ajax_kadence_subscribe_process_data', [ $this, 'process_subscribe_ajax_callback' ] ); } // Add a cleanup routine. $this->schedule_cleanup(); - add_filter( 'cron_schedules', array( $this, 'add_monthly_to_cron_schedule' ), 10, 1 ); - add_action( 'delete_block_library_folder', array( $this, 'delete_block_library_folder' ) ); + add_filter( 'cron_schedules', [ $this, 'add_monthly_to_cron_schedule' ], 10, 1 ); + add_action( 'delete_block_library_folder', [ $this, 'delete_block_library_folder' ] ); } /** * Get the section data if available locally. */ public function get_section_prebuilt_data( $pro_data ) { - $pro_key = ( isset( $pro_data['api_key'] ) && ! empty( $pro_data['api_key'] ) ? $pro_data['api_key'] : '' ); - $api_email = ( isset( $pro_data['api_email'] ) && ! empty( $pro_data['api_email'] ) ? $pro_data['api_email'] : '' ); + $pro_key = ( isset( $pro_data['api_key'] ) && ! empty( $pro_data['api_key'] ) ? $pro_data['api_key'] : '' ); + $api_email = ( isset( $pro_data['api_email'] ) && ! empty( $pro_data['api_email'] ) ? $pro_data['api_email'] : '' ); $product_id = ( isset( $pro_data['product_id'] ) && ! empty( $pro_data['product_id'] ) ? $pro_data['product_id'] : '' ); if ( empty( $pro_key ) ) { $pro_key = ( isset( $pro_data['ithemes_key'] ) && ! empty( $pro_data['ithemes_key'] ) ? $pro_data['ithemes_key'] : '' ); @@ -210,12 +217,13 @@ public function get_section_prebuilt_data( $pro_data ) { $api_email = 'iThemes'; } } - $this->api_key = $pro_key; - $this->api_email = $api_email; - $this->product_id = $product_id; - $this->package = 'section'; - $this->url = $this->remote_url; - $this->key = 'section'; + $this->api_key = $pro_key; + $this->api_email = $api_email; + $this->product_id = $product_id; + $this->product_slug = ( ! empty( $pro_data['product_slug'] ) ? $pro_data['product_slug'] : '' ); + $this->package = 'section'; + $this->url = $this->remote_url; + $this->key = 'section'; // Do you have the data? $get_data = $this->get_only_local_template_data(); if ( ! $get_data ) { @@ -229,8 +237,8 @@ public function get_section_prebuilt_data( $pro_data ) { * Get the section data if available locally. */ public function get_page_prebuilt_data( $pro_data ) { - $pro_key = ( isset( $pro_data['api_key'] ) && ! empty( $pro_data['api_key'] ) ? $pro_data['api_key'] : '' ); - $api_email = ( isset( $pro_data['api_email'] ) && ! empty( $pro_data['api_email'] ) ? $pro_data['api_email'] : '' ); + $pro_key = ( isset( $pro_data['api_key'] ) && ! empty( $pro_data['api_key'] ) ? $pro_data['api_key'] : '' ); + $api_email = ( isset( $pro_data['api_email'] ) && ! empty( $pro_data['api_email'] ) ? $pro_data['api_email'] : '' ); $product_id = ( isset( $pro_data['product_id'] ) && ! empty( $pro_data['product_id'] ) ? $pro_data['product_id'] : '' ); if ( empty( $pro_key ) ) { $pro_key = ( isset( $pro_data['ithemes_key'] ) && ! empty( $pro_data['ithemes_key'] ) ? $pro_data['ithemes_key'] : '' ); @@ -238,12 +246,13 @@ public function get_page_prebuilt_data( $pro_data ) { $api_email = 'iThemes'; } } - $this->api_key = $pro_key; - $this->api_email = $api_email; - $this->product_id = $product_id; - $this->package = 'pages'; - $this->url = $this->remote_pages_url; - $this->key = 'pages'; + $this->api_key = $pro_key; + $this->api_email = $api_email; + $this->product_id = $product_id; + $this->product_slug = ( ! empty( $pro_data['product_slug'] ) ? $pro_data['product_slug'] : '' ); + $this->package = 'pages'; + $this->url = $this->remote_pages_url; + $this->key = 'pages'; // Do you have the data? $get_data = $this->get_only_local_template_data(); if ( ! $get_data ) { @@ -273,7 +282,7 @@ public function get_only_local_template_data( $skip_local = false ) { */ public function get_template_data( $skip_local = false ) { if ( 'custom' === $this->package ) { - return wp_json_encode( apply_filters( 'kadence_block_library_custom_array', array() ) ); + return wp_json_encode( apply_filters( 'kadence_block_library_custom_array', [] ) ); } // Check if the local data file exists. (true means the file doesn't exist). if ( $skip_local || $this->local_file_exists() ) { @@ -284,9 +293,9 @@ public function get_template_data( $skip_local = false ) { } // // If it's empty lets try to get the data. // if ( '[]' === $this->get_local_template_data_contents() ) { - // if ( $this->create_template_data_file( $skip_local ) ) { - // return $this->get_local_template_data_contents(); - // } + // if ( $this->create_template_data_file( $skip_local ) ) { + // return $this->get_local_template_data_contents(); + // } // } // If it's a Kadence Pattern Hub connect, lets make sure it's within date. if ( 'templates' !== $this->package && 'pages' !== $this->package && 'section' !== $this->package && ! $this->is_template ) { @@ -387,23 +396,24 @@ public function get_local_template_data_contents() { */ public function get_remote_url_contents() { $site_url = \KadenceWP\KadenceBlocks\StellarWP\Uplink\get_original_domain(); - $args = array( + $args = [ 'key' => $this->key, 'site' => $site_url, - ); + ]; if ( 'templates' === $this->package || 'section' === $this->package || 'pages' === $this->package || $this->is_template ) { - $args['api_email'] = $this->api_email; - $args['api_key'] = $this->api_key; + $args['api_email'] = $this->api_email; + $args['api_key'] = $this->api_key; $args['product_id'] = $this->product_id; - $args['site_url'] = $site_url; + $args['product_slug'] = $this->product_id; + $args['site_url'] = $site_url; if ( 'iThemes' === $this->api_email ) { if ( is_callable( 'network_home_url' ) ) { $site_url = network_home_url( '', 'http' ); } else { $site_url = get_bloginfo( 'url' ); } - $site_url = preg_replace( '/^https/', 'http', $site_url ); - $site_url = preg_replace( '|/$|', '', $site_url ); + $site_url = preg_replace( '/^https/', 'http', $site_url ); + $site_url = preg_replace( '|/$|', '', $site_url ); $args['site_url'] = $site_url; } } @@ -414,9 +424,9 @@ public function get_remote_url_contents() { $api_url = add_query_arg( $args, $this->url ); $response = wp_safe_remote_get( $api_url, - array( + [ 'timeout' => 20, - ) + ] ); // Early exit if there was an error. if ( is_wp_error( $response ) ) { @@ -487,11 +497,11 @@ public function prebuilt_connection_info_ajax_callback() { // Verify if the AJAX call is valid (checks nonce and current_user_can). $this->verify_ajax_call(); $this->local_template_data_path = ''; - $this->api_key = empty( $_POST['api_key'] ) ? '' : sanitize_text_field( $_POST['api_key'] ); - $this->api_email = empty( $_POST['api_email'] ) ? '' : sanitize_text_field( $_POST['api_email'] ); - $this->package = empty( $_POST['package'] ) ? 'section' : sanitize_text_field( $_POST['package'] ); - $this->url = empty( $_POST['url'] ) ? '' : rtrim( sanitize_text_field( $_POST['url'] ), '/' ) . '/wp-json/kadence-cloud/v1/info/'; - $this->key = empty( $_POST['key'] ) ? 'section' : sanitize_text_field( $_POST['key'] ); + $this->api_key = empty( $_POST['api_key'] ) ? '' : sanitize_text_field( $_POST['api_key'] ); + $this->api_email = empty( $_POST['api_email'] ) ? '' : sanitize_text_field( $_POST['api_email'] ); + $this->package = empty( $_POST['package'] ) ? 'section' : sanitize_text_field( $_POST['package'] ); + $this->url = empty( $_POST['url'] ) ? '' : rtrim( sanitize_text_field( $_POST['url'] ), '/' ) . '/wp-json/kadence-cloud/v1/info/'; + $this->key = empty( $_POST['key'] ) ? 'section' : sanitize_text_field( $_POST['key'] ); // Do you have the data? $get_data = $this->get_connection_data(); if ( ! $get_data ) { @@ -499,8 +509,8 @@ public function prebuilt_connection_info_ajax_callback() { wp_send_json( esc_html__( 'No Connection data', 'kadence-blocks' ) ); } else { // Sanitize the connection data. - $temp_data = json_decode( $get_data, true ); - $final_data = array(); + $temp_data = json_decode( $get_data, true ); + $final_data = []; $final_data['name'] = ! empty( $temp_data['name'] ) ? sanitize_text_field( $temp_data['name'] ) : ''; $final_data['slug'] = ! empty( $temp_data['slug'] ) ? sanitize_text_field( $temp_data['slug'] ) : ''; $final_data['refresh'] = ! empty( $temp_data['refresh'] ) ? sanitize_text_field( $temp_data['refresh'] ) : ''; @@ -520,17 +530,17 @@ public function prebuilt_connection_info_ajax_callback() { */ public function get_connection_data( $skip_local = false ) { $site_url = \KadenceWP\KadenceBlocks\StellarWP\Uplink\get_original_domain(); - $args = array( + $args = [ 'key' => $this->key, 'site' => $site_url, - ); + ]; // Get the response. $api_url = add_query_arg( $args, $this->url ); $response = wp_safe_remote_get( $api_url, - array( + [ 'timeout' => 20, - ) + ] ); // Early exit if there was an error. if ( is_wp_error( $response ) ) { @@ -558,13 +568,13 @@ public function prebuilt_data_ajax_callback() { // Verify if the AJAX call is valid (checks nonce and current_user_can). $this->verify_ajax_call(); $this->local_template_data_path = ''; - $this->api_key = empty( $_POST['api_key'] ) ? '' : sanitize_text_field( $_POST['api_key'] ); - $this->api_email = empty( $_POST['api_email'] ) ? '' : sanitize_text_field( $_POST['api_email'] ); - $this->product_id = empty( $_POST['product_id'] ) ? '' : sanitize_text_field( $_POST['product_id'] ); - $this->package = empty( $_POST['package'] ) ? 'section' : sanitize_text_field( $_POST['package'] ); - $this->url = empty( $_POST['url'] ) ? $this->remote_url : rtrim( sanitize_text_field( $_POST['url'] ), '/' ) . '/wp-json/kadence-cloud/v1/get/'; - $this->key = isset( $_POST['key'] ) && ! empty( $_POST['key'] ) ? sanitize_text_field( $_POST['key'] ) : 'section'; - $this->is_template = isset( $_POST['is_template'] ) && ! empty( $_POST['is_template'] ) ? true : false; + $this->api_key = empty( $_POST['api_key'] ) ? '' : sanitize_text_field( $_POST['api_key'] ); + $this->api_email = empty( $_POST['api_email'] ) ? '' : sanitize_text_field( $_POST['api_email'] ); + $this->product_id = empty( $_POST['product_id'] ) ? '' : sanitize_text_field( $_POST['product_id'] ); + $this->package = empty( $_POST['package'] ) ? 'section' : sanitize_text_field( $_POST['package'] ); + $this->url = empty( $_POST['url'] ) ? $this->remote_url : rtrim( sanitize_text_field( $_POST['url'] ), '/' ) . '/wp-json/kadence-cloud/v1/get/'; + $this->key = isset( $_POST['key'] ) && ! empty( $_POST['key'] ) ? sanitize_text_field( $_POST['key'] ) : 'section'; + $this->is_template = isset( $_POST['is_template'] ) && ! empty( $_POST['is_template'] ) ? true : false; // Do you have the data? $get_data = $this->get_template_data(); if ( ! $get_data ) { @@ -582,12 +592,13 @@ public function prebuilt_templates_data_ajax_callback() { // Verify if the AJAX call is valid (checks nonce and current_user_can). $this->verify_ajax_call(); $this->local_template_data_path = ''; - $this->api_key = empty( $_POST['api_key'] ) ? '' : sanitize_text_field( $_POST['api_key'] ); - $this->api_email = empty( $_POST['api_email'] ) ? '' : sanitize_text_field( $_POST['api_email'] ); - $this->product_id = empty( $_POST['product_id'] ) ? '' : sanitize_text_field( $_POST['product_id'] ); - $this->package = 'templates'; - $this->url = $this->remote_templates_url; - $this->key = 'blocks'; + $this->api_key = empty( $_POST['api_key'] ) ? '' : sanitize_text_field( $_POST['api_key'] ); + $this->api_email = empty( $_POST['api_email'] ) ? '' : sanitize_text_field( $_POST['api_email'] ); + $this->product_id = empty( $_POST['product_id'] ) ? '' : sanitize_text_field( $_POST['product_id'] ); + $this->product_slug = empty( $_POST['product_slug'] ) ? '' : sanitize_text_field( $_POST['product_slug'] ); + $this->package = 'templates'; + $this->url = $this->remote_templates_url; + $this->key = 'blocks'; // Do you have the data? $get_data = $this->get_template_data(); if ( ! $get_data ) { @@ -605,12 +616,13 @@ public function prebuilt_pages_data_ajax_callback() { // Verify if the AJAX call is valid (checks nonce and current_user_can). $this->verify_ajax_call(); $this->local_pages_data_path = ''; - $this->api_key = empty( $_POST['api_key'] ) ? '' : sanitize_text_field( $_POST['api_key'] ); - $this->api_email = empty( $_POST['api_email'] ) ? '' : sanitize_text_field( $_POST['api_email'] ); - $this->product_id = empty( $_POST['product_id'] ) ? '' : sanitize_text_field( $_POST['product_id'] ); - $this->package = 'pages'; - $this->url = $this->remote_pages_url; - $this->key = 'pages'; + $this->api_key = empty( $_POST['api_key'] ) ? '' : sanitize_text_field( $_POST['api_key'] ); + $this->api_email = empty( $_POST['api_email'] ) ? '' : sanitize_text_field( $_POST['api_email'] ); + $this->product_id = empty( $_POST['product_id'] ) ? '' : sanitize_text_field( $_POST['product_id'] ); + $this->product_slug = empty( $_POST['product_slug'] ) ? '' : sanitize_text_field( $_POST['product_slug'] ); + $this->package = 'pages'; + $this->url = $this->remote_pages_url; + $this->key = 'pages'; // Do you have the data? $get_data = $this->get_template_data(); if ( ! $get_data ) { @@ -633,16 +645,17 @@ public function prebuilt_templates_data_reload_ajax_callback() { // Verify if the AJAX call is valid (checks nonce and current_user_can). $this->verify_ajax_call(); $this->local_template_data_path = ''; - $this->api_key = empty( $_POST['api_key'] ) ? '' : sanitize_text_field( $_POST['api_key'] ); - $this->api_email = empty( $_POST['api_email'] ) ? '' : sanitize_text_field( $_POST['api_email'] ); - $this->product_id = empty( $_POST['product_id'] ) ? '' : sanitize_text_field( $_POST['product_id'] ); - $this->package = 'templates'; - $this->url = $this->remote_templates_url; - $this->key = 'blocks'; + $this->api_key = empty( $_POST['api_key'] ) ? '' : sanitize_text_field( $_POST['api_key'] ); + $this->api_email = empty( $_POST['api_email'] ) ? '' : sanitize_text_field( $_POST['api_email'] ); + $this->product_id = empty( $_POST['product_id'] ) ? '' : sanitize_text_field( $_POST['product_id'] ); + $this->product_slug = empty( $_POST['product_slug'] ) ? '' : sanitize_text_field( $_POST['product_slug'] ); + $this->package = 'templates'; + $this->url = $this->remote_templates_url; + $this->key = 'blocks'; - //$removed = $this->delete_block_library_folder(); + // $removed = $this->delete_block_library_folder(); // if ( ! $removed ) { - // wp_send_json_error( 'failed_to_flush' ); + // wp_send_json_error( 'failed_to_flush' ); // } // Do you have the data? $get_data = $this->get_template_data( true ); @@ -667,16 +680,17 @@ public function prebuilt_pages_data_reload_ajax_callback() { // Verify if the AJAX call is valid (checks nonce and current_user_can). $this->verify_ajax_call(); $this->local_pages_data_path = ''; - $this->api_key = empty( $_POST['api_key'] ) ? '' : sanitize_text_field( $_POST['api_key'] ); - $this->api_email = empty( $_POST['api_email'] ) ? '' : sanitize_text_field( $_POST['api_email'] ); - $this->product_id = empty( $_POST['product_id'] ) ? '' : sanitize_text_field( $_POST['product_id'] ); - $this->package = 'pages'; - $this->url = $this->remote_pages_url; - $this->key = 'pages'; + $this->api_key = empty( $_POST['api_key'] ) ? '' : sanitize_text_field( $_POST['api_key'] ); + $this->api_email = empty( $_POST['api_email'] ) ? '' : sanitize_text_field( $_POST['api_email'] ); + $this->product_id = empty( $_POST['product_id'] ) ? '' : sanitize_text_field( $_POST['product_id'] ); + $this->product_slug = empty( $_POST['product_slug'] ) ? '' : sanitize_text_field( $_POST['product_slug'] ); + $this->package = 'pages'; + $this->url = $this->remote_pages_url; + $this->key = 'pages'; - //$removed = $this->delete_block_library_folder(); + // $removed = $this->delete_block_library_folder(); // if ( ! $removed ) { - // wp_send_json_error( 'failed_to_flush' ); + // wp_send_json_error( 'failed_to_flush' ); // } // Do you have the data? $get_data = $this->get_template_data( true ); @@ -698,27 +712,27 @@ public function process_subscribe_ajax_callback() { $email = empty( $_POST['email'] ) ? '' : sanitize_text_field( $_POST['email'] ); // Do you have the data? if ( $email && is_email( $email ) && filter_var( $email, FILTER_VALIDATE_EMAIL ) ) { - list( $user, $domain ) = explode( '@', $email ); + list( $user, $domain ) = explode( '@', $email ); list( $pre_domain, $post_domain ) = explode( '.', $domain ); - $spell_issue_domains = array( 'gmaiil', 'gmai', 'gmaill' ); - $spell_issue_domain_ends = array( 'local', 'comm', 'orgg', 'cmm' ); + $spell_issue_domains = [ 'gmaiil', 'gmai', 'gmaill' ]; + $spell_issue_domain_ends = [ 'local', 'comm', 'orgg', 'cmm' ]; if ( in_array( $pre_domain, $spell_issue_domain_ends, true ) ) { return wp_send_json( 'emailDomainPreError' ); } if ( in_array( $post_domain, $spell_issue_domain_ends, true ) ) { return wp_send_json( 'emailDomainPostError' ); } - $args = array( - 'email' => $email, - 'tag' => 'wire', - ); + $args = [ + 'email' => $email, + 'tag' => 'wire', + ]; // Get the response. $api_url = add_query_arg( $args, 'https://www.kadencewp.com/kadence-blocks/wp-json/kadence-subscribe/v1/subscribe/' ); $response = wp_safe_remote_get( $api_url, - array( + [ 'timeout' => 20, - ) + ] ); // Early exit if there was an error. if ( is_wp_error( $response ) ) { @@ -761,9 +775,9 @@ public function process_image_data_ajax_callback() { // Verify if the AJAX call is valid (checks nonce and current_user_can). $this->verify_ajax_call(); - $content = empty( $_POST['import_content'] ) ? '' : stripslashes( $_POST['import_content'] ); - $image_library = empty( $_POST['image_library'] ) ? '' : json_decode( $_POST['image_library'], true ); - $data = $this->process_image_content( $content, $image_library ); + $content = empty( $_POST['import_content'] ) ? '' : stripslashes( $_POST['import_content'] ); + $image_library = empty( $_POST['image_library'] ) ? '' : json_decode( $_POST['image_library'], true ); + $data = $this->process_image_content( $content, $image_library ); if ( ! $data ) { // Send JSON Error response to the AJAX call. wp_send_json( esc_html__( 'No data', 'kadence-blocks' ) ); @@ -788,8 +802,8 @@ public function process_image_content( $content = '', $image_library = '' ) { return $content; } - $map_urls = array(); - $image_urls = array(); + $map_urls = []; + $image_urls = []; // Find all the images. foreach ( $all_urls as $key => $link ) { if ( $this->check_for_image( $link ) ) { @@ -807,18 +821,18 @@ public function process_image_content( $content = '', $image_library = '' ) { if ( ! empty( $image_urls ) ) { foreach ( $image_urls as $key => $image_url ) { // Download remote image. - $image = array( + $image = [ 'url' => $image_url, 'id' => 0, - ); + ]; if ( substr( $image_url, 0, strlen( 'https://images.pexels.com' ) ) === 'https://images.pexels.com' ) { $image_data = $this->get_image_info( $image_library, $image_url ); if ( $image_data ) { - $image['alt'] = $image_data['alt']; - $image['photographer'] = $image_data['photographer']; - $image['photographer_url'] = $image_data['photographer_url']; - $image['alt'] = $image_data['alt']; - $image['title'] = __( 'Photo by', 'kadence-blocks' ) . ' ' . $image_data['photographer']; + $image['alt'] = $image_data['alt']; + $image['photographer'] = $image_data['photographer']; + $image['photographer_url'] = $image_data['photographer_url']; + $image['alt'] = $image_data['alt']; + $image['title'] = __( 'Photo by', 'kadence-blocks' ) . ' ' . $image_data['photographer']; } } $downloaded_image = $this->import_image( $image ); @@ -846,11 +860,11 @@ public function process_data_ajax_callback() { $import_type = empty( $_POST['import_type'] ) ? 'pattern' : sanitize_text_field( $_POST['import_type'] ); $import_id = empty( $_POST['import_item_id'] ) ? '' : sanitize_text_field( $_POST['import_item_id'] ); $import_style = empty( $_POST['import_style'] ) ? 'normal' : sanitize_text_field( $_POST['import_style'] ); - $this->api_key = empty( $_POST['api_key'] ) ? '' : sanitize_text_field( $_POST['api_key'] ); - $this->package = empty( $_POST['package'] ) ? 'section' : sanitize_text_field( $_POST['package'] ); - $this->url = empty( $_POST['url'] ) ? $this->remote_url : rtrim( sanitize_text_field( $_POST['url'] ), '/' ) . '/wp-json/kadence-cloud/v1/get/'; - $this->key = isset( $_POST['key'] ) && ! empty( $_POST['key'] ) ? sanitize_text_field( $_POST['key'] ) : 'section'; - $data = $this->process_content( $data, $import_library, $import_type, $import_id, $import_style ); + $this->api_key = empty( $_POST['api_key'] ) ? '' : sanitize_text_field( $_POST['api_key'] ); + $this->package = empty( $_POST['package'] ) ? 'section' : sanitize_text_field( $_POST['package'] ); + $this->url = empty( $_POST['url'] ) ? $this->remote_url : rtrim( sanitize_text_field( $_POST['url'] ), '/' ) . '/wp-json/kadence-cloud/v1/get/'; + $this->key = isset( $_POST['key'] ) && ! empty( $_POST['key'] ) ? sanitize_text_field( $_POST['key'] ) : 'section'; + $data = $this->process_content( $data, $import_library, $import_type, $import_id, $import_style ); if ( ! $data ) { // Send JSON Error response to the AJAX call. wp_send_json( esc_html__( 'No data', 'kadence-blocks' ) ); @@ -873,8 +887,8 @@ public function process_pattern_content( $content = '' ) { return $content; } - $map_urls = array(); - $image_urls = array(); + $map_urls = []; + $image_urls = []; // Find all the images. foreach ( $all_urls as $key => $link ) { if ( $this->check_for_image( $link ) ) { @@ -892,10 +906,10 @@ public function process_pattern_content( $content = '' ) { if ( ! empty( $image_urls ) ) { foreach ( $image_urls as $key => $image_url ) { // Download remote image. - $image = array( + $image = [ 'url' => $image_url, 'id' => 0, - ); + ]; $downloaded_image = $this->import_image( $image ); $map_urls[ $image_url ] = $downloaded_image['url']; } @@ -925,8 +939,8 @@ public function process_content( $content = '', $import_library = '', $import_ty return $content; } - $map_urls = array(); - $image_urls = array(); + $map_urls = []; + $image_urls = []; // Find all the images. foreach ( $all_urls as $key => $link ) { if ( $this->check_for_image( $link ) ) { @@ -944,10 +958,10 @@ public function process_content( $content = '', $import_library = '', $import_ty if ( ! empty( $image_urls ) ) { foreach ( $image_urls as $key => $image_url ) { // Download remote image. - $image = array( + $image = [ 'url' => $image_url, 'id' => 0, - ); + ]; $downloaded_image = $this->import_image( $image ); $map_urls[ $image_url ] = $downloaded_image['url']; } @@ -977,7 +991,7 @@ public function import_image( $image_data ) { // Check if the image is from Pexels and get the filename. if ( substr( $image_data['url'], 0, strlen( 'https://images.pexels.com' ) ) === 'https://images.pexels.com' ) { $image_path = parse_url( $image_data['url'], PHP_URL_PATH ); - $filename = basename( $image_path ); + $filename = basename( $image_path ); } $info = wp_check_filetype( $image_path ); $ext = empty( $info['ext'] ) ? '' : $info['ext']; @@ -990,10 +1004,10 @@ public function import_image( $image_data ) { $file_content = wp_remote_retrieve_body( wp_safe_remote_get( $image_data['url'], - array( + [ 'timeout' => '60', 'sslverify' => false, - ) + ] ) ); // Empty file content? @@ -1001,14 +1015,14 @@ public function import_image( $image_data ) { return $image_data; } $upload = wp_upload_bits( $filename, null, $file_content ); - $post = array( + $post = [ 'post_title' => ( ! empty( $image_data['title'] ) ? $image_data['title'] : $filename ), 'guid' => $upload['url'], - ); + ]; $post['post_mime_type'] = $type; if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) { - include( ABSPATH . 'wp-admin/includes/image.php' ); + include ABSPATH . 'wp-admin/includes/image.php'; } $post_id = wp_insert_attachment( $post, $upload['file'] ); wp_update_attachment_metadata( @@ -1026,10 +1040,10 @@ public function import_image( $image_data ) { } update_post_meta( $post_id, '_kadence_blocks_image_hash', sha1( $image_data['url'] ) ); - return array( + return [ 'id' => $post_id, 'url' => $upload['url'], - ); + ]; } /** @@ -1052,19 +1066,19 @@ public function check_for_local_image( $image_data ) { ) ); if ( $image_id ) { - $local_image = array( + $local_image = [ 'id' => $image_id, 'url' => wp_get_attachment_url( $image_id ), - ); - return array( + ]; + return [ 'status' => true, 'image' => $local_image, - ); + ]; } - return array( + return [ 'status' => false, 'image' => $image_data, - ); + ]; } /** * Check if link is for an image. @@ -1083,7 +1097,7 @@ public function check_for_image( $link = '' ) { /** * Get information for our image. * - * @param array $images the image url. + * @param array $images the image url. * @param string $target_src the image url. */ public function get_image_info( $images, $target_src ) { @@ -1091,11 +1105,11 @@ public function get_image_info( $images, $target_src ) { foreach ( $image_group['images'] as $image ) { foreach ( $image['sizes'] as $size ) { if ( $size['src'] === $target_src ) { - return array( - 'alt' => $image['alt'], - 'photographer' => $image['photographer'], + return [ + 'alt' => $image['alt'], + 'photographer' => $image['photographer'], 'photographer_url' => $image['photographer_url'], - ); + ]; break; } } @@ -1108,20 +1122,20 @@ public function get_image_info( $images, $target_src ) { */ public function process_individual_import( $content, $import_library, $import_type, $import_id, $import_style ) { if ( isset( $import_library ) && 'pattern' === $import_library ) { - $args = array( + $args = [ 'type' => $import_type, 'id' => $import_id, 'style' => $import_style, 'library' => $import_library, 'key' => $this->key, - ); + ]; // Get the response. $api_url = add_query_arg( $args, 'https://patterns.startertemplatecloud.com/wp-json/kadence-cloud/v1/single/' ); $response = wp_safe_remote_get( $api_url, - array( + [ 'timeout' => 20, - ) + ] ); // Early exit if there was an error. if ( is_wp_error( $response ) ) { @@ -1164,17 +1178,18 @@ public function prebuilt_data_reload_ajax_callback() { // Verify if the AJAX call is valid (checks nonce and current_user_can). $this->verify_ajax_call(); $this->local_template_data_path = ''; - $this->api_key = empty( $_POST['api_key'] ) ? '' : sanitize_text_field( $_POST['api_key'] ); - $this->api_email = empty( $_POST['api_email'] ) ? '' : sanitize_text_field( $_POST['api_email'] ); - $this->product_id = empty( $_POST['product_id'] ) ? '' : sanitize_text_field( $_POST['product_id'] ); - $this->package = empty( $_POST['package'] ) ? 'section' : sanitize_text_field( $_POST['package'] ); - $this->url = empty( $_POST['url'] ) ? $this->remote_url : rtrim( sanitize_text_field( $_POST['url'] ), '/' ) . '/wp-json/kadence-cloud/v1/get/'; - $this->key = empty( $_POST['key'] ) ? 'section' : sanitize_text_field( $_POST['key'] ); - $this->is_template = isset( $_POST['is_template'] ) && ! empty( $_POST['is_template'] ) ? true : false; + $this->api_key = empty( $_POST['api_key'] ) ? '' : sanitize_text_field( $_POST['api_key'] ); + $this->api_email = empty( $_POST['api_email'] ) ? '' : sanitize_text_field( $_POST['api_email'] ); + $this->product_id = empty( $_POST['product_id'] ) ? '' : sanitize_text_field( $_POST['product_id'] ); + $this->product_slug = empty( $_POST['product_slug'] ) ? '' : sanitize_text_field( $_POST['product_slug'] ); + $this->package = empty( $_POST['package'] ) ? 'section' : sanitize_text_field( $_POST['package'] ); + $this->url = empty( $_POST['url'] ) ? $this->remote_url : rtrim( sanitize_text_field( $_POST['url'] ), '/' ) . '/wp-json/kadence-cloud/v1/get/'; + $this->key = empty( $_POST['key'] ) ? 'section' : sanitize_text_field( $_POST['key'] ); + $this->is_template = isset( $_POST['is_template'] ) && ! empty( $_POST['is_template'] ) ? true : false; // $removed = $this->delete_block_library_folder(); // if ( ! $removed ) { - // wp_send_json_error( 'failed_to_flush' ); + // wp_send_json_error( 'failed_to_flush' ); // } // Do you have the data? $get_data = $this->get_template_data( true ); @@ -1200,15 +1215,15 @@ public function prebuilt_data_reload_ajax_callback() { */ public function get_importer_files( $slug, $type ) { $this->package = $type; - $get_data = $this->get_template_data(); + $get_data = $this->get_template_data(); if ( ! $get_data ) { - return array(); + return []; } $data = json_decode( $get_data, true ); if ( isset( $data[ $slug ] ) ) { return $data; } - return array(); + return []; } /** * Schedule a cleanup. @@ -1235,10 +1250,10 @@ public function schedule_cleanup() { public function add_monthly_to_cron_schedule( $schedules ) { // Adds once monthly to the existing schedules. if ( ! isset( $schedules[ self::CLEANUP_FREQUENCY ] ) ) { - $schedules[ self::CLEANUP_FREQUENCY ] = array( + $schedules[ self::CLEANUP_FREQUENCY ] = [ 'interval' => MONTH_IN_SECONDS, - 'display' => __( 'Once Monthly', 'kadence-blocks' ), - ); + 'display' => __( 'Once Monthly', 'kadence-blocks' ), + ]; } return $schedules; } @@ -1263,8 +1278,7 @@ public function delete_block_library_folder() { * @return string */ public function get_old_block_library_folder() { - $old_block_library_folder = trailingslashit( $this->get_filesystem()->wp_content_dir() ) . 'kadence_blocks_library'; - return $old_block_library_folder; + return trailingslashit( $this->get_filesystem()->wp_content_dir() ) . 'kadence_blocks_library'; } /** * Get the folder for templates data. @@ -1301,7 +1315,7 @@ public function get_subfolder_name() { */ public function get_base_path() { if ( ! $this->base_path ) { - $upload_dir = wp_upload_dir(); + $upload_dir = wp_upload_dir(); $this->base_path = apply_filters( 'kadence_block_library_local_data_base_path', trailingslashit( $upload_dir['basedir'] ) ); } return $this->base_path; diff --git a/src/plugins/prebuilt-library/cloud-library.js b/src/plugins/prebuilt-library/cloud-library.js index a8e1b1829..4ace5c6e8 100644 --- a/src/plugins/prebuilt-library/cloud-library.js +++ b/src/plugins/prebuilt-library/cloud-library.js @@ -360,6 +360,7 @@ function CloudSections({ importContent, clientId, reload = false, onReload, tab, setIsError(true); } } else { + console.log('error, library content incorrect', response); if (subTab === 'pages') { setPages('error'); } else { diff --git a/src/plugins/prebuilt-library/editor.scss b/src/plugins/prebuilt-library/editor.scss index 6a7e896cc..aa5d7cdf2 100644 --- a/src/plugins/prebuilt-library/editor.scss +++ b/src/plugins/prebuilt-library/editor.scss @@ -348,12 +348,14 @@ button.components-button.kb-category-button { .kt-prebuilt-item .kb-pro-template { position: absolute; z-index: 10000; - top: 20px; + bottom: 38px; right: 30px; background: #c53030; border-radius: 10px; - width: 40px; + width: auto; + padding-left: 6px; line-height: 20px; + padding-right: 6px; color: #fff; text-transform: uppercase; font-weight: bold; diff --git a/src/plugins/prebuilt-library/single-template-library.js b/src/plugins/prebuilt-library/single-template-library.js index ba7f0264b..9479d940b 100644 --- a/src/plugins/prebuilt-library/single-template-library.js +++ b/src/plugins/prebuilt-library/single-template-library.js @@ -78,6 +78,9 @@ class SingleTemplateLibrary extends Component { kadence_blocks_params.proData && kadence_blocks_params.proData.api_email ? kadence_blocks_params.proData.api_email : ''; + let data_product = kadence_blocks_params?.proData?.product_slug + ? kadence_blocks_params.proData.product_slug + : ''; if (!data_key) { data_key = kadence_blocks_params.proData && kadence_blocks_params.proData.ithemes_key @@ -93,6 +96,7 @@ class SingleTemplateLibrary extends Component { data.append('api_key', data_key); data.append('api_email', data_email); data.append('package', this.props.selectedSlug); + data.append('product_slug', data_product); data.append('url', this.props.selectedURL); data.append('is_template', 'is_template'); data.append('key', 'kadence-blocks'); @@ -108,6 +112,7 @@ class SingleTemplateLibrary extends Component { .done(function (response, status, stately) { if (response) { const o = SafeParseJSON(response, false); + console.log(o); if (o) { control.setState({ items: o, errorItems: false, isLoading: false }); } else { @@ -130,6 +135,9 @@ class SingleTemplateLibrary extends Component { kadence_blocks_params.proData && kadence_blocks_params.proData.api_email ? kadence_blocks_params.proData.api_email : ''; + let data_product = kadence_blocks_params?.proData?.product_slug + ? kadence_blocks_params.proData.product_slug + : ''; if (!data_key) { data_key = kadence_blocks_params.proData && kadence_blocks_params.proData.ithemes_key @@ -144,6 +152,7 @@ class SingleTemplateLibrary extends Component { data.append('security', kadence_blocks_params.ajax_nonce); data.append('api_key', data_key); data.append('api_email', data_email); + data.append('product_slug', data_product); data.append('package', this.props.selectedSlug); data.append('url', this.props.selectedURL); data.append('is_template', 'is_template'); @@ -266,16 +275,16 @@ class SingleTemplateLibrary extends Component { {undefined !== pro && pro && ( - {__('Pro', 'kadence-blocks')} + {__('Premium', 'kadence-blocks')} {locked && (
-

{__('Kadence Blocks Pro required for this item')}

+

{__('Kadence Premium Designs required for this item')}

- {__('Upgrade to Pro', 'kadence-blocks')} + {__('Upgrade to Get Access', 'kadence-blocks')}
)} diff --git a/src/plugins/prebuilt-library/templates-library.js b/src/plugins/prebuilt-library/templates-library.js index f06ea15d1..ae8e72385 100644 --- a/src/plugins/prebuilt-library/templates-library.js +++ b/src/plugins/prebuilt-library/templates-library.js @@ -293,7 +293,9 @@ class TemplatesLibrary extends Component { {undefined !== pro && pro && ( - {__('Pro', 'kadence-blocks')} + + {__('Premium', 'kadence-blocks')} + )}