Code standardisation updates
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 4 Jan 2017 21:54:55 +0000 (16:54 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 4 Jan 2017 21:59:01 +0000 (16:59 -0500)
Adding in phpdocs.
Adjusting code for WP standards.

class-streamsendfeedaddon.php

index 464726c..4ab726b 100644 (file)
 <?php
+/**
+ * GFStreamSendAddOn
+ *
+ * Add on for sending the Gravity Form lead data to StreamSend.
+ *
+ * @link http://www.gaslightmedia.com
+ *
+ * @package Worpress
+ * @subpackage StreamSend
+ * @since 1.0.0
+ */
+/**
+ * Require the Stream Send library class.
+ */
 require_once 'lib/class_streamsend_api.php';
 GFForms::include_feed_addon_framework();
 
+/**
+ * GFStreamSendAddOn
+ *
+ * This class extends the GFFeedAddOn. Allows the Gravity form to add a contact
+ * or update contact into StreamSend.
+ */
 class GFStreamSendAddOn extends GFFeedAddOn
 {
-    
+
+    /**
+     * _version
+     *
+     * @var mixed
+     * @access protected
+     */
     protected $_version = GLM_STREAMSEND_FEED_ADDON_VERSION;
+
+    /**
+     * _min_gravityforms_version
+     *
+     * @var string
+     * @access protected
+     */
     protected $_min_gravityforms_version = '1.9';
+
+    /**
+     * _slug
+     *
+     * @var bool
+     * @access protected
+     */
     protected $_slug = 'StreamSend';
+
+    /**
+     * _path
+     *
+     * @var bool
+     * @access protected
+     */
     protected $_path = 'glm-gravityform-streamsend/index.php';
+
+    /**
+     * _full_path
+     *
+     * @var mixed
+     * @access protected
+     */
     protected $_full_path = __FILE__;
+
+    /**
+     * _title
+     *
+     * @var bool
+     * @access protected
+     */
     protected $_title = 'Gravity Forms StreamSend Integration';
+
+    /**
+     * _short_title
+     *
+     * @var bool
+     * @access protected
+     */
     protected $_short_title = 'StreamSend';
+
+    /**
+     * _errorFlag
+     *
+     * @var bool
+     * @access protected
+     */
     protected $_errorFlag = false;
+
+    /**
+     * listID
+     *
+     * @var mixed
+     * @access protected
+     */
     protected $listID;
+
+    /**
+     * optIn
+     *
+     * @var mixed
+     * @access protected
+     */
     protected $optIn;
 
+    /**
+     * _instance
+     */
     private static $_instance = null;
+
+    /**
+     * streamSendFields
+     *
+     * @var bool
+     * @access protected
+     */
     protected $streamSendFields = array(
-        'email'        => 'email-address',
-        'first_name'   => 'first-name',
-        'last_name'    => 'last-name',
-        'address'      => 'address1',
-        'city'         => 'city',
-        'state'        => 'stateprovince',
-        'zip'          => 'postal-code',
-        'phone'        => 'phone-number',
+        'email'      => 'email-address',
+        'first_name' => 'first-name',
+        'last_name'  => 'last-name',
+        'address'    => 'address1',
+        'city'       => 'city',
+        'state'      => 'stateprovince',
+        'zip'        => 'postal-code',
+        'phone'      => 'phone-number',
     );
+
+    /**
+     * debug
+     *
+     * @var mixed
+     * @access public
+     */
     public $debug;
+
     /**
      * Get an instance of this class.
      *
@@ -46,23 +153,41 @@ class GFStreamSendAddOn extends GFFeedAddOn
      */
     public function init() {
         parent::init();
-
     }
 
+    /**
+     * init_admin
+     *
+     * @access public
+     * @return void
+     */
     public function init_admin() {
         parent::init_admin();
         // add tasks or filters here that you want to perform only in admin
     }
 
+    /**
+     * init_frontend
+     *
+     * @access public
+     * @return void
+     */
     public function init_frontend() {
         parent::init_frontend();
         // add tasks or filters here that you want to perform only in the front end
     }
 
+    /**
+     * init_ajax
+     *
+     * @access public
+     * @return void
+     */
     public function init_ajax() {
         parent::init_ajax();
-        // add tasks or filters here that you want to perform only during ajax requests
+        // add tasks or filters here that you want to perform only during Ajax requests
     }
+
     /**
      * Configures the settings which should be rendered on the add-on settings tab.
      *
@@ -102,19 +227,28 @@ class GFStreamSendAddOn extends GFFeedAddOn
         );
     }
 
+    /**
+     * feed_settings_fields
+     *
+     * @access public
+     * @return void
+     */
     public function feed_settings_fields() {
-        $settings = $this->get_plugin_settings();
-        $apiLogin = rgar($settings, 'glm_streamsend_login');
-        $apiKey = rgar($settings, 'glm_streamsend_key');
-        $ss = new StreamSend(STREAMSEND_BASE_URL, $apiLogin, $apiKey);
-        $getList = $ss->listList();
-        $listData = $getList->responseData;
-        $lists = array();
-        for($i = 0; $i < count($listData->list[$i]); $i++){
-            $lists[] =array('label'=>$listData->list[$i]->name,'value'=>$listData->list[$i]->id);
+        $settings    = $this->get_plugin_settings();
+        $apiLogin    = rgar($settings, 'glm_streamsend_login');
+        $apiKey      = rgar($settings, 'glm_streamsend_key');
+        $ss          = new StreamSend(STREAMSEND_BASE_URL, $apiLogin, $apiKey);
+        $getList     = $ss->listList();
+        $listData    = $getList->responseData;
+        $lists       = array();
+        for ( $i = 0; $i < count( $listData->list[$i] ); $i++ ) {
+            $lists[] = array(
+                'label' => $listData->list[$i]->name,
+                'value' => $listData->list[$i]->id
+            );
         }
-        array_unshift($lists,array('label'=>'Select a List', 'value'=>''));
-        
+        array_unshift( $lists,array( 'label' => 'Select a List', 'value' => '' ) );
+
         if(!empty($lists)){
             $listArray = array(
                 'label'   => 'Lists',
@@ -131,23 +265,24 @@ class GFStreamSendAddOn extends GFFeedAddOn
                 'value'   => 'No lists detected'
             );
         }
-        ?>  
+        ?>
         <script type="text/javascript">
-            document.addEventListener("DOMContentLoaded", function(event) { 
+            document.addEventListener("DOMContentLoaded", function(event) {
                 var list = document.getElementById('noLists');
                 list.style.background = "none";
-                list.readOnly = true;   
+                list.readOnly = true;
                 list.style.border = "none";
                 list.style.color = "red";
                 list.style.boxShadow = "none";
         });
         </script>
         <?php
+
         return array(
             array(
                 'title'  => 'StreamSend Form Settings',
                 'fields' => array(
-                    
+
                     array(
                         'label'   => esc_html__( 'Feed name', 'StreamSend' ),
                         'type'    => 'text',
@@ -157,17 +292,17 @@ class GFStreamSendAddOn extends GFFeedAddOn
                     ),
                     $listArray,
                     array(
-                         'label'   => 'Enable Opt-In',
-                         'type'    => 'checkbox',
-                         'name'    => 'opt-in',
-                         'tooltip' => 'Added contacts are "pending" if the checkbox is selected, and "active" if the checkbox is empty',
-                         'choices' => array(
-                                          array(
-                                              'label' => '',
-                                              'name'  => 'opt-in',
-                                              'default_value' => 1
-                                          ),
-                             )
+                        'label'   => 'Enable Opt-In',
+                        'type'    => 'checkbox',
+                        'name'    => 'opt-in',
+                        'tooltip' => 'Added contacts are "pending" if the checkbox is selected, and "active" if the checkbox is empty',
+                        'choices' => array(
+                            array(
+                                  'label' => '',
+                                  'name'  => 'opt-in',
+                                  'default_value' => 1
+                                ),
+                            )
                         ),
                     array(
                         'name'      => 'mappedFields',
@@ -230,67 +365,108 @@ class GFStreamSendAddOn extends GFFeedAddOn
                         'checkbox_label' => esc_html__( 'Enable Condition', 'StreamSend' ),
                         'instructions'   => esc_html__( 'Process this StreamSend feed if', 'StreamSend' ),
                     ),
-                    
-                    
-                    
+
+
+
                 )
             )
-        ); 
+        );
     }
-    // was originally a protected method, it changing to public does not work , may be to revert it back to protected
+
+    /**
+     * feed_list_columns
+     *
+     * Was originally a protected method, it changing to public does not work ,
+     * may be to revert it back to protected.
+     *
+     * @access public
+     * @return void
+     */
     public function feed_list_columns() {
         return array(
             'feedName' => __( 'Name', 'StreamSend' ),
         );
     }
-    
-    // validate the api key and login input
+
+    /**
+     * is_valid_setting
+     *
+     * Validate the api key and login input.
+     *
+     * @access public
+     * @return void
+     */
     public function is_valid_setting(){
-        
+
         $settings = $this->get_plugin_settings();
         $apiLogin = rgar($settings, 'glm_streamsend_login');
-        $apiKey = rgar($settings, 'glm_streamsend_key');
-        $ss = new StreamSend(STREAMSEND_BASE_URL, $apiLogin, $apiKey);
-        $ss->accountList();
-        $responseData[] = $ss->audienceList();
-        $audienceId = $responseData[0]->responseData->audience->id;
-        if ( $ss->responseHTTPStatus != 200){
-             echo '<div> ';
-                     echo '<h4 style="color:red; margin: 2px;"> Invalid Login or API Key</h4>';
-               echo '</div>';
+        $apiKey   = rgar($settings, 'glm_streamsend_key');
+        if ( $apiLogin && $apiKey ) {
+            $ss = new StreamSend(STREAMSEND_BASE_URL, $apiLogin, $apiKey);
+            $ss->accountList();
+            if ( $ss->responseHTTPStatus != 200) {
+                echo '<div> ';
+                echo '<h4 style="color:red; margin: 2px;"> Invalid Login or API Key</h4>';
+                echo '</div>';
+            } else {
+                $responseData[] = $ss->audienceList();
+                $audienceId     = $responseData[0]->responseData->audience->id;
+            }
         }
-        
-        // use JS to auto input the audience ID into the audience field and make the input readOnly
+
+        // Use JS to auto input the audience ID into the audience field and make the input readOnly.
         ?>
         <script type="text/javascript">
-            document.addEventListener("DOMContentLoaded", function(event) { 
-   
-            var audienceId = "<?php echo $audienceId; ?>";  
+        document.addEventListener("DOMContentLoaded", function(event) {
+            var audienceId = "<?php echo $audienceId; ?>";
             document.getElementById("glm_streamsend_audience").value = audienceId;
             document.getElementById("glm_streamsend_audience").readOnly = true;
         });
         </script>
         <?php
     }
-    
-    // generate the error notice to be displayed if the api or login input is invalid
+
+    /**
+     * errorNotice
+     *
+     * Generate the error notice to be displayed if the api or login input is invalid.
+     *
+     * @access public
+     * @return void
+     */
     public function errorNotice(){
         if($this->errorFlag){
             echo '<div class="push-alert-red" style="border-left: 1px solid #E6DB55; border-right: 1px solid #E6DB55;"> ';
-                     esc_html_e( 'This add-on needs to be updated. Please contact the developer.', 'gravityforms' ); 
-               echo '</div>';
+            esc_html_e( 'This add-on needs to be updated. Please contact the developer.', 'gravityforms' );
+            echo '</div>';
         }
     }
+
+    /**
+     * __construct
+     *
+     * @access public
+     * @return void
+     */
     public function __construct() {
         parent::__construct();
     }
 
+    /**
+     * process_feed
+     *
+     * @param mixed $feed
+     * @param mixed $entry
+     * @param mixed $form
+     * @access public
+     * @return void
+     */
     public function process_feed( $feed, $entry, $form ) {
-        $feedName           = $feed['meta']['feedName'];
-        $field_map = $this->get_field_map_fields( $feed, 'mappedFields' );
+        $feedName     = $feed['meta']['feedName'];
+        $field_map    = $this->get_field_map_fields( $feed, 'mappedFields' );
         $this->listID = $feed['meta']['lists']; // get the list ID from a selected list
-        $this->optIn = $feed['meta']['opt-in'];
-        
+        $this->optIn  = $feed['meta']['opt-in'];
+
         if($this->optIn == 1){
             $this->optIn = 'false';
         } else if($this->optIn == 0){
@@ -301,21 +477,29 @@ class GFStreamSendAddOn extends GFFeedAddOn
             $merge_vars[ $name ] = $this->get_field_value( $form, $entry, $field_id );
         }
         // if a list is selected there will be an id, then use the contactUpload function since it takes a list id parameter
-        if(!empty($this->listID)){
+        if ( !empty( $this->listID ) ) {
             // contact upload function here
              $return = $this->add_contact( $merge_vars, $this->listID );
         }
         $return = $this->add_contact( $merge_vars );
     }
 
+    /**
+     * add_contact
+     *
+     * @param mixed $values
+     * @param string $listId
+     * @access public
+     * @return void
+     */
     public function add_contact($values, $listId='')
     {
         $settings = $this->get_plugin_settings();
         $apiLogin = rgar( $settings, 'glm_streamsend_login' );
         $apiKey   = rgar( $settings, 'glm_streamsend_key' );
         $audience = rgar( $settings, 'glm_streamsend_audience' );
-       
-        
+
+
 
         // check the array $values to make sure it is correct
         if (is_array($values) && !empty($values)) {
@@ -329,7 +513,7 @@ class GFStreamSendAddOn extends GFFeedAddOn
             foreach ($this->streamSendFields as $glmName => $ssName) {
                 $contactData[$ssName] = $values[$glmName];
             }
-            if(!empty($this->listID)){
+            if (!empty( $this->listID ) ) {
                 $contacts = $ss->contactCreate(
                     $contactData,
                     $this->optIn,
@@ -337,7 +521,7 @@ class GFStreamSendAddOn extends GFFeedAddOn
                     STREAMSEND_DEFAULT_DELIVER_WELCOME,
                     $listId
                 );
-            }else if($ret->contact){
+            } else if ( $ret->contact ) {
                  $contacts = $ss->contactUpdate(
                     $ret->contact->id,
                     $contactData
@@ -351,7 +535,7 @@ class GFStreamSendAddOn extends GFFeedAddOn
                 );
             }
 
-            if (!$contacts) {
+            if ( !$contacts ) {
                 return false;
                 //echo '<p><h3>Debug Results</h3>'.$ss->debugBuffer.'</p>';
             }