block_library_cache = kadence_starter_templates()->get( Block_Library_Cache::class ); $this->ai_cache = kadence_starter_templates()->get( Ai_Cache::class ); $this->cache_primer = kadence_starter_templates()->get( Cache_Primer::class ); } /** * Retrieves a collection of objects. * * @param bool $reload Whether to reload the data. * @return string The contents. */ public function get_ai_base_site( $site_id, $reload = false ) { $this->get_license_keys(); $identifier = 'ai-base-site-' . $site_id . KADENCE_STARTER_TEMPLATES_VERSION; if ( ! empty( $this->api_key ) ) { $identifier .= '_' . $this->api_key; } // Check if we have a local file. if ( ! $reload ) { try { return json_decode( $this->block_library_cache->get( $identifier ), true ); } catch ( NotFoundException $e ) { } } $args = array( 'key' => $this->api_key, 'site_url' => $this->site_url, ); if ( ! empty( $this->env ) ) { $args['env'] = $this->env; } $api_url = add_query_arg( $args, 'https://base.startertemplatecloud.com/' . $site_id . '/wp-json/kadence-starter-base/v1/single-site' ); // Get the response. $response = wp_safe_remote_get( $api_url, array( 'timeout' => 30, ) ); // Early exit if there was an error. if ( is_wp_error( $response ) || $this->is_response_code_error( $response ) ) { return new WP_Error( 'getting_ai_sites_failed', __( 'Failed to get AI Template' ), array( 'status' => 500 ) ); } // Get the CSS from our response. $contents = wp_remote_retrieve_body( $response ); // Early exit if there was an error. if ( is_wp_error( $contents ) ) { return new WP_Error( 'getting_ai_sites_failed', __( 'Failed to get AI Template' ), array( 'status' => 500 ) ); } $this->block_library_cache->cache( $identifier, $contents ); return json_decode( $contents, true ); } /** * Retrieves a collection of objects. * * @param bool $reload Whether to reload the data. * @return string The contents. */ public function get_ai_base_sites( $reload = false ) { $this->get_license_keys(); $identifier = 'ai-base-templates' . KADENCE_STARTER_TEMPLATES_VERSION; if ( ! empty( $this->api_key ) ) { $identifier .= '_' . $this->api_key; } // Check if we have a local file. if ( ! $reload ) { try { return json_decode( $this->block_library_cache->get( $identifier ), true ); } catch ( NotFoundException $e ) { } } $args = array( 'key' => $this->api_key, 'site_url' => $this->site_url, 'beta' => defined( 'KADENCE_STARTER_TEMPLATES_BETA' ) && KADENCE_STARTER_TEMPLATES_BETA ? 'true' : 'false', ); if ( ! empty( $this->env ) ) { $args['env'] = $this->env; } $api_url = add_query_arg( $args, 'https://base.startertemplatecloud.com/wp-json/kadence-starter-base/v1/sites' ); // Get the response. $response = wp_safe_remote_get( $api_url, array( 'timeout' => 20, ) ); // Early exit if there was an error. if ( is_wp_error( $response ) || $this->is_response_code_error( $response ) ) { return new WP_Error( 'getting_ai_sites_failed', __( 'Failed to get AI Templates' ), array( 'status' => 500 ) ); } // Get the CSS from our response. $contents = wp_remote_retrieve_body( $response ); // Early exit if there was an error. if ( is_wp_error( $contents ) ) { return new WP_Error( 'getting_ai_sites_failed', __( 'Failed to get AI Templates' ), array( 'status' => 500 ) ); } $this->block_library_cache->cache( $identifier, $contents ); return json_decode( $contents, true ); } /** * Retrieves all the currently available ai content. * * @param array $available_prompts The available prompts. * @return array The ai content. */ public function get_all_local_ai_items( $available_prompts, $auth = null ) { $this->get_license_keys(); $return_data = []; $error_messages = []; if ( ! empty( $available_prompts ) && is_array( $available_prompts ) ) { foreach ( $available_prompts as $context => $prompt ) { // Check local cache. try { $return_data[ $context ] = json_decode( $this->ai_cache->get( $available_prompts[ $context ] ), true ); } catch ( NotFoundException $e ) { // Check if we have a remote file. $response = $this->get_remote_job( $available_prompts[ $context ], $auth ); if ( is_wp_error( $response ) ) { $has_error = true; $error_messages[] = $response->get_error_message(); } else if ( !empty( $response ) && is_string( $response ) && 'error' === $response ) { $error_messages[] = 'Unknown Error'; $has_error = true; } else if ( !empty( $response ) && is_string( $response ) && 'not-found' === $response ) { $error_messages[] = 'Not Found'; $has_error = true; // Clean up, the token and job are no longer valid. $current_prompts = get_option( 'kb_design_library_prompts', [] ); if ( isset( $current_prompts[ $context ] ) ) { unset( $current_prompts[ $context ] ); update_option( 'kb_design_library_prompts', $current_prompts ); } } else { $data = json_decode( $response, true ); if ( $response === 'processing' || isset( $data['data']['status'] ) && 409 === $data['data']['status'] ) { $error_messages[] = 'Processing'; $has_error = true; $ready = false; } else if ( isset( $data['data']['status'] ) ) { $error_messages[] = 'Unknown Error'; $has_error = true; } else { $this->ai_cache->cache( $available_prompts[ $context ], $response ); $return_data[ $context ] = $data; } } } } } // Return data if we have some. if ( ! empty( $return_data ) ) { return $return_data; } // Return error if we have some. if ( ! empty( $error_messages ) ) { return new WP_Error( 'getting_ai_items_failed', __( 'Failed to get AI Items' ), array( 'status' => 500 ) ); } return []; } /** * Get remote file contents. * * @access public * @return string Returns the remote URL contents. */ public function get_remote_job( $job, $auth = null ) { if ( empty( $auth ) ) { $auth = base64_encode( json_encode( [ 'domain' => $this->site_url, 'key' => $this->api_key, ] ) ); } $api_url = $this->remote_ai_url . 'content/job/' . $job; $response = wp_safe_remote_get( $api_url, array( 'timeout' => 20, 'headers' => array( 'X-Prophecy-Token' => $auth, ), ) ); // Early exit if there was an error. if ( is_wp_error( $response ) ) { return 'error'; } $response_code = (int) wp_remote_retrieve_response_code( $response ); if ( 409 === $response_code ) { return 'processing'; } if ( 404 === $response_code ) { return 'not-found'; } if ( $this->is_response_code_error( $response ) ) { return 'error'; } // Get the CSS from our response. $contents = wp_remote_retrieve_body( $response ); // Early exit if there was an error. if ( is_wp_error( $contents ) ) { return 'error'; } return $contents; } /** * Get remote file contents. * * @access public * @return string Returns the remote URL contents. */ public function get_new_remote_contents( $context ) { $auth = array( 'domain' => $this->site_url, 'key' => $this->api_key, ); $prophecy_data = json_decode( get_option( 'kadence_blocks_prophecy' ), true ); // Get the response. $body = array( 'context' => 'kadence', ); $body['company'] = ! empty( $prophecy_data['companyName'] ) ? $prophecy_data['companyName'] : ''; if ( ! empty( $prophecy_data['industrySpecific'] ) && 'Other' !== $prophecy_data['industrySpecific'] ) { $body['industry'] = ! empty( $prophecy_data['industrySpecific'] ) ? $prophecy_data['industrySpecific'] : ''; } elseif ( ! empty( $prophecy_data['industrySpecific'] ) && 'Other' === $prophecy_data['industrySpecific'] && ! empty( $prophecy_data['industryOther'] ) ) { $body['industry'] = ! empty( $prophecy_data['industryOther'] ) ? $prophecy_data['industryOther'] : ''; } elseif ( ! empty( $prophecy_data['industry'] ) && 'Other' === $prophecy_data['industry'] && ! empty( $prophecy_data['industryOther'] ) ) { $body['industry'] = ! empty( $prophecy_data['industryOther'] ) ? $prophecy_data['industryOther'] : ''; } else { $body['industry'] = ! empty( $prophecy_data['industry'] ) ? $prophecy_data['industry'] : ''; } $body['location'] = ! empty( $prophecy_data['location'] ) ? $prophecy_data['location'] : ''; $body['mission'] = ! empty( $prophecy_data['missionStatement'] ) ? $prophecy_data['missionStatement'] : ''; $body['tone'] = ! empty( $prophecy_data['tone'] ) ? $prophecy_data['tone'] : ''; $body['keywords'] = ! empty( $prophecy_data['keywords'] ) ? $prophecy_data['keywords'] : ''; $body['lang'] = ! empty( $prophecy_data['lang'] ) ? $prophecy_data['lang'] : ''; switch ( $context ) { case 'about': $body['prompts'] = array( 'about', 'about-hero', 'about-columns', 'about-list', 'about-videos', ); break; case 'achievements': $body['prompts'] = array( 'achievements', 'achievements-columns', 'achievements-list', 'achievements-videos', ); break; case 'blog': $body['prompts'] = array( 'blog-post-loop', 'blog-table-contents', ); break; case 'call-to-action': $body['prompts'] = array( 'call-to-action', 'call-to-action-columns', 'call-to-action-list', 'call-to-action-videos', ); break; case 'careers': $body['prompts'] = array( 'careers', 'careers-hero', 'careers-columns', 'careers-list', 'careers-videos', ); break; case 'contact-form': $body['prompts'] = array( 'contact-form', ); break; case 'donate': $body['prompts'] = array( 'donate', 'donate-hero', 'donate-columns', 'donate-list', 'donate-videos', ); break; case 'events': $body['prompts'] = array( 'events', 'events-hero', 'events-columns', 'events-list', 'events-videos', ); break; case 'faq': $body['prompts'] = array( 'faq-accordion', ); break; case 'get-started': $body['prompts'] = array( 'get-started', 'get-started-accordion', 'get-started-columns', 'get-started-list', ); break; case 'history': $body['prompts'] = array( 'history', 'history-columns', 'history-list', 'history-videos', ); break; case 'industries': $body['prompts'] = array( 'industries', 'industries-accordion', 'industries-list', 'industries-columns', 'industries-tabs', ); break; case 'location': $body['prompts'] = array( 'location', 'location-columns', 'location-tabs', ); break; case 'mission': $body['prompts'] = array( 'mission', 'mission-columns', 'mission-list', 'mission-videos', ); break; case 'news': $body['prompts'] = array( 'news-post-loop', ); break; case 'partners': $body['prompts'] = array( 'partners', 'partners-columns', 'partners-list', ); break; case 'podcast': $body['prompts'] = array( 'podcast', ); break; case 'pricing-table': $body['prompts'] = array( 'pricing-pricing-table', ); break; case 'product-details': $body['prompts'] = array( 'product-details-accordion', ); break; case 'products-services': $body['prompts'] = array( 'products-services', 'products-services-columns', 'products-services-hero', 'products-services-list', 'products-services-single', 'products-services-tabs', 'products-services-videos', 'product-details-accordion', ); break; case 'profile': $body['prompts'] = array( 'profile', 'profile-columns', 'profile-list', 'profile-videos', ); break; case 'subscribe-form': $body['prompts'] = array( 'subscribe-form', ); break; case 'support': $body['prompts'] = array( 'support', 'support-columns', 'support-list', 'support-videos', ); break; case 'team': $body['prompts'] = array( 'team', 'team-columns', 'team-list', 'team-people', 'team-videos', ); break; case 'testimonials': $body['prompts'] = array( 'testimonials-testimonials', ); break; case 'value-prop': $body['prompts'] = array( 'value-prop', 'value-prop-columns', 'value-prop-hero', 'value-prop-list', 'value-prop-tabs', 'value-prop-videos', ); break; case 'volunteer': $body['prompts'] = array( 'volunteer', 'volunteer-hero', 'volunteer-list', 'volunteer-columns', 'volunteer-videos', ); break; case 'welcome': $body['prompts'] = array( 'welcome', 'welcome-hero', 'welcome-list', 'welcome-columns', 'welcome-videos', ); break; case 'work': $body['prompts'] = array( 'work', 'work-columns', 'work-counter-stats', 'work-list', 'work-videos', ); break; } $response = wp_remote_post( $this->remote_ai_url . 'content/create', array( 'timeout' => 20, 'headers' => array( 'X-Prophecy-Token' => base64_encode( json_encode( $auth ) ), 'Content-Type' => 'application/json', ), 'body' => json_encode( $body ), ) ); // Early exit if there was an error. if ( is_wp_error( $response ) || $this->is_response_code_error( $response ) ) { $contents = wp_remote_retrieve_body( $response ); if ( ! empty( $contents ) && is_string( $contents ) && json_decode( $contents, true ) ) { $error_message = json_decode( $contents, true ); if ( ! empty( $error_message['detail'] ) && 'Failed, unable to use credits.' === $error_message['detail'] ) { return 'credits'; } } return 'error'; } // Get the CSS from our response. $contents = wp_remote_retrieve_body( $response ); // Early exit if there was an error. if ( is_wp_error( $contents ) ) { return 'error'; } return $contents; } /** * Write the data to the filesystem. * * @access protected * @return string|false Returns the absolute path of the file on success, or false on fail. */ protected function create_ai_data_file( $content, $prompt_data ) { $file_path = $this->get_local_ai_data_path( $prompt_data ); $filesystem = $this->get_filesystem(); // If the folder doesn't exist, create it. if ( ! file_exists( $this->get_ai_library_folder() ) ) { $chmod_dir = ( 0755 & ~ umask() ); if ( defined( 'FS_CHMOD_DIR' ) ) { $chmod_dir = FS_CHMOD_DIR; } $this->get_filesystem()->mkdir( $this->get_ai_library_folder(), $chmod_dir ); } // If the file doesn't exist, create it. Return false if it can not be created. if ( ! $filesystem->exists( $file_path ) && ! $filesystem->touch( $file_path ) ) { return false; } // Put the contents in the file. Return false if that fails. if ( ! $filesystem->put_contents( $file_path, $content ) ) { return false; } return $file_path; } /** * Check if a response code is an error. * * @access public * @return string Returns the remote URL contents. */ public function is_response_code_error( $response ) { $response_code = (int) wp_remote_retrieve_response_code( $response ); if ( $response_code >= 200 && $response_code < 300 ) { return false; } else { return true; } } /** * Retrieves the path to the local data file. * * @param array $prompt_data The prompt data. * * @return string of the path to local data file. */ public function get_local_ai_data_path( $prompt_data ) { return $this->get_ai_library_folder() . '/' . $this->get_local_ai_data_filename( $prompt_data ) . '.json'; } /** * Get the local data filename. * * This is a hash, generated from the current site url, the wp-content path, the prompt data. * This way we can avoid issues with sites changing their URL, or the wp-content path etc. * * @param array $prompt_data The prompt data. * * @return string */ public function get_local_ai_data_filename( $prompt_data ) { return $this->hash( array( 'kadence-ai-generated-content', $prompt_data ) ); } /** * Create a hash from different types of data. * * @param string|object|array|int|float $data The data to hash. * @param bool $binary Output in raw binary. * * @return string * * @throws InvalidArgumentException|RuntimeException */ public function hash( $data, bool $binary = false ): string { if ( $data === null ) { throw new InvalidArgumentException( '$data cannot be null.' ); } $data = is_scalar( $data ) ? (string) $data : (string) json_encode( $data ); if ( strlen( $data ) <= 0 ) { throw new RuntimeException( 'Cannot hash an empty data string. Perhaps JSON encoding failed?' ); } return hash( 'md5', $data, $binary ); } /** * Get local data contents. * * @access public * @return string|false Returns the data contents. */ public function get_local_data_contents( $file_path ) { // Check if the file path is set. if ( empty( $file_path ) ) { return false; } ob_start(); include $file_path; return ob_get_clean(); } /** * Get the folder for templates data. * * @access public * @return string */ public function get_ai_library_folder() { if ( ! $this->block_ai_folder ) { $this->block_ai_folder = $this->get_base_path(); $this->block_ai_folder .= $this->get_ai_subfolder_name(); } return $this->block_ai_folder; } /** * Remove Past Content. * * @return boolean/WP_Error */ public function remove_content() { global $wpdb; // Prevents elementor from pushing out an confrimation and breaking the import. $_GET['force_delete_kit'] = true; $removed_content = true; $post_ids = $wpdb->get_col( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key='_kadence_starter_templates_imported_post'" ); $term_ids = $wpdb->get_col( "SELECT term_id FROM {$wpdb->termmeta} WHERE meta_key='_kadence_starter_templates_imported_term'" ); if ( isset( $post_ids ) && is_array( $post_ids ) ) { foreach ( $post_ids as $post_id ) { $worked = wp_delete_post( $post_id, true ); if ( false === $worked ) { $removed_content = false; } } } if ( isset( $term_ids ) && is_array( $term_ids ) ) { foreach ( $term_ids as $term_id ) { $term = get_term( $term_id ); if ( ! is_wp_error( $term ) ) { wp_delete_term( $term_id, $term->taxonomy ); } } } if ( false === $removed_content ) { return new WP_Error( 'remove_failed', __( 'Remove past content failed.' ), array( 'status' => 500 ) ); } /** * Clean up default contents. */ $hello_world = $this->get_post_by_title( 'Hello World', OBJECT, 'post' ); if ( $hello_world ) { wp_delete_post( $hello_world->ID, true );// Hello World. } $sample_page = $this->get_post_by_title( 'Sample Page' ); if ( $sample_page ) { wp_delete_post( $sample_page->ID, true ); // Sample Page. } wp_delete_comment( 1, true ); // WordPress comment. return true; } /** * Get Post by title. * * @param string $page_title The title of the post. * @param string $output The output type. * @param string $post_type The post type. * @return object|null The post object or null if not found. */ public function get_post_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) { $query = new WP_Query( array( 'post_type' => $post_type, 'title' => $page_title, 'post_status' => 'all', 'posts_per_page' => 1, 'no_found_rows' => true, 'ignore_sticky_posts' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'orderby' => 'date', 'order' => 'ASC', ) ); if ( ! empty( $query->post ) ) { $_post = $query->post; if ( ARRAY_A === $output ) { return $_post->to_array(); } elseif ( ARRAY_N === $output ) { return array_values( $_post->to_array() ); } return $_post; } return null; } /** * Get remote download link. * * @access public * @return string */ public function get_bundle_download_link( $base ) { $data = $this->get_license_keys(); if ( empty( $data['api_key'] ) ) { return ''; } return 'https://licensing.kadencewp.com/api/plugins/v2/download?plugin=' . $base . '&key=' . urlencode( $data['api_key'] ); } /** * Install Plugins. * * @param array $plugins The plugins to install. * @param string $import_key The import key. * @return boolean/WP_Error */ public function install_plugins( $plugins, $import_key ) { update_option( '_kadence_starter_templates_last_import_data', array( $import_key ), 'no' ); $install = true; if ( ! empty( $plugins ) && is_array( $plugins ) ) { $importer_plugins = $this->get_allowed_plugins(); if ( ! function_exists( 'request_filesystem_credentials' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } if ( ! function_exists( 'plugins_api' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; } if ( ! class_exists( 'WP_Upgrader' ) ) { require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; } foreach ( $plugins as $plugin ) { $path = false; if ( strpos( $plugin, '/' ) !== false ) { $path = $plugin; $arr = explode( '/', $plugin, 2 ); $base = $arr[0]; if ( isset( $importer_plugins[ $base ] ) && isset( $importer_plugins[ $base ]['src'] ) ) { $src = $importer_plugins[ $base ]['src']; } else { $src = 'unknown'; } } elseif ( isset( $importer_plugins[ $plugin ] ) ) { $path = $importer_plugins[ $plugin ]['path']; $base = $importer_plugins[ $plugin ]['base']; $src = $importer_plugins[ $plugin ]['src']; } if ( $path ) { $state = Plugin_Check::active_check( $path ); if ( 'unknown' === $src ) { $check_api = plugins_api( 'plugin_information', array( 'slug' => $base, 'fields' => array( 'short_description' => false, 'sections' => false, 'requires' => false, 'rating' => false, 'ratings' => false, 'downloaded' => false, 'last_updated' => false, 'added' => false, 'tags' => false, 'compatibility' => false, 'homepage' => false, 'donate_link' => false, ), ) ); if ( ! is_wp_error( $check_api ) ) { $src = 'repo'; } } if ( 'notactive' === $state && 'repo' === $src ) { if ( ! current_user_can( 'install_plugins' ) ) { return new WP_Error( 'install_failed', __( 'Permissions Issue.' ), array( 'status' => 500 ) ); } $api = plugins_api( 'plugin_information', array( 'slug' => $base, 'fields' => array( 'short_description' => false, 'sections' => false, 'requires' => false, 'rating' => false, 'ratings' => false, 'downloaded' => false, 'last_updated' => false, 'added' => false, 'tags' => false, 'compatibility' => false, 'homepage' => false, 'donate_link' => false, ), ) ); if ( ! is_wp_error( $api ) ) { // Use AJAX upgrader skin instead of plugin installer skin. // ref: function wp_ajax_install_plugin(). $upgrader = new Plugin_Upgrader( new WP_Ajax_Upgrader_Skin() ); $installed = $upgrader->install( $api->download_link ); if ( $installed ) { $silent = ( 'give' === $base || 'elementor' === $base || 'wp-smtp' === $base || 'fluentform' === $base || 'restrict-content' === $base ? false : true ); if ( 'give' === $base ) { add_option( 'give_install_pages_created', 1, '', false ); } if ( 'restrict-content' === $base ) { update_option( 'rcp_install_pages_created', current_time( 'mysql' ) ); } $activate = activate_plugin( $path, '', false, $silent ); if ( is_wp_error( $activate ) ) { $install = false; } } else { $install = false; } } else { $install = false; } } elseif ( 'notactive' === $state && 'bundle' === $src ) { if ( ! current_user_can( 'install_plugins' ) ) { return new WP_Error( 'install_failed', __( 'Permissions Issue.' ), array( 'status' => 500 ) ); } $download_link = $this->get_bundle_download_link( $base ); if ( $download_link ) { // Use AJAX upgrader skin instead of plugin installer skin. // ref: function wp_ajax_install_plugin(). $upgrader = new Plugin_Upgrader( new WP_Ajax_Upgrader_Skin() ); $installed = $upgrader->install( $download_link ); if ( $installed ) { $silent = ( 'give' === $base || 'elementor' === $base || 'wp-smtp' === $base || 'fluentform' === $base || 'restrict-content' === $base ? false : true ); if ( 'give' === $base ) { add_option( 'give_install_pages_created', 1, '', false ); } if ( 'restrict-content' === $base ) { update_option( 'rcp_install_pages_created', current_time( 'mysql' ) ); } $activate = activate_plugin( $path, '', false, $silent ); if ( is_wp_error( $activate ) ) { $install = false; } } else { $install = false; } } else { $install = false; } } elseif ( 'installed' === $state ) { if ( ! current_user_can( 'install_plugins' ) ) { return new WP_Error( 'install_failed', __( 'Permissions Issue.' ), array( 'status' => 500 ) ); } $silent = ( 'give' === $base || 'elementor' === $base || 'wp-smtp' === $base || 'fluentform' === $base || 'restrict-content' === $base ? false : true ); if ( 'give' === $base ) { // Make sure give doesn't add it's pages, prevents having two sets. update_option( 'give_install_pages_created', 1, '', false ); } if ( 'restrict-content' === $base ) { $silent = true; update_option( 'rcp_install_pages_created', current_time( 'mysql' ) ); } $activate = activate_plugin( $path, '', false, $silent ); if ( is_wp_error( $activate ) ) { $install = false; } } if ( 'give' === $base ) { update_option( 'give_version_upgraded_from', '2.13.2' ); } if ( 'kadence-pro' === $base ) { $enabled = json_decode( get_option( 'kadence_pro_theme_config' ), true ); $enabled['elements'] = true; $enabled['header_addons'] = true; $enabled['mega_menu'] = true; $enabled = json_encode( $enabled ); update_option( 'kadence_pro_theme_config', $enabled ); } if ( 'elementor' === $base ) { $elementor_redirect = get_transient( 'elementor_activation_redirect' ); if ( ! empty( $elementor_redirect ) && '' !== $elementor_redirect ) { delete_transient( 'elementor_activation_redirect' ); } } if ( 'woocommerce' === $base ) { // Create WooCommerce database tables. if ( is_callable( '\Automattic\WooCommerce\Admin\Install::create_tables' ) ) { \Automattic\WooCommerce\Admin\Install::create_tables(); \Automattic\WooCommerce\Admin\Install::create_events(); } if ( is_callable( 'WC_Install::install' ) ) { WC_Install::install(); } } } } } if ( false === $install ) { return new WP_Error( 'install_failed', __( 'Install failed.' ), array( 'status' => 500 ) ); } return true; } /** * Replace Colors. * * @param string $content The content to replace colors in. * @param string $style The style to replace colors in. * @param array $color_palette The color palette to use. * @return string The content. */ public function replace_colors( $content, $style, $color_palette ) { if ( in_array( $style, array( 'dark', 'highlight' ) ) ) { $content = Color_Handler::replace_colors( $content, $style, ( !empty( $color_palette['btnColor'] ) ? $color_palette['btnColor'] : '#ffffff' ) ); } if ( isset( $color_palette['isLight'] ) && $color_palette['isLight'] === false && 'highlight' === $style && '#ffffff' === $color_palette['btnColor'] ) { $content = Color_Handler::replace_contrast_colors( $content, 'highlight' ); } if ( isset( $color_palette['isLight'] ) && $color_palette['isLight'] === false && 'light' === $style ) { $content = Color_Handler::replace_logo_farm_colors( $content, 'dark' ); } return $content; } /** * Build Page Content. * * @param array $rows The rows to build. * @param array $image_library The image library to use. * @return string The content. */ public function build_page_content( $rows, $image_library, $ai_content, $color_palette, $goals, $product_ids, $page_data, $team_image_collection, $i, $give_form_id ) { $content = ''; $real_variation = 0; $variation = $i; $is_home = ( $page_data['slug'] === 'home' ? true : false ); foreach ( $rows as $row ) { if ( $variation > 11 ) { $variation = 0; } // Get only the keys in the pattern category array. $row_categories = ( isset( $row['pattern_category'] ) ? array_keys( $row['pattern_category'] ) : [] ); $row_style = ( isset( $row['pattern_style'] ) ? $row['pattern_style'] : 'light' ); $row_content = ( isset( $row['pattern_content'] ) ? $row['pattern_content'] : '' ); $row_context = ( isset( $row['pattern_context'] ) ? $row['pattern_context'] : '' ); $row_pattern_id = ( isset( $row['pattern_id'] ) ? $row['pattern_id'] : $row_context ); $row_condition = ( isset( $row['pattern_condition'] ) ? $row['pattern_condition'] : '' ); if ( $row_context === 'contact' ) { $row_context = 'contact-form'; } else if ( $row_context === 'subscribe' ) { $row_context = 'subscribe-form'; } else if ( $row_context === 'pricing' ) { $row_context = 'pricing-table'; } $row_hero = false; if ( $is_home && $real_variation === 0 ) { $row_hero = 'hero'; } else if ( $is_home && $real_variation === 1 ) { $row_hero = 'secondary'; } // Remove content that should be removed. $row_content = Content_Remover::remove_content( $row_content ); // Don't output product loops if we don't have any products. if ( in_array( 'product-loop', $row_categories ) ) { if ( in_array( 'ecommerce', $goals ) && $product_ids ) { $row_content = Woo_Content_Handler::replace_woo_content( $row_content, $product_ids ); } else { continue; } } // Don't output donation form if the site isn't wanting to accept donations. if ( in_array( 'donation-form', $row_categories ) ) { if ( in_array( 'donations', $goals ) ) { $row_content = Donation_Form_Handler::replace_donation_content( $row_content, $give_form_id ); } else { continue; } } // Don't output sections that don't match the goals. if ( $row_condition !== '' && $row_condition !== 'general' ) { if ( $row_condition === 'replace' && ( in_array( 'ecommerce', $goals ) || in_array( 'events', $goals ) || in_array( 'donations', $goals ) || in_array( 'learning', $goals ) || in_array( 'membership', $goals ) || in_array( 'photography', $goals ) || in_array( 'landing', $goals ) || in_array( 'blogging', $goals ) ) ) { continue; } else if ( ! in_array( $row_condition, $goals ) ) { continue; } } // Replace colors. $row_content = $this->replace_colors( $row_content, $row_style, $color_palette ); // Replace images. $row_content = Image_Replacer::replace_images( $row_content, $image_library, $row_categories, $row_pattern_id, $variation, $team_image_collection, $row_hero, ); // Replace content. $row_content = Content_Replacer::replace_content( $row_content, $ai_content, $row_categories, $row_context, $variation, false, $page_data, ); $content .= $row_content; $variation++; $real_variation++; } return $content; } /** * Prepare Posts. * * @param array $pages The pages to install. * @param array $image_library The image library to use. * @return array The pages. */ public function prepare_pages( $pages, $image_library, $ai_content, $color_palette, $goals, $product_ids, $team_image_collection, $give_form_id ) { if ( empty( $pages ) || ! is_array( $pages ) ) { return new WP_Error( 'no_pages', __( 'No pages to prepare.' ), array( 'status' => 500 ) ); } $processed_pages = $this->process_pages( $pages ); if ( is_wp_error( $processed_pages ) ) { return $processed_pages; } $i = 0; $prepared_pages = array(); foreach ( $processed_pages as $page_data ) { if ( empty( $page_data['rows'] ) ) { continue; } $page_item = [ 'key' => $i, 'slug' => ( isset( $page_data['slug'] ) ? $page_data['slug'] : '' ), 'title' => ( isset( $page_data['title'] ) ? $page_data['title'] : '' ), 'content' => $this->build_page_content( $page_data['rows'], $image_library, $ai_content, $color_palette, $goals, $product_ids, $page_data, $team_image_collection, $i, $give_form_id ), ]; $prepared_pages[] = $page_item; $i++; } return $prepared_pages; } /** * Install Pages. * * @param array $pages The pages to install. * @param array $image_library The image library to use. * @return array The pages. */ public function install_pages_extras( $pages, $image_library ) { if ( empty( $pages ) ) { return new WP_Error( 'no_pages', __( 'No pages to install.' ), array( 'status' => 500 ) ); } $new_pages = array(); foreach ( $pages as $page_data ) { // Create page using wp_insert_post. $page_item = array( 'post_title' => ( isset( $page_data['title'] ) ? wp_strip_all_tags( $page_data['title'] ) : '' ), 'post_content' => $this->process_page_content( $page_data['content'], $image_library ), ); $new_pages[] = $page_item; } if ( empty( $new_pages ) ) { return new WP_Error( 'install_failed', __( 'Install failed.' ), array( 'status' => 500 ) ); } return $new_pages; } /** * Install Pages. * * @param array $pages The posts to install. * @return bool/WP_Error The posts. */ public function install_pages( $pages ) { if ( empty( $pages ) ) { return new WP_Error( 'no_posts', __( 'No posts to install.' ), array( 'status' => 500 ) ); } $new_pages = []; foreach ( $pages as $post_data ) { $args = [ 'post_title' => $post_data['post_title'], 'post_content' => $post_data['post_content'], 'post_status' => 'publish', 'post_type' => 'page', ]; $page_id = wp_insert_post(wp_slash( $args ) ); if ( is_wp_error( $page_id ) ) { return new WP_Error( 'install_failed', __( 'Install failed.' ), array( 'status' => 500 ) ); } update_post_meta( $page_id, '_kad_post_title', 'hide' ); update_post_meta( $page_id, '_kad_post_content_style', 'unboxed' ); update_post_meta( $page_id, '_kad_post_vertical_padding', 'hide' ); update_post_meta( $page_id, '_kad_post_feature', 'hide' ); update_post_meta( $page_id, '_kad_post_layout', 'fullwidth' ); update_post_meta( $page_id, '_kadence_starter_templates_imported_post', true ); $new_pages[] = $page_id; if ( isset( $post_data['post_title'] ) && 'Home' === $post_data['post_title'] ) { update_option( 'page_on_front', $page_id ); update_option( 'show_on_front', 'page' ); } } return $new_pages; } /** * Trigger writing cache. * * @access public * @return void */ public function trigger_writing_cache() { $this->block_library_cache->terminate(); $this->ai_cache->terminate(); } /** * Get remote file contents. * * @access public * @return string Returns the remote URL contents. */ public function get_remote_image_collections() { $api_url = $this->remote_ai_url . 'images/collections'; $response = wp_safe_remote_get( $api_url, array( 'timeout' => 20, 'headers' => array( 'X-Prophecy-Token' => $this->get_token_header(), ), ) ); // Early exit if there was an error. if ( is_wp_error( $response ) || $this->is_response_code_error( $response ) ) { return 'error'; } // Get the CSS from our response. $contents = wp_remote_retrieve_body( $response ); // Early exit if there was an error. if ( is_wp_error( $contents ) ) { return 'error'; } return $contents; } /** * Install Navigation. * * @param string $site_id The site ID. * @param array $install_goals The install goals. * @return bool|WP_Error True on success, or WP_Error object on failure. */ public function install_navigation( $site_id, $install_goals ) { $install_goal = ( isset( $install_goals[0] ) ? $install_goals[0] : '' ); $url = 'https://base.startertemplatecloud.com/' . $site_id . '/wp-json/kadence-starter-base/v1/navigation'; $response = wp_safe_remote_get( $url, array( 'timeout' => 20, ) ); // Early exit if there was an error. if ( is_wp_error( $response ) || $this->is_response_code_error( $response ) ) { return new WP_Error( 'install_failed', __( 'Could not get navigation from source.' ), array( 'status' => 500 ) ); } // Get the body from our response. $navigation = wp_remote_retrieve_body( $response ); // Early exit if there was an error. if ( is_wp_error( $navigation ) ) { return new WP_Error( 'install_failed', __( 'Could not get navigation from source.' ), array( 'status' => 500 ) ); } $navigation = json_decode( $navigation, true ); if ( ! is_array( $navigation ) ) { return new WP_Error( 'install_failed', __( 'Could not get navigation from source.' ), array( 'status' => 500 ) ); } $data = array(); foreach ( $navigation as $location_key => $menu ) { $menu_exists = wp_get_nav_menu_object( $menu['name'] ); if ( $menu_exists ) { if ( $location_key !== 'primary' && $navigation[$location_key] === $navigation['primary'] ) { $locations = get_theme_mod( 'nav_menu_locations' ); $locations[ $location_key ] = $menu_exists->term_id; set_theme_mod( 'nav_menu_locations', $locations ); continue; } else { wp_delete_nav_menu( $menu_exists->term_id ); } } $menu_id = wp_create_nav_menu( $menu['name'] ); $updates = array(); $extra_order = 0; // Set up default menu items foreach ( $menu['items'] as $item ) { if ( 'Shop' === $item['title'] ) { $extra_order = $item['menu_order']; continue; } $args = array( 'menu-item-title' => $item['title'], 'menu-item-url' => '#', 'menu-item-status' => 'publish', 'menu-item-position' => $item['menu_order'], ); // Lets not duplicate pages. $has_page = get_posts( [ 'post_type' => 'page', 'title' => $item['title'], ] ); if ( $has_page ) { $args = array( 'menu-item-title' => get_the_title( $has_page[0]->ID ), 'menu-item-object-id' => $has_page[0]->ID, 'menu-item-object' => 'page', 'menu-item-status' => 'publish', 'menu-item-type' => 'post_type', 'menu-item-position' => $item['menu_order'], ); } else if ( ! empty( $item['title'] ) && 'Blog' === $item['title'] ) { // Create Blog page using wp_insert_post $page_id = wp_insert_post( array( 'post_title' => wp_strip_all_tags( $item['title'] ), 'post_content' => '', 'post_status' => 'publish', 'post_type' => 'page', ) ); if ( ! is_wp_error( $page_id ) ) { $args = array( 'menu-item-title' => $item['title'], 'menu-item-object-id' => $page_id, 'menu-item-object' => 'page', 'menu-item-status' => 'publish', 'menu-item-type' => 'post_type', 'menu-item-position' => $item['menu_order'], ); update_option( 'page_for_posts', $page_id ); update_post_meta( $page_id, '_kadence_starter_templates_imported_post', true ); } } if ( ! empty( $item['menu_item_parent'] ) ) { $args['menu-item-parent-id'] = $updates[ $item['menu_item_parent'] ]; } $updates[ $item['id'] ] = wp_update_nav_menu_item( $menu_id, 0, $args ); } update_term_meta( $menu_id, '_kadence_starter_templates_imported_term', true ); $header_button_text = 'Get Started'; $header_button_url = '#'; $extra_added = false; if ( $location_key === 'primary' || $location_key === 'mobile' ) { if ( 'events' === $install_goal && post_type_exists( 'tribe_events' ) ) { $args = array( 'menu-item-title' => 'Events', 'menu-item-url' => get_post_type_archive_link( 'tribe_events' ), 'menu-item-status' => 'publish', 'menu-item-position' => $extra_order, ); $item_id = wp_update_nav_menu_item( $menu_id, 0, $args ); $header_button_text = 'Calendar'; $header_button_url = get_post_type_archive_link( 'tribe_events' ); $extra_added = true; } else if ( 'tickets' === $install_goal ) { $has_page = get_posts( [ 'post_type' => 'page', 'title' => 'Pricing', ] ); if ( $has_page ) { $args = array( 'menu-item-title' => 'Pricing', 'menu-item-object-id' => $has_page[0]->ID, 'menu-item-object' => 'page', 'menu-item-status' => 'publish', 'menu-item-type' => 'post_type', 'menu-item-position' => $extra_order, ); $item_id = wp_update_nav_menu_item( $menu_id, 0, $args ); $header_button_text = 'Get Tickets'; $header_button_url = get_the_permalink( $has_page[0]->ID ); $extra_added = true; } } else if ( 'ecommerce' === $install_goal && class_exists( 'WooCommerce' ) ) { $page_id = wc_get_page_id( 'shop' ); $shop_page = get_post( $page_id ); if ( ! $shop_page ) { // Create Shop page using wp_insert_post $page_id = wp_insert_post( array( 'post_title' => 'Shop', 'post_content' => '', 'post_status' => 'publish', 'post_type' => 'page', ) ); if ( ! is_wp_error( $page_id ) ) { update_option( 'woocommerce_shop_page_id', $page_id ); update_post_meta( $page_id, '_kadence_starter_templates_imported_post', true ); } } if ( ! empty( $page_id ) && ! is_wp_error( $page_id ) ) { $args = array( 'menu-item-title' => 'Shop', 'menu-item-object-id' => $page_id, 'menu-item-object' => 'page', 'menu-item-status' => 'publish', 'menu-item-type' => 'post_type', 'menu-item-position' => $extra_order, ); $item_id = wp_update_nav_menu_item( $menu_id, 0, $args ); $header_button_text = 'Shop Now'; $header_button_url = get_the_permalink( $page_id ); $extra_added = true; } } else if ( 'courses' === $install_goal && post_type_exists( 'sfwd-courses' ) ) { // Lets not duplicate pages. $has_page = get_posts( [ 'post_type' => 'page', 'title' => 'Courses', ] ); if ( $has_page ) { $args = array( 'menu-item-title' => get_the_title( $has_page[0]->ID ), 'menu-item-object-id' => $has_page[0]->ID, 'menu-item-object' => 'page', 'menu-item-status' => 'publish', 'menu-item-type' => 'post_type', 'menu-item-position' => $extra_order, ); $item_id = wp_update_nav_menu_item( $menu_id, 0, $args ); $header_button_text = 'View Courses'; $header_button_url = get_the_permalink( $has_page[0]->ID ); $extra_added = true; } else { if ( defined( 'LEARNDASH_COURSE_GRID_VERSION' ) ) { $page_content = ''; // Create Courses using wp_insert_post $page_id = wp_insert_post( array( 'post_title' => 'Courses', 'post_name' => 'our-courses', 'post_content' => $page_content, 'post_status' => 'publish', 'post_type' => 'page', ) ); if ( ! is_wp_error( $page_id ) ) { update_post_meta( $page_id, '_kadence_starter_templates_imported_post', true ); update_post_meta( $page_id, '_kad_post_layout', 'normal' ); $args = array( 'menu-item-title' => 'Courses', 'menu-item-object-id' => $page_id, 'menu-item-object' => 'page', 'menu-item-status' => 'publish', 'menu-item-type' => 'post_type', 'menu-item-position' => $extra_order, ); $item_id = wp_update_nav_menu_item( $menu_id, 0, $args ); $header_button_text = 'View Courses'; $header_button_url = get_the_permalink( $page_id ); $extra_added = true; } } else { $args = array( 'menu-item-title' => 'Courses', 'menu-item-url' => get_post_type_archive_link( 'sfwd-courses' ), 'menu-item-status' => 'publish', 'menu-item-position' => $extra_order, ); $item_id = wp_update_nav_menu_item( $menu_id, 0, $args ); $extra_added = true; $header_button_text = 'View Courses'; $header_button_url = get_post_type_archive_link( 'sfwd-courses' ); } } } else if ( 'donations' === $install_goal ) { // Find the our mission page. $has_page = get_posts( [ 'post_type' => 'page', 'title' => 'Our Mission', ] ); if ( $has_page ) { $args = array( 'menu-item-title' => get_the_title( $has_page[0]->ID ), 'menu-item-object-id' => $has_page[0]->ID, 'menu-item-object' => 'page', 'menu-item-status' => 'publish', 'menu-item-type' => 'post_type', 'menu-item-position' => $extra_order, ); $item_id = wp_update_nav_menu_item( $menu_id, 0, $args ); $header_button_text = 'Donate Now'; $header_button_url = get_the_permalink( $has_page[0]->ID ); $extra_added = true; } } else if ( 'services' === $install_goal ) { // Find the our mission page. $has_page = get_posts( [ 'post_type' => 'page', 'title' => 'Services', ] ); if ( $has_page ) { $args = array( 'menu-item-title' => get_the_title( $has_page[0]->ID ), 'menu-item-object-id' => $has_page[0]->ID, 'menu-item-object' => 'page', 'menu-item-status' => 'publish', 'menu-item-type' => 'post_type', 'menu-item-position' => $extra_order, ); $item_id = wp_update_nav_menu_item( $menu_id, 0, $args ); $header_button_text = 'Get Started'; $header_button_url = get_the_permalink( $has_page[0]->ID ); $extra_added = true; } } else if ( 'landing' === $install_goal || 'booking' === $install_goal || 'membership' === $install_goal ) { // Find the our pricing page. $has_page = get_posts( [ 'post_type' => 'page', 'title' => 'Pricing', ] ); if ( $has_page ) { $args = array( 'menu-item-title' => get_the_title( $has_page[0]->ID ), 'menu-item-object-id' => $has_page[0]->ID, 'menu-item-object' => 'page', 'menu-item-status' => 'publish', 'menu-item-type' => 'post_type', 'menu-item-position' => $extra_order, ); $item_id = wp_update_nav_menu_item( $menu_id, 0, $args ); $header_button_text = 'Get Started'; $header_button_url = get_the_permalink( $has_page[0]->ID ); $extra_added = true; } } else if ( 'blogging' === $install_goal ) { // Create Blog page using wp_insert_post $page_id = wp_insert_post( array( 'post_title' => 'Blog', 'post_content' => '', 'post_status' => 'publish', 'post_type' => 'page', ) ); if ( ! is_wp_error( $page_id ) ) { $args = array( 'menu-item-title' => 'Blog', 'menu-item-object-id' => $page_id, 'menu-item-object' => 'page', 'menu-item-status' => 'publish', 'menu-item-type' => 'post_type', 'menu-item-position' => $extra_order, ); $item_id = wp_update_nav_menu_item( $menu_id, 0, $args ); $extra_added = true; update_option( 'page_for_posts', $page_id ); update_post_meta( $page_id, '_kadence_starter_templates_imported_post', true ); } $has_page = get_posts( [ 'post_type' => 'page', 'title' => 'Contact', ] ); if ( $has_page ) { $header_button_text = 'Subscribe'; $header_button_url = get_the_permalink( $has_page[0]->ID ); } } else if ( 'podcasting' === $install_goal ) { // Create Blog page using wp_insert_post $page_id = wp_insert_post( array( 'post_title' => 'Podcast', 'post_content' => '', 'post_status' => 'publish', 'post_type' => 'page', ) ); if ( ! is_wp_error( $page_id ) ) { $args = array( 'menu-item-title' => 'Podcast', 'menu-item-object-id' => $page_id, 'menu-item-object' => 'page', 'menu-item-status' => 'publish', 'menu-item-type' => 'post_type', 'menu-item-position' => $extra_order, ); $item_id = wp_update_nav_menu_item( $menu_id, 0, $args ); $extra_added = true; update_option( 'page_for_posts', $page_id ); update_post_meta( $page_id, '_kadence_starter_templates_imported_post', true ); } $has_page = get_posts( [ 'post_type' => 'page', 'title' => 'Contact', ] ); if ( $has_page ) { $header_button_text = 'Subscribe'; $header_button_url = get_the_permalink( $has_page[0]->ID ); } } else if ( 'photography' === $install_goal ) { // Find the our Gallery page. $has_page = get_posts( [ 'post_type' => 'page', 'title' => 'Gallery', ] ); if ( $has_page ) { $args = array( 'menu-item-title' => get_the_title( $has_page[0]->ID ), 'menu-item-object-id' => $has_page[0]->ID, 'menu-item-object' => 'page', 'menu-item-status' => 'publish', 'menu-item-type' => 'post_type', 'menu-item-position' => $extra_order, ); $item_id = wp_update_nav_menu_item( $menu_id, 0, $args ); $extra_added = true; } $has_page = get_posts( [ 'post_type' => 'page', 'title' => 'Contact', ] ); if ( $has_page ) { $header_button_text = 'Contact Me'; $header_button_url = get_the_permalink( $has_page[0]->ID ); } } } // Update the header button text and url. set_theme_mod( 'header_button_label', $header_button_text ); set_theme_mod( 'header_button_link', $header_button_url ); // if ( ! $extra_added ) { // // Check if any of the goals contained in $install_goals are installed. // } $locations = get_theme_mod( 'nav_menu_locations' ); $locations[ $location_key ] = $menu_id; set_theme_mod( 'nav_menu_locations', $locations ); } // Make sure woocommerce pages are built and set. if ( class_exists( 'WooCommerce' ) ) { if ( is_callable( 'WC_Install::create_pages' ) ) { WC_Install::create_pages(); } } flush_rewrite_rules(); wp_cache_flush(); return true; } /** * Available widgets. * * Gather site's widgets into array with ID base, name, etc. * * @global array $wp_registered_widget_controls * @return array $available_widgets, Widget information */ private function available_widgets() { global $wp_registered_widget_controls; $widget_controls = $wp_registered_widget_controls; $available_widgets = array(); foreach ( $widget_controls as $widget ) { if ( ! empty( $widget['id_base'] ) && ! isset( $available_widgets[ $widget['id_base'] ] ) ) { $available_widgets[ $widget['id_base'] ]['id_base'] = $widget['id_base']; $available_widgets[ $widget['id_base'] ]['name'] = $widget['name']; } } return $available_widgets; } /** * Move footer widgets to inactive. */ public function move_widgets_to_inactive() { // Get all widgets. $sidebars_widgets = wp_get_sidebars_widgets(); // Check if the footer widget areas are set and not empty. foreach ( array( 'sidebar-primary', 'sidebar-secondary', 'footer1', 'footer2', 'footer3', 'footer4', 'footer5', 'footer6' ) as $widget_area ) { if ( ! empty( $sidebars_widgets[ $widget_area ] ) ) { // Move all footer-1 widgets to inactive widgets. foreach ( $sidebars_widgets[ $widget_area ] as $widget_id ) { $sidebars_widgets['wp_inactive_widgets'][] = $widget_id; } $sidebars_widgets[ $widget_area ] = array(); } } // Save the updated widgets configuration. wp_set_sidebars_widgets( $sidebars_widgets ); } /** * Install Widgets. * * @param string $site_id The site ID. * @param string $site_name The site name. * @return array The results. */ public function install_widgets( $site_id, $site_name ) { global $wp_registered_sidebars; $url = 'https://base.startertemplatecloud.com/' . $site_id . '/wp-json/kadence-starter-base/v1/widgets'; $response = wp_safe_remote_get( $url, array( 'timeout' => 20, ) ); // Early exit if there was an error. if ( is_wp_error( $response ) || $this->is_response_code_error( $response ) ) { return new WP_Error( 'install_failed', __( 'Could not get widgets from source.' ), array( 'status' => 500 ) ); } // Get the body from our response. $sidebars = wp_remote_retrieve_body( $response ); // Early exit if there was an error. if ( empty( $sidebars ) ) { return rest_ensure_response( 'no widgets to import' ); } // Early exit if there was an error. if ( is_wp_error( $sidebars ) ) { return new WP_Error( 'install_failed', __( 'Could not get widgets from source.' ), array( 'status' => 500 ) ); } $sidebars = json_decode( $sidebars, true ); if ( ! is_array( $sidebars ) ) { return new WP_Error( 'install_failed', __( 'Could not get widgets from source.' ), array( 'status' => 500 ) ); } $this->move_widgets_to_inactive(); // Get all available widgets site supports. $available_widgets = $this->available_widgets(); // Begin results. $results = array(); foreach ( $sidebars as $sidebar_id => $widgets ) { // Skip inactive widgets (should not be in export). if ( 'wp_inactive_widgets' == $sidebar_id ) { continue; } // Check if sidebar is available on this site. Otherwise add widgets to inactive, and say so. if ( isset( $wp_registered_sidebars[ $sidebar_id ] ) ) { $sidebar_available = true; $use_sidebar_id = $sidebar_id; $sidebar_message_type = 'success'; $sidebar_message = ''; } else { $sidebar_available = false; $use_sidebar_id = 'wp_inactive_widgets'; // Add to inactive if sidebar does not exist in theme. $sidebar_message_type = 'error'; $sidebar_message = __( 'Sidebar does not exist in theme (moving widget to Inactive)', 'kadence-starter-templates' ); } // Result for sidebar. $results[ $sidebar_id ]['name'] = ! empty( $wp_registered_sidebars[ $sidebar_id ]['name'] ) ? $wp_registered_sidebars[ $sidebar_id ]['name'] : $sidebar_id; // Sidebar name if theme supports it; otherwise ID. $results[ $sidebar_id ]['message_type'] = $sidebar_message_type; $results[ $sidebar_id ]['message'] = $sidebar_message; $results[ $sidebar_id ]['widgets'] = array(); // Loop widgets. foreach ( $widgets as $widget_instance_id => $widget ) { $fail = false; // Get id_base (remove -# from end) and instance ID number. $id_base = preg_replace( '/-[0-9]+$/', '', $widget_instance_id ); $instance_id_number = str_replace( $id_base . '-', '', $widget_instance_id ); // Does site support this widget? if ( ! $fail && ! isset( $available_widgets[ $id_base ] ) ) { $fail = true; $widget_message_type = 'error'; $widget_message = __( 'Site does not support widget', 'kadence-starter-templates' ); // Explain why widget not imported. } // Convert multidimensional objects to multidimensional arrays. // Some plugins like Jetpack Widget Visibility store settings as multidimensional arrays. // Without this, they are imported as objects and cause fatal error on Widgets page. $widget = json_decode( json_encode( $widget ), true ); // Filter to modify settings array. $widget = apply_filters( 'kadence-starter-templates/rest_widget_settings_array', $widget ); // Skip (no changes needed), if this is not a custom menu widget. if ( array_key_exists( 'nav_menu', $widget ) && ! empty( $widget['nav_menu'] ) && ! is_int( $widget['nav_menu'] ) ) { $menu_exists = wp_get_nav_menu_object( $widget['nav_menu'] ); if ( $menu_exists ) { $widget['nav_menu'] = $menu_exists->term_id; } } if ( ! empty( $widget['content'] ) ) { $widget['content'] = str_replace( 'Redwood', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Laurel', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Acorn', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Cedar', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Maple', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Sequoia', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Acacia', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Magnolia', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Willow', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Hemlock', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Fig', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Aspen', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Juniper', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Almond', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Elm', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Mahogany', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Oakleaf', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Olive', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Pinecone', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Birch', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Cherry', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Beech', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Cypress', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Fir', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Eucalyptus', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Banyan', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Ash', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Sycamore', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Palm', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Hawthorn', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Chestnut', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Mango', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Pecan', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Baobab', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Teak', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Apple', $site_name, $widget['content'] ); $widget['content'] = str_replace( 'Pear', $site_name, $widget['content'] ); } // No failure. if ( ! $fail ) { // Add widget instance. $single_widget_instances = get_option( 'widget_' . $id_base ); // All instances for that widget ID base, get fresh every time. $single_widget_instances = ! empty( $single_widget_instances ) ? $single_widget_instances : array( '_multiwidget' => 1 ); // Start fresh if have to. $single_widget_instances[] = $widget; // Add it. // Get the key it was given. end( $single_widget_instances ); $new_instance_id_number = key( $single_widget_instances ); // If key is 0, make it 1. // When 0, an issue can occur where adding a widget causes data from other widget to load, and the widget doesn't stick (reload wipes it). if ( '0' === strval( $new_instance_id_number ) ) { $new_instance_id_number = 1; $single_widget_instances[ $new_instance_id_number ] = $single_widget_instances[0]; unset( $single_widget_instances[0] ); } // Move _multiwidget to end of array for uniformity. if ( isset( $single_widget_instances['_multiwidget'] ) ) { $multiwidget = $single_widget_instances['_multiwidget']; unset( $single_widget_instances['_multiwidget'] ); $single_widget_instances['_multiwidget'] = $multiwidget; } // Update option with new widget. update_option( 'widget_' . $id_base, $single_widget_instances ); // Assign widget instance to sidebar. $sidebars_widgets = get_option( 'sidebars_widgets' ); // Which sidebars have which widgets, get fresh every time. // Avoid rarely fatal error when the option is an empty string // https://github.com/churchthemes/widget-importer-exporter/pull/11. if ( ! $sidebars_widgets ) { $sidebars_widgets = array(); } $new_instance_id = $id_base . '-' . $new_instance_id_number; // Use ID number from new widget instance. $sidebars_widgets[ $use_sidebar_id ][] = $new_instance_id; // Add new instance to sidebar. update_option( 'sidebars_widgets', $sidebars_widgets ); // Save the amended data. // After widget import action. $after_widget_import = array( 'sidebar' => $use_sidebar_id, 'sidebar_old' => $sidebar_id, 'widget' => $widget, 'widget_type' => $id_base, 'widget_id' => $new_instance_id, 'widget_id_old' => $widget_instance_id, 'widget_id_num' => $new_instance_id_number, 'widget_id_num_old' => $instance_id_number, ); // Success message. if ( $sidebar_available ) { $widget_message_type = 'success'; $widget_message = __( 'Imported', 'kadence-starter-templates' ); } else { $widget_message_type = 'warning'; $widget_message = __( 'Imported to Inactive', 'kadence-starter-templates' ); } } // Result for widget instance. $results[ $sidebar_id ]['widgets'][ $widget_instance_id ]['name'] = isset( $available_widgets[ $id_base ]['name'] ) ? $available_widgets[ $id_base ]['name'] : $id_base; // Widget name or ID if name not available (not supported by site). $results[ $sidebar_id ]['widgets'][ $widget_instance_id ]['title'] = ! empty( $widget['title'] ) ? $widget['title'] : __( 'No Title', 'kadence-starter-templates' ); // Show "No Title" if widget instance is untitled. $results[ $sidebar_id ]['widgets'][ $widget_instance_id ]['message_type'] = $widget_message_type; $results[ $sidebar_id ]['widgets'][ $widget_instance_id ]['message'] = $widget_message; } } return $results; } /** * Install Pages. * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function install_settings( $site_id, $site_name, $color_palette, $dark_footer, $fonts, $donation_form_id = '' ) { if ( empty( $site_id ) ) { return new WP_Error( 'instal_failed', __( 'No settings set.' ), array( 'status' => 500 ) ); } $url = 'https://base.startertemplatecloud.com/' . $site_id . '/wp-json/kadence-starter-base/v1/settings'; $response = wp_safe_remote_get( $url, array( 'timeout' => 20, ) ); // Early exit if there was an error. if ( is_wp_error( $response ) || $this->is_response_code_error( $response ) ) { return new WP_Error( 'install_failed', __( 'Could not get settings from source.' ), array( 'status' => 500 ) ); } // Get the body from our response. $settings = wp_remote_retrieve_body( $response ); // Early exit if there was an error. if ( is_wp_error( $settings ) ) { return new WP_Error( 'install_failed', __( 'Could not get settings from source.' ), array( 'status' => 500 ) ); } $settings = json_decode( $settings, true ); if ( ! is_array( $settings ) ) { return new WP_Error( 'install_failed', __( 'Could not get settings from source.' ), array( 'status' => 500 ) ); } $data = array(); // Clear out the theme mods. delete_option( 'theme_mods_' . get_option( 'stylesheet' ) ); if ( isset( $settings['mods'] ) ) { $data['mods'] = $this->process_options_images( $settings['mods'] ); } if ( isset( $settings['wp_css'] ) ) { $data['wp_css'] = $settings['wp_css']; } if ( isset( $settings['options'] ) ) { $keys = array_keys( $settings['options'] ); $keys = array_map( 'sanitize_key', $keys ); $values = array_values( $settings['options'] ); $values = array_map( 'sanitize_text_field', $values ); $options_array = array_combine( $keys, $values ); $data['options'] = $options_array; } // Set the site name. if ( ! empty( $site_name ) ) { update_option( 'blogname', $site_name ); } $primary_color = ''; // Import custom options. if ( isset( $data['options'] ) && is_array( $data['options'] ) ) { foreach ( $data['options'] as $option_key => $option_value ) { update_option( $option_key, $option_value ); if ( 'kadence_global_palette' === $option_key ) { $palette = json_decode( $option_value, true ); $active = ( ! empty($palette['active'] ) ? $palette['active'] : 'palette' ); $primary_color = ( ! empty( $palette[$active][0]['color'] ) ? $palette[$active][0]['color'] : '' ); } } } // Loop through the mods. foreach ( $data['mods'] as $key => $val ) { // Save the mod. set_theme_mod( $key, $val ); } if ( empty( $color_palette['colors'] ) && ! empty( $primary_color ) && ! empty( $donation_form_id ) ) { $this->update_donation_form_primary_color( $primary_color, $donation_form_id ); } if ( ! empty( $color_palette ) ) { if ( ! empty( $color_palette['colors'] ) && is_array( $color_palette['colors'] ) ) { $palette = get_option( 'kadence_global_palette' ); if ( ! empty( $palette ) ) { $palette = json_decode( $palette, true ); $palette['palette'][0]['color'] = $color_palette['colors'][0]; $palette['palette'][1]['color'] = $color_palette['colors'][1]; $palette['palette'][2]['color'] = $color_palette['colors'][2]; $palette['palette'][3]['color'] = $color_palette['colors'][3]; $palette['palette'][4]['color'] = $color_palette['colors'][4]; $palette['palette'][5]['color'] = $color_palette['colors'][5]; $palette['palette'][6]['color'] = $color_palette['colors'][6]; $palette['palette'][7]['color'] = $color_palette['colors'][7]; $palette['palette'][8]['color'] = $color_palette['colors'][8]; $palette['active'] = 'palette'; update_option( 'kadence_global_palette', json_encode( $palette ) ); } if ( ! empty( $color_palette['btnColor'] ) ) { set_theme_mod( 'buttons_color', array( 'color' => $color_palette['btnColor'], 'hover' => $color_palette['btnColor'], ) ); } if ( isset( $color_palette['isLight'] ) && ! $color_palette['isLight'] ) { if ( isset( $dark_footer ) && $dark_footer ) { $color_check = array( 'palette3', 'palette4', 'palette5', 'palette6', 'palette7', 'palette8', 'palette9', ); $color_conversion = array( 'palette3' => 'palette7', 'palette4' => 'palette8', 'palette5' => 'palette9', 'palette6' => 'palette9', 'palette7' => 'palette6', 'palette8' => 'palette5', 'palette9' => 'palette4', ); foreach ( array( 'footer_wrap_background', 'footer_top_background', 'footer_middle_background', 'footer_bottom_background' ) as $footer_area ) { $footer_area_mod = get_theme_mod( $footer_area ); if ( ! empty( $footer_area_mod['desktop']['color'] ) && in_array( $footer_area_mod['desktop']['color'], $color_check ) ) { $footer_area_mod['desktop']['color'] = $color_conversion[ $footer_area_mod['desktop']['color'] ]; set_theme_mod( $footer_area, $footer_area_mod ); } } foreach ( array( 'footer_top_widget_title', 'footer_top_widget_content', 'footer_middle_widget_title', 'footer_middle_widget_content', 'footer_bottom_widget_title', 'footer_bottom_widget_content', 'footer_html_typography' ) as $footer_title ) { $footer_title_mod = get_theme_mod( $footer_title ); if ( ! empty( $footer_title_mod['color'] ) && in_array( $footer_title_mod['color'], $color_check ) ) { $footer_title_mod['color'] = $color_conversion[ $footer_title_mod['color'] ]; set_theme_mod( $footer_title, $footer_title_mod ); } } foreach ( array( 'footer_top_widget_content_color', 'footer_middle_widget_content_color', 'footer_bottom_widget_content_color', 'footer_navigation_color', 'footer_navigation_background', 'footer_social_color', 'footer_social_background', 'footer_social_border_colors', 'footer_html_link_color' ) as $footer_color ) { $footer_color_mod = get_theme_mod( $footer_color ); $update = false; if ( ! empty( $footer_color_mod['color'] ) && in_array( $footer_color_mod['color'], $color_check ) ) { $footer_color_mod['color'] = $color_conversion[ $footer_color_mod['color'] ]; $update = true; } if ( ! empty( $footer_color_mod['hover'] ) && in_array( $footer_color_mod['hover'], $color_check ) ) { $footer_color_mod['hover'] = $color_conversion[ $footer_color_mod['hover'] ]; $update = true; } if ( ! empty( $footer_color_mod['active'] ) && in_array( $footer_color_mod['active'], $color_check ) ) { $footer_color_mod['active'] = $color_conversion[ $footer_color_mod['active'] ]; $update = true; } if ( $update ) { set_theme_mod( $footer_color, $footer_color_mod ); } } foreach ( array( 'footer_top_top_border', 'footer_top_bottom_border', 'footer_top_column_border', 'footer_middle_top_border', 'footer_middle_bottom_border', 'footer_middle_column_border', 'footer_bottom_top_border', 'footer_bottom_bottom_border', 'footer_bottom_column_border' ) as $footer_border ) { $footer_border_mod = get_theme_mod( $footer_border ); $update = false; if ( ! empty( $footer_border_mod['desktop']['color'] ) && in_array( $footer_border_mod['desktop']['color'], $color_check ) ) { $footer_border_mod['desktop']['color'] = $color_conversion[ $footer_border_mod['desktop']['color'] ]; $update = true; } if ( ! empty( $footer_border_mod['tablet']['color'] ) && in_array( $footer_border_mod['tablet']['color'], $color_check ) ) { $footer_border_mod['tablet']['color'] = $color_conversion[ $footer_border_mod['tablet']['color'] ]; $update = true; } if ( ! empty( $footer_border_mod['mobile']['color'] ) && in_array( $footer_border_mod['mobile']['color'], $color_check ) ) { $footer_border_mod['mobile']['color'] = $color_conversion[ $footer_border_mod['mobile']['color'] ]; $update = true; } if ( $update ) { set_theme_mod( $footer_border, $footer_border_mod ); } } } } } } // If wp_css is set then import it. if ( function_exists( 'wp_update_custom_css_post' ) && isset( $data['wp_css'] ) && '' !== $data['wp_css'] ) { wp_update_custom_css_post( $data['wp_css'] ); } if ( ! empty( $fonts ) ) { if ( ! empty( $fonts['font'] ) ) { switch ( $fonts['font'] ) { case 'montserrat': $current = \Kadence\kadence()->option( 'heading_font' ); $current['family'] = 'Montserrat'; $current['google'] = true; $current['variant'] = array( '100', '100italic', '200', '200italic', '300', '300italic', 'regular', 'italic', '500', '500italic', '600', '600italic', '700', '700italic', '800', '800italic', '900', '900italic' ); set_theme_mod( 'heading_font', $current ); $body = \Kadence\kadence()->option( 'base_font' ); $body['family'] = 'Source Sans Pro'; $body['google'] = true; set_theme_mod( 'base_font', $body ); break; case 'playfair': $current = \Kadence\kadence()->option( 'heading_font' ); $current['family'] = 'Playfair Display'; $current['google'] = true; $current['variant'] = array( 'regular', 'italic', '500', '500italic', '600', '600italic', '700', '700italic', '800', '800italic', '900', '900italic' ); set_theme_mod( 'heading_font', $current ); $h1_font = \Kadence\kadence()->option( 'h1_font' ); $h1_font['weight'] = 'normal'; $h1_font['variant'] = 'regualar'; set_theme_mod( 'h1_font', $h1_font ); $h2_font = \Kadence\kadence()->option( 'h2_font' ); $h2_font['weight'] = 'normal'; $h2_font['variant'] = 'regualar'; set_theme_mod( 'h2_font', $h2_font ); $h3_font = \Kadence\kadence()->option( 'h3_font' ); $h3_font['weight'] = 'normal'; $h3_font['variant'] = 'regualar'; set_theme_mod( 'h3_font', $h3_font ); $h4_font = \Kadence\kadence()->option( 'h4_font' ); $h4_font['weight'] = 'normal'; $h4_font['variant'] = 'regualar'; set_theme_mod( 'h4_font', $h4_font ); $h5_font = \Kadence\kadence()->option( 'h5_font' ); $h5_font['weight'] = 'normal'; $h5_font['variant'] = 'regualar'; set_theme_mod( 'h5_font', $h5_font ); $body = \Kadence\kadence()->option( 'base_font' ); $body['family'] = 'Raleway'; $body['google'] = true; set_theme_mod( 'base_font', $body ); break; case 'oswald': $current = \Kadence\kadence()->option( 'heading_font' ); $current['family'] = 'Oswald'; $current['google'] = true; $current['variant'] = array( '200', '300', 'regular', '500', '600', '700' ); set_theme_mod( 'heading_font', $current ); $body = \Kadence\kadence()->option( 'base_font' ); $body['family'] = 'Open Sans'; $body['google'] = true; set_theme_mod( 'base_font', $body ); break; case 'antic': $current = \Kadence\kadence()->option( 'heading_font' ); $current['family'] = 'Antic Didone'; $current['google'] = true; $current['variant'] = array( 'regular' ); set_theme_mod( 'heading_font', $current ); $h1_font = \Kadence\kadence()->option( 'h1_font' ); $h1_font['weight'] = 'normal'; $h1_font['variant'] = 'regualar'; set_theme_mod( 'h1_font', $h1_font ); $h2_font = \Kadence\kadence()->option( 'h2_font' ); $h2_font['weight'] = 'normal'; $h2_font['variant'] = 'regualar'; set_theme_mod( 'h2_font', $h2_font ); $h3_font = \Kadence\kadence()->option( 'h3_font' ); $h3_font['weight'] = 'normal'; $h3_font['variant'] = 'regualar'; set_theme_mod( 'h3_font', $h3_font ); $h4_font = \Kadence\kadence()->option( 'h4_font' ); $h4_font['weight'] = 'normal'; $h4_font['variant'] = 'regualar'; set_theme_mod( 'h4_font', $h4_font ); $h5_font = \Kadence\kadence()->option( 'h5_font' ); $h5_font['weight'] = 'normal'; $h5_font['variant'] = 'regualar'; set_theme_mod( 'h5_font', $h5_font ); $body = \Kadence\kadence()->option( 'base_font' ); $body['family'] = 'Raleway'; $body['google'] = true; set_theme_mod( 'base_font', $body ); break; case 'gilda': $current = \Kadence\kadence()->option( 'heading_font' ); $current['family'] = 'Gilda Display'; $current['google'] = true; $current['variant'] = array( 'regular' ); set_theme_mod( 'heading_font', $current ); $h1_font = \Kadence\kadence()->option( 'h1_font' ); $h1_font['weight'] = 'normal'; $h1_font['variant'] = 'regualar'; set_theme_mod( 'h1_font', $h1_font ); $h2_font = \Kadence\kadence()->option( 'h2_font' ); $h2_font['weight'] = 'normal'; $h2_font['variant'] = 'regualar'; set_theme_mod( 'h2_font', $h2_font ); $h3_font = \Kadence\kadence()->option( 'h3_font' ); $h3_font['weight'] = 'normal'; $h3_font['variant'] = 'regualar'; set_theme_mod( 'h3_font', $h3_font ); $h4_font = \Kadence\kadence()->option( 'h4_font' ); $h4_font['weight'] = 'normal'; $h4_font['variant'] = 'regualar'; set_theme_mod( 'h4_font', $h4_font ); $h5_font = \Kadence\kadence()->option( 'h5_font' ); $h5_font['weight'] = 'normal'; $h5_font['variant'] = 'regualar'; set_theme_mod( 'h5_font', $h5_font ); $body = \Kadence\kadence()->option( 'base_font' ); $body['family'] = 'Raleway'; $body['google'] = true; set_theme_mod( 'base_font', $body ); break; case 'cormorant': $current = \Kadence\kadence()->option( 'heading_font' ); $current['family'] = 'Cormorant Garamond'; $current['google'] = true; $current['variant'] = array( '300', '300italic', 'regular', 'italic', '500', '500italic', '600', '600italic', '700', '700italic' ); set_theme_mod( 'heading_font', $current ); $body = \Kadence\kadence()->option( 'base_font' ); $body['family'] = 'Proza Libre'; $body['google'] = true; set_theme_mod( 'base_font', $body ); break; case 'libre': $current = \Kadence\kadence()->option( 'heading_font' ); $current['family'] = 'Libre Franklin'; $current['google'] = true; $current['variant'] = array( '100', '100italic', '200', '200italic', '300', '300italic', 'regular', 'italic', '500', '500italic', '600', '600italic', '700', '700italic', '800', '800italic', '900', '900italic' ); set_theme_mod( 'heading_font', $current ); $body = \Kadence\kadence()->option( 'base_font' ); $body['family'] = 'Libre Baskerville'; $body['google'] = true; set_theme_mod( 'base_font', $body ); break; case 'lora': $current = \Kadence\kadence()->option( 'heading_font' ); $current['family'] = 'Lora'; $current['google'] = true; $current['variant'] = array( 'regular', 'italic', '500', '500italic', '600', '600italic', '700', '700italic' ); set_theme_mod( 'heading_font', $current ); $h1_font = \Kadence\kadence()->option( 'h1_font' ); $h1_font['weight'] = 'normal'; $h1_font['variant'] = 'regualar'; set_theme_mod( 'h1_font', $h1_font ); $h2_font = \Kadence\kadence()->option( 'h2_font' ); $h2_font['weight'] = 'normal'; $h2_font['variant'] = 'regualar'; set_theme_mod( 'h2_font', $h2_font ); $h3_font = \Kadence\kadence()->option( 'h3_font' ); $h3_font['weight'] = 'normal'; $h3_font['variant'] = 'regualar'; set_theme_mod( 'h3_font', $h3_font ); $h4_font = \Kadence\kadence()->option( 'h4_font' ); $h4_font['weight'] = 'normal'; $h4_font['variant'] = 'regualar'; set_theme_mod( 'h4_font', $h4_font ); $h5_font = \Kadence\kadence()->option( 'h5_font' ); $h5_font['weight'] = 'normal'; $h5_font['variant'] = 'regualar'; set_theme_mod( 'h5_font', $h5_font ); $body = \Kadence\kadence()->option( 'base_font' ); $body['family'] = 'Merriweather'; $body['google'] = true; $body['weight'] = '300'; $body['variant'] = '300'; set_theme_mod( 'base_font', $body ); break; case 'proza': $current = \Kadence\kadence()->option( 'heading_font' ); $current['family'] = 'Proza Libre'; $current['google'] = true; $current['variant'] = array( 'regular', 'italic', '500', '500italic', '600', '600italic', '700', '700italic', '800', '800italic' ); set_theme_mod( 'heading_font', $current ); $body = \Kadence\kadence()->option( 'base_font' ); $body['family'] = 'Open Sans'; $body['google'] = true; set_theme_mod( 'base_font', $body ); break; case 'worksans': $current = \Kadence\kadence()->option( 'heading_font' ); $current['family'] = 'Work Sans'; $current['google'] = true; $current['variant'] = array( '100', '100italic', '200', '200italic', '300', '300italic', 'regular', 'italic', '500', '500italic', '600', '600italic', '700', '700italic', '800', '800italic', '900', '900italic' ); set_theme_mod( 'heading_font', $current ); $body = \Kadence\kadence()->option( 'base_font' ); $body['family'] = 'Work Sans'; $body['google'] = true; set_theme_mod( 'base_font', $body ); break; case 'josefin': $current = \Kadence\kadence()->option( 'heading_font' ); $current['family'] = 'Josefin Sans'; $current['google'] = true; $current['variant'] = array( '100', '100italic', '200', '200italic', '300', '300italic', 'regular', 'italic', '500', '500italic', '600', '600italic', '700', '700italic' ); set_theme_mod( 'heading_font', $current ); $body = \Kadence\kadence()->option( 'base_font' ); $body['family'] = 'Lato'; $body['google'] = true; set_theme_mod( 'base_font', $body ); break; case 'nunito': $current = \Kadence\kadence()->option( 'heading_font' ); $current['family'] = 'Nunito'; $current['google'] = true; $current['variant'] = array( '200', '200italic', '300', '300italic', 'regular', 'italic', '600', '600italic', '700', '700italic', '800', '800italic', '900', '900italic' ); set_theme_mod( 'heading_font', $current ); $body = \Kadence\kadence()->option( 'base_font' ); $body['family'] = 'Roboto'; $body['google'] = true; set_theme_mod( 'base_font', $body ); break; case 'rubik': $current = \Kadence\kadence()->option( 'heading_font' ); $current['family'] = 'Rubik'; $current['google'] = true; $current['variant'] = array( '300', '300italic', 'regular', 'italic', '500', '500italic', '600', '600italic', '700', '700italic', '800', '800italic', '900', '900italic' ); set_theme_mod( 'heading_font', $current ); $body = \Kadence\kadence()->option( 'base_font' ); $body['family'] = 'Karla'; $body['google'] = true; set_theme_mod( 'base_font', $body ); break; } } } // Check if sitename is longer then 16 characters. if ( strlen( $site_name ) > 16 ) { $logo_font = \Kadence\kadence()->option( 'brand_typography' ); if ( isset( $logo_font['size']['desktop'] ) ) { $size = $logo_font['size']['desktop']; $size_type = ( ! empty( $logo_font['sizeType'] ) ? $logo_font['sizeType'] : 'px' ); if ( 'px' === $size_type ) { // Make the size 70% of the original size. $size = $size * 0.7; // Round to the nearest whole number. $size = round( $size ); $logo_font['size']['desktop'] = $size; set_theme_mod( 'brand_typography', $logo_font ); } } } // Setup Learndash. $this->setup_learndash(); // Check permalink settings: $current_permalink_structure = get_option( 'permalink_structure' ); // Check if permalinks are set to default. if ( empty( $current_permalink_structure ) ) { update_option( 'permalink_structure', '/%postname%/' ); } // Flush Permalinks. flush_rewrite_rules(); return true; } /** * Imports images for settings saved as mods. * * @since 0.1 * @access private * @param array $mods An array of customizer mods. * @return array The mods array with any new import data. */ private function process_options_images( $mods ) { foreach ( $mods as $key => $val ) { if ( $this->is_image_url( $val ) ) { $image = array( 'id' => 0, 'url' => $val, ); $data = $this->import_image( $image ); $mods[ $key ] = $data['url']; } } return $mods; } /** * Checks to see whether a string is an image url or not. * * @since 0.1 * @access private * @param string $string The string to check. * @return bool Whether the string is an image url or not. */ private function is_image_url( $string = '' ) { if ( is_string( $string ) ) { if ( preg_match( '/\.(jpg|jpeg|png|webp|gif)/i', $string ) ) { return true; } } return false; } /** * Get Posts. * * @param string $post_group The post group to get. * @return array/WP_Error The posts. */ public function get_remote_posts( $post_group = 'normal' ) { switch ( $post_group ) { case 'soap': $url = 'https://base.startertemplatecloud.com/g32/wp-json/kadence-starter-base/v1/posts'; break; default: $url = 'https://base.startertemplatecloud.com/wp-json/kadence-starter-base/v1/posts'; break; } // Get the response. $response = wp_safe_remote_get( $url, array( 'timeout' => 20, ) ); // Early exit if there was an error. if ( is_wp_error( $response ) || $this->is_response_code_error( $response ) ) { return new WP_Error( 'install_failed', __( 'Could not get posts from source.' ), array( 'status' => 500 ) ); } // Get the body from our response. $posts = wp_remote_retrieve_body( $response ); // Early exit if there was an error. if ( is_wp_error( $posts ) ) { return new WP_Error( 'install_failed', __( 'Could not get posts from source.' ), array( 'status' => 500 ) ); } $posts = json_decode( $posts, true ); if ( ! is_array( $posts ) ) { return new WP_Error( 'install_failed', __( 'Could not get posts from source.' ), array( 'status' => 500 ) ); } return $posts; } /** * Prepare Posts. * * @param array $posts The posts to install. * @param array $image_library The image library to use. * @return array The posts. */ public function prepare_posts( $posts, $image_library ) { if ( empty( $posts ) || ! is_array( $posts ) ) { return new WP_Error( 'no_posts', __( 'No posts to prepare.' ), array( 'status' => 500 ) ); } $i = 0; $prepared_posts = array(); foreach ( $posts as $post_data ) { $post_item = [ 'key' => $i, 'title' => $post_data['title'], 'categories' => $post_data['categories'], 'tags' => $post_data['tags'], 'image' => Image_Replacer::replace_images( $post_data['image'], $image_library, [], '', $i, [], false ), 'content' => Image_Replacer::replace_images( $post_data['content'], $image_library, [], '', $i, [], ), ]; $prepared_posts[] = $post_item; $i++; } return $prepared_posts; } /** * Install Posts Extras. * * @param array $posts The posts to install. * @param array $image_library The image library to use. * @return array The posts. */ public function install_posts_extras( $posts, $image_library ) { if ( empty( $posts ) ) { return new WP_Error( 'no_posts', __( 'No posts to install extras.' ), array( 'status' => 500 ) ); } $new_posts = array(); foreach ( $posts as $post_data ) { // Prepare Post content. $categories = $this->set_post_category_data( $post_data ); $tags = $this->set_post_tag_data( $post_data ); $downloaded_image = array(); if ( ! empty( $post_data['image'] ) ) { $image = array( 'url' => $post_data['image'], 'id' => 0, ); if ( substr( $post_data['image'], 0, strlen( 'https://images.pexels.com' ) ) === 'https://images.pexels.com' ) { $image_data = $this->get_image_info( $image_library, $post_data['image'] ); if ( $image_data ) { $alt = ! empty( $image_data['alt'] ) ? $image_data['alt'] : ''; $image['filename'] = ! empty( $image_data['filename'] ) ? $image_data['filename'] : $this->create_filename_from_alt( $alt ); $image['photographer'] = ! empty( $image_data['photographer'] ) ? $image_data['photographer'] : ''; $image['photographer_url'] = ! empty( $image_data['photographer_url'] ) ? $image_data['photographer_url'] : ''; $image['photograph_url'] = ! empty( $image_data['url'] ) ? $image_data['url'] : ''; $image['alt'] = $alt; $image['title'] = __( 'Photo by', 'kadence-starter-templates' ) . ' ' . $image['photographer']; } } $downloaded_image = $this->import_image( $image ); } $post_item = array( 'post_title' => ( isset( $post_data['title'] ) ? wp_strip_all_tags( $post_data['title'] ) : '' ), 'post_content' => $this->process_page_content( $post_data['content'], $image_library ), 'image' => ! empty( $downloaded_image['id'] ) ? $downloaded_image['id'] : '', 'categories' => $categories, 'tags' => $tags, ); $new_posts[] = $post_item; } if ( empty( $new_posts ) ) { return new WP_Error( 'install_failed', __( 'Install failed.' ), array( 'status' => 500 ) ); } return $new_posts; } /** * Install Posts. * * @param array $posts The posts to install. * @return bool/WP_Error The posts. */ public function install_posts( $posts ) { if ( empty( $posts ) ) { return new WP_Error( 'no_posts', __( 'No posts to install.' ), array( 'status' => 500 ) ); } foreach ( $posts as $post_data ) { $args = [ 'post_title' => $post_data['post_title'], 'post_content' => $post_data['post_content'], 'post_status' => 'publish', 'post_type' => 'post', ]; $post_id = wp_insert_post( wp_slash( $args ) ); if ( is_wp_error( $post_id ) ) { return new WP_Error( 'install_failed', __( 'Install failed.' ), array( 'status' => 500 ) ); } update_post_meta( $post_id, '_kadence_starter_templates_imported_post', true ); // Set the post thumbnail. if ( ! empty( $post_data['image'] ) ) { set_post_thumbnail( $post_id, $post_data['image'] ); } // Set the post categories. if ( ! empty( $post_data['categories'] ) ) { wp_set_post_terms( $post_id, $post_data['categories'], 'category' ); } // Set the post tags. if ( ! empty( $post_data['tags'] ) ) { wp_set_post_terms( $post_id, $post_data['tags'], 'post_tag' ); } } return true; } /** * Get Products. * * @param string $post_group The post group to get. * @return array/WP_Error The posts. */ public function get_remote_products( $post_group = 'normal' ) { switch ( $post_group ) { case 'soap': $url = 'https://base.startertemplatecloud.com/g32/wp-json/kadence-starter-base/v1/products'; break; default: $url = 'https://base.startertemplatecloud.com/wp-json/kadence-starter-base/v1/products'; break; } // Get the response. $response = wp_safe_remote_get( $url, array( 'timeout' => 20, ) ); // Early exit if there was an error. if ( is_wp_error( $response ) || $this->is_response_code_error( $response ) ) { return new WP_Error( 'install_failed', __( 'Could not get products from source.' ), array( 'status' => 500 ) ); } // Get the body from our response. $posts = wp_remote_retrieve_body( $response ); // Early exit if there was an error. if ( is_wp_error( $posts ) ) { return new WP_Error( 'install_failed', __( 'Could not get products from source.' ), array( 'status' => 500 ) ); } $posts = json_decode( $posts, true ); if ( ! is_array( $posts ) ) { return new WP_Error( 'install_failed', __( 'Could not get products from source.' ), array( 'status' => 500 ) ); } return $posts; } /** * Prepare Products. * * @param array $products The products to prepare. * @param array $image_library The image library to use. * @return array/WP_Error The products. */ public function prepare_products( $products, $image_library ) { if ( empty( $products ) ) { return new WP_Error( 'no_products', __( 'No products to prepare.' ), array( 'status' => 500 ) ); } $i = 0; $prepared_products = array(); foreach ( $products as $product_data ) { if ( ! empty( $product_data['image'][0]['src'] ) ) { $product_data['image'][0]['src'] = Image_Replacer::replace_images( $product_data['image'][0]['src'], $image_library, [], '', $i, [], ); } if ( ! empty( $product_data['gallery_images'] ) ) { // Replace the images in the gallery images. foreach ( $product_data['gallery_images'] as $key => $gallery_image ) { $product_data['gallery_images'][ $key ]['src'] = Image_Replacer::replace_images( $gallery_image['src'], $image_library, [], '', $i, [], ); } } $prepared_products[] = $product_data; $i++; } return $prepared_products; } /** * Install Products. * * @param array $products The products to install. * @param array $image_library The image library to use. * @return array/WP_Error The products. */ public function install_products( $products, $image_library ) { if ( empty( $products ) ) { return new WP_Error( 'no_products', __( 'No products to install.' ), array( 'status' => 500 ) ); } $new_products = array(); foreach ( $products as $product_data ) { if ( empty( $product_data['name'] ) ) { continue; } // Lets not duplicate products. $has_product = get_posts( [ 'post_type' => 'product', 'title' => $product_data['name'], ] ); if ( $has_product ) { $new_products[] = $has_product[0]->ID; continue; } $product = wc_get_product_object( $product_data['type'] ); // new WC_Product_Simple(); // Use WC_Product_Variable for variable products if ( is_wp_error( $product ) ) { return $product; } if ( 'external' === $product->get_type() ) { unset( $product_data['manage_stock'], $product_data['stock_status'], $product_data['backorders'], $product_data['low_stock_amount'] ); } $product->set_name( $product_data['name'] ); $product->set_status( 'publish' ); // or 'draft', 'pending', etc. $product->set_regular_price( $product_data['regular_price'] ); if ( ! empty( $product_data['sale_price'] ) ) { $product->set_sale_price( $product_data['sale_price'] ); } $product->set_description( $product_data['description'] ); $product->set_short_description( $product_data['short_description'] ); $this->set_image_data( $product, $product_data, $image_library ); $this->set_category_data( $product, $product_data ); $this->set_attribute_data( $product, $product_data ); $product_id = $product->save(); // Check for errors and handle them accordingly if ( is_wp_error( $product_id ) ) { return new WP_Error( 'install_failed', __( 'Install failed.' ), array( 'status' => 500 ) ); } if ( 'external' === $product->get_type() ) { if ( ! empty( $product_data['product_url'] ) ) { update_post_meta( $product_id, '_product_url', esc_url_raw( $product_data['product_url'] ) ); } // Update the button text. if ( ! empty( $product_data['button_text'] ) ) { update_post_meta( $product_id, '_button_text', sanitize_text_field( $product_data['button_text'] ) ); } } if ( $product_data['type'] === 'variable' ) { $variations = $product_data['variations']; foreach ( $variations as $variation_data ) { $variation = new WC_Product_Variation(); $variation->set_parent_id( $product_id ); $variation->set_status( 'publish' ); $variation->set_regular_price( $variation_data['display_regular_price'] ); if ( ! empty( $variation_data['display_sale_price'] ) ) { $variation->set_sale_price( $variation_data['display_sale_price'] ); } if ( ! empty( $variation_data['variation_description'] ) ) { $variation->set_description( $variation_data['variation_description'] ); } $this->set_image_data( $variation, $variation_data, $image_library ); $variation->set_attributes( $variation_data['attributes'] ); $variation_id = $variation->save(); } } update_post_meta( $product_id, '_kadence_starter_templates_imported_post', true ); $new_products[] = $product_id; } if ( empty( $new_products ) ) { return new WP_Error( 'install_failed', __( 'Install failed.' ), array( 'status' => 500 ) ); } return $new_products; } /** * Get Remote Events. * * @return array/WP_Error The events. */ public function get_remote_events() { $url = 'https://base.startertemplatecloud.com/wp-json/kadence-starter-base/v1/events'; // Get the response. $response = wp_safe_remote_get( $url, array( 'timeout' => 20, ) ); // Early exit if there was an error. if ( is_wp_error( $response ) || $this->is_response_code_error( $response ) ) { return new WP_Error( 'install_failed', __( 'Could not get events from source.' ), array( 'status' => 500 ) ); } // Get the body from our response. $posts = wp_remote_retrieve_body( $response ); // Early exit if there was an error. if ( is_wp_error( $posts ) ) { return new WP_Error( 'install_failed', __( 'Could not get events from source.' ), array( 'status' => 500 ) ); } $posts = json_decode( $posts, true ); if ( ! is_array( $posts ) ) { return new WP_Error( 'install_failed', __( 'Could not get events from source.' ), array( 'status' => 500 ) ); } return $posts; } /** * Prepare Events. * * @param array $events The events to prepare. * @param array $image_library The image library to use. * @return array/WP_Error The events. */ public function prepare_events( $events, $image_library ) { if ( empty( $events ) ) { return new WP_Error( 'no_events', __( 'No events to prepare.' ), array( 'status' => 500 ) ); } $i = 0; $prepared_events = array(); foreach ( $events as $event_data ) { if ( ! empty( $event_data['image'] ) ) { $event_data['image'] = Image_Replacer::replace_images( $event_data['image'], $image_library, [], '', $i, [], ); } $prepared_events[] = $event_data; $i++; } return $prepared_events; } /** * */ public function install_donation_form( $form_data ) { if ( empty( $form_data ) ) { return new WP_Error( 'no_form_data', __( 'No form data to install.' ), array( 'status' => 500 ) ); } if ( ! class_exists( '\Give' ) ) { return 'none'; } $site_name = ( !empty( $form_data['companyName'] ) ? $form_data['companyName'] : 'GiveWP' ); $form_args = [ 'enableDonationGoal' => true, 'goalAmount' => 1000, 'designId' => 'multi-step', 'showHeading' => true, 'showDescription' => true, 'formTitle' => $site_name . ' - ' . __('Donation Form', 'kadence-starter-templates'), 'heading' => $form_data['heading'], 'description' => $form_data['description'], 'formStatus' => DonationFormStatus::PUBLISHED(), ]; if ( !empty( $form_data['primaryColor'] ) ) { $form_args['primaryColor'] = $form_data['primaryColor']; } if ( ! empty( $form_data['type'] ) && $form_data['type'] === 'image' && ! empty( $form_data['image'] ) ) { $form_args['designSettingsImageUrl'] = $form_data['image']; $form_args['designSettingsImageStyle'] = 'above'; $form_args['designSettingsImageAlt'] = $form_data['heading']; } if ( version_compare( GIVE_VERSION, '4.0.0', '<' ) ) { $form = DonationForm::create([ 'title' => $site_name . ' - ' . __('Donation Form', 'kadence-starter-templates'), 'status' => DonationFormStatus::PUBLISHED(), 'settings' => FormSettings::fromArray($form_args), 'blocks' => (new GenerateDefaultDonationFormBlockCollection())(), ]); } else { $campaign_args = [ 'type' => CampaignType::CORE(), 'title' => $site_name . ' - ' . __('Donation Campaign', 'kadence-starter-templates'), 'shortDescription' => $form_data['description'], 'longDescription' => '', 'logo' => '', 'image' => '', 'primaryColor' => '#0b72d9', 'secondaryColor' => '#27ae60', 'goal' => 1000, 'goalType' => CampaignGoalType::AMOUNT(), 'status' => CampaignStatus::ACTIVE(), ]; if ( ! empty( $form_data['image'] ) ) { $campaign_args['image'] = $form_data['image']; } if ( !empty( $form_data['primaryColor'] ) ) { $campaign_args['primaryColor'] = $form_data['primaryColor']; } $campaign = Campaign::create( $campaign_args ); $form = DonationForm::find($campaign->defaultFormId); $form->title = $site_name . ' - ' . __('Donation Form', 'kadence-starter-templates'); $form->status = DonationFormStatus::PUBLISHED(); $form->settings = FormSettings::fromArray($form_args); $form->save(); } if ( ! isset( $form->id ) ) { return new WP_Error( 'install_failed', __( 'Install failed.' ), array( 'status' => 500 ) ); } update_post_meta( $form->id, '_kadence_starter_templates_imported_post', true ); return $form->id; } /** * Update Donation Form Primary Color. * * @param string $primary_color The primary color to update. * @param string $form_id The form id to update. * */ public function update_donation_form_primary_color( $primary_color, $form_id ) { if ( empty( $primary_color ) || empty( $form_id ) ) { return; } if ( ! class_exists( '\Give' ) ) { return; } $form = DonationForm::find( $form_id ); if ( ! $form ) { return; } $form->settings->primaryColor = $primary_color; $form->save(); } /** * Install Give Form. * * @param array $image_library The image library to use. * @return string/WP_Error The form. */ public function install_give_form( $ai_content, $image_library, $site_name = '', $primary_color = '' ) { if ( ! class_exists( '\Give' ) ) { return 'none'; } $form_data = json_encode(['heading' => 'Write a short headline', 'description' => 'Use this paragraph section to get your website visitors to know you. Consider writing about you or your organization, the products or services you offer, or why you exist. Keep a consistent communication style.'], true); $form_data = Content_Replacer::replace_content( $form_data, $ai_content, ['donation-form'], 'donate', 1, false, [], ); $form_data = json_decode($form_data, true); $form_data['companyName'] = $site_name; if ( ! empty( $primary_color ) ) { $form_data['primaryColor'] = $primary_color; } // $image = Image_Replacer::replace_images( // 'https://patterns.startertemplatecloud.com/wp-content/uploads/2023/02/Example-A-Roll-Image-1024x793.jpg', // $image_library, // [], // '', // 3, // [], // false // ); // $image_data = $this->get_image_info( $image_library, $image ); // if ( $image_data ) { // $alt = ! empty( $image_data['alt'] ) ? $image_data['alt'] : 'Donation Form'; // } return $this->install_donation_form( $form_data ); } /** * Install Events. * * @param array $events The events to install. * @param array $image_library The image library to use. * @return array/WP_Error The events. */ public function install_events( $events, $image_library ) { if ( empty( $events ) ) { return new WP_Error( 'no_events', __( 'No events to install.' ), array( 'status' => 500 ) ); } if ( ! class_exists( '\Tribe__Events__Main' ) ) { return new WP_Error( 'no_events', __( 'Tribe Events is not installed.' ), array( 'status' => 500 ) ); } $new_events = array(); $variation = 1; foreach ( $events as $event_data ) { // Lets not duplicate products. $has_event = get_posts( [ 'post_type' => 'tribe_events', 'title' => $event_data['title'], ] ); if ( $has_event ) { $new_events[] = $has_event[0]->ID; continue; } // Prepare Post content. $category_ids = $this->set_taxonomy_data( $event_data, 'categories', 'tribe_events_cat' ); $venue_ids = $this->set_event_venue_data( $event_data ); $organizer_ids = $this->set_event_organizers_data( $event_data ); $downloaded_image = array(); if ( ! empty( $event_data['image'] ) ) { $image = array( 'url' => $event_data['image'], 'id' => 0, ); if ( substr( $event_data['image'], 0, strlen( 'https://images.pexels.com' ) ) === 'https://images.pexels.com' ) { $image_data = $this->get_image_info( $image_library, $event_data['image'] ); if ( $image_data ) { $alt = ! empty( $image_data['alt'] ) ? $image_data['alt'] : ''; $image['filename'] = ! empty( $image_data['filename'] ) ? $image_data['filename'] : $this->create_filename_from_alt( $alt ); $image['photographer'] = ! empty( $image_data['photographer'] ) ? $image_data['photographer'] : ''; $image['photographer_url'] = ! empty( $image_data['photographer_url'] ) ? $image_data['photographer_url'] : ''; $image['photograph_url'] = ! empty( $image_data['url'] ) ? $image_data['url'] : ''; $image['alt'] = $alt; $image['title'] = __( 'Photo by', 'kadence-starter-templates' ) . ' ' . $image['photographer']; } } $downloaded_image = $this->import_image( $image ); } $date = strtotime( '+' . (string)$variation .' months' ); $start_date = date( 'Y-m-d', $date ); $event_item = array( 'post_status' => 'publish', 'post_title' => ( isset( $event_data['title'] ) ? wp_strip_all_tags( $event_data['title'] ) : '' ), 'post_content' => $this->process_page_content( $event_data['content'], $image_library ), 'EventStartDate' => $start_date, 'EventStartMeridian' => 'pm', 'EventStartMinute' => '00', 'EventStartHour' => '01', 'EventEndMeridian' => 'pm', 'EventEndMinute' => '00', 'EventEndHour' => '05', 'EventEndDate' => $start_date, 'FeaturedImage' => ! empty( $downloaded_image['id'] ) ? $downloaded_image['id'] : '', 'EventCost' => '0', 'venue' => isset( $event_data['venues'][0] ) ? $event_data['venues'][0] : '', ); $event_id = tribe_create_event( $event_item ); // Check for errors and handle them accordingly if ( is_wp_error( $event_id ) ) { return new WP_Error( 'install_failed', __( 'Install failed.' ), array( 'status' => 500 ) ); } update_post_meta( $event_id, '_kadence_starter_templates_imported_post', true ); foreach ( $organizer_ids as $organizer_id ) { add_post_meta( $event_id, '_EventOrganizerID', $organizer_id ); } wp_set_post_terms( $event_id, $category_ids, 'tribe_events_cat' ); $new_events[] = $event_id; $variation++; } if ( empty( $new_events ) ) { return new WP_Error( 'install_failed', __( 'Install failed.' ), array( 'status' => 500 ) ); } return $new_events; } /** * Install Course. * * @return array/WP_Error The course. */ public function install_course() { if ( ! class_exists( '\Learndash_Admin_Import_Export' ) ) { return new WP_Error( 'no_course', __( 'No LearnDash' ), array( 'status' => 500 ) ); } $has_course = get_posts( [ 'post_type' => 'sfwd-courses', 'title' => 'Getting Started with LearnDash', ] ); if ( $has_course ) { return $has_course[0]->ID; } $user_id = get_current_user_id(); $options = json_decode( '{"post_types":["sfwd-courses","sfwd-lessons","sfwd-topic","sfwd-quiz","sfwd-question","groups","sfwd-assignment","sfwd-certificates"],"post_type_settings":["sfwd-courses","sfwd-lessons","sfwd-topic","sfwd-quiz","sfwd-question","groups","sfwd-assignment","sfwd-certificates"],"users":[],"other":["settings"],"info":{"ld_version":"4.15.2","wp_version":"6.5.5","db_prefix":"wp_","is_multisite":true,"blog_id":1,"home_url":"https:\/\/base.startertemplatecloud.com"}}', true ); $ld_file_handler = new \Learndash_Admin_Import_File_Handler(); $ld_file_handler->set_working_directory( KADENCE_STARTER_TEMPLATES_PATH . 'assets/ld-demo/learndash-demo' ); $ld_importers_mapper = new \Learndash_Admin_Import_Mapper( $ld_file_handler, new \Learndash_Import_Export_Logger( \Learndash_Import_Export_Logger::$log_type_import ) ); $course_ids = array(); foreach ( $ld_importers_mapper->map( $options, $user_id ) as $importer ) { $importer->import_data(); \Learndash_Admin_Import::clear_wpdb_query_cache(); /** * Fires after an importer had been processed. * * @param Learndash_Admin_Import $importer The Learndash_Admin_Import instance. * * @since 4.3.0 */ do_action( 'learndash_import_importer_processed', $importer ); $new_post = $importer->get_new_post_id_by_old_post_id( 7214 ); if ( $new_post && ! in_array( $new_post, $course_ids ) ) { $course_ids[] = $new_post; } } ( new \Learndash_Admin_Import_Associations_Handler() )->handle(); $new_courses = true; if ( empty( $new_courses ) ) { return new WP_Error( 'install_failed', __( 'Install failed.' ), array( 'status' => 500 ) ); } return $new_courses; } /** * Install Learndash Settings. * * @return null; */ public function setup_learndash() { if ( ! class_exists( '\LearnDash_Settings_Section' ) ) { return; } // LearnDash Theme Settings. $instance = \LearnDash_Settings_Section::get_section_instance( 'LearnDash_Settings_Theme_LD30' ); $instance::set_setting( 'color_primary', 'var(--global-palette1, #0073aa)' ); $instance::set_setting( 'color_secondary', 'var(--global-palette2, #215387)' ); // Enable login and registration. $instance::set_setting( 'login_mode_enabled', 'yes' ); // Focused mode. $instance::set_setting( 'focus_mode_enabled', 'yes' ); // LearnDash Page Settings. $ld_page_instance = \LearnDash_Settings_Section::get_section_instance( 'LearnDash_Settings_Section_Registration_Pages' ); $success_id = $ld_page_instance::get_setting( 'registration_success', '' ); if ( ! empty( $success_id ) ) { update_post_meta( $success_id, '_kad_post_layout', 'narrow' ); update_post_meta( $success_id, '_kad_post_vertical_padding', 'show' ); } else { $success_id = wp_insert_post( array( 'post_title' => 'Registration Success', 'post_type' => 'page', 'post_status' => 'publish', ) ); if ( ! is_wp_error( $success_id ) ) { update_post_meta( $success_id, '_kadence_starter_templates_imported_post', true ); update_post_meta( $success_id, '_kad_post_layout', 'narrow' ); update_post_meta( $success_id, '_kad_post_vertical_padding', 'show' ); $ld_page_instance::set_setting( 'registration_success', $success_id ); } } $registration_id = $ld_page_instance::get_setting( 'registration', '' ); if ( ! empty( $registration_id ) ) { update_post_meta( $registration_id, '_kad_post_layout', 'narrow' ); update_post_meta( $registration_id, '_kad_post_vertical_padding', 'show' ); } else { $registration_id = wp_insert_post( array( 'post_title' => 'Registration', 'post_type' => 'page', 'post_content' => '', 'post_status' => 'publish', ) ); if ( ! is_wp_error( $registration_id ) ) { update_post_meta( $registration_id, '_kadence_starter_templates_imported_post', true ); update_post_meta( $registration_id, '_kad_post_layout', 'narrow' ); update_post_meta( $registration_id, '_kad_post_vertical_padding', 'show' ); $ld_page_instance::set_setting( 'registration', $registration_id ); } } $reset_id = $ld_page_instance::get_setting( 'reset_password', '' ); if ( ! empty( $reset_id ) ) { update_post_meta( $reset_id, '_kad_post_layout', 'narrow' ); update_post_meta( $reset_id, '_kad_post_vertical_padding', 'show' ); } else { $reset_id = wp_insert_post( array( 'post_title' => 'Reset Password', 'post_type' => 'page', 'post_content' => '', 'post_status' => 'publish', ) ); if ( ! is_wp_error( $reset_id ) ) { update_post_meta( $reset_id, '_kadence_starter_templates_imported_post', true ); update_post_meta( $reset_id, '_kad_post_layout', 'narrow' ); update_post_meta( $reset_id, '_kad_post_vertical_padding', 'show' ); $ld_page_instance::set_setting( 'reset_password', $reset_id ); } } // Update Course Layout. set_theme_mod( 'sfwd-courses_layout', 'narrow' ); set_theme_mod( 'sfwd-courses_content_style', 'unboxed' ); // Update Lesson Layout. set_theme_mod( 'sfwd-lessons_layout', 'narrow' ); set_theme_mod( 'sfwd-lessons_content_style', 'unboxed' ); // Make sure anyone can register. update_option( 'users_can_register', 1 ); return; } /** * Update block ID in content with new ID */ private function update_block_ids($content, $id_map) { $blocks = parse_blocks($content); foreach ($blocks as &$block) { if ( in_array( $block['blockName'], $this->kadence_cpt_blocks ) && !empty($block['attrs']['id']) && isset($id_map[$block['attrs']['id']])) { $block['attrs']['id'] = $id_map[$block['attrs']['id']]; } if (!empty($block['innerBlocks'])) { $inner_content = serialize_blocks($block['innerBlocks']); $updated_inner_content = $this->update_block_ids($inner_content, $id_map); $block['innerBlocks'] = parse_blocks($updated_inner_content); } } return serialize_blocks($blocks); } /** * Install CPT. * * @param array $cpt_data The cpt data. * @param string $style The style. * @return int The cpt id. */ public function install_single_cpt( $cpt_data, $id_map = [], $style = 'light' ) { // Check if the post already exists. $title = ! empty( $style ) && 'light' !== $style ? $cpt_data['post_title'] . ' ' . ucfirst( $style ) : $cpt_data['post_title']; $post_exists = get_posts( [ 'post_type' => $cpt_data['post_type'], 'title' => $title, ] ); if ( $post_exists ) { return $post_exists[0]->ID; } $temp_content = $cpt_data['post_content']; $new_post_id = wp_insert_post([ 'post_type' => $cpt_data['post_type'], 'post_title' => $title, 'post_content' => '', 'post_status' => 'publish', ], true); if ( ! is_wp_error($new_post_id) ) { if ( ! empty( $cpt_data['meta'])) { foreach ($cpt_data['meta'] as $meta_key => $meta_values) { foreach ($meta_values as $meta_value) { if ( ! empty( $style ) && 'light' !== $style ) { $meta_value = $this->cpt_switch_meta_color( $meta_value, $meta_key, $style ); } add_post_meta($new_post_id, $meta_key, $meta_value); } } } if ( ! empty( $id_map ) ) { $temp_content = $this->update_block_ids($temp_content, $id_map); } if ( ! empty( $style ) && 'light' !== $style ) { $temp_content = $this->cpt_switch_color( $temp_content, $style ); } wp_update_post(array( 'ID' => $new_post_id, 'post_content' => $temp_content )); return $new_post_id; } return false; } /** * Retrieves a collection of objects. * * @param string $content The content to search. * @param string $style The style to search for. * @return string The content with the style replaced. */ public function cpt_switch_meta_color( $meta_value, $meta_key, $style ) { $temp_meta_value = wp_json_encode( $meta_value ); if ( $style === 'highlight' ) { $temp_meta_value = str_replace( 'palette1', 'placeholder-kb-pal9', $temp_meta_value ); $temp_meta_value = str_replace( 'palette2', 'placeholder-kb-pal8', $temp_meta_value ); $temp_meta_value = str_replace( 'palette3', 'placeholder-kb-pal9', $temp_meta_value ); $temp_meta_value = str_replace( 'palette4', 'placeholder-kb-pal9', $temp_meta_value ); $temp_meta_value = str_replace( 'palette5', 'placeholder-kb-pal8', $temp_meta_value ); $temp_meta_value = str_replace( 'palette6', 'placeholder-kb-pal7', $temp_meta_value ); $temp_meta_value = str_replace( 'palette7', 'placeholder-kb-pal2', $temp_meta_value ); $temp_meta_value = str_replace( 'palette8', 'placeholder-kb-pal2', $temp_meta_value ); $temp_meta_value = str_replace( 'palette9', 'placeholder-kb-pal1', $temp_meta_value ); } else { $temp_meta_value = str_replace( 'palette3', 'placeholder-kb-pal9', $temp_meta_value ); $temp_meta_value = str_replace( 'palette4', 'placeholder-kb-pal8', $temp_meta_value ); $temp_meta_value = str_replace( 'palette5', 'placeholder-kb-pal7', $temp_meta_value ); $temp_meta_value = str_replace( 'palette6', 'placeholder-kb-pal7', $temp_meta_value ); $temp_meta_value = str_replace( 'palette7', 'placeholder-kb-pal3', $temp_meta_value ); $temp_meta_value = str_replace( 'palette8', 'placeholder-kb-pal3', $temp_meta_value ); $temp_meta_value = str_replace( 'palette9', 'placeholder-kb-pal4', $temp_meta_value ); } $temp_meta_value = str_replace( 'placeholder-kb-pal1', 'palette1', $temp_meta_value ); $temp_meta_value = str_replace( 'placeholder-kb-pal2', 'palette2', $temp_meta_value ); $temp_meta_value = str_replace( 'placeholder-kb-pal3', 'palette3', $temp_meta_value ); $temp_meta_value = str_replace( 'placeholder-kb-pal4', 'palette4', $temp_meta_value ); $temp_meta_value = str_replace( 'placeholder-kb-pal5', 'palette5', $temp_meta_value ); $temp_meta_value = str_replace( 'placeholder-kb-pal6', 'palette6', $temp_meta_value ); $temp_meta_value = str_replace( 'placeholder-kb-pal7', 'palette7', $temp_meta_value ); $temp_meta_value = str_replace( 'placeholder-kb-pal8', 'palette8', $temp_meta_value ); $temp_meta_value = str_replace( 'placeholder-kb-pal9', 'palette9', $temp_meta_value ); return json_decode( $temp_meta_value, true ); } /** * Retrieves a collection of objects. */ public function cpt_switch_color( $content, $style ) { $content = str_replace( 'Logo-ploaceholder.png', 'Logo-ploaceholder-white.png', $content ); $content = str_replace( 'Logo-ploaceholder-1.png', 'Logo-ploaceholder-1-white.png', $content ); $content = str_replace( 'Logo-ploaceholder-2.png', 'Logo-ploaceholder-2-white.png', $content ); $content = str_replace( 'Logo-ploaceholder-3.png', 'Logo-ploaceholder-3-white.png', $content ); $content = str_replace( 'Logo-ploaceholder-4.png', 'Logo-ploaceholder-4-white.png', $content ); $content = str_replace( 'Logo-ploaceholder-5.png', 'Logo-ploaceholder-5-white.png', $content ); $content = str_replace( 'Logo-ploaceholder-6.png', 'Logo-ploaceholder-6-white.png', $content ); $content = str_replace( 'Logo-ploaceholder-7.png', 'Logo-ploaceholder-7-white.png', $content ); $content = str_replace( 'Logo-ploaceholder-8.png', 'Logo-ploaceholder-8-white.png', $content ); $content = str_replace( 'logo-placeholder.png', 'logo-placeholder-white.png', $content ); $content = str_replace( 'logo-placeholder-1.png', 'logo-placeholder-1-white.png', $content ); $content = str_replace( 'logo-placeholder-2.png', 'logo-placeholder-2-white.png', $content ); $content = str_replace( 'logo-placeholder-3.png', 'logo-placeholder-3-white.png', $content ); $content = str_replace( 'logo-placeholder-4.png', 'logo-placeholder-4-white.png', $content ); $content = str_replace( 'logo-placeholder-5.png', 'logo-placeholder-5-white.png', $content ); $content = str_replace( 'logo-placeholder-6.png', 'logo-placeholder-6-white.png', $content ); $content = str_replace( 'logo-placeholder-7.png', 'logo-placeholder-7-white.png', $content ); $content = str_replace( 'logo-placeholder-8.png', 'logo-placeholder-8-white.png', $content ); $content = str_replace( 'logo-placeholder-9.png', 'logo-placeholder-9-white.png', $content ); $content = str_replace( 'logo-placeholder-10.png', 'logo-placeholder-10-white.png', $content ); if ( $style === 'highlight' ) { $form_content = $this->get_string_inbetween( $content, '"submit":[{', ']}', 'wp:kadence/form' ); if ( $form_content ) { $form_content_org = $form_content; $form_content = str_replace( '"color":""', '"color":"placeholder-kb-pal9"', $form_content ); $form_content = str_replace( '"background":""', '"background":"placeholder-kb-pal3"', $form_content ); $form_content = str_replace( '"colorHover":""', '"colorHover":"placeholder-kb-pal9"', $form_content ); $form_content = str_replace( '"backgroundHover":""', '"backgroundHover":"placeholder-kb-pal4"', $form_content ); $content = str_replace( $form_content_org, $form_content, $content ); } $content = str_replace( '"inheritStyles":"inherit"', '"color":"placeholder-kb-pal9","background":"placeholder-kb-pal3","colorHover":"placeholder-kb-pal9","backgroundHover":"placeholder-kb-pal4","inheritStyles":"inherit"', $content ); $content = str_replace( 'has-theme-palette-1', 'placeholder-kb-class9', $content ); $content = str_replace( 'has-theme-palette-2', 'placeholder-kb-class8', $content ); $content = str_replace( 'has-theme-palette-3', 'placeholder-kb-class9', $content ); $content = str_replace( 'has-theme-palette-4', 'placeholder-kb-class9', $content ); $content = str_replace( 'has-theme-palette-5', 'placeholder-kb-class8', $content ); $content = str_replace( 'has-theme-palette-6', 'placeholder-kb-class7', $content ); $content = str_replace( 'has-theme-palette-7', 'placeholder-kb-class2', $content ); $content = str_replace( 'has-theme-palette-8', 'placeholder-kb-class2', $content ); $content = str_replace( 'has-theme-palette-9', 'placeholder-kb-class1', $content ); $content = str_replace( 'theme-palette1', 'placeholder-class-pal9', $content ); $content = str_replace( 'theme-palette2', 'placeholder-class-pal8', $content ); $content = str_replace( 'theme-palette3', 'placeholder-class-pal9', $content ); $content = str_replace( 'theme-palette4', 'placeholder-class-pal9', $content ); $content = str_replace( 'theme-palette5', 'placeholder-class-pal8', $content ); $content = str_replace( 'theme-palette6', 'placeholder-class-pal7', $content ); $content = str_replace( 'theme-palette7', 'placeholder-class-pal2', $content ); $content = str_replace( 'theme-palette8', 'placeholder-class-pal2', $content ); $content = str_replace( 'theme-palette9', 'placeholder-class-pal1', $content ); $content = str_replace( 'palette1', 'placeholder-kb-pal9', $content ); $content = str_replace( 'palette2', 'placeholder-kb-pal8', $content ); $content = str_replace( 'palette3', 'placeholder-kb-pal9', $content ); $content = str_replace( 'palette4', 'placeholder-kb-pal9', $content ); $content = str_replace( 'palette5', 'placeholder-kb-pal8', $content ); $content = str_replace( 'palette6', 'placeholder-kb-pal7', $content ); $content = str_replace( 'palette7', 'placeholder-kb-pal2', $content ); $content = str_replace( 'palette8', 'placeholder-kb-pal2', $content ); $content = str_replace( 'palette9', 'placeholder-kb-pal1', $content ); } else { $white_text_content = $this->get_string_inbetween_when( $content, '