Adding changes from the Staffords work.
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 31 Dec 2015 19:32:04 +0000 (14:32 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 31 Dec 2015 19:32:04 +0000 (14:32 -0500)
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
controllers/front.php
glm-employment.php
views/deniedAccess.php [new file with mode: 0644]
views/front/thankYou.php
views/optionsPage.php [new file with mode: 0644]
views/text.php [new file with mode: 0644]
views/textArea.php [new file with mode: 0644]

index 3f70b22..e893882 100644 (file)
@@ -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 '<p>Job Settings</p>';
+        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 *
index 4a84ed8..12524f5 100644 (file)
@@ -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  .= '<a href="' . admin_url() . 'edit.php?post_type=staffords_jobs&page=applications">' . get_bloginfo('name') . ' Admin Area</a>.' . "\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 = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<html>
+<head>
+    <meta http-equiv="content-type" content="text/html;charset=utf-8">
+    <title>Online Application Form</title>
+</head>
+<body>
+    <!-- {{{ body -->
+    <p>
+        <font size="4" face="arial, sans-serif">
+            <b>New Online Application Submission</b>
+        </font>
+    </p>
+    <table cellspacing="0" cellpadding="0" bgcolor="#c0c0c0" border="0">
+        <tr>
+            <td>
+                <table cellspacing="1" cellpadding="5" border="0" bgcolor="#c0c0c0" width="400">
+                        <tr flexy:if="v[element]" bgcolor="#c0c0c0">
+                            <td bgcolor="#ffffff">
+            ';
+        $html_foot = '
+                                </td>
+                        </tr>
+            </table>
+            </td>
+        </tr>
+        <tr>
+            <td>
+                <table cellspacing="0" cellpadding="10" border="0" width="400">
+                    <tr>
+                        <td bgcolor="#eeeeee">
+                            <font size="1" face="arial, sans-serif">
+                                To ensure the delivery of these e-mails to your inbox, please add ' . $from . ' to your e-mail Address Book or Safe List.
+                            </font>
+                        </td>
+                    </tr>
+                </table>
+            </td>
+        </tr>
+
+    </table>
+    <!-- }}} -->
+</body>
+</html>';
+
+        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;
index a081020..7b83a7d 100644 (file)
@@ -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 (file)
index 0000000..d8b6753
--- /dev/null
@@ -0,0 +1 @@
+<div class="wrap"><p>You do not have permission to edit Client Info</p></div>
\ No newline at end of file
index 15615a0..bd15ad9 100644 (file)
@@ -1,4 +1,3 @@
 <div class="alert-box success">
-    Thank You For Completing This Application Form And For Your Interest In
-    Preston Feather Building Centers.
+<?php echo nl2br($plugin_options['thank_you_text']); ?>
 </div>
diff --git a/views/optionsPage.php b/views/optionsPage.php
new file mode 100644 (file)
index 0000000..29bc24d
--- /dev/null
@@ -0,0 +1,16 @@
+<style>
+table.form-table th {width: 250px; text-align: right;}
+table.form-table td input[type=text] {width: 400px;}
+table.form-table td textarea {width: 500px; height: 200px;}
+</style>
+<div class="wrap">
+    <form action="options.php" method="post">
+        <h2>Employment Plugin Settings</h2>
+<?php
+settings_errors(GLM_EMP_SETTINGS);
+settings_fields(GLM_EMP_SETTINGS);
+do_settings_sections(GLM_EMP_SETTINGS);
+submit_button();
+?>
+    </form>
+</div>
diff --git a/views/text.php b/views/text.php
new file mode 100644 (file)
index 0000000..25f9928
--- /dev/null
@@ -0,0 +1,2 @@
+<input type="text" name="staffordsjobs_settings[<?php echo $fieldName;?>]"
+           value="<?php echo str_replace('"', '&quote;', $options[$fieldName]); ?>">
diff --git a/views/textArea.php b/views/textArea.php
new file mode 100644 (file)
index 0000000..1c2044f
--- /dev/null
@@ -0,0 +1 @@
+<textarea cols="40" rows="5" name="staffordsjobs_settings[<?php echo $fieldName;?>]"><?php echo htmlspecialchars($options[$fieldName]); ?></textarea>