Нема описа

Provider.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Abstract_Provider
  10. */
  11. abstract class PUM_Abstract_Provider implements PUM_Interface_Provider {
  12. /**
  13. * Option name prefix.
  14. *
  15. * @var string
  16. */
  17. public $opt_prefix = '';
  18. /**
  19. * Email provider name such as 'mailchimp'
  20. *
  21. * @var string
  22. */
  23. public $id = '';
  24. /**
  25. * Email provider name for labeling such as 'MailChimp's
  26. *
  27. * @var string
  28. */
  29. public $name = '';
  30. /**
  31. * Version of the email provider implementation. Used for compatibility.
  32. *
  33. * @var int
  34. */
  35. public $version = 1;
  36. /**
  37. * Latest current version.
  38. *
  39. * @var int
  40. */
  41. public $current_version = 2;
  42. /**
  43. * The constructor method which sets up all filters and actions to prepare fields and messages
  44. */
  45. public function __construct() {
  46. /** Register Provider Globally */
  47. PUM_Newsletter_Providers::instance()->add_provider( $this );
  48. /** Settings */
  49. add_filter( 'pum_settings_fields', array( $this, 'register_settings' ) );
  50. add_filter( 'pum_settings_tab_sections', array( $this, 'register_settings_tab_section' ) );
  51. /**
  52. * Don't add the shortcodes or default options or process anything if the provider is disabled.
  53. */
  54. if ( ! $this->enabled() ) {
  55. return;
  56. }
  57. /** Shortcodes Fields */
  58. add_filter( 'pum_sub_form_shortcode_tabs', array( $this, 'shortcode_tabs' ) );
  59. add_filter( 'pum_sub_form_shortcode_subtabs', array( $this, 'shortcode_subtabs' ) );
  60. add_filter( 'pum_sub_form_shortcode_fields', array( $this, 'shortcode_fields' ) );
  61. add_filter( 'pum_sub_form_shortcode_defaults', array( $this, 'shortcode_defaults' ) );
  62. /** Forms Processing & AJAX */
  63. add_filter( 'pum_sub_form_sanitization', array( $this, 'process_form_sanitization' ), 10 );
  64. add_filter( 'pum_sub_form_validation', array( $this, 'process_form_validation' ), 10, 2 );
  65. add_action( 'pum_sub_form_submission', array( $this, 'process_form_submission' ), 10, 3 );
  66. /** Form Rendering */
  67. add_action( 'pum_sub_form_fields', array( $this, 'render_fields' ) );
  68. }
  69. /**
  70. * Determines whether to load this providers fields in the shortcode editor among other things.
  71. *
  72. * @return bool
  73. */
  74. abstract public function enabled();
  75. /**
  76. * Contains each providers unique fields.
  77. *
  78. * @deprecated 1.7.0 Use instead: $this->shortcode_tabs, $this->shortcode_subtabs & $this->shortcode_fields instead.
  79. * @uses self::instance()->shortcode_tabs()
  80. *
  81. * @return array
  82. */
  83. public function fields() {
  84. return PUM_Admin_Helpers::flatten_fields_array( $this->shortcode_fields() );
  85. }
  86. /**
  87. * Contains each providers unique global settings.
  88. *
  89. * @return array
  90. */
  91. abstract public function register_settings();
  92. /**
  93. * Contains each providers unique global settings tab sections..
  94. *
  95. * @param array $sections Array of settings page tab sections.
  96. *
  97. * @return array
  98. */
  99. public function register_settings_tab_section( $sections = array() ) {
  100. $sections['subscriptions'][ $this->id ] = $this->name;
  101. return $sections;
  102. }
  103. /**
  104. * Creates the inputs for each of the needed fields for the email provider
  105. *
  106. * TODO Determine how this should really work for visible custom fields.
  107. *
  108. * @param array $shortcode_atts Array of shortcodee attrs.
  109. */
  110. public function render_fields( $shortcode_atts ) {
  111. $fields = PUM_Admin_Helpers::flatten_fields_array( $this->shortcode_fields() );
  112. foreach ( $fields as $key => $field ) {
  113. if ( ! $field['private'] && isset( $shortcode_atts[ $key ] ) ) {
  114. echo esc_html( '<input type="hidden" name="' . $key . '" value="' . $shortcode_atts[ $key ] . '" />' );
  115. }
  116. }
  117. }
  118. /**
  119. * Process form value sanitization.
  120. *
  121. * @param array $values Values.
  122. *
  123. * @return array $values
  124. */
  125. public function form_sanitization( $values = array() ) {
  126. return $values;
  127. }
  128. /**
  129. * Process form values for errors.
  130. *
  131. * @param WP_Error $errors Errors object.
  132. * @param array $values Values.
  133. *
  134. * @return WP_Error
  135. */
  136. public function form_validation( WP_Error $errors, $values = array() ) {
  137. return $errors;
  138. }
  139. /**
  140. * Subscribes the user to the list
  141. *
  142. * @param array $values Values.
  143. * @param array $json_response JSON Response.
  144. * @param WP_Error $errors Errors object.
  145. */
  146. public function form_submission( $values, &$json_response, WP_Error &$errors ) {
  147. }
  148. /**
  149. * Internally processes sanitization only for the current provider.
  150. *
  151. * @param array $values Values.
  152. *
  153. * @return array $values
  154. */
  155. public function process_form_sanitization( $values = array() ) {
  156. if ( $this->id !== $values['provider'] && ( 'none' === $values['provider'] && PUM_Utils_Options::get( 'newsletter_default_provider' ) !== $this->id ) ) {
  157. return $values;
  158. }
  159. return $this->form_sanitization( $values );
  160. }
  161. /**
  162. * Internally processes validation only for the current provider.
  163. *
  164. * @param WP_Error $errors Errors object.
  165. * @param array $values Values.
  166. *
  167. * @return WP_Error
  168. */
  169. public function process_form_validation( WP_Error $errors, $values = array() ) {
  170. if ( $this->id !== $values['provider'] && ( 'none' === $values['provider'] && PUM_Utils_Options::get( 'newsletter_default_provider' ) !== $this->id ) ) {
  171. return $errors;
  172. }
  173. return $this->form_validation( $errors, $values );
  174. }
  175. /**
  176. * Internally processes submission only for the current provider.
  177. *
  178. * @param array $values Values.
  179. * @param array $json_response AJAX JSON Response array.
  180. * @param WP_Error $errors Errors object.
  181. */
  182. public function process_form_submission( $values, &$json_response, WP_Error &$errors ) {
  183. if ( $this->id !== $values['provider'] && ( 'none' === $values['provider'] && PUM_Utils_Options::get( 'newsletter_default_provider' ) !== $this->id ) ) {
  184. return;
  185. }
  186. $this->form_submission( $values, $json_response, $errors );
  187. }
  188. /**
  189. *
  190. *
  191. * @return string $tab_id;
  192. */
  193. public function shortcode_tab_id() {
  194. return 'provider_' . $this->id;
  195. }
  196. /**
  197. * Adds a tab for each provider. These will be hidden except for the chosen provider.
  198. *
  199. * @param array $tabs Array of tab.
  200. *
  201. * @return array
  202. */
  203. public function shortcode_tabs( $tabs = array() ) {
  204. $resorted_tabs = array();
  205. foreach ( $tabs as $tab_id => $label ) {
  206. $resorted_tabs[ $tab_id ] = $label;
  207. if ( 'general' == $tab_id ) {
  208. $resorted_tabs[ $this->shortcode_tab_id() ] = $this->name;
  209. }
  210. }
  211. return $resorted_tabs;
  212. }
  213. /**
  214. * Adds a subtabs for each provider. These will be hidden except for the chosen provider.
  215. *
  216. * @param array $subtabs Array of tab=>subtabs.
  217. *
  218. * @return array
  219. */
  220. public function shortcode_subtabs( $subtabs = array() ) {
  221. return array_merge( $subtabs, array(
  222. $this->shortcode_tab_id() => array(
  223. 'main' => $this->name,
  224. ),
  225. ) );
  226. }
  227. /**
  228. * Registers the fields for this providers shortcode tab.
  229. *
  230. * @param array $fields Array of fields.
  231. *
  232. * @return array
  233. */
  234. public function shortcode_fields( $fields = array() ) {
  235. $new_fields = $this->version < 2 ? PUM_Admin_Helpers::flatten_fields_array( $this->fields() ) : array();
  236. foreach ( $new_fields as $field_id => $field ) {
  237. if ( isset( $field['options'] ) ) {
  238. $new_fields[ $field_id ]['options'] = array_flip( $field['options'] );
  239. }
  240. }
  241. return array_merge( $fields, array(
  242. $this->shortcode_tab_id() => array(
  243. 'main' => $new_fields,
  244. ),
  245. ) );
  246. }
  247. /**
  248. * Registers the defaults for this provider.
  249. *
  250. * @param array $defaults Array of default values.
  251. *
  252. * @return array
  253. */
  254. public function shortcode_defaults( $defaults ) {
  255. // Flatten fields array.
  256. $fields = PUM_Admin_Helpers::flatten_fields_array( $this->shortcode_fields() );
  257. return array_merge( $defaults, PUM_Admin_Helpers::get_field_defaults( $fields ) );
  258. }
  259. /**
  260. * Gets default messages.
  261. *
  262. * @param string|null $context Context of the message to be returned.
  263. *
  264. * @return array|mixed|string
  265. */
  266. public function default_messages( $context = null ) {
  267. return pum_get_newsletter_default_messages( $context );
  268. }
  269. /**
  270. * Get default or customized messages.
  271. *
  272. * @param string $context Context.
  273. * @param array $values Array of values.
  274. *
  275. * @return string
  276. */
  277. public function get_message( $context, $values = array() ) {
  278. $message = PUM_Utils_Options::get( "{$this->opt_prefix}{$context}_message", '' );
  279. if ( empty( $message ) ) {
  280. $message = $this->default_messages( $context );
  281. }
  282. if ( strpos( $message, '{' ) ) {
  283. $message = $this->dynamic_message( $message, $values );
  284. }
  285. return apply_filters( "pum_newsletter_{$context}_message", $message, $this );
  286. }
  287. /**
  288. * Process a message with dynamic values.
  289. *
  290. * @param string $message Message.
  291. * @param array $values Array of values.
  292. *
  293. * @return mixed|string
  294. */
  295. protected function dynamic_message( $message = '', $values = array() ) {
  296. preg_match_all( '/{(.*?)}/', $message, $found );
  297. if ( count( $found[1] ) ) {
  298. foreach ( $found[1] as $key => $match ) {
  299. $message = $this->message_text_replace( $message, $match, $values );
  300. }
  301. }
  302. return $message;
  303. }
  304. /**
  305. * Replaces a single matched message.
  306. *
  307. * @param string $message Message.
  308. * @param string $match Matched phrase.
  309. * @param array $values Values for replacement.
  310. *
  311. * @return mixed|string
  312. */
  313. protected function message_text_replace( $message = '', $match = '', $values = array() ) {
  314. if ( empty( $match ) ) {
  315. return $message;
  316. }
  317. if ( strpos( $match, '||' ) !== false ) {
  318. $matches = explode( '||', $match );
  319. } else {
  320. $matches = array( $match );
  321. }
  322. $replace = '';
  323. foreach ( $matches as $string ) {
  324. if ( ! array_key_exists( $string, $values ) ) {
  325. // If its not a valid code it is likely a fallback.
  326. $replace = $string;
  327. } else {
  328. // This is a form field value, replace accordingly.
  329. switch ( $string ) {
  330. default:
  331. $replace = $values[ $string ];
  332. break;
  333. }
  334. }
  335. // If we found a replacement stop the loop.
  336. if ( ! empty( $replace ) ) {
  337. break;
  338. }
  339. }
  340. return str_replace( '{' . $match . '}', $replace, $message );
  341. }
  342. /**
  343. * Magic method replacement.
  344. *
  345. * @param string $name Function or field name.
  346. *
  347. * @return mixed
  348. */
  349. public function __get( $name ) {
  350. if ( method_exists( $this, 'get_' . $name ) ) {
  351. $method = 'get_' . $name;
  352. return $this->$method();
  353. }
  354. if ( property_exists( $this, $name ) ) {
  355. return $this->$name;
  356. }
  357. return false;
  358. }
  359. }