public static function getInstance($key) { if (is_null(self::$Instance)) { self::$Instance = new self($key); } return self::$Instance; }
/** * setUp */ public function setUp() { parent::setUp(); $this->Field = new MW_WP_Form_Field_Checkbox(); $this->form_key = MWF_Config::NAME . '-1'; $this->Data = MW_WP_Form_Data::getInstance($this->form_key); }
/** * __construct * @param string $key 識別子 */ public function __construct($key) { if (!$this->getName()) { exit('MW_Validation_Rule::$name must override.'); } $this->Data = MW_WP_Form_Data::getInstance($key); }
/** * setUp */ public function setUp() { parent::setUp(); $form_key = MWF_Config::NAME . '-1'; $this->Data = MW_WP_Form_Data::getInstance($form_key); $this->Rule = new MW_WP_Form_Validation_Rule_Between(); $this->Rule->set_Data($this->Data); }
/** * __construct * 取得データを保存 * @param string $key 識別子 */ public function __construct($key = '') { $this->Data = MW_WP_Form_Data::getInstance($key); if ($key) { $this->key = $key . '_key'; } $this->modeCheck = $this->modeCheck(); }
/** * @group push_uploaded_file_keys */ public function test_push_uploaded_file_keys() { $this->Data->set(MWF_Config::UPLOAD_FILE_KEYS, array('file1')); $this->Data->push_uploaded_file_keys(array('file1' => 'http://exemple.com/dummy.txt')); $this->assertSame(array('file1'), $this->Data->get_post_value_by_key(MWF_Config::UPLOAD_FILE_KEYS)); $this->Data->set(MWF_Config::UPLOAD_FILE_KEYS, array('file1')); $this->Data->push_uploaded_file_keys(array('file2' => 'http://exemple.com/dummy.txt')); $this->assertSame(array('file1', 'file2'), $this->Data->get_post_value_by_key(MWF_Config::UPLOAD_FILE_KEYS)); }
/** * トークンチェック * * @return bool */ protected function token_check() { if (isset($_POST[$this->token_name])) { $request_token = $_POST[$this->token_name]; } $values = $this->Data->gets(); $form_key = $this->ExecShortcode->get('key'); if (isset($request_token) && wp_verify_nonce($request_token, $form_key)) { return true; } elseif (empty($_POST) && $values) { $this->complete_twice = true; return true; } return false; }
/** * @backupStaticAttributes enabled */ public function test_管理者宛メール関連フックのテスト_送信内容に応じてメール設定を書き換える() { $self = $this; add_filter('mwform_admin_mail_raw_' . $this->form_key, function ($Mail, $values) use($self) { $Mail->from = '{メールアドレス}'; return $Mail; }, 10, 2); add_filter('mwform_admin_mail_' . $this->form_key, function ($Mail, $values) use($self) { $self->assertEquals($Mail->from, '*****@*****.**'); return $Mail; }, 10, 2); $this->Data->set('メールアドレス', '*****@*****.**'); $Mail_Service = new MW_WP_Form_Mail_Service($this->Mail, $this->form_key, $this->Setting); $Mail_Service->send_admin_mail(); $Mail_Service->send_reply_mail(); }
/** * getInstance * * @param null|string $form_key フォーム識別子 * @param null|array $POST $_POSTを想定 * @param null|array $FILES $_FILESを想定 */ public static function getInstance($form_key = null, $POST = null, $FILES = null) { if (is_null($POST) || !is_array($POST)) { $POST = array(); } if (is_null($FILES) || !is_array($FILES)) { $FILES = array(); } if (is_null($form_key) && !is_null(self::$Instance)) { return self::$Instance; } if (!is_null($form_key)) { self::$Instance = new self($form_key, $POST, $FILES); return self::$Instance; } exit('MW_WP_Form_Data instantiation error.'); }
/** * @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; } }
/** * $this->_parse_mail_content(), $this->_save_mail_content の本体 * 第2引数でDB保存するか判定 * * @param array $matches * @param bool $do_update * @return string $value */ protected function parse($matches, $do_update = false) { $match = $matches[1]; $form_id = $this->Setting->get('post_id'); // MWF_Config::TRACKINGNUMBER のときはお問い合せ番号を参照する if ($match === MWF_Config::TRACKINGNUMBER) { if ($form_id) { $value = $this->Setting->get_tracking_number($form_id); } } else { $form_key = MWF_Functions::get_form_key_from_form_id($form_id); $value = $this->Data->get($match); $value = $this->apply_filters_mwform_custom_mail_tag($form_key, $value, $match); } // 値が null でも保存(チェッボックス未チェックで直送信でも保存させるため) // ただし、画像の場合はURLが保存されないように調整がはいるため除外が必要 if ($do_update) { if (!array_key_exists($match, $this->Mail->attachments)) { update_post_meta($this->insert_contact_data_id, $match, $value); } } return $value; }
/** * set_rules * * @param MW_WP_Form_Setting $Setting */ public function set_rules(MW_WP_Form_Setting $Setting) { $Data = MW_WP_Form_Data::getInstance(); $rules = array(); $validations = $Setting->get('validation'); if ($validations) { foreach ($validations as $validation) { foreach ($validation as $rule => $options) { if ($rule == 'target') { continue; } if (!is_array($options)) { $options = array(); } $this->set_rule($validation['target'], $rule, $options); } } } $Akismet = new MW_WP_Form_Akismet(); $akismet_check = $Akismet->check($Setting->get('akismet_author'), $Setting->get('akismet_author_email'), $Setting->get('akismet_author_url'), $Data); if ($akismet_check) { $this->set_rule(MWF_Config::AKISMET, 'akismet_check'); } }
/** * メールを送信内容に置換 * * @param MW_WP_Form_Setting $Setting * @param bool $do_update */ public function parse($Setting, $do_update = false) { $Data = MW_WP_Form_Data::getInstance(); $Mail_Parser = new MW_WP_Form_Mail_Parser($this, $Setting); $Mail = $Mail_Parser->get_parsed_mail_object($do_update); foreach (get_object_vars($Mail) as $key => $value) { $this->{$key} = $value; } }
/** * フォームを表示するためのショートコードを登録 * * @param string $view_flg * @param MW_WP_Form_Setting $Setting * @param MW_WP_Form_Form $Form */ public function add_shortcode($view_flg, MW_WP_Form_Setting $Setting, MW_WP_Form_Form $Form) { $this->view_flg = $view_flg; $this->Setting = $Setting; $this->Form = $Form; $this->Data = MW_WP_Form_Data::getInstance(); add_shortcode('mwform_formkey', array($this, 'mwform_formkey')); add_shortcode('mwform', array($this, 'mwform')); add_shortcode('mwform_complete_message', array($this, 'mwform_complete_message')); }
/** * add_shortcode した後の ExecShortcode を返す * * @param string $view_flg * @return MW_WP_Form_Exec_Shortcode */ protected function get_ExecShortcode_after_add_shortcode($view_flg) { $post = $this->generate_page_has_mwform_formkey($this->Setting); $ExecShortcode = new MW_WP_Form_Exec_Shortcode($post, ''); $Form = new MW_WP_Form_Form(); $Data = MW_WP_Form_Data::getInstance($ExecShortcode->get('key')); $ExecShortcode->add_shortcode($view_flg, $this->Setting, $Form); $attributes = array('key' => $this->Setting->get('post_id')); $ExecShortcode->set_settings_by_mwform_formkey($attributes); return $ExecShortcode; }
/** * add_shortcode * フォーム項目を返す * @param MW_WP_Form_Form $Form * @param string $view_flg * @param MW_WP_Form_Error $Error * @param string $form_key */ public function add_shortcode(MW_WP_Form_Form $Form, $view_flg, MW_WP_Form_Error $Error, $form_key) { if (!empty($this->shortcode_name)) { $this->Form = $Form; $this->Error = $Error; $this->form_key = $form_key; $this->Data = MW_WP_Form_Data::getInstance(); switch ($view_flg) { case 'input': add_shortcode($this->shortcode_name, array($this, '_input_page')); break; case 'confirm': add_shortcode($this->shortcode_name, array($this, '_confirm_page')); break; case 'complete': break; default: exit('$view_flg is not right value. $view_flg is ' . $view_flg . ' now.'); } } }
/** * check * @param string $akismet_author * @param string $akismet_author_email * @param string $akismet_author_url * @param MW_WP_Form_Data $Data * @return bool */ public function check($akismet_author, $akismet_author_email, $akismet_author_url, $Data) { global $akismet_api_host, $akismet_api_port; if (!$this->is_enable()) { return false; } $doAkismet = false; $author = ''; if ($Data->get_post_value_by_key($akismet_author)) { $author = $Data->get_post_value_by_key($akismet_author); $doAkismet = true; } $author_email = ''; if ($Data->get_post_value_by_key($akismet_author_email)) { $author_email = $Data->get_post_value_by_key($akismet_author_email); $doAkismet = true; } $author_url = ''; if ($Data->get_post_value_by_key($akismet_author_url)) { $author_url = $Data->get_post_value_by_key($akismet_author_url); $doAkismet = true; } if ($doAkismet) { $content = ''; foreach ($Data->gets() as $key => $value) { $value = $Data->get($key); $content .= $value . "\n\n"; } $permalink = get_permalink(); $akismet = array(); $akismet['blog'] = home_url(); $akismet['blog_lang'] = get_locale(); $akismet['blog_charset'] = get_option('blog_charset'); $akismet['user_ip'] = preg_replace('/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR']); $akismet['user_agent'] = $_SERVER['HTTP_USER_AGENT']; $akismet['referrer'] = $_SERVER['HTTP_REFERER']; $akismet['comment_type'] = MWF_Config::NAME; if ($permalink) { $akismet['permalink'] = $permalink; } if ($author) { $akismet['comment_author'] = $author; } if ($author_email) { $akismet['comment_author_email'] = $author_email; } if ($author_url) { $akismet['comment_author_url'] = $author_url; } if ($content) { $akismet['comment_content'] = $content; } foreach ($_SERVER as $key => $value) { if (!in_array($key, array('HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW'))) { $akismet[$key] = $value; } } $query_string = http_build_query($akismet, null, '&'); if (is_callable(array('Akismet', 'http_post'))) { $response = Akismet::http_post($query_string, 'comment-check'); } else { $response = akismet_http_post($query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port); } $response = apply_filters('mwform_akismet_responce', $response); return $response[1] == 'true' ? true : false; } }
/** * main * 表示画面でのプラグインの処理等。 * @param string $template * @return string $template */ public function main($template) { global $post; // URL設定を取得 add_shortcode('mwform', array($this, '_meta_mwform')); // formkeyでのフォーム生成の場合はそれをもとに設定を取得 add_shortcode('mwform_formkey', array($this, '_meta_mwform_formkey')); if (is_singular() && !empty($post->ID)) { $shortcode = $this->get_shortcode($post->post_content); } if (empty($shortcode) && !(defined('MWFORM_NOT_USE_TEMPLATE') && MWFORM_NOT_USE_TEMPLATE === true)) { $template_data = @file_get_contents($template); $shortcode = $this->get_shortcode($template_data); } if (is_array($shortcode) && !empty($shortcode[0])) { do_shortcode($shortcode[0]); } remove_shortcode('mwform'); remove_shortcode('mwform_formkey'); // フォームが定義されていない場合は終了 if (is_null($this->key) || is_null($this->input) || is_null($this->confirm) || is_null($this->complete) || is_null($this->validation_error)) { return $template; } nocache_headers(); // セッション初期化 $this->Data = MW_WP_Form_Data::getInstance($this->key); // $_POSTがあるときは$_POST if (!empty($_POST)) { $this->Data->setValues(stripslashes_deep($_POST)); } // $_FILESがあるときは$this->dataに統合 $files = array(); foreach ($_FILES as $key => $file) { if (!isset($_POST[$key]) || !empty($file['name'])) { if ($file['error'] == UPLOAD_ERR_OK && is_uploaded_file($file['tmp_name'])) { $this->Data->setValue($key, $file['name']); } else { $this->Data->setValue($key, ''); } if (!empty($file['name'])) { $files[$key] = $file; } } } // この条件判定がないと fileSize チェックが正しく動作しない if ($files) { $this->Data->setValue(MWF_Config::UPLOAD_FILES, $files); } // フォームオブジェクト生成 $this->Form = new MW_Form($this->key); // バリデーションオブジェクト生成 $this->Validation = new MW_Validation($this->key); foreach ($this->validation_rules as $validation_name => $instance) { if (is_callable(array($instance, 'rule'))) { $this->Validation->add_validation_rule($instance->getName(), $instance); } } // バリデーション実行(Validation->dataに値がないと$Errorは返さない(true)) $this->apply_filters_mwform_validation(); // ファイル操作オブジェクト生成 $this->File = new MW_WP_Form_File(); // 入力画面(戻る)のとき if ($this->Form->isBack()) { $this->redirect($this->input); } elseif ($this->Form->isConfirm()) { $this->fileUpload(); if ($this->Validation->check()) { $this->viewFlg = 'confirm'; $this->redirect($this->confirm); } else { if (!empty($this->validation_error)) { $this->redirect($this->validation_error); } else { $this->redirect($this->input); } } } elseif ($this->Form->isComplete()) { $this->fileUpload(); if ($this->Validation->check()) { $this->viewFlg = 'complete'; if ($this->Data->getValue($this->Form->getTokenName())) { $this->apply_filters_mwform_mail(); $this->Data->clearValue($this->Form->getTokenName()); // 手動フォーム対応 $REQUEST_URI = $this->parse_url($this->get_request_uri()); $input = $this->parse_url($this->input); $complete = $this->parse_url($this->complete); if (!$this->options_by_formkey && $REQUEST_URI !== $complete && $input !== $complete) { $this->Data->clearValues(); } } $this->redirect($this->complete); } else { if (!empty($this->validation_error)) { $this->redirect($this->validation_error); } else { $this->redirect($this->input); } } } else { // 完了 or 確認画面 or エラーURLが設定済みで // 完了 or 確認画面 or エラーに直接アクセスした場合、 // 入力画面に戻れれば戻る。戻れない場合はトップに戻す $REQUEST_URI = $this->parse_url($this->get_request_uri()); $check_urls = array($this->confirm, $this->complete); $back_url = $this->input ? $this->input : home_url(); foreach ($check_urls as $check_url) { if ($REQUEST_URI === $check_url) { $this->Data->clearValues(); $this->redirect($back_url); } } $this->redirect($this->input); if ($this->Validation->check() && $REQUEST_URI == $this->validation_error) { $this->Data->clearValues(); $this->redirect($back_url); } } // スクロール用スクリプトのロード if (!empty($this->options_by_formkey['scroll'])) { if ($this->Form->isConfirm() || $this->Form->isComplete() || !$this->Validation->check()) { add_action('wp_enqueue_scripts', array($this, 'scroll_script')); } } add_shortcode('mwform_formkey', array($this, '_mwform_formkey')); add_shortcode('mwform', array($this, '_mwform')); add_shortcode('mwform_complete_message', array($this, '_mwform_complete_message')); add_action('wp_footer', array($this->Data, 'clearValues')); add_action('wp_enqueue_scripts', array($this, 'original_style')); add_action('wp_enqueue_scripts', array($this, 'original_script')); return $template; }
/** * @group parse * @backupStaticAttributes enabled */ public function test_parse_データベースに保存() { $post_id = $this->factory->post->create(array('post_type' => MWF_Config::NAME)); $Setting = new MW_WP_Form_Setting($post_id); $Data = MW_WP_Form_Data::getInstance(MWF_Functions::get_form_key_from_form_id($post_id)); $Data->set('example', 'example'); $this->Mail->body = '{example}'; $this->Mail->parse($Setting, true); $posts = get_posts(array('post_type' => MWF_Functions::get_contact_data_post_type_from_form_id($post_id))); foreach ($posts as $post) { $this->assertEquals('example', get_post_meta($post->ID, 'example', true)); break; } }
/** * apply_filters_mwform_auto_mail * * @param MW_WP_Form_Mail $Mail * @return MW_WP_Form_Mail $Mail */ protected function apply_filters_mwform_auto_mail(MW_WP_Form_Mail $Mail) { return apply_filters('mwform_auto_mail_' . $this->form_key, $Mail, $this->Data->gets(), clone $this->Data); }
/** * 配列データを整形して返す ( チェックボックス等用 )。配列の場合はpost値を連結して返す * * @param string $key name属性 * @param array $children 選択肢 * @return string データ */ public function get_separated_raw_value($key, array $children = array()) { MWF_Functions::deprecated_message('MW_WP_Form_Form::get_separated_raw_value()', 'MW_WP_Form_Data::get_separated_raw_value()'); $Data = MW_WP_Form_Data::getInstance(); return $Data->get_separated_raw_value($key, $children); }
/** * singleCheck * 特定の項目のvalidate実行 * @param string $key * @return bool エラーがなければ true */ public function singleCheck($key) { $Data = MW_WP_Form_Data::getInstance($this->key); $rules = array(); if (is_array($this->validate) && isset($this->validate[$key])) { $rules = $this->validate[$key]; if ($this->_check($key, $rules)) { return false; } } return true; }