function _dp_resize_logo()
{
    $dp_resize_message = array('error' => true, 'message' => '');
    //値をチェック
    $ratio = intval($_REQUEST['dp_resize_ratio']);
    if (!($ratio > 0 && $ratio <= 100)) {
        $ratio = 100;
    }
    $orignal_to_display_ratio = (double) $_REQUEST['dp_logo_to_resize_ratio'];
    $width = round($_REQUEST['dp_logo_resize_width'] / $orignal_to_display_ratio);
    $height = round($_REQUEST['dp_logo_resize_height'] / $orignal_to_display_ratio);
    $top = round($_REQUEST['dp_logo_resize_top'] / $orignal_to_display_ratio);
    $left = round($_REQUEST['dp_logo_resize_left'] / $orignal_to_display_ratio);
    $new_width = round($width * $ratio / 100);
    $new_height = round($height * $ratio / 100);
    //画像を読み込んでみる
    $info = dp_logo_info();
    if (!$info) {
        $dp_resize_message['message'] = __('Logo file not exists.', 'tcd-w');
        return $dp_resize_message;
    }
    // 保存ファイル名を決める
    $path = preg_replace("/logo\\.(png|gif|jpe?g)\$/i", "logo-resized.\$1", $info['path']);
    // 3.5以前と以降で処理を分岐
    try {
        // 3.5以降はwp_get_image_editorが存在する
        if (function_exists('wp_get_image_editor')) {
            // 新APIを利用
            $orig_image = wp_get_image_editor($info['path']);
            // 読み込み失敗ならエラー
            if (is_wp_error($orig_image)) {
                throw new Exception(__('Logo file not exists.', 'tcd-w'));
            }
            // リサイズしてダメだったらエラー
            $size = $orig_image->get_size();
            $resize_result = $orig_image->resize(round($size['width'] * $ratio / 100), round($size['height'] * $ratio / 100));
            if (is_wp_error($resize_result)) {
                // WordPressが返すエラーメッセージを利用
                // 適宜変更してください。
                throw new Exception($resize_result->get_error_message());
            }
            // 切り抜きしてダメだったらエラー
            $crop_result = $orig_image->crop(round($left * $ratio / 100), round($top * $ratio / 100), $new_width, $new_height);
            if (is_wp_error($crop_result)) {
                // WordPressが返すエラーメッセージを利用
                // 適宜変更してください。
                throw new Exception($crop_result->get_error_message());
            }
            // 保存してダメだったらエラー
            // 基本は上書きOK.
            $save_result = $orig_image->save($path);
            if (is_wp_error($save_result)) {
                // WordPressが返すエラーメッセージを利用
                // 適宜変更してください。
                throw new Exception($save_result->get_error_message());
            }
        } else {
            // それ以前は昔の方法で行う
            $orig_image = wp_load_image($info['path']);
            // 画像を読み込めなかったらエラー
            if (!is_resource($orig_image)) {
                throw new Exception(__('Logo file not exists.', 'tcd-w'));
            }
            $newimage = wp_imagecreatetruecolor($new_width, $new_height);
            imagecopyresampled($newimage, $orig_image, 0, 0, $left, $top, $new_width, $new_height, $width, $height);
            if (IMAGETYPE_PNG == $info['mime'] && function_exists('imageistruecolor')) {
                @imagetruecolortopalette($newimage, false, imagecolorstotal($orig_image));
            }
            imagedestroy($orig_image);
            //ファイルを保存する前に削除
            $dest_path = dp_logo_exists(true);
            if ($dest_path && !@unlink($dest_path)) {
                throw new Exception('Cannot delete existing resized logo.', 'tcd-w');
            }
            //名前を決めて保存
            $result = null;
            if (IMAGETYPE_GIF == $info['mime']) {
                $result = imagegif($newimage, $path);
            } elseif (IMAGETYPE_PNG == $info['mime']) {
                $result = imagepng($newimage, $path);
            } else {
                $result = imagejpeg($newimage, $path, 100);
            }
            imagedestroy($newimage);
            if (!$result) {
                throw new Exception(sprintf(__('Failed to save resized logo. Please check permission of <code>%s</code>', 'tcd-w'), dp_logo_basedir()));
            }
        }
    } catch (Exception $e) {
        // 上記処理中で一回でも例外が発生したら、エラーを返す
        $dp_resize_message['message'] = $e->getMessage();
        return $dp_resize_message;
    }
    // ここまで来たということはエラーなし
    $dp_resize_message['error'] = false;
    $dp_resize_message['message'] = __('Logo image is successfully resized.', 'tcd-w');
    return $dp_resize_message;
}
/**
 * チェック
 */
function theme_options_validate($input)
{
    global $color_options, $single_post_list_options, $bg_type1_options, $bg_type2_options;
    // フォントサイズ
    $input['content_font_size'] = wp_filter_nohtml_kses($input['content_font_size']);
    // 投稿者・タグ・コメント
    if (!isset($input['show_author'])) {
        $input['show_author'] = null;
    }
    $input['show_author'] = $input['show_author'] == 1 ? 1 : 0;
    if (!isset($input['show_comment'])) {
        $input['show_comment'] = null;
    }
    $input['show_comment'] = $input['show_comment'] == 1 ? 1 : 0;
    if (!isset($input['show_trackback'])) {
        $input['show_trackback'] = null;
    }
    $input['show_trackback'] = $input['show_trackback'] == 1 ? 1 : 0;
    if (!isset($input['show_bookmark'])) {
        $input['show_bookmark'] = null;
    }
    $input['show_bookmark'] = $input['show_bookmark'] == 1 ? 1 : 0;
    if (!isset($input['show_next_post'])) {
        $input['show_next_post'] = null;
    }
    $input['show_next_post'] = $input['show_next_post'] == 1 ? 1 : 0;
    if (!isset($input['show_related_post'])) {
        $input['show_related_post'] = null;
    }
    $input['show_related_post'] = $input['show_related_post'] == 1 ? 1 : 0;
    if (!isset($input['show_rss'])) {
        $input['show_rss'] = null;
    }
    $input['show_rss'] = $input['show_rss'] == 1 ? 1 : 0;
    if (!isset($input['show_search'])) {
        $input['show_search'] = null;
    }
    $input['show_search'] = $input['show_search'] == 1 ? 1 : 0;
    // 色の設定
    if (!isset($input['color_type'])) {
        $input['color_type'] = null;
    }
    if (!array_key_exists($input['color_type'], $color_options)) {
        $input['color_type'] = null;
    }
    // 背景色の設定
    if (!isset($input['bg_type1'])) {
        $input['bg_type1'] = null;
    }
    if (!array_key_exists($input['bg_type1'], $bg_type1_options)) {
        $input['bg_type1'] = null;
    }
    // 背景画像の設定
    if (!isset($input['bg_type2'])) {
        $input['bg_type2'] = null;
    }
    if (!array_key_exists($input['bg_type2'], $bg_type2_options)) {
        $input['bg_type2'] = null;
    }
    $input['original_pattern'] = wp_filter_nohtml_kses($input['original_pattern']);
    // 詳細記事ページの記事一覧の設定
    if (!isset($input['single_post_list'])) {
        $input['single_post_list'] = null;
    }
    if (!array_key_exists($input['single_post_list'], $single_post_list_options)) {
        $input['single_post_list'] = null;
    }
    $input['single_post_list_num'] = wp_filter_nohtml_kses($input['single_post_list_num']);
    // 左サイドメニューの設定
    $input['headline_side_category'] = wp_filter_nohtml_kses($input['headline_side_category']);
    $input['headline_side_archive'] = wp_filter_nohtml_kses($input['headline_side_archive']);
    $input['headline_side_menu'] = wp_filter_nohtml_kses($input['headline_side_menu']);
    if (!isset($input['show_side_category'])) {
        $input['show_side_category'] = null;
    }
    $input['show_side_category'] = $input['show_side_category'] == 1 ? 1 : 0;
    if (!isset($input['show_side_archive'])) {
        $input['show_side_archive'] = null;
    }
    $input['show_side_archive'] = $input['show_side_archive'] == 1 ? 1 : 0;
    if (!isset($input['show_side_menu'])) {
        $input['show_side_menu'] = null;
    }
    $input['show_side_menu'] = $input['show_side_menu'] == 1 ? 1 : 0;
    // twitter,facebook URL
    $input['twitter_url'] = wp_filter_nohtml_kses($input['twitter_url']);
    $input['facebook_url'] = wp_filter_nohtml_kses($input['facebook_url']);
    // 検索の設定
    $input['custom_search_id'] = wp_filter_nohtml_kses($input['custom_search_id']);
    // 詳細記事の広告
    $input['single_ad_code1'] = $input['single_ad_code1'];
    $input['single_ad_image1'] = wp_filter_nohtml_kses($input['single_ad_image1']);
    $input['single_ad_url1'] = wp_filter_nohtml_kses($input['single_ad_url1']);
    $input['single_ad_code2'] = $input['single_ad_code2'];
    $input['single_ad_image2'] = wp_filter_nohtml_kses($input['single_ad_image2']);
    $input['single_ad_url2'] = wp_filter_nohtml_kses($input['single_ad_url2']);
    $input['single_ad_code3'] = $input['single_ad_code3'];
    $input['single_ad_image3'] = wp_filter_nohtml_kses($input['single_ad_image3']);
    $input['single_ad_url3'] = wp_filter_nohtml_kses($input['single_ad_url3']);
    $input['single_ad_code4'] = $input['single_ad_code4'];
    $input['single_ad_image4'] = wp_filter_nohtml_kses($input['single_ad_image4']);
    $input['single_ad_url4'] = wp_filter_nohtml_kses($input['single_ad_url4']);
    // 記事一覧の広告
    $input['post_ad1'] = $input['post_ad1'];
    $input['post_ad_image1'] = wp_filter_nohtml_kses($input['post_ad_image1']);
    $input['post_ad_url1'] = wp_filter_nohtml_kses($input['post_ad_url1']);
    $input['post_ad2'] = $input['post_ad2'];
    $input['post_ad_image2'] = wp_filter_nohtml_kses($input['post_ad_image2']);
    $input['post_ad_url2'] = wp_filter_nohtml_kses($input['post_ad_url2']);
    $input['post_ad3'] = $input['post_ad3'];
    $input['post_ad_image3'] = wp_filter_nohtml_kses($input['post_ad_image3']);
    $input['post_ad_url3'] = wp_filter_nohtml_kses($input['post_ad_url3']);
    // スライダーの設定
    if (!isset($input['show_slider'])) {
        $input['show_slider'] = null;
    }
    $input['show_slider'] = $input['show_slider'] == 1 ? 1 : 0;
    $input['slider_image1'] = wp_filter_nohtml_kses($input['slider_image1']);
    $input['slider_url1'] = wp_filter_nohtml_kses($input['slider_url1']);
    $input['slider_image2'] = wp_filter_nohtml_kses($input['slider_image2']);
    $input['slider_url2'] = wp_filter_nohtml_kses($input['slider_url2']);
    $input['slider_image3'] = wp_filter_nohtml_kses($input['slider_image3']);
    $input['slider_url3'] = wp_filter_nohtml_kses($input['slider_url3']);
    $input['slider_image4'] = wp_filter_nohtml_kses($input['slider_image4']);
    $input['slider_url4'] = wp_filter_nohtml_kses($input['slider_url4']);
    $input['slider_image5'] = wp_filter_nohtml_kses($input['slider_image5']);
    $input['slider_url5'] = wp_filter_nohtml_kses($input['slider_url5']);
    $input['slider_image6'] = wp_filter_nohtml_kses($input['slider_image6']);
    $input['slider_url6'] = wp_filter_nohtml_kses($input['slider_url6']);
    $input['slider_image7'] = wp_filter_nohtml_kses($input['slider_image7']);
    $input['slider_url7'] = wp_filter_nohtml_kses($input['slider_url7']);
    $input['slider_image8'] = wp_filter_nohtml_kses($input['slider_image8']);
    $input['slider_url8'] = wp_filter_nohtml_kses($input['slider_url8']);
    $input['slider_image9'] = wp_filter_nohtml_kses($input['slider_image9']);
    $input['slider_url9'] = wp_filter_nohtml_kses($input['slider_url9']);
    $input['slider_image10'] = wp_filter_nohtml_kses($input['slider_image10']);
    $input['slider_url10'] = wp_filter_nohtml_kses($input['slider_url10']);
    //ロゴの位置
    if (isset($input['logotop'])) {
        $input['logotop'] = intval($input['logotop']);
    }
    if (isset($input['logoleft'])) {
        $input['logoleft'] = intval($input['logoleft']);
    }
    //ファイルアップロード
    if (isset($_FILES['dp_image'])) {
        $message = _dp_upload_logo();
        add_settings_error('design_plus_options', 'default', $message['message'], $message['error'] ? 'error' : 'updated');
    }
    //画像リサイズ
    if (isset($_REQUEST['dp_logo_resize_left'], $_REQUEST['dp_logo_resize_top']) && is_numeric($_REQUEST['dp_logo_resize_left']) && is_numeric($_REQUEST['dp_logo_resize_top'])) {
        $message = _dp_resize_logo();
        add_settings_error('design_plus_options', 'default', $message['message'], $message['error'] ? 'error' : 'updated');
    }
    //詳細記事ページ右上の広告バナー
    if (isset($_FILES['single_ad_image_file1'])) {
        //画像のアップロードに問題はないか
        if ($_FILES['single_ad_image_file1']['error'] === 0) {
            $name = sanitize_file_name($_FILES['single_ad_image_file1']['name']);
            //ファイル形式をチェック
            if (!preg_match("/\\.(png|jpe?g|gif)\$/i", $name)) {
                add_settings_error('design_plus_options', 'dp_uploader', sprintf(__('You uploaded %s but allowed file format is PNG, GIF and JPG.', 'tcd-w'), $name), 'error');
            } else {
                //ディレクトリの存在をチェック
                if ((file_exists(dp_logo_basedir()) && is_dir(dp_logo_basedir()) && is_writable(dp_logo_basedir()) || @mkdir(dp_logo_basedir())) && move_uploaded_file($_FILES['single_ad_image_file1']['tmp_name'], dp_logo_basedir() . DIRECTORY_SEPARATOR . $name)) {
                    $input['single_ad_image1'] = dp_logo_baseurl() . '/' . $name;
                } else {
                    add_settings_error('default', 'dp_uploader', sprintf(__('Directory %s is not writable. Please check permission.', 'tcd-w'), dp_logo_basedir()), 'error');
                }
            }
        } elseif ($_FILES['single_ad_image_file1']['error'] !== UPLOAD_ERR_NO_FILE) {
            add_settings_error('default', 'dp_uploader', _dp_get_upload_err_msg($_FILES['single_ad_image_file1']['error']), 'error');
        }
    }
    //詳細記事ページ右下の広告バナー
    if (isset($_FILES['single_ad_image_file2'])) {
        //画像のアップロードに問題はないか
        if ($_FILES['single_ad_image_file2']['error'] === 0) {
            $name = sanitize_file_name($_FILES['single_ad_image_file2']['name']);
            //ファイル形式をチェック
            if (!preg_match("/\\.(png|jpe?g|gif)\$/i", $name)) {
                add_settings_error('design_plus_options', 'dp_uploader', sprintf(__('You uploaded %s but allowed file format is PNG, GIF and JPG.', 'tcd-w'), $name), 'error');
            } else {
                //ディレクトリの存在をチェック
                if ((file_exists(dp_logo_basedir()) && is_dir(dp_logo_basedir()) && is_writable(dp_logo_basedir()) || @mkdir(dp_logo_basedir())) && move_uploaded_file($_FILES['single_ad_image_file2']['tmp_name'], dp_logo_basedir() . DIRECTORY_SEPARATOR . $name)) {
                    $input['single_ad_image2'] = dp_logo_baseurl() . '/' . $name;
                } else {
                    add_settings_error('default', 'dp_uploader', sprintf(__('Directory %s is not writable. Please check permission.', 'tcd-w'), dp_logo_basedir()), 'error');
                }
            }
        } elseif ($_FILES['single_ad_image_file2']['error'] !== UPLOAD_ERR_NO_FILE) {
            add_settings_error('default', 'dp_uploader', _dp_get_upload_err_msg($_FILES['single_ad_image_file2']['error']), 'error');
        }
    }
    //詳細記事ページ上部の広告バナー
    if (isset($_FILES['single_ad_image_file3'])) {
        //画像のアップロードに問題はないか
        if ($_FILES['single_ad_image_file3']['error'] === 0) {
            $name = sanitize_file_name($_FILES['single_ad_image_file3']['name']);
            //ファイル形式をチェック
            if (!preg_match("/\\.(png|jpe?g|gif)\$/i", $name)) {
                add_settings_error('design_plus_options', 'dp_uploader', sprintf(__('You uploaded %s but allowed file format is PNG, GIF and JPG.', 'tcd-w'), $name), 'error');
            } else {
                //ディレクトリの存在をチェック
                if ((file_exists(dp_logo_basedir()) && is_dir(dp_logo_basedir()) && is_writable(dp_logo_basedir()) || @mkdir(dp_logo_basedir())) && move_uploaded_file($_FILES['single_ad_image_file3']['tmp_name'], dp_logo_basedir() . DIRECTORY_SEPARATOR . $name)) {
                    $input['single_ad_image3'] = dp_logo_baseurl() . '/' . $name;
                } else {
                    add_settings_error('default', 'dp_uploader', sprintf(__('Directory %s is not writable. Please check permission.', 'tcd-w'), dp_logo_basedir()), 'error');
                }
            }
        } elseif ($_FILES['single_ad_image_file3']['error'] !== UPLOAD_ERR_NO_FILE) {
            add_settings_error('default', 'dp_uploader', _dp_get_upload_err_msg($_FILES['single_ad_image_file3']['error']), 'error');
        }
    }
    //詳細記事ページ下部の広告バナー
    if (isset($_FILES['single_ad_image_file4'])) {
        //画像のアップロードに問題はないか
        if ($_FILES['single_ad_image_file4']['error'] === 0) {
            $name = sanitize_file_name($_FILES['single_ad_image_file4']['name']);
            //ファイル形式をチェック
            if (!preg_match("/\\.(png|jpe?g|gif)\$/i", $name)) {
                add_settings_error('design_plus_options', 'dp_uploader', sprintf(__('You uploaded %s but allowed file format is PNG, GIF and JPG.', 'tcd-w'), $name), 'error');
            } else {
                //ディレクトリの存在をチェック
                if ((file_exists(dp_logo_basedir()) && is_dir(dp_logo_basedir()) && is_writable(dp_logo_basedir()) || @mkdir(dp_logo_basedir())) && move_uploaded_file($_FILES['single_ad_image_file4']['tmp_name'], dp_logo_basedir() . DIRECTORY_SEPARATOR . $name)) {
                    $input['single_ad_image4'] = dp_logo_baseurl() . '/' . $name;
                } else {
                    add_settings_error('default', 'dp_uploader', sprintf(__('Directory %s is not writable. Please check permission.', 'tcd-w'), dp_logo_basedir()), 'error');
                }
            }
        } elseif ($_FILES['single_ad_image_file4']['error'] !== UPLOAD_ERR_NO_FILE) {
            add_settings_error('default', 'dp_uploader', _dp_get_upload_err_msg($_FILES['single_ad_image_file4']['error']), 'error');
        }
    }
    //記事一覧の広告
    for ($i = 1; $i <= 3; $i++) {
        if (isset($_FILES['post_ad_image_file_' . $i])) {
            //画像のアップロードに問題はないか
            if ($_FILES['post_ad_image_file_' . $i]['error'] === 0) {
                $name = sanitize_file_name($_FILES['post_ad_image_file_' . $i]['name']);
                //ファイル形式をチェック
                if (!preg_match("/\\.(png|jpe?g|gif)\$/i", $name)) {
                    add_settings_error('design_plus_options', 'dp_uploader', sprintf(__('You uploaded %s but allowed file format is PNG, GIF and JPG.', 'tcd-w'), $name), 'error');
                } else {
                    //ディレクトリの存在をチェック
                    if ((file_exists(dp_logo_basedir()) && is_dir(dp_logo_basedir()) && is_writable(dp_logo_basedir()) || @mkdir(dp_logo_basedir())) && move_uploaded_file($_FILES['post_ad_image_file_' . $i]['tmp_name'], dp_logo_basedir() . DIRECTORY_SEPARATOR . $name)) {
                        $input['post_ad_image' . $i] = dp_logo_baseurl() . '/' . $name;
                    } else {
                        add_settings_error('default', 'dp_uploader', sprintf(__('Directory %s is not writable. Please check permission.', 'tcd-w'), dp_logo_basedir()), 'error');
                        break;
                    }
                }
            } elseif ($_FILES['post_ad_image_file_' . $i]['error'] !== UPLOAD_ERR_NO_FILE) {
                add_settings_error('default', 'dp_uploader', _dp_get_upload_err_msg($_FILES['post_ad_image_file_' . $i]['error']), 'error');
                continue;
            }
        }
    }
    //スライダーの画像
    for ($i = 1; $i <= 10; $i++) {
        if (isset($_FILES['slider_image_file' . $i])) {
            //画像のアップロードに問題はないか
            if ($_FILES['slider_image_file' . $i]['error'] === 0) {
                $name = sanitize_file_name($_FILES['slider_image_file' . $i]['name']);
                //ファイル形式をチェック
                if (!preg_match("/\\.(png|jpe?g|gif)\$/i", $name)) {
                    add_settings_error('design_plus_options', 'dp_uploader', sprintf(__('You uploaded %s but allowed file format is PNG, GIF and JPG.', 'tcd-w'), $name), 'error');
                } else {
                    //ディレクトリの存在をチェック
                    if ((file_exists(dp_logo_basedir()) && is_dir(dp_logo_basedir()) && is_writable(dp_logo_basedir()) || @mkdir(dp_logo_basedir())) && move_uploaded_file($_FILES['slider_image_file' . $i]['tmp_name'], dp_logo_basedir() . DIRECTORY_SEPARATOR . $name)) {
                        $input['slider_image' . $i] = dp_logo_baseurl() . '/' . $name;
                    } else {
                        add_settings_error('default', 'dp_uploader', sprintf(__('Directory %s is not writable. Please check permission.', 'tcd-w'), dp_logo_basedir()), 'error');
                        break;
                    }
                }
            } elseif ($_FILES['slider_image_file' . $i]['error'] !== UPLOAD_ERR_NO_FILE) {
                add_settings_error('default', 'dp_uploader', _dp_get_upload_err_msg($_FILES['slider_image_file' . $i]['error']), 'error');
                continue;
            }
        }
    }
    //オリジナルパターン
    if (isset($_FILES['original_pattern_file'])) {
        //画像のアップロードに問題はないか
        if ($_FILES['original_pattern_file']['error'] === 0) {
            $name = sanitize_file_name($_FILES['original_pattern_file']['name']);
            //ファイル形式をチェック
            if (!preg_match("/\\.(png|jpe?g|gif)\$/i", $name)) {
                add_settings_error('design_plus_options', 'dp_uploader', sprintf(__('You uploaded %s but allowed file format is PNG, GIF and JPG.', 'tcd-w'), $name), 'error');
            } else {
                //ディレクトリの存在をチェック
                if ((file_exists(dp_logo_basedir()) && is_dir(dp_logo_basedir()) && is_writable(dp_logo_basedir()) || @mkdir(dp_logo_basedir())) && move_uploaded_file($_FILES['original_pattern_file']['tmp_name'], dp_logo_basedir() . DIRECTORY_SEPARATOR . $name)) {
                    $input['original_pattern'] = dp_logo_baseurl() . '/' . $name;
                } else {
                    add_settings_error('default', 'dp_uploader', sprintf(__('Directory %s is not writable. Please check permission.', 'tcd-w'), dp_logo_basedir()), 'error');
                }
            }
        } elseif ($_FILES['original_pattern_file']['error'] !== UPLOAD_ERR_NO_FILE) {
            add_settings_error('default', 'dp_uploader', _dp_get_upload_err_msg($_FILES['original_pattern_file']['error']), 'error');
        }
    }
    return $input;
}
/**
 * チェック
 */
function theme_options_validate($input)
{
    global $layout_options, $header_layout_options, $color_type_options;
    // 色の設定
    if (!isset($input['color_type'])) {
        $input['color_type'] = null;
    }
    if (!array_key_exists($input['color_type'], $color_type_options)) {
        $input['color_type'] = null;
    }
    // 色の設定2
    $input['pickedcolor'] = wp_filter_nohtml_kses($input['pickedcolor']);
    $input['pickedcolor2'] = wp_filter_nohtml_kses($input['pickedcolor2']);
    $input['pickedcolor3'] = wp_filter_nohtml_kses($input['pickedcolor3']);
    // フォントサイズ
    $input['content_font_size'] = wp_filter_nohtml_kses($input['content_font_size']);
    // 投稿者・タグ・コメント
    if (!isset($input['show_author'])) {
        $input['show_author'] = null;
    }
    $input['show_author'] = $input['show_author'] == 1 ? 1 : 0;
    if (!isset($input['show_comment'])) {
        $input['show_comment'] = null;
    }
    $input['show_comment'] = $input['show_comment'] == 1 ? 1 : 0;
    if (!isset($input['show_trackback'])) {
        $input['show_trackback'] = null;
    }
    $input['show_trackback'] = $input['show_trackback'] == 1 ? 1 : 0;
    if (!isset($input['show_next_post'])) {
        $input['show_next_post'] = null;
    }
    $input['show_next_post'] = $input['show_next_post'] == 1 ? 1 : 0;
    if (!isset($input['show_rss'])) {
        $input['show_rss'] = null;
    }
    $input['show_rss'] = $input['show_rss'] == 1 ? 1 : 0;
    if (!isset($input['show_related_post'])) {
        $input['show_related_post'] = null;
    }
    $input['show_related_post'] = $input['show_related_post'] == 1 ? 1 : 0;
    if (!isset($input['show_bookmark'])) {
        $input['show_bookmark'] = null;
    }
    $input['show_bookmark'] = $input['show_bookmark'] == 1 ? 1 : 0;
    // レイアウトの設定
    if (!isset($input['layout'])) {
        $input['layout'] = null;
    }
    if (!array_key_exists($input['layout'], $layout_options)) {
        $input['layout'] = null;
    }
    // ヘッダーレイアウトの設定
    if (!isset($input['header_layout'])) {
        $input['header_layout'] = null;
    }
    if (!array_key_exists($input['header_layout'], $header_layout_options)) {
        $input['header_layout'] = null;
    }
    // twitter,facebook URL
    $input['twitter_url'] = wp_filter_nohtml_kses($input['twitter_url']);
    $input['facebook_url'] = wp_filter_nohtml_kses($input['facebook_url']);
    // ヘッダーの広告バナー
    $input['header_ad_code1'] = $input['header_ad_code1'];
    $input['header_ad_image1'] = wp_filter_nohtml_kses($input['header_ad_image1']);
    $input['header_ad_url1'] = wp_filter_nohtml_kses($input['header_ad_url1']);
    // 詳細記事上部の広告バナー
    $input['single_ad_code1'] = $input['single_ad_code1'];
    $input['single_ad_image1'] = wp_filter_nohtml_kses($input['single_ad_image1']);
    $input['single_ad_url1'] = wp_filter_nohtml_kses($input['single_ad_url1']);
    // 詳細記事下部の広告バナー
    $input['single_ad_code2'] = $input['single_ad_code2'];
    $input['single_ad_image2'] = wp_filter_nohtml_kses($input['single_ad_image2']);
    $input['single_ad_url2'] = wp_filter_nohtml_kses($input['single_ad_url2']);
    // モバイル用の広告バナー(上部)
    $input['mobile_ad_code1'] = $input['mobile_ad_code1'];
    $input['mobile_ad_image1'] = wp_filter_nohtml_kses($input['mobile_ad_image1']);
    $input['mobile_ad_url1'] = wp_filter_nohtml_kses($input['mobile_ad_url1']);
    // モバイル用の広告バナー(下部)
    $input['mobile_ad_code2'] = $input['mobile_ad_code2'];
    $input['mobile_ad_image2'] = wp_filter_nohtml_kses($input['mobile_ad_image2']);
    $input['mobile_ad_url2'] = wp_filter_nohtml_kses($input['mobile_ad_url2']);
    //ロゴの位置
    if (isset($input['logotop'])) {
        $input['logotop'] = intval($input['logotop']);
    }
    if (isset($input['logoleft'])) {
        $input['logoleft'] = intval($input['logoleft']);
    }
    // 広告ウィジェット1
    $input['side_ad_top_code1'] = $input['side_ad_top_code1'];
    $input['side_ad_top_image1'] = wp_filter_nohtml_kses($input['side_ad_top_image1']);
    $input['side_ad_top_url1'] = wp_filter_nohtml_kses($input['side_ad_top_url1']);
    $input['side_ad_top_code2'] = $input['side_ad_top_code2'];
    $input['side_ad_top_image2'] = wp_filter_nohtml_kses($input['side_ad_top_image2']);
    $input['side_ad_top_url2'] = wp_filter_nohtml_kses($input['side_ad_top_url2']);
    $input['side_ad_top_code3'] = $input['side_ad_top_code3'];
    $input['side_ad_top_image3'] = wp_filter_nohtml_kses($input['side_ad_top_image3']);
    $input['side_ad_top_url3'] = wp_filter_nohtml_kses($input['side_ad_top_url3']);
    if (!isset($input['fix_ad'])) {
        $input['fix_ad'] = null;
    }
    $input['fix_ad'] = $input['fix_ad'] == 1 ? 1 : 0;
    // 広告ウィジェット2
    $input['side_ad_bottom_code1'] = $input['side_ad_bottom_code1'];
    $input['side_ad_bottom_image1'] = wp_filter_nohtml_kses($input['side_ad_bottom_image1']);
    $input['side_ad_bottom_url1'] = wp_filter_nohtml_kses($input['side_ad_bottom_url1']);
    $input['side_ad_bottom_code2'] = $input['side_ad_bottom_code2'];
    $input['side_ad_bottom_image2'] = wp_filter_nohtml_kses($input['side_ad_bottom_image2']);
    $input['side_ad_bottom_url2'] = wp_filter_nohtml_kses($input['side_ad_bottom_url2']);
    $input['side_ad_bottom_code3'] = $input['side_ad_bottom_code3'];
    $input['side_ad_bottom_image3'] = wp_filter_nohtml_kses($input['side_ad_bottom_image3']);
    $input['side_ad_bottom_url3'] = wp_filter_nohtml_kses($input['side_ad_bottom_url3']);
    //ファイルアップロード
    if (isset($_FILES['dp_image'])) {
        $message = _dp_upload_logo();
        add_settings_error('design_plus_options', 'default', $message['message'], $message['error'] ? 'error' : 'updated');
    }
    //画像リサイズ
    if (isset($_REQUEST['dp_logo_resize_left'], $_REQUEST['dp_logo_resize_top']) && is_numeric($_REQUEST['dp_logo_resize_left']) && is_numeric($_REQUEST['dp_logo_resize_top'])) {
        $message = _dp_resize_logo();
        add_settings_error('design_plus_options', 'default', $message['message'], $message['error'] ? 'error' : 'updated');
    }
    //ヘッダーの広告バナー
    if (isset($_FILES['header_ad_image_file1'])) {
        //画像のアップロードに問題はないか
        if ($_FILES['header_ad_image_file1']['error'] === 0) {
            $name = sanitize_file_name($_FILES['header_ad_image_file1']['name']);
            //ファイル形式をチェック
            if (!preg_match("/\\.(png|jpe?g|gif)\$/i", $name)) {
                add_settings_error('design_plus_options', 'dp_uploader', sprintf(__('You uploaded %s but allowed file format is PNG, GIF and JPG.', 'tcd-w'), $name), 'error');
            } else {
                //ディレクトリの存在をチェック
                if ((file_exists(dp_logo_basedir()) && is_dir(dp_logo_basedir()) && is_writable(dp_logo_basedir()) || @mkdir(dp_logo_basedir())) && move_uploaded_file($_FILES['header_ad_image_file1']['tmp_name'], dp_logo_basedir() . DIRECTORY_SEPARATOR . $name)) {
                    $input['header_ad_image1'] = dp_logo_baseurl() . '/' . $name;
                } else {
                    add_settings_error('default', 'dp_uploader', sprintf(__('Directory %s is not writable. Please check permission.', 'tcd-w'), dp_logo_basedir()), 'error');
                    //break;
                }
            }
        } elseif ($_FILES['header_ad_image_file1']['error'] !== UPLOAD_ERR_NO_FILE) {
            add_settings_error('default', 'dp_uploader', _dp_get_upload_err_msg($_FILES['header_ad_image_file1']['error']), 'error');
            //continue;
        }
    }
    //詳細記事ページ上部の広告バナー
    if (isset($_FILES['single_ad_image_file1'])) {
        //画像のアップロードに問題はないか
        if ($_FILES['single_ad_image_file1']['error'] === 0) {
            $name = sanitize_file_name($_FILES['single_ad_image_file1']['name']);
            //ファイル形式をチェック
            if (!preg_match("/\\.(png|jpe?g|gif)\$/i", $name)) {
                add_settings_error('design_plus_options', 'dp_uploader', sprintf(__('You uploaded %s but allowed file format is PNG, GIF and JPG.', 'tcd-w'), $name), 'error');
            } else {
                //ディレクトリの存在をチェック
                if ((file_exists(dp_logo_basedir()) && is_dir(dp_logo_basedir()) && is_writable(dp_logo_basedir()) || @mkdir(dp_logo_basedir())) && move_uploaded_file($_FILES['single_ad_image_file1']['tmp_name'], dp_logo_basedir() . DIRECTORY_SEPARATOR . $name)) {
                    $input['single_ad_image1'] = dp_logo_baseurl() . '/' . $name;
                } else {
                    add_settings_error('default', 'dp_uploader', sprintf(__('Directory %s is not writable. Please check permission.', 'tcd-w'), dp_logo_basedir()), 'error');
                    //break;
                }
            }
        } elseif ($_FILES['single_ad_image_file1']['error'] !== UPLOAD_ERR_NO_FILE) {
            add_settings_error('default', 'dp_uploader', _dp_get_upload_err_msg($_FILES['single_ad_image_file1']['error']), 'error');
            //continue;
        }
    }
    //詳細記事ページ下部の広告バナー
    if (isset($_FILES['single_ad_image_file2'])) {
        //画像のアップロードに問題はないか
        if ($_FILES['single_ad_image_file2']['error'] === 0) {
            $name = sanitize_file_name($_FILES['single_ad_image_file2']['name']);
            //ファイル形式をチェック
            if (!preg_match("/\\.(png|jpe?g|gif)\$/i", $name)) {
                add_settings_error('design_plus_options', 'dp_uploader', sprintf(__('You uploaded %s but allowed file format is PNG, GIF and JPG.', 'tcd-w'), $name), 'error');
            } else {
                //ディレクトリの存在をチェック
                if ((file_exists(dp_logo_basedir()) && is_dir(dp_logo_basedir()) && is_writable(dp_logo_basedir()) || @mkdir(dp_logo_basedir())) && move_uploaded_file($_FILES['single_ad_image_file2']['tmp_name'], dp_logo_basedir() . DIRECTORY_SEPARATOR . $name)) {
                    $input['single_ad_image2'] = dp_logo_baseurl() . '/' . $name;
                } else {
                    add_settings_error('default', 'dp_uploader', sprintf(__('Directory %s is not writable. Please check permission.', 'tcd-w'), dp_logo_basedir()), 'error');
                    //break;
                }
            }
        } elseif ($_FILES['single_ad_image_file2']['error'] !== UPLOAD_ERR_NO_FILE) {
            add_settings_error('default', 'dp_uploader', _dp_get_upload_err_msg($_FILES['single_ad_image_file2']['error']), 'error');
            //continue;
        }
    }
    //広告ウィジェット1
    for ($i = 1; $i <= 3; $i++) {
        if (isset($_FILES['side_ad_top_image_file_' . $i])) {
            //画像のアップロードに問題はないか
            if ($_FILES['side_ad_top_image_file_' . $i]['error'] === 0) {
                $name = sanitize_file_name($_FILES['side_ad_top_image_file_' . $i]['name']);
                //ファイル形式をチェック
                if (!preg_match("/\\.(png|jpe?g|gif)\$/i", $name)) {
                    add_settings_error('design_plus_options', 'dp_uploader', sprintf(__('You uploaded %s but allowed file format is PNG, GIF and JPG.', 'tcd-w'), $name), 'error');
                } else {
                    //ディレクトリの存在をチェック
                    if ((file_exists(dp_logo_basedir()) && is_dir(dp_logo_basedir()) && is_writable(dp_logo_basedir()) || @mkdir(dp_logo_basedir())) && move_uploaded_file($_FILES['side_ad_top_image_file_' . $i]['tmp_name'], dp_logo_basedir() . DIRECTORY_SEPARATOR . $name)) {
                        $input['side_ad_top_image' . $i] = dp_logo_baseurl() . '/' . $name;
                    } else {
                        add_settings_error('default', 'dp_uploader', sprintf(__('Directory %s is not writable. Please check permission.', 'tcd-w'), dp_logo_basedir()), 'error');
                        break;
                    }
                }
            } elseif ($_FILES['side_ad_top_image_file_' . $i]['error'] !== UPLOAD_ERR_NO_FILE) {
                add_settings_error('default', 'dp_uploader', _dp_get_upload_err_msg($_FILES['side_ad_top_image_file_' . $i]['error']), 'error');
                continue;
            }
        }
    }
    //広告ウィジェット2
    for ($i = 1; $i <= 3; $i++) {
        if (isset($_FILES['side_ad_bottom_image_file_' . $i])) {
            //画像のアップロードに問題はないか
            if ($_FILES['side_ad_bottom_image_file_' . $i]['error'] === 0) {
                $name = sanitize_file_name($_FILES['side_ad_bottom_image_file_' . $i]['name']);
                //ファイル形式をチェック
                if (!preg_match("/\\.(png|jpe?g|gif)\$/i", $name)) {
                    add_settings_error('design_plus_options', 'dp_uploader', sprintf(__('You uploaded %s but allowed file format is PNG, GIF and JPG.', 'tcd-w'), $name), 'error');
                } else {
                    //ディレクトリの存在をチェック
                    if ((file_exists(dp_logo_basedir()) && is_dir(dp_logo_basedir()) && is_writable(dp_logo_basedir()) || @mkdir(dp_logo_basedir())) && move_uploaded_file($_FILES['side_ad_bottom_image_file_' . $i]['tmp_name'], dp_logo_basedir() . DIRECTORY_SEPARATOR . $name)) {
                        $input['side_ad_bottom_image' . $i] = dp_logo_baseurl() . '/' . $name;
                    } else {
                        add_settings_error('default', 'dp_uploader', sprintf(__('Directory %s is not writable. Please check permission.', 'tcd-w'), dp_logo_basedir()), 'error');
                        break;
                    }
                }
            } elseif ($_FILES['side_ad_bottom_image_file_' . $i]['error'] !== UPLOAD_ERR_NO_FILE) {
                add_settings_error('default', 'dp_uploader', _dp_get_upload_err_msg($_FILES['side_ad_bottom_image_file_' . $i]['error']), 'error');
                continue;
            }
        }
    }
    //モバイル用広告(上部)
    if (isset($_FILES['mobile_ad_image_file1'])) {
        //画像のアップロードに問題はないか
        if ($_FILES['mobile_ad_image_file1']['error'] === 0) {
            $name = sanitize_file_name($_FILES['mobile_ad_image_file1']['name']);
            //ファイル形式をチェック
            if (!preg_match("/\\.(png|jpe?g|gif)\$/i", $name)) {
                add_settings_error('design_plus_options', 'dp_uploader', sprintf(__('You uploaded %s but allowed file format is PNG, GIF and JPG.', 'tcd-w'), $name), 'error');
            } else {
                //ディレクトリの存在をチェック
                if ((file_exists(dp_logo_basedir()) && is_dir(dp_logo_basedir()) && is_writable(dp_logo_basedir()) || @mkdir(dp_logo_basedir())) && move_uploaded_file($_FILES['mobile_ad_image_file1']['tmp_name'], dp_logo_basedir() . DIRECTORY_SEPARATOR . $name)) {
                    $input['mobile_ad_image1'] = dp_logo_baseurl() . '/' . $name;
                } else {
                    add_settings_error('default', 'dp_uploader', sprintf(__('Directory %s is not writable. Please check permission.', 'tcd-w'), dp_logo_basedir()), 'error');
                }
            }
        } elseif ($_FILES['mobile_ad_image_file1']['error'] !== UPLOAD_ERR_NO_FILE) {
            add_settings_error('default', 'dp_uploader', _dp_get_upload_err_msg($_FILES['mobile_ad_image_file1']['error']), 'error');
        }
    }
    //モバイル用広告(下部)
    if (isset($_FILES['mobile_ad_image_file2'])) {
        //画像のアップロードに問題はないか
        if ($_FILES['mobile_ad_image_file2']['error'] === 0) {
            $name = sanitize_file_name($_FILES['mobile_ad_image_file2']['name']);
            //ファイル形式をチェック
            if (!preg_match("/\\.(png|jpe?g|gif)\$/i", $name)) {
                add_settings_error('design_plus_options', 'dp_uploader', sprintf(__('You uploaded %s but allowed file format is PNG, GIF and JPG.', 'tcd-w'), $name), 'error');
            } else {
                //ディレクトリの存在をチェック
                if ((file_exists(dp_logo_basedir()) && is_dir(dp_logo_basedir()) && is_writable(dp_logo_basedir()) || @mkdir(dp_logo_basedir())) && move_uploaded_file($_FILES['mobile_ad_image_file2']['tmp_name'], dp_logo_basedir() . DIRECTORY_SEPARATOR . $name)) {
                    $input['mobile_ad_image2'] = dp_logo_baseurl() . '/' . $name;
                } else {
                    add_settings_error('default', 'dp_uploader', sprintf(__('Directory %s is not writable. Please check permission.', 'tcd-w'), dp_logo_basedir()), 'error');
                }
            }
        } elseif ($_FILES['mobile_ad_image_file2']['error'] !== UPLOAD_ERR_NO_FILE) {
            add_settings_error('default', 'dp_uploader', _dp_get_upload_err_msg($_FILES['mobile_ad_image_file2']['error']), 'error');
        }
    }
    return $input;
}
function _dp_resize_logo()
{
    $dp_resize_message = array('error' => true, 'message' => '');
    //値をチェック
    $ratio = intval($_REQUEST['dp_resize_ratio']);
    if (!($ratio > 0 && $ratio <= 100)) {
        $ratio = 100;
    }
    $orignal_to_display_ratio = (double) $_REQUEST['dp_logo_to_resize_ratio'];
    $width = round($_REQUEST['dp_logo_resize_width'] / $orignal_to_display_ratio);
    $height = round($_REQUEST['dp_logo_resize_height'] / $orignal_to_display_ratio);
    $top = round($_REQUEST['dp_logo_resize_top'] / $orignal_to_display_ratio);
    $left = round($_REQUEST['dp_logo_resize_left'] / $orignal_to_display_ratio);
    //画像を読み込んでみる
    $info = dp_logo_info();
    if (!$info) {
        $dp_resize_message['message'] = __('Logo file not exists.', 'tcd-w');
        return $dp_resize_message;
    }
    $orig_image = wp_load_image($info['path']);
    if (!is_resource($orig_image)) {
        $dp_resize_message['message'] = __('Logo file not exists.', 'tcd-w');
        return $dp_resize_message;
    }
    $new_width = round($width * $ratio / 100);
    $new_height = round($height * $ratio / 100);
    $newimage = wp_imagecreatetruecolor($new_width, $new_height);
    imagecopyresampled($newimage, $orig_image, 0, 0, $left, $top, $new_width, $new_height, $width, $height);
    if (IMAGETYPE_PNG == $info['mime'] && function_exists('imageistruecolor')) {
        @imagetruecolortopalette($newimage, false, imagecolorstotal($orig_image));
    }
    imagedestroy($orig_image);
    //ファイルを保存する前に削除
    $dest_path = dp_logo_exists(true);
    if ($dest_path && !@unlink($dest_path)) {
        $dp_resize_message['message'] = __('Cannot delete existing resized logo.', 'tcd-w');
        return $dp_resize_message;
    }
    //名前を決めて保存
    $path = preg_replace("/logo\\.(png|gif|jpe?g)\$/i", "logo-resized.\$1", $info['path']);
    $result = null;
    if (IMAGETYPE_GIF == $info['mime']) {
        $result = imagegif($newimage, $path);
    } elseif (IMAGETYPE_PNG == $info['mime']) {
        $result = imagepng($newimage, $path);
    } else {
        $result = imagejpeg($newimage, $path, 100);
    }
    imagedestroy($newimage);
    if ($result) {
        $dp_resize_message['error'] = false;
        $dp_resize_message['message'] = __('Logo image is successfully resized.', 'tcd-w');
    } else {
        $dp_resize_message['message'] = sprintf(__('Failed to save resized logo. Please check permission of <code>%s</code>', 'tcd-w'), dp_logo_basedir());
    }
    return $dp_resize_message;
}