/** * バリデーションチェック * * @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']; } } } }
/** * アンインストールのテスト */ public function test_uninstall() { $post_ids = $this->factory->post->create_many(5, array('post_type' => MWF_Config::NAME)); foreach ($post_ids as $post_id) { update_option(MWF_Config::NAME . '-chart-' . $post_id, 1); $data_post_ids = $this->factory->post->create_many(5, array('post_type' => MWF_Functions::get_contact_data_post_type_from_form_id($post_id))); } $MW_WP_Form_File = new MW_WP_Form_File(); $temp_dir = $MW_WP_Form_File->get_temp_dir(); $temp_dir = $temp_dir['dir']; system("sudo chmod 777 " . WP_CONTENT_DIR . '/uploads'); $MW_WP_Form_File->create_temp_dir(); $this->assertEquals(true, file_exists($temp_dir)); update_option(MWF_Config::NAME, 1); MW_WP_Form::uninstall(); $posts = get_posts(array('post_type' => MWF_Config::NAME, 'posts_per_page' => -1)); $this->assertEquals(0, count($posts)); foreach ($post_ids as $post_id) { $option = get_option(MWF_Config::NAME . '-chart-' . $post_id); $this->assertEquals(null, $option); $data_posts = get_posts(array('post_type' => MWF_Functions::get_contact_data_post_type_from_form_id($post_id), 'posts_per_page' => -1)); $this->assertEquals(0, count($data_posts)); } $this->assertEquals(false, file_exists($temp_dir)); $option = get_option(MWF_Config::NAME); $this->assertEquals(null, $option); }
/** * setUp */ public function setUp() { parent::setUp(); $post_id = $this->factory->post->create(array('post_type' => MWF_Config::NAME)); $form_key = MWF_Functions::get_form_key_from_form_id($post_id); $this->Data = MW_WP_Form_Data::getInstance($form_key); }
/** * バリデーションチェック * * @param string $key name属性 * @param array $option * @return string エラーメッセージ */ public function rule($key, array $options = array()) { $value = $this->Data->get($key); if (!is_null($value) && MWF_Functions::is_empty($value)) { $defaults = array('message' => __('Please enter.', MWF_Config::DOMAIN)); $options = array_merge($defaults, $options); return $options['message']; } }
/** * setUp */ public function setUp() { parent::setUp(); $post_id = $this->factory->post->create(array('post_type' => MWF_Config::NAME)); $Setting = new MW_WP_Form_Setting($post_id); $Setting->set('usedb', 1); $post_ids = $this->factory->post->create_many(50, array('post_type' => MWF_Functions::get_contact_data_post_type_from_form_id($Setting->get('post_id')))); $this->post_type = get_post_type($post_ids[0]); $this->CSV = new MW_WP_Form_CSV($this->post_type); }
/** * rule * @param string $key name属性 * @param array $option * @return string エラーメッセージ */ public function rule($key, array $options = array()) { $value = $this->Data->get($key); if (!is_null($value) && !MWF_Functions::is_empty($value)) { if (!preg_match('/^[^@]+@([^@^\\.]+\\.)+[^@^\\.]+$/', $value)) { $defaults = array('message' => __('This is not the format of a mail address.', MWF_Config::DOMAIN)); $options = array_merge($defaults, $options); return $options['message']; } } }
/** * バリデーションチェック * * @param string $key name属性 * @param array $option * @return string エラーメッセージ */ public function rule($key, array $options = array()) { $value = $this->Data->get($key); if (!MWF_Functions::is_empty($value)) { if (!preg_match('/^[A-Za-z]+$/', $value)) { $defaults = array('message' => __('Please enter with a half-width alphabetic character.', MWF_Config::DOMAIN)); $options = array_merge($defaults, $options); return $options['message']; } } }
/** * バリデーションチェック * * @param string $key name属性 * @param array $option * @return string エラーメッセージ */ public function rule($key, array $options = array()) { $value = $this->Data->get($key); if (!MWF_Functions::is_empty($value)) { $defaults = array('message' => __('This is not the format of a zip code.', MWF_Config::DOMAIN)); $options = array_merge($defaults, $options); if (!preg_match('/^\\d{3}-\\d{4}$/', $value)) { return $options['message']; } } }
/** * rule * @param string $key name属性 * @param array $option * @return string エラーメッセージ */ public function rule($key, array $options = array()) { $value = $this->Data->get($key); if (!is_null($value) && !MWF_Functions::is_empty($value)) { if (!preg_match('/^[ぁ-ゞ ]*?[ぁ-ゞ]+?[ぁ-ゞ ]*?$/u', $value)) { $defaults = array('message' => __('Please enter with a Japanese Hiragana.', MWF_Config::DOMAIN)); $options = array_merge($defaults, $options); return $options['message']; } } }
/** * rule * @param string $key name属性 * @param array $option * @return string エラーメッセージ */ public function rule($key, array $options = array()) { $value = $this->Data->get($key); if (!is_null($value) && !MWF_Functions::is_empty($value)) { $defaults = array('options' => array(), 'message' => __('This value is invalid.', MWF_Config::DOMAIN)); $options = array_merge($defaults, $options); if (!(is_array($options['options']) && in_array($value, $options['options']))) { return $options['message']; } } }
/** * バリデーションチェック * * @param string $key name属性 * @param array $option * @return string エラーメッセージ */ public function rule($key, array $options = array()) { $value = $this->Data->get($key); if (!MWF_Functions::is_empty($value)) { if (!preg_match('/^https{0,1}:\\/\\/[^\\/]+\\.[^\\.]+/', $value)) { $defaults = array('message' => __('This is not the format of a url.', MWF_Config::DOMAIN)); $options = array_merge($defaults, $options); return $options['message']; } } }
/** * バリデーションチェック * * @param string $key name属性 * @param array $option * @return string エラーメッセージ */ public function rule($key, array $options = array()) { $value = $this->Data->get($key); if (!MWF_Functions::is_empty($value)) { $defaults = array('target' => null, 'message' => __('This is not in agreement.', MWF_Config::DOMAIN)); $options = array_merge($defaults, $options); $target_value = $this->Data->get($options['target']); if ((string) $value !== (string) $target_value) { return $options['message']; } } }
/** * confirm_page * 確認ページでのフォーム項目を返す * @return string HTML */ protected function confirm_page() { $value = $this->Data->get_raw($this->atts['name']); if ($value) { $filepath = MWF_Functions::fileurl_to_path($value); if (file_exists($filepath)) { return sprintf('<div class="%s_image"> <img src="%s" alt="" /> %s </div>', esc_attr(MWF_Config::NAME), esc_attr($value), $this->Form->hidden($this->atts['name'], $value)); } } }
/** * confirmPage * 確認ページでのフォーム項目を返す * @return string HTML */ protected function confirmPage() { $value = $this->Form->getValue($this->atts['name']); if ($value) { $filepath = MWF_Functions::fileurl_to_path($value); if (file_exists($filepath)) { $_ret = '<div class="' . MWF_Config::NAME . '_file">'; $_ret .= '<a href="' . esc_attr($value) . '" target="_blank">' . __('Uploaded.', MWF_Config::DOMAIN) . '</a>'; $_ret .= '</div>'; $_ret .= $this->Form->hidden($this->atts['name'], $value); return $_ret; } } }
/** * confirmPage * 確認ページでのフォーム項目を返す * @return string HTML */ protected function confirmPage() { $value = $this->Form->getValue($this->atts['name']); if ($value) { $filepath = MWF_Functions::fileurl_to_path($value); if (file_exists($filepath)) { $_ret = '<div class="' . MWF_Config::NAME . '_image">'; $_ret .= '<img src="' . esc_attr($value) . '" alt="" />'; $_ret .= $this->Form->hidden($this->atts['name'], $value); $_ret .= '</div>'; return $_ret; } } }
/** * ファイルアップロードの実処理 * * @param arary $file $_FILES['hoge'] の配列 * @return string アップロードしたファイルの URL */ protected function _file_upload($file) { if (empty($file['tmp_name'])) { return; } $is_uploaded = false; if (MWF_Functions::check_file_type($file['tmp_name'], $file['name']) && $file['error'] == UPLOAD_ERR_OK && is_uploaded_file($file['tmp_name'])) { $extension = pathinfo($file['name'], PATHINFO_EXTENSION); $uploadfile = $this->set_upload_file_name($extension); $is_uploaded = move_uploaded_file($file['tmp_name'], $uploadfile['file']); if ($is_uploaded) { return $uploadfile['url']; } } }
/** * rule * @param string $key name属性 * @param array $option * @return string エラーメッセージ */ public function rule($key, array $options = array()) { $value = $this->Data->get($key); if (!is_null($value) && !MWF_Functions::is_empty($value)) { $defaults = array('message' => __('This is not the format of a date.', MWF_Config::DOMAIN)); $options = array_merge($defaults, $options); $timestamp = strtotime($value); $year = date('Y', $timestamp); $month = date('m', $timestamp); $day = date('d', $timestamp); $checkdate = checkdate($month, $day, $year); if (!$timestamp || !$checkdate || preg_match('/^[a-zA-Z]$/', $value) || preg_match('/^\\s+$/', $value)) { return $options['message']; } } }
/** * rule * @param string $key name属性 * @param array $option * @return string エラーメッセージ */ public function rule($key, array $options = array()) { $value = $this->Data->get($key); if (!is_null($value) && !MWF_Functions::is_empty($value)) { $defaults = array('types' => '', 'message' => __('This file is invalid.', MWF_Config::DOMAIN)); $options = array_merge($defaults, $options); $_types = explode(',', $options['types']); foreach ($_types as $type) { $types[] = preg_quote(trim($type), '/'); } $types = implode('|', MWF_Functions::array_clean($types)); $pattern = '/\\.(' . $types . ')$/i'; if (!preg_match($pattern, $value)) { return $options['message']; } } }
/** * バリデーションチェック * * @param string $key name属性 * @param array $option * @return string エラーメッセージ */ public function rule($key, array $options = array()) { $value = $this->Data->get($key); $value = (string) $value; if (!MWF_Functions::is_empty($value)) { $defaults = array('options' => array(), 'message' => __('This value is invalid.', MWF_Config::DOMAIN)); $options = array_merge($defaults, $options); if (is_array($options['options'])) { foreach ($options['options'] as $option) { $option = (string) $option; if ($value === $option) { return; } } } return $options['message']; } }
/** * setUp */ public function setUp() { parent::setUp(); // 問い合わせデータをデータベースに保存する設定のフォームを作成 $post_ids = $this->factory->post->create_many(5, array('post_type' => MWF_Config::NAME)); foreach ($post_ids as $post_id) { $meta = array('usedb' => 1); update_post_meta($post_id, MWF_Config::NAME, $meta); } // 問い合わせデータを保存(最初のフォームに1件だけ) foreach ($post_ids as $post_id) { $post_id = $this->factory->post->create(array('post_type' => MWF_Functions::get_contact_data_post_type_from_form_id($post_id))); update_post_meta($post_id, MWF_config::CONTACT_DATA_NAME, array('test-1' => 'aaa', 'test-2' => 'bbb')); $Setting = new MW_WP_Form_Contact_Data_Setting($post_id); $this->settings[] = $Setting; $this->post_id = $post_id; break; } }
/** * 送信されたデータをもとに添付ファイル用の配列を生成して返す * * @return array $attachments pathの配列 */ protected function get_attachments() { $attachments = array(); $upload_file_keys = $this->Data->get_post_value_by_key(MWF_Config::UPLOAD_FILE_KEYS); if ($upload_file_keys !== null && is_array($upload_file_keys)) { $wp_upload_dir = wp_upload_dir(); foreach ($upload_file_keys as $key) { $upload_file_url = $this->Data->get_post_value_by_key($key); if (!$upload_file_url) { continue; } $filepath = MWF_Functions::fileurl_to_path($upload_file_url); if (file_exists($filepath)) { $filepath = MWF_Functions::move_temp_file_to_upload_dir($filepath); $new_upload_file_url = MWF_Functions::filepath_to_url($filepath); $this->Data->set($key, $new_upload_file_url); $attachments[$key] = $filepath; } } } return $attachments; }
/** * 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; } }
/** * id属性を返す * * @param string $id * @param string $suffix * @return string */ protected function get_attr_id($id, $suffix = '') { if (!MWF_Functions::is_empty($id)) { if ($suffix) { $id .= '-' . $suffix; } return $id; } }
/** * 引数 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]); } }
/** * @group get_parsed_mail_object * @backupStaticAttributes enabled */ public function test_get_parsed_mail_object_Nullでもデータベースに保存_ただし添付の場合は保存しない() { $MW_WP_Form_File = new MW_WP_Form_File(); $temp_dir = $MW_WP_Form_File->get_temp_dir(); $temp_dir = $temp_dir['dir']; system("sudo chmod 777 " . WP_CONTENT_DIR . '/uploads'); $MW_WP_Form_File->create_temp_dir(); file_put_contents($temp_dir . '/attachment_1.txt', 'hoge'); file_put_contents($temp_dir . '/attachment_2.txt', 'fuga'); $this->Data->set('attachment_1', null); $this->Mail->body = '{attachment_1}'; $this->Mail->attachments = array('attachment_1' => $temp_dir . '/attachment_1.txt', 'attachment_2' => $temp_dir . '/attachment_2.txt'); $Mail_Parser = new MW_WP_Form_Mail_Parser($this->Mail, $this->Setting); $Mail_Parser->get_parsed_mail_object(true); $posts = get_posts(array('post_type' => MWF_Functions::get_contact_data_post_type_from_form_id($this->Setting->get('post_id')))); foreach ($posts as $post) { $post_metas = get_post_meta($post->ID); $this->assertFalse(isset($post_metas['attachment_1'])); break; } }
/** * アンインストールした時の処理 */ public static function uninstall() { $plugin_dir_path = plugin_dir_path(__FILE__); include_once $plugin_dir_path . 'classes/models/class.admin.php'; $Admin = new MW_WP_Form_Admin(); $forms = $Admin->get_forms(); $data_post_ids = array(); foreach ($forms as $form) { $data_post_ids[] = $form->ID; wp_delete_post($form->ID, true); } foreach ($data_post_ids as $data_post_id) { delete_option(MWF_Config::NAME . '-chart-' . $data_post_id); $data_posts = get_posts(array('post_type' => MWF_Functions::get_contact_data_post_type_from_form_id($data_post_id), 'posts_per_page' => -1)); if (empty($data_posts)) { continue; } foreach ($data_posts as $data_post) { wp_delete_post($data_post->ID, true); } } include_once plugin_dir_path(__FILE__) . 'classes/models/class.file.php'; $File = new MW_WP_Form_File(); $File->remove_temp_dir(); delete_option(MWF_Config::NAME); }
/** * 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; }
/** * CSVの各行を生成 * * @param array $posts * @param array $headings * @return array */ protected function get_rows(array $posts, array $headings) { global $post; $rows = array(); foreach ($posts as $post) { setup_postdata($post); $columns = array(); foreach ($headings as $key => $value) { $Contact_Data_Setting = new MW_WP_Form_Contact_Data_Setting($post->ID); $response_statuses = $Contact_Data_Setting->get_response_statuses(); $column = ''; if ($value === __('Response Status', MWF_Config::DOMAIN)) { $response_status = $Contact_Data_Setting->get('response_status'); $column = $response_statuses[$response_status]; } elseif ($value === __('Memo', MWF_Config::DOMAIN)) { $column = $Contact_Data_Setting->get('memo'); } elseif ($value === MWF_Functions::get_tracking_number_title($this->post_type)) { $column = get_post_meta(get_the_ID(), MWF_Config::TRACKINGNUMBER, true); } elseif (isset($post->{$value})) { $post_meta = $post->{$value}; if ($Contact_Data_Setting->is_upload_file_key($post, $value)) { // 過去バージョンでの不具合でメタデータが空になっていることがあるのでその場合は代替処理 if ($post_meta === '') { $post_meta = MWF_Functions::get_multimedia_id__fallback($post, $value); } $column = wp_get_attachment_url($post_meta); } else { $column = $post_meta ? $post_meta : ''; } } $columns[$key] = $column; } $rows[] = $columns; } wp_reset_postdata(); return $rows; }
/** * アップロードに失敗、もしくはファイルが削除されている key を UPLOAD_FILE_KEYS から削除 */ public function set_upload_file_keys() { $upload_file_keys = $this->get_post_value_by_key(MWF_Config::UPLOAD_FILE_KEYS); if (!$upload_file_keys) { $upload_file_keys = array(); } $wp_upload_dir = wp_upload_dir(); foreach ($upload_file_keys as $key => $upload_file_key) { $upload_file_url = $this->get_post_value_by_key($upload_file_key); if ($upload_file_url) { $filepath = MWF_Functions::fileurl_to_path($upload_file_url); if (!file_exists($filepath)) { unset($upload_file_keys[$key]); } } } $this->set(MWF_Config::UPLOAD_FILE_KEYS, $upload_file_keys); }
/** * 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(); }