/** * バリデーションチェック * * @param string $key name属性 * @param array $option * @return string エラーメッセージ */ public function rule($key, array $options = array()) { $value = $this->Data->get($key); $value = MWF_Functions::convert_eol($value); if (!MWF_Functions::is_empty($value)) { $defaults = array('min' => 0, 'max' => 0, 'message' => __('The number of characters is invalid.', MWF_Config::DOMAIN)); $options = array_merge($defaults, $options); $length = mb_strlen($value, get_bloginfo('charset')); if (MWF_Functions::is_numeric($options['min'])) { if (MWF_Functions::is_numeric($options['max'])) { if (!($options['min'] <= $length && $length <= $options['max'])) { return $options['message']; } } else { if ($options['min'] > $length) { return $options['message']; } } } elseif (MWF_Functions::is_numeric($options['max'])) { if ($options['max'] < $length) { return $options['message']; } } } }
/** * csv_download * CSVを生成、出力 */ public function csv_download() { if (!isset($_GET['post_type'])) { return; } $post_type = $_GET['post_type']; if (in_array($post_type, $this->form_post_type) && !empty($_POST) && isset($_POST[MWF_Config::NAME . '-csv-download']) && check_admin_referer(MWF_Config::NAME)) { // posts_per_page $posts_per_page = -1; if ((isset($_POST['download-all']) && $_POST['download-all'] === 'true') === false) { $current_user = wp_get_current_user(); $_posts_per_page = get_user_meta($current_user->ID, 'edit_' . $post_type . '_per_page', true); if (!empty($_posts_per_page)) { $posts_per_page = $_posts_per_page; } } // paged $paged = 1; $_paged = $_GET['paged']; if (!empty($_paged) && MWF_Functions::is_numeric($_paged) && $posts_per_page > 0) { $paged = $_paged; } $posts_mwf = get_posts(array('post_type' => $post_type, 'posts_per_page' => $posts_per_page, 'paged' => $paged, 'post_status' => 'any')); $csv = ''; // 見出しを追加 $default_headings = array('ID', __('Response Status', MWF_Config::DOMAIN), 'post_date', 'post_modified', 'post_title'); $rows[] = $default_headings; foreach ($posts_mwf as $post) { setup_postdata($post); $columns = array(); foreach ($posts_mwf as $post) { $post_custom_keys = get_post_custom_keys($post->ID); if (!empty($post_custom_keys) && is_array($post_custom_keys)) { foreach ($post_custom_keys as $key) { if (preg_match('/^_/', $key)) { continue; } $columns[$key] = $key; } } } $rows[0] = array_merge($rows[0], $columns); } wp_reset_postdata(); $rows[0] = array_merge($rows[0], array(__('Memo', MWF_Config::DOMAIN))); // 各データを追加 foreach ($posts_mwf as $post) { setup_postdata($post); $column = array(); foreach ($rows[0] as $key => $value) { $_column = ''; if ($value === __('Response Status', MWF_Config::DOMAIN)) { $_column = $this->get_post_data_value('response_status', $post->ID); } elseif ($value === __('Memo', MWF_Config::DOMAIN)) { $_column = $this->get_post_data_value('memo', $post->ID); } elseif (isset($post->{$value})) { $post_meta = $post->{$value}; if ($this->is_upload_file_key($post, $value)) { $_column = wp_get_attachment_url($post_meta); } else { $_column = $post_meta ? $post_meta : ''; } } $column[$key] = $this->escape_double_quote($_column); } $rows[] = $column; } // 見出し行をエスケープ foreach ($rows[0] as $key => $value) { $rows[0][$key] = $this->escape_double_quote($value); } wp_reset_postdata(); // エンコード foreach ($rows as $key => $row) { if ($key === 0) { foreach ($row as $row_key => $column_name) { if (in_array($column_name, array('Response Status', 'Memo'))) { $column_name = esc_html__($column_name, MWF_Config::DOMAIN); } $row[$row_key] = $column_name; } } $csv .= implode(',', $row) . "\r\n"; } $csv = mb_convert_encoding($csv, 'sjis-win', get_option('blog_charset')); $file_name = 'mw_wp_form_' . date('YmdHis') . '.csv'; header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . $file_name); echo $csv; exit; } }
/** * 引数 post_id が無効の場合、投稿情報を取得するために preg_replace_callback から呼び出される。 * * @param array $matches * @return string|null */ protected function get_post_property_from_this($matches) { global $post; if (!is_singular()) { return; } if (isset($post->ID) && MWF_Functions::is_numeric($post->ID)) { return $this->get_post_property($post, $matches[1]); } }
/** * http:// からはじまるURLに変換する * * @param string URL * @return string URL */ protected function parse_url($url) { if (empty($url)) { return ''; } $query_string = array(); preg_match('/\\?(.*)$/', $url, $reg); if (!empty($reg[1])) { $url = str_replace('?' . $reg[1], '', $url); parse_str($reg[1], $query_string); } if (!preg_match('/^https?:\\/\\//', $url)) { $home_url = home_url(); $url = $home_url . $url; } $url = preg_replace('/([^:])\\/+/', '$1/', $url); // URL設定でURL引数が使用されている場合はそれを使う。 // 「URL引数を有効にする」が有効の場合は $_GET を利用する(重複するURL引数はURL設定のものが優先される ※post_id除く) if ($this->querystring) { $query_string = array_merge($_GET, $query_string); if (isset($_GET['post_id']) && MWF_Functions::is_numeric($_GET['post_id'])) { $query_string['post_id'] = $_GET['post_id']; } } if (!empty($query_string)) { $url = $url . '?' . http_build_query($query_string, null, '&'); } return $url; }
/** * save_post * @param int $post_id */ public function save_post($post_id) { if (!(isset($_POST['post_type']) && $_POST['post_type'] === MWF_Config::NAME)) { return $post_id; } if (!isset($_POST[MWF_Config::NAME . '_nonce'])) { return $post_id; } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } if (!wp_verify_nonce($_POST[MWF_Config::NAME . '_nonce'], MWF_Config::NAME)) { return $post_id; } if (!current_user_can(MWF_Config::CAPABILITY)) { return $post_id; } $data = $_POST[MWF_Config::NAME]; $triminglists = array('mail_from', 'mail_to', 'mail_cc', 'mail_bcc', 'admin_mail_from'); foreach ($triminglists as $name) { $data[$name] = trim(mb_convert_kana($data[$name], 's', get_option('blog_charset'))); } if (!empty($data['validation']) && is_array($data['validation'])) { $validation = array(); foreach ($data['validation'] as $_validation) { if (empty($_validation['target'])) { continue; } foreach ($_validation as $key => $value) { // between min, max if ($key == 'between') { if (!MWF_Functions::is_numeric($value['min'])) { unset($_validation[$key]['min']); } if (!MWF_Functions::is_numeric($value['max'])) { unset($_validation[$key]['max']); } } elseif ($key == 'minlength' && !MWF_Functions::is_numeric($value['min'])) { unset($_validation[$key]); } elseif ($key == 'fileType' && isset($value['types']) && !preg_match('/^[0-9A-Za-z,]+$/', $value['types'])) { unset($_validation[$key]); } elseif ($key == 'fileSize' && !MWF_Functions::is_numeric($value['bytes'])) { unset($_validation[$key]); } // 要素が空のときは削除 // 単一項目のとき if (empty($value)) { unset($_validation[$key]); } elseif (is_array($value) && !array_diff($value, array(''))) { unset($_validation[$key]); } } $validation[] = $_validation; } $data['validation'] = $validation; } // チェックボックスの項目は、未設定のときはデータが来ないのでここで処理する if (empty($data['querystring'])) { $data['querystring'] = false; } if (empty($data['usedb'])) { $data['usedb'] = false; } if (empty($data['scroll'])) { $data['scroll'] = false; } $Setting = new MW_WP_Form_Setting($post_id); $Setting->sets($data); if (isset($_POST[MWF_Config::TRACKINGNUMBER])) { $tracking_number = $_POST[MWF_Config::TRACKINGNUMBER]; $Setting->update_tracking_number($tracking_number); } $Setting->save(); }
/** * CSVで出力するページ番号を取得 * * @return int */ public function get_paged() { $posts_per_page = $this->get_posts_per_page(); $paged = 1; if (isset($_GET['paged'])) { $_paged = $_GET['paged']; if (MWF_Functions::is_numeric($_paged) && $posts_per_page > 0) { $paged = $_paged; } } return $paged; }
/** * get_post_property_from_this * 引数 post_id が無効の場合、投稿情報を取得するために preg_replace_callback から呼び出される。 * @param array $matches * @return string */ public function get_post_property_from_this($matches) { global $post; if (!is_singular()) { return; } $post_id = get_the_ID(); if (isset($post->ID) && MWF_Functions::is_numeric($post->ID)) { if (isset($post->{$matches}[1])) { return $post->{$matches}[1]; } else { // post_meta の処理 $pm = get_post_meta($post->ID, $matches[1], true); if (!empty($pm)) { return $pm; } } } return; }
/** * 問い合わせ番号を更新 * * @param null|int $count 指定があればそれに更新 */ public function update_tracking_number($count = null) { $new_tracking_number = null; if (is_null($count)) { $tracking_number = $this->get_tracking_number(); $new_tracking_number = $tracking_number + 1; } elseif (MWF_Functions::is_numeric($count)) { $new_tracking_number = $count; } if (!is_null($new_tracking_number)) { update_post_meta($this->post_id, MWF_Config::TRACKINGNUMBER, $new_tracking_number); } }
/** * save_post * @param int $post_ID */ public function save_post($post_ID) { if (!(isset($_POST['post_type']) && $_POST['post_type'] === MWF_Config::NAME)) { return $post_ID; } if (!isset($_POST[MWF_Config::NAME . '_nonce'])) { return $post_ID; } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_ID; } if (!wp_verify_nonce($_POST[MWF_Config::NAME . '_nonce'], MWF_Config::NAME)) { return $post_ID; } if (!current_user_can(MWF_Config::CAPABILITY)) { return $post_ID; } $data = $_POST[MWF_Config::NAME]; if (!empty($data['validation']) && is_array($data['validation'])) { $validation = array(); foreach ($data['validation'] as $_validation) { if (empty($_validation['target'])) { continue; } foreach ($_validation as $key => $value) { // between min, max if ($key == 'between') { if (!MWF_Functions::is_numeric($value['min'])) { unset($_validation[$key]['min']); } if (!MWF_Functions::is_numeric($value['max'])) { unset($_validation[$key]['max']); } } elseif ($key == 'minlength' && !MWF_Functions::is_numeric($value['min'])) { unset($_validation[$key]); } elseif ($key == 'fileType' && isset($value['types']) && !preg_match('/^[0-9A-Za-z,]+$/', $value['types'])) { unset($_validation[$key]); } elseif ($key == 'fileSize' && !MWF_Functions::is_numeric($value['bytes'])) { unset($_validation[$key]); } // 要素が空のときは削除 // 単一項目のとき if (empty($value)) { unset($_validation[$key]); } elseif (is_array($value) && !array_diff($value, array(''))) { unset($_validation[$key]); } } $validation[] = $_validation; } $data['validation'] = $validation; } //$old_data = get_post_meta( $post_ID, MWF_Config::NAME, true ); update_post_meta($post_ID, MWF_Config::NAME, $data, $this->postdata); }