function wm_default_setup() { //Requirements check if (!function_exists('wma_read_local_file')) { return; } //Processing /** * Theme installation: Step 2 */ if (2 > absint(get_option(WM_THEME_SETTINGS_INSTALL))) { //Files setup $file_path = WM_SKINS . 'default.json'; //Check if file exists if (file_exists($file_path)) { //Save default theme skin $replacements = (array) apply_filters('wmhook_generate_css_replacements', array()); $saving = strtr(wma_read_local_file($file_path), $replacements); $saving = json_decode(trim($saving), true); update_option(WM_THEME_SETTINGS_SKIN, $saving); //Step 2 of theme installation done! update_option(WM_THEME_SETTINGS_INSTALL, 2); do_action('wmhook_wm_install_step_2'); } } }
function wm_save_skin($value = array(), $old_value = array()) { //Requirements check if (empty($value) && !is_array($value)) { return $value; } //Helper variables $skin_new = $skin_load = ''; $wp_upload_dir = wp_upload_dir(); $theme_skin_dir = apply_filters('wmhook_wm_save_skin_theme_skin_dir', trailingslashit($wp_upload_dir['basedir']) . 'wmtheme-' . WM_THEME_SHORTNAME . '/skins'); //Preparing output //Set a new skin file name //New skin if (isset($value[WM_THEME_SETTINGS_PREFIX . 'skin-new'])) { $skin_new = trim(sanitize_title($value[WM_THEME_SETTINGS_PREFIX . 'skin-new'])); unset($value[WM_THEME_SETTINGS_PREFIX . 'skin-new']); } //Load skin if (isset($value[WM_THEME_SETTINGS_PREFIX . 'skin-load'])) { $skin_load = trim($value[WM_THEME_SETTINGS_PREFIX . 'skin-load']); if ($pos = strpos($skin_load, trailingslashit(WM_SETUP_DIR) . 'skins')) { $skin_load = substr($skin_load, $pos, -4); if (is_child_theme()) { $skin_load = trailingslashit(get_stylesheet_directory()) . $skin_load; } else { $skin_load = trailingslashit(get_template_directory()) . $skin_load; } $skin_load .= 'json'; } unset($value[WM_THEME_SETTINGS_PREFIX . 'skin-load']); } //Create a new skin if ($skin_new) { //Create the theme skins folder if (!wma_create_folder($theme_skin_dir)) { set_transient('wmamp-admin-notice', array("<strong>ERROR: Wasn't able to create a theme skins folder! Contact the theme support.</strong>", 'error', 'switch_themes', 2), 60 * 60 * 48); delete_option(WM_THEME_SETTINGS_PREFIX . WM_THEME_SHORTNAME . '-skins'); } //Write the skin JSON file $json_path = apply_filters('wmhook_wm_save_skin_json_path', trailingslashit($theme_skin_dir) . $skin_new . '.json'); $value = apply_filters('wmhook_wm_save_skin_output', $value); if (wma_write_local_file($json_path, json_encode($value))) { update_option(WM_THEME_SETTINGS_PREFIX . WM_THEME_SHORTNAME . '-skins', array_unique(array(WM_SKINS, WM_SKINS_CHILD, $theme_skin_dir))); } else { delete_option(WM_THEME_SETTINGS_PREFIX . WM_THEME_SHORTNAME . '-skins'); } //Run additional actions do_action('wmhook_save_skin', $skin_new, $value, $old_value); //Load a selected skin } elseif ($skin_load && file_exists($skin_load)) { //Get the skin slug $skin_slug = str_replace(array('.json', WM_SKINS, WM_SKINS_CHILD, $theme_skin_dir), '', $skin_load); //We don't need to write to the file, so just open for reading. $skin_load = wma_read_local_file($skin_load); $replacements = (array) apply_filters('wmhook_generate_css_replacements', array()); $skin_load = strtr($skin_load, $replacements); //Decoding new imported skin JSON string and converting object to array if (!empty($skin_load)) { $value = json_decode(trim($skin_load), true); update_option(WM_THEME_SETTINGS_PREFIX . WM_THEME_SHORTNAME . '-skin-used', $skin_slug); } //Run additional actions do_action('wmhook_load_skin', $skin_load, $value, $old_value); } //Output return $value; }