WIP for adding emails
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 9 Dec 2015 22:05:58 +0000 (17:05 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 9 Dec 2015 22:05:58 +0000 (17:05 -0500)
form does email
need to work on the setting page.

controllers/admin.php
controllers/front.php
glm-employment.php
views/deniedAccess.php [new file with mode: 0644]
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 9b89e32..27b4899 100644 (file)
@@ -29,6 +29,7 @@ class staffords_employment_admin
         $this->wpdb = $wpdb;
         $jobs = new staffords_employment_job($path);
         add_action('admin_menu', array($this, 'add_job_menus'));
+        add_action('admin-init', array($this, 'job_settings_init') );
     }
 
     /**
@@ -44,14 +45,133 @@ class staffords_employment_admin
             'applications',
             array($this, 'get_applications')
         );
-//        add_submenu_page(
-//            'edit.php?post_type=' . STAFFORDS_EMP_POST_TYPE,
-//            'settings',
-//            'Settings',
-//            'manage_options',
-//            'job_settings',
-//            array($this, 'show_job_settings')
-//        );
+        add_submenu_page(
+            'edit.php?post_type=' . STAFFORDS_EMP_POST_TYPE,
+            'settings',
+            'Settings',
+            STAFFORDS_EMP_NEW_CAPABILITY,
+            STAFFORDS_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(STAFFORDS_EMP_BUILT_IN_CAPABILITY)) {
+                $role->add_cap(STAFFORDS_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(STAFFORDS_EMP_NEW_CAPABILITY)) {
+                $role->remove_cap(STAFFORDS_EMP_NEW_CAPABILITY);
+            }
+        }
+    }
+
+    public function staffords_settings_exist()
+    {
+        if (false == get_option(STAFFORDS_EMP_SETTINGS)) {
+            add_option(STAFFORDS_EMP_SETTINGS);
+        }
+    }
+
+    public function job_settings_init()
+    {
+        register_setting(STAFFORDS_EMP_SETTINGS, STAFFORDS_EMP_SETTINGS);
+        add_filter(
+            'option_page_capability_job-setting-group',
+            array($this, 'staffords_option_page_capability')
+        );
+        add_settings_section(
+            'staffordsjobs_main',
+            __('Edit Jobs Settings', 'wordpress'),
+            array($this, 'staffords_settings_section_callback'),
+            'job-setting-group'
+        );
+        $fieldNames = array(
+            array(
+                'name'  => 'notificationEmail',
+                'label' => 'Notification Email Address',
+                'type'  => 'text'
+            )
+        );
+
+        foreach ($fieldNames as $field) {
+            $this->staffords_add_setting_text_field(
+                $field['name'],
+                $field['label'],
+                $field['type']
+            );
+        }
+    }
+
+    public function staffords_add_setting_text_field($name, $label, $type)
+    {
+        switch ($type) {
+            case 'text':
+                $callback = 'glmclientinfoRenderText';
+                break;
+            case 'textarea':
+                $callback = 'glmclientinfoRenderTextArea';
+                break;
+            case 'state':
+                $callback = 'glmclientinfoRenderStateSelect';
+                break;
+            default:
+                return false;
+                break;
+        }
+        add_settings_field(
+            $name,
+            __($label, 'wordpress'),
+            array($this, $callback),
+            'job-setting-group',
+            STAFFORDS_EMP_SETTINGS,
+            $name
+        );
+    }
+
+    public function glmclientinfoRenderText($fieldName)
+    {
+        static $options;
+        if (!$options) {
+            $options = get_option(STAFFORDS_EMP_SETTINGS);
+        }
+        include $this->pluginDirName . 'views/text.php';
+    }
+
+    public function glmclientinfoRenderTextArea($fieldName)
+    {
+        static $options;
+        $options = get_option(STAFFORDS_EMP_SETTINGS);
+        include $this->pluginDirName . 'views/textArea.php';
+    }
+
+    public function staffords_settings_section_callback()
+    {
+        echo __('Employment Form Email Notification', 'wordpress');
+    }
+
+    public function staffords_option_page_capability()
+    {
+        return STAFFORDS_EMP_NEW_CAPABILITY;
     }
 
     /**
@@ -59,7 +179,11 @@ class staffords_employment_admin
      */
     public function show_job_settings()
     {
-        echo '<p>Job Settings</p>';
+        if (current_user_can(STAFFORDS_EMP_NEW_CAPABILITY)) {
+            include $this->pluginDirName . 'views/optionsPage.php';
+        } else {
+            include $this->pluginDirName . 'views/deniedAccess.php';
+        }
     }
 
     /**
index 5879ca0..4698bbc 100644 (file)
@@ -111,6 +111,24 @@ class staffords_employment_front
         return $form;
     }
 
+    public function email_owner()
+    {
+        $to      = 'steve@localhost';
+        $name    = 'Person Test';
+        $email   = 'user@gaslightmedia.com';
+        $message .= "A new employment application has been submitted. To review this "
+            . "application, log into your "
+            . "Web site administration area.\n\n";
+        $message .= admin_url() . "edit.php?post_type@staffords_jobs&page=applications\n\n";
+        $headers = "From: Online Employment Application Form <server@gaslightmedia.com>\r\nReply-To: {$name} <{$email}>";
+        wp_mail(
+            $to,
+            'New Online Application Submission',
+            $message,
+            $headers
+        );
+    }
+
     /**
      * Show the application form
      */
@@ -144,6 +162,7 @@ class staffords_employment_front
                     if ($formPart == self::FINAL_FORM) {
                         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 1cfa662..66d5d1c 100644 (file)
@@ -14,6 +14,9 @@ defined('ABSPATH') or die();
  */
 define('STAFFORDS_EMP_PLUGIN_PATH_FILE', __FILE__);
 define('STAFFORDS_EMP_VERSION', '0.2.1');
+define('STAFFORDS_EMP_SETTINGS', 'staffordsjobs_settings');
+define('STAFFORDS_EMP_NEW_CAPABILITY', 'staffordsjobs_edit_jobs');
+define('STAFFORDS_EMP_BUILT_IN_CAPABILITY', 'edit_posts');
 define('STAFFORDS_EMP_VRS_OPTION_NAME', 'staffords_jobs_db_version');
 define('STAFFORDS_EMP_POST_TYPE', 'staffords_jobs');
 define('STAFFORDS_EMP_TAX_CATEGORIES', 'staffords_jobscategory');
@@ -27,6 +30,10 @@ define('STAFFORDS_EMP_FORM_TABLE', 'staffords_jobs_app_form');
 $wp_upload_dir = wp_upload_dir();
 define('STAFFORDS_EMP_UPLOAD_DIR', $wp_upload_dir['basedir'] . '/glm-emp/');
 define('STAFFORDS_EMP_UPLOAD_URL', $wp_upload_dir['baseurl'] . '/glm-emp/');
+
+register_activation_hook(__FILE__, array('staffords_employment_admin', 'activate_plugin'));
+register_deactivation_hook(__FILE__, array('staffords_employment_admin', 'deactivate_plugin'));
+
 /**
  * Create the upload directory for the resume files if not already there
  */
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
diff --git a/views/optionsPage.php b/views/optionsPage.php
new file mode 100644 (file)
index 0000000..1ad3c8e
--- /dev/null
@@ -0,0 +1,10 @@
+<div class="wrap">
+    <form action="options.php" method="post">
+        <h2>Employment Plugin Settings</h2>
+        <?php
+            settings_fields('job-setting-group');
+            do_settings_sections('job-setting-group');
+            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>