function init() { $sidebars = wip_sidebar_generator::get_sidebars(); if (is_array($sidebars)) { $z = 1; foreach ($sidebars as $sidebar) { $sidebar_class = wip_sidebar_generator::convert_class($sidebar); register_sidebar(array('name' => $sidebar, 'before_widget' => '<div class="sidebarbox %2$s">', 'after_widget' => '</div>', 'before_title' => '<h3 class="sidebar-title"><span>', 'after_title' => '</span></h3>')); $z++; } } }
function WIPanel_ajax_icons_callback() { global $wpdb; #check the type of form if (isset($_POST['type'])) { $save_type = $_POST['type']; } else { $save_type = null; } # upload new image if ($save_type == 'upload') { #grab the data if (!isset($_FILES['icon_upload'])) { $error = array("error" => true, "errorText" => __('No image detected!', 'wip')); echo json_encode($error); die; } check_ajax_referer('icon_image_nonce'); $arr_file_type = wp_check_filetype(basename($_FILES['icon_upload']['name'])); $uploaded_file_type = $arr_file_type['type']; # Set an array containing a list of acceptable formats $allowed_file_types = array('image/jpg', 'image/jpeg', 'image/gif', 'image/png', 'image/x-icon'); # if data is an image - upload it if (in_array($uploaded_file_type, $allowed_file_types)) { # override the upload process, WordPress need to detect 'wp_handle_upload' before upload the doc $filename = $_FILES['icon_upload']; $filename['name'] = preg_replace('/[^a-zA-Z0-9._\\-]/', '', $filename['name']); $override['test_form'] = false; $override['action'] = 'wp_handle_upload'; # upload process... $uploaded_file = wp_handle_upload($filename, $override); $upload_tracking[] = 'icon_upload'; } else { #if error $uploaded_file['error'] = __('Unsupported file type!', 'wip'); } if (!empty($uploaded_file['error'])) { $error = array("error" => true, "errorText" => $uploaded_file['error']); echo json_encode($error); die; } else { $path_info = wp_upload_dir(); $file_info = pathinfo($uploaded_file['file']); $image_filename = $file_info['filename'] . '.' . $file_info['extension']; $info_to_save = array(); $info_to_save[] = $path_info['subdir']; $info_to_save[] = $image_filename; $iconInfo = implode(',', $info_to_save); $return = array("error" => false, "icon" => $uploaded_file['url'], "iconData" => $iconInfo); echo json_encode($return); die; } } elseif ($save_type == 'image_reset') { #if user click "delete image" icon $data = isset($_POST['data']) ? $_POST['data'] : ''; if ($data != "") { $path_info = wp_upload_dir(); $ic = explode(',', $data); if (isset($ic[0]) && isset($ic[1])) { $tF = $path_info['basedir'] . $ic[0] . '/' . $ic[1]; if (file_exists($tF)) { unlink($tF); } } } die; } elseif ($save_type == 'add_icon') { if (isset($_POST['data'])) { $data = $_POST['data']; #explode them to get each data, check http://php.net/manual/en/function.parse-str.php parse_str($data, $p); } $err_r = array(); $error = false; if ($p['wip_icon_image'] == "") { $error = true; $err_r[] = __('Please upload the icon!', 'wip'); } if ($p['wip_icon_url'] == "") { $error = true; $err_r[] = __('Please enter the URL!', 'wip'); } if ($error) { $ert = '<ul>' . "\n"; foreach ($err_r as $e) { $ert .= '<li>' . $e . '</li>' . "\n"; } $ert .= '</ul>'; $errorCallback = array("error" => true, "errorText" => $ert); echo json_encode($errorCallback); die; } else { $path_info = wp_upload_dir(); $sn = $p['shortname']; $ic = explode(',', $p['wip_icon_image']); $icon_img = array('subdir' => $ic[0], 'file' => $ic[1]); $icon_url = $p['wip_icon_url']; $icon_text = wptexturize($p['wip_icon_title']); $iconToShow = $path_info['baseurl'] . $ic[0] . '/' . $ic[1]; $toPost = array("icon" => $icon_img, "text" => $icon_text, "link" => $icon_url); $curent = get_option($sn . '_fan_icons'); if (is_array($curent)) { $new_icon[] = $toPost; $saved_icon = array_merge((array) $curent, (array) $new_icon); } else { $saved_icon[] = $toPost; } update_option($sn . '_fan_icons', $saved_icon); $callBack = array("error" => false, "iconURL" => $iconToShow); echo json_encode($callBack); die; } } elseif ($save_type == 'delete_icon') { if (isset($_POST['data'])) { $data = $_POST['data']; parse_str($data, $p); } $icon_id = $p['data']; $sn = $p['shortname']; $curent = get_option($sn . '_fan_icons'); if (isset($curent[$icon_id])) { $toDel = $curent[$icon_id]; if (isset($toDel['icon']) && is_array($toDel['icon'])) { $path_info = wp_upload_dir(); $file = $path_info['basedir'] . $toDel['icon']['subdir'] . '/' . $toDel['icon']['file']; if (file_exists($file)) { unlink($file); } } if (count($curent) == '1') { delete_option($sn . '_fan_icons'); } else { unset($curent[$icon_id]); $saved_icon = $curent; update_option($sn . '_fan_icons', $saved_icon); } } } elseif ($save_type == 'uploadsscript') { #grab the data $clickedID = $_POST['data']; $arr_file_type = wp_check_filetype(basename($_FILES[$clickedID]['name'])); $uploaded_file_type = $arr_file_type['ext']; # if data is an image - upload it if ($uploaded_file_type == "js") { # override the upload process, WordPress need to detect 'wp_handle_upload' before upload the doc $filename = $_FILES[$clickedID]; $filename['name'] = preg_replace('/[^a-zA-Z0-9._\\-]/', '', $filename['name']); $override['test_form'] = false; $override['action'] = 'wp_handle_upload'; # upload process... $uploaded_file = wp_handle_upload($filename, $override); # at once, we save the option. update_option($clickedID, $uploaded_file['url']); } else { #if error $uploaded_file['error'] = __('Unsupported file type!', 'wip'); } if (!empty($uploaded_file['error'])) { printf(__('Upload Error: %1$s', 'wip'), $uploaded_file['error']); } else { echo $uploaded_file['url']; } } elseif ($save_type == 'add_sidebar') { if (isset($_POST['data'])) { $data = $_POST['data']; #explode them to get each data, check http://php.net/manual/en/function.parse-str.php parse_str($data, $p); } $err_r = array(); $error = false; if ($p['wip_sidebar_name'] == "") { $error = true; $err_r[] = __('Error : Please enter the sidebar name!', 'wip'); } if ($error) { echo '<ul>' . "\n"; foreach ($err_r as $e) { echo '<li>' . $e . '</li>' . "\n"; } echo '</ul>'; } else { $sn = $p['shortname']; $sb_name = $p['wip_sidebar_name']; $get_sidebar_options = wip_sidebar_generator::get_sidebars(); $sidebar_name = str_replace(array("\n", "\r", "\t"), '', $sb_name); $sidebar_id = wip_sidebar_generator::convert_class($sidebar_name); if ($sidebar_id == '') { $options_sidebar = $get_sidebar_options; } else { if (isset($get_sidebar_options[$sidebar_id])) { echo "2"; #alert the user that sidebar name exists die; } if (is_array($get_sidebar_options)) { $new_sidebar[$sidebar_id] = $sidebar_id; $options_sidebar = $get_sidebar_options + $new_sidebar; } else { $options_sidebar[$sidebar_id] = $sidebar_id; } } update_option($sn . '_sidebar_gen', $options_sidebar); echo $sidebar_id; } } elseif ($save_type == 'delete_sidebar') { if (isset($_POST['data'])) { $data = $_POST['data']; parse_str($data, $p); } $sidebar_id = $p['data']; $sn = $p['shortname']; $get_sidebar_options = wip_sidebar_generator::get_sidebars(); $sidebarcount = count($get_sidebar_options); if (isset($get_sidebar_options[$sidebar_id])) { unset($get_sidebar_options[$sidebar_id]); autoDeleteMeta($sidebar_id); if ($sidebarcount == 1) { $options_sidebar = ''; } else { $options_sidebar = $get_sidebar_options; } update_option($sn . '_sidebar_gen', $options_sidebar); } } die; }