Action.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Abstracts_Action
  4. */
  5. abstract class NF_Abstracts_Action
  6. {
  7. /**
  8. * @var string
  9. */
  10. protected $_name = '';
  11. /**
  12. * @var string
  13. */
  14. protected $_nicename = '';
  15. /**
  16. * @var string
  17. */
  18. protected $_section = 'installed';
  19. /**
  20. * @var string
  21. */
  22. protected $_image = '';
  23. /**
  24. * @var array
  25. */
  26. protected $_tags = array();
  27. /**
  28. * @var string
  29. */
  30. protected $_timing = 'normal';
  31. /**
  32. * @var int
  33. */
  34. protected $_priority = '10';
  35. /**
  36. * @var array
  37. */
  38. protected $_settings = array();
  39. /**
  40. * @var array
  41. */
  42. protected $_settings_all = array( 'label', 'active' );
  43. /**
  44. * @var array
  45. */
  46. protected $_settings_exclude = array();
  47. /**
  48. * @var array
  49. */
  50. protected $_settings_only = array();
  51. /**
  52. * Constructor
  53. */
  54. public function __construct()
  55. {
  56. $this->_settings_all = apply_filters( 'ninja_forms_actions_settings_all', $this->_settings_all );
  57. if( ! empty( $this->_settings_only ) ){
  58. $this->_settings = array_merge( $this->_settings, $this->_settings_only );
  59. } else {
  60. $this->_settings = array_merge( $this->_settings_all, $this->_settings );
  61. $this->_settings = array_diff( $this->_settings, $this->_settings_exclude );
  62. }
  63. $this->_settings = $this->load_settings( $this->_settings );
  64. }
  65. //-----------------------------------------------------
  66. // Public Methods
  67. //-----------------------------------------------------
  68. /**
  69. * Save
  70. */
  71. public function save( $action_settings )
  72. {
  73. // This section intentionally left blank.
  74. }
  75. /**
  76. * Process
  77. */
  78. public abstract function process( $action_id, $form_id, $data );
  79. /**
  80. * Get Timing
  81. *
  82. * Returns the timing for an action.
  83. *
  84. * @return mixed
  85. */
  86. public function get_timing()
  87. {
  88. $timing = array( 'early' => -1, 'normal' => 0, 'late' => 1 );
  89. return intval( $timing[ $this->_timing ] );
  90. }
  91. /**
  92. * Get Priority
  93. *
  94. * Returns the priority for an action.
  95. *
  96. * @return int
  97. */
  98. public function get_priority()
  99. {
  100. return intval( $this->_priority );
  101. }
  102. /**
  103. * Get Name
  104. *
  105. * Returns the name of an action.
  106. *
  107. * @return string
  108. */
  109. public function get_name()
  110. {
  111. return $this->_name;
  112. }
  113. /**
  114. * Get Nicename
  115. *
  116. * Returns the nicename of an action.
  117. *
  118. * @return string
  119. */
  120. public function get_nicename()
  121. {
  122. return $this->_nicename;
  123. }
  124. /**
  125. * Get Section
  126. *
  127. * Returns the drawer section for an action.
  128. *
  129. * @return string
  130. */
  131. public function get_section()
  132. {
  133. return $this->_section;
  134. }
  135. /**
  136. * Get Image
  137. *
  138. * Returns the url of a branded action's image.
  139. *
  140. * @return string
  141. */
  142. public function get_image()
  143. {
  144. return $this->_image;
  145. }
  146. /**
  147. * Get Settings
  148. *
  149. * Returns the settings for an action.
  150. *
  151. * @return array|mixed
  152. */
  153. public function get_settings()
  154. {
  155. return $this->_settings;
  156. }
  157. /**
  158. * Sort Actions
  159. *
  160. * A static method for sorting two actions by timing, then priority.
  161. *
  162. * @param $a
  163. * @param $b
  164. * @return int
  165. */
  166. public static function sort_actions( $a, $b )
  167. {
  168. if( ! isset( Ninja_Forms()->actions[ $a->get_setting( 'type' ) ] ) ) return 1;
  169. if( ! isset( Ninja_Forms()->actions[ $b->get_setting( 'type' ) ] ) ) return 1;
  170. $a->timing = Ninja_Forms()->actions[ $a->get_setting( 'type' ) ]->get_timing();
  171. $a->priority = Ninja_Forms()->actions[ $a->get_setting( 'type' ) ]->get_priority();
  172. $b->timing = Ninja_Forms()->actions[ $b->get_setting( 'type' ) ]->get_timing();
  173. $b->priority = Ninja_Forms()->actions[ $b->get_setting( 'type' ) ]->get_priority();
  174. // Compare Priority if Timing is the same
  175. if( $a->timing == $b->timing)
  176. return $a->priority > $b->priority ? 1 : -1;
  177. // Compare Timing
  178. return $a->timing < $b->timing ? 1 : -1;
  179. }
  180. protected function load_settings( $only_settings = array() )
  181. {
  182. $settings = array();
  183. // Loads a settings array from the FieldSettings configuration file.
  184. $all_settings = Ninja_Forms::config( 'ActionSettings' );
  185. foreach( $only_settings as $setting ){
  186. if( isset( $all_settings[ $setting ]) ){
  187. $settings[ $setting ] = $all_settings[ $setting ];
  188. }
  189. }
  190. return $settings;
  191. }
  192. } // END CLASS NF_Abstracts_Action