From cd140e3f8e90e27eb24946bf2269b216fd82906a Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Thu, 31 Dec 2015 14:32:04 -0500 Subject: [PATCH] Adding changes from the Staffords work. Mostly in getting the setting page up and running. Has setting for the notification email and message. Also has the thank you text given after the user fills out the online application form. --- controllers/admin.php | 246 +++++++++++++++++++++++++++++++++++++-- controllers/front.php | 91 +++++++++++++++ glm-employment.php | 4 + views/deniedAccess.php | 1 + views/front/thankYou.php | 3 +- views/optionsPage.php | 16 +++ views/text.php | 2 + views/textArea.php | 1 + 8 files changed, 351 insertions(+), 13 deletions(-) create mode 100644 views/deniedAccess.php create mode 100644 views/optionsPage.php create mode 100644 views/text.php create mode 100644 views/textArea.php diff --git a/controllers/admin.php b/controllers/admin.php index 3f70b22..e893882 100644 --- a/controllers/admin.php +++ b/controllers/admin.php @@ -29,6 +29,7 @@ class glm_employment_admin $this->wpdb = $wpdb; $jobs = new glm_employment_job($path); add_action('admin_menu', array($this, 'add_job_menus')); + add_action('admin_init', array($this, 'job_settings_init') ); } /** @@ -44,14 +45,234 @@ class glm_employment_admin 'applications', array($this, 'get_applications') ); -// add_submenu_page( -// 'edit.php?post_type=' . GLM_EMP_POST_TYPE, -// 'settings', -// 'Settings', -// 'manage_options', -// 'job_settings', -// array($this, 'show_job_settings') -// ); + add_submenu_page( + 'edit.php?post_type=' . GLM_EMP_POST_TYPE, + 'settings', + 'Settings', + GLM_EMP_NEW_CAPABILITY, + GLM_EMP_SETTINGS, + array($this, 'show_job_settings') + ); + } + + static public function activate_plugin() + { + staffords_employment_admin::add_capability(); + } + + static public function deactivate_plugin() + { + staffords_employment_admin::remove_capability(); + } + + static public function add_capability() + { + $roles = get_editable_roles(); + foreach ($GLOBALS['wp_roles']->role_objects as $key => $role) { + if (isset($roles[$key]) && $role->has_cap(GLM_EMP_BUILT_IN_CAPABILITY)) { + $role->add_cap(GLM_EMP_NEW_CAPABILITY); + } + } + } + + static public function remove_capability() + { + $roles = get_editable_roles(); + foreach ($GLOBALS['wp_roles']->role_objects as $key => $role) { + if (isset($roles[$key]) && $role->has_cap(GLM_EMP_NEW_CAPABILITY)) { + $role->remove_cap(GLM_EMP_NEW_CAPABILITY); + } + } + } + + public function staffords_settings_exist() + { + if (false == get_option(GLM_EMP_SETTINGS)) { + add_option(GLM_EMP_SETTINGS); + } + } + + public function job_settings_init() + { + add_settings_section( + 'staffordsjobs_setting_section_two', + 'Edit Confirmations', + array($this, 'staffords_settings_two_section_callback'), + 'staffordsjobs_settings' + ); + add_settings_field( + 'thank_you_text', + 'Thank You Message', + array($this, 'render_textarea_field'), + 'staffordsjobs_settings', + 'staffordsjobs_setting_section_two', + 'thank_you_text' + ); + add_settings_section( + 'staffordsjobs_setting_section', + 'Edit Notifications', + array($this, 'staffords_settings_section_callback'), + 'staffordsjobs_settings' + ); + $fieldNames = array( + array( + 'name' => 'notification_name', + 'label' => 'Online Form Notify: Name', + 'type' => 'text' + ), + array( + 'name' => 'notification_email', + 'label' => 'Online Form Notify: Email', + 'type' => 'text' + ), + array( + 'name' => 'from_name', + 'label' => 'From: Name', + 'type' => 'text' + ), + array( + 'name' => 'from_email', + 'label' => 'From: Email', + 'type' => 'text' + ), + array( + 'name' => 'replyto_name', + 'label' => 'Reply-To: Name', + 'type' => 'text' + ), + array( + 'name' => 'replyto_email', + 'label' => 'Reply-To: Email', + 'type' => 'text' + ), + array( + 'name' => 'notification_message', + 'label' => 'Notification Message', + 'type' => 'textarea' + ), + ); + + foreach ($fieldNames as $field) { + $this->staffords_add_setting_text_field( + $field['name'], + $field['label'], + $field['type'] + ); + } + register_setting(GLM_EMP_SETTINGS, GLM_EMP_SETTING_NAME, array($this, 'sanitize_options')); + } + + public function sanitize_options($input) + { + if ( !$valid_notification_email = $this->validate_email_string($input['notification_email']) ) { + add_settings_error( + GLM_EMP_SETTINGS, + 'setting-error-notify-email', + 'invalid email for Online Form Notify: Email', + 'error' + ); + } + if ( !$valid_reply_email = $this->validate_email_string($input['replyto_email']) ) { + add_settings_error( + GLM_EMP_SETTINGS, + 'setting-error-notify-email', + 'invalid email for Reply-To: Email', + 'error' + ); + } + $terms = get_terms( + GLM_EMP_TAX_DEPARTMENTS, + array( + 'hide_empty' => false + ) + ); + foreach ( $terms as $term ) { + if ( !$valid_email = $this->validate_email_string( $input[$term->slug . '_notification_email'] ) ) { + add_settings_error( + GLM_EMP_SETTINGS, + 'setting-error-notify-email', + 'invalid email for ' . $term->name . ': Email', + 'error' + ); + } + } + return $input; + } + + public function validate_email_string($string) + { + if ( isset( $string ) && strpos( $string, ',' ) ) { + $emails = explode( ',', $string ); + foreach ( $emails as $email ) { + $validEmail = filter_var(trim($email), FILTER_VALIDATE_EMAIL); + if ( !$validEmail ) { + return false; + } + } + } else if ( isset( $string ) && $string ) { + $validEmail = filter_var($string, FILTER_VALIDATE_EMAIL); + if ( !$validEmail ) { + return false; + } + } + return true; + } + + public function staffords_add_setting_text_field($name, $label, $type) + { + switch ($type) { + case 'text': + $callback = 'render_text_field'; + break; + case 'textarea': + $callback = 'render_textarea_field'; + break; + case 'state': + $callback = 'glmclientinfoRenderStateSelect'; + break; + default: + return false; + break; + } + add_settings_field( + $name, + $label, + array($this, $callback), + 'staffordsjobs_settings', + 'staffordsjobs_setting_section', + $name + ); + } + + public function render_text_field($fieldName) + { + static $options; + if (!$options) { + $options = get_option(GLM_EMP_SETTINGS); + } + include $this->pluginDirName . 'views/text.php'; + } + + public function render_textarea_field($fieldName) + { + static $options; + $options = get_option(GLM_EMP_SETTINGS); + include $this->pluginDirName . 'views/textArea.php'; + } + + public function staffords_settings_section_callback() + { + echo __('Employment Form Email Notification', 'wordpress'); + } + + public function staffords_settings_two_section_callback() + { + echo __('Employment Form Thank You', 'wordpress'); + } + + public function staffords_option_page_capability() + { + return GLM_EMP_NEW_CAPABILITY; } /** @@ -59,7 +280,11 @@ class glm_employment_admin */ public function show_job_settings() { - echo '

Job Settings

'; + if (current_user_can(GLM_EMP_NEW_CAPABILITY)) { + include $this->pluginDirName . 'views/optionsPage.php'; + } else { + include $this->pluginDirName . 'views/deniedAccess.php'; + } } /** @@ -95,8 +320,7 @@ class glm_employment_admin '1.0', true ); - wp_enqueue_style('foundation', - get_template_directory_uri() . '/css/app.js'); + wp_enqueue_style('foundation', get_template_directory_uri() . '/css/app.js'); $id = filter_var($_REQUEST['application'], FILTER_VALIDATE_INT); $sql = " SELECT * diff --git a/controllers/front.php b/controllers/front.php index 4a84ed8..12524f5 100644 --- a/controllers/front.php +++ b/controllers/front.php @@ -112,6 +112,95 @@ class glm_employment_front return $form; } + public function email_owner() + { + $properies_applied_for = $this->get_job_application_properties(); + $plugin_options = get_option(GLM_EMP_SETTING_NAME); + if ($jobId = filter_var($_REQUEST['job'], FILTER_VALIDATE_INT)) { + $jobData = get_post($jobId); + $this->jobTitle = $jobTitle = $jobData->post_title; + } + // get the notification emails for this plugin + $fromName = $plugin_options['from_name']; + $from = $plugin_options['from_email']; + $to = $plugin_options['notification_email']; + $name = $plugin_options['replyto_name']; + $email = $plugin_options['replyto_email']; + $message = $plugin_options['notification_message'] . "\n"; + $message .= '' . get_bloginfo('name') . ' Admin Area.' . "\n\n"; + $headers = array(); + $headers[] = "From: {$fromName} <{$from}>"; + $headers[] = "Reply-To: {$name} <{$email}>"; + foreach ( $properies_applied_for as $cc_email ) { + $headers[] = "Cc: " . $plugin_options[$cc_email . '_notification_name'] . " <" . $plugin_options[$cc_email . '_notification_email']. ">"; + } + // get the property for this application + + $depTax = wp_get_post_terms( $job->ID, GLM_EMP_TAX_DEPARTMENTS, 'name' ); + foreach ($depTax as $depTerm) { + $deps[] = $depTerm->name; + } + add_filter( 'wp_mail_content_type', array( $this, 'set_html_content_type' ) ); + $html_head = ' + + + + Online Application Form + + + +

+ + New Online Application Submission + +

+ + + + + + + + +
+ + + + +
+ '; + $html_foot = ' +
+
+ + + + +
+ + To ensure the delivery of these e-mails to your inbox, please add ' . $from . ' to your e-mail Address Book or Safe List. + +
+
+ + +'; + + wp_mail( + $to, + 'New Online Application Submission', + $html_head . nl2br( $message ) . $html_foot, + $headers + ); + + remove_filter( 'wp_mail_content_type', array( $this, 'set_html_content_type' ) ); + } + + public function set_html_content_type() + { + return 'text/html'; + } + /** * Show the application form */ @@ -147,8 +236,10 @@ class glm_employment_front // next form part ++$formPart; if ($formPart == self::FINAL_FORM) { + $plugin_options = get_option(GLM_EMP_SETTING_NAME); include $this->frontViewDir . '/' . self::SUCCESS_TEMPLATE; unset($_SESSION['glmJobsApplication'], $_SESSION['glmJobsAppForm']); + $this->email_owner(); } else { $form = $this->load_form_settings($formPart); include $this->frontViewDir . '/' . self::FORM_TEMPLATE; diff --git a/glm-employment.php b/glm-employment.php index a081020..7b83a7d 100644 --- a/glm-employment.php +++ b/glm-employment.php @@ -14,6 +14,10 @@ defined('ABSPATH') or die(); */ define('GLM_EMP_PLUGIN_PATH_FILE', __FILE__); define('GLM_EMP_VERSION', '0.0.1'); +define('GLM_EMP_SETTING_NAME', 'staffordsjobs_settings'); +define('GLM_EMP_SETTINGS', 'staffordsjobs_settings'); +define('GLM_EMP_NEW_CAPABILITY', 'edit_staffords_jobs'); +define('GLM_EMP_BUILT_IN_CAPABILITY', 'edit_posts'); define('GLM_EMP_VRS_OPTION_NAME', 'glm_jobs_db_version'); define('GLM_EMP_POST_TYPE', 'glm_jobs'); define('GLM_EMP_TAX_CATEGORIES', 'glm_jobscategory'); diff --git a/views/deniedAccess.php b/views/deniedAccess.php new file mode 100644 index 0000000..d8b6753 --- /dev/null +++ b/views/deniedAccess.php @@ -0,0 +1 @@ +

You do not have permission to edit Client Info

\ No newline at end of file diff --git a/views/front/thankYou.php b/views/front/thankYou.php index 15615a0..bd15ad9 100644 --- a/views/front/thankYou.php +++ b/views/front/thankYou.php @@ -1,4 +1,3 @@
- Thank You For Completing This Application Form And For Your Interest In - Preston Feather Building Centers. +
diff --git a/views/optionsPage.php b/views/optionsPage.php new file mode 100644 index 0000000..29bc24d --- /dev/null +++ b/views/optionsPage.php @@ -0,0 +1,16 @@ + +
+
+

Employment Plugin Settings

+ +
+
diff --git a/views/text.php b/views/text.php new file mode 100644 index 0000000..25f9928 --- /dev/null +++ b/views/text.php @@ -0,0 +1,2 @@ +"> diff --git a/views/textArea.php b/views/textArea.php new file mode 100644 index 0000000..1c2044f --- /dev/null +++ b/views/textArea.php @@ -0,0 +1 @@ + -- 2.17.1