Nenhuma Descrição

Integrations.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Integrations
  10. */
  11. class PUM_Integrations {
  12. /**
  13. * @var PUM_Abstract_Integration[]|PUM_Abstract_Integration_Form[]
  14. */
  15. public static $integrations = array();
  16. /**
  17. * @var bool
  18. */
  19. public static $preload_posts = false;
  20. public static $form_success;
  21. public static $form_submission;
  22. /**
  23. * Initializes all form plugin and page builder integrations.
  24. */
  25. public static function init() {
  26. self::$integrations = apply_filters(
  27. 'pum_integrations',
  28. [
  29. // Forms.
  30. 'ninjaforms' => new PUM_Integration_Form_NinjaForms(),
  31. 'gravityforms' => new PUM_Integration_Form_GravityForms(),
  32. 'contactform7' => new PUM_Integration_Form_ContactForm7(),
  33. 'calderaforms' => new PUM_Integration_Form_CalderaForms(),
  34. 'mc4wp' => new PUM_Integration_Form_MC4WP(),
  35. 'wpforms' => new PUM_Integration_Form_WPForms(),
  36. 'formidableforms' => new PUM_Integration_Form_FormidableForms(),
  37. // Builders.
  38. 'kingcomposer' => new PUM_Integration_Builder_KingComposer(),
  39. 'visualcomposer' => new PUM_Integration_Builder_VisualComposer(),
  40. ]
  41. );
  42. self::$preload_posts = isset( $_GET['page'] ) && $_GET['page'] == 'pum-settings';
  43. add_filter( 'pum_settings_fields', array( __CLASS__, 'settings_fields' ) );
  44. add_action( 'pum_preload_popup', array( __CLASS__, 'enqueue_assets' ) );
  45. add_filter( 'pum_registered_conditions', array( __CLASS__, 'register_conditions' ) );
  46. add_filter( 'pum_vars', array( __CLASS__, 'pum_vars' ) );
  47. add_action( 'init', array( __CLASS__, 'wp_init_late' ), 99 );
  48. add_action( 'admin_init', array( __CLASS__, 'admin_init' ) );
  49. add_filter( 'pum_popup_post_type_args', array( __CLASS__, 'popup_post_type_args' ) );
  50. add_filter( 'pum_generated_js', array( __CLASS__, 'generated_js' ) );
  51. add_filter( 'pum_generated_css', array( __CLASS__, 'generated_css' ) );
  52. add_filter( 'pum_popup_settings', array( __CLASS__, 'popup_settings' ), 10, 2 );
  53. PUM_Integration_GoogleFonts::init();
  54. }
  55. /**
  56. * Checks if a 3rd party integration should be enabled.
  57. *
  58. * @param $key
  59. *
  60. * @return bool
  61. */
  62. public static function enabled( $key ) {
  63. return (bool) isset( self::$integrations[ $key ] ) && self::$integrations[ $key ]->enabled();
  64. }
  65. /**
  66. * @return PUM_Abstract_Integration_Form[]
  67. */
  68. public static function get_enabled_form_integrations() {
  69. $enabled_forms = [];
  70. foreach ( self::$integrations as $object ) {
  71. if ( $object instanceof PUM_Abstract_Integration_Form && $object->enabled() ) {
  72. $enabled_forms[ $object->key ] = $object;
  73. }
  74. }
  75. return $enabled_forms;
  76. }
  77. /**
  78. * Returns an array of value=>labels for select fields containing enabled form plugin integrations.
  79. *
  80. * @return array
  81. */
  82. public static function get_enabled_forms_selectlist() {
  83. $enabled_form_integrations = self::get_enabled_form_integrations();
  84. $form_types = [];
  85. foreach ( $enabled_form_integrations as $key => $object ) {
  86. $form_types[ $key ] = $object->label();
  87. }
  88. return $form_types;
  89. }
  90. /**
  91. * @param $key
  92. *
  93. * @return bool|PUM_Abstract_Integration|PUM_Abstract_Integration_Form
  94. */
  95. public static function get_integration_info( $key ) {
  96. return isset( self::$integrations[ $key ] ) ? self::$integrations[ $key ] : false;
  97. }
  98. /**
  99. * @param string $key
  100. *
  101. * @return array
  102. */
  103. public static function get_form_provider_forms( $key ) {
  104. $integration = self::get_integration_info( $key );
  105. if ( ! ( $integration instanceof PUM_Abstract_Integration_Form ) || ! $integration->enabled() ) {
  106. return [];
  107. }
  108. return $integration->get_forms();
  109. }
  110. /**
  111. * @param $key
  112. * @param $id
  113. *
  114. * @return array|mixed
  115. */
  116. public static function get_form_provider_form( $key, $id ) {
  117. $integration = self::get_integration_info( $key );
  118. if ( ! ( $integration instanceof PUM_Abstract_Integration_Form ) || ! $integration->enabled() ) {
  119. return [];
  120. }
  121. return $integration->get_form( $id );
  122. }
  123. /**
  124. * @param $key
  125. *
  126. * @return array
  127. */
  128. public static function get_form_provider_forms_selectlist( $key ) {
  129. $integration = self::get_integration_info( $key );
  130. if ( ! ( $integration instanceof PUM_Abstract_Integration_Form ) || ! $integration->enabled() ) {
  131. return [];
  132. }
  133. return $integration->get_form_selectlist();
  134. }
  135. /**
  136. * Adds additional settings to help better integrate with 3rd party plugins.
  137. *
  138. * @param array $fields
  139. *
  140. * @return array
  141. */
  142. public static function settings_fields( $fields = array() ) {
  143. foreach ( self::$integrations as $key => $integration ) {
  144. if ( ! ( $integration instanceof PUM_Interface_Integration_Settings ) || ! $integration->enabled() ) {
  145. continue;
  146. }
  147. // TODO LEFT OFF HERE.
  148. // TODO Could this be done via add_filter( 'pum_settings_fields', array( $integration, 'append_fields' ) );
  149. // TODO If so, do we do it inside the __construct for the PUM_Abstract_Integration, or the Integration_{Provider} class itself.
  150. // TODO Alternatively do we simply loop over all enabled providers during self::init() and add the filters/hooks there instead.
  151. $fields = $integration->append_fields( $fields );
  152. }
  153. return $fields;
  154. }
  155. public static function enqueue_assets( $popup_id = 0 ) {
  156. $popup = pum_get_popup( $popup_id );
  157. if ( ! pum_is_popup( $popup ) ) {
  158. return;
  159. }
  160. // Do stuff here.
  161. }
  162. public static function register_conditions( $conditions = array() ) {
  163. foreach ( self::$integrations as $key => $enabled ) {
  164. if ( ! $enabled ) {
  165. continue;
  166. }
  167. switch ( $key ) {
  168. }
  169. }
  170. return $conditions;
  171. }
  172. /**
  173. * Runs during init
  174. */
  175. public static function wp_init_late() {
  176. /**
  177. * Force KingComposer support for popups.
  178. */
  179. if ( self::enabled( 'kingcomposer' ) ) {
  180. global $kc;
  181. $kc->add_content_type( 'popup' );
  182. }
  183. }
  184. /**
  185. * Runs during admin_init
  186. */
  187. public static function admin_init() {
  188. if ( ! self::enabled( 'visualcomposer' ) && ( is_admin() && isset( $_GET['page'] ) && in_array( $_GET['page'], array(
  189. 'vc_settings',
  190. 'fl-builder-settings',
  191. ) ) ) || pum_is_popup_editor() ) {
  192. add_filter( 'vc_role_access_with_post_types_get_state', '__return_true' );
  193. add_filter( 'vc_role_access_with_backend_editor_get_state', '__return_true' );
  194. add_filter( 'vc_role_access_with_frontend_editor_get_state', '__return_false' );
  195. add_filter( 'vc_check_post_type_validation', '__return_true' );
  196. }
  197. }
  198. public static function popup_post_type_args( $args = array() ) {
  199. if ( self::enabled( 'kingcomposer' ) && ( ( is_admin() && isset( $_GET['page'] ) && $_GET['page'] == 'kingcomposer' ) || pum_is_popup_editor() ) ) {
  200. $args = array_merge( $args, array(
  201. 'public' => true,
  202. 'exclude_from_search' => true,
  203. 'publicly_queryable' => false,
  204. 'show_in_nav_menus' => false,
  205. ) );
  206. }
  207. if ( self::enabled( 'visualcomposer' ) && ( is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) && ( ( isset( $_GET['page'] ) && in_array( $_GET['page'], array(
  208. 'vc_settings',
  209. 'fl-builder-settings',
  210. ) ) ) || ( isset( $_POST['option_page'] ) && $_POST['option_page'] == 'wpb_js_composer_settings_general' ) || pum_is_popup_editor() ) ) ) {
  211. $args = array_merge( $args, array(
  212. 'public' => true,
  213. 'exclude_from_search' => true,
  214. 'publicly_queryable' => false, // Was true, verify this isn't a problem.
  215. 'show_in_nav_menus' => false,
  216. ) );
  217. }
  218. return $args;
  219. }
  220. /**
  221. * @param array $js
  222. *
  223. * @return array
  224. */
  225. public static function generated_js( $js = [] ) {
  226. foreach ( self::$integrations as $integration ) {
  227. if ( $integration->enabled() && method_exists( $integration, 'custom_scripts' ) ) {
  228. $js = $integration->custom_scripts( $js );
  229. }
  230. }
  231. return $js;
  232. }
  233. /**
  234. * @param array $css
  235. *
  236. * @return array $css
  237. */
  238. public static function generated_css( $css = array() ) {
  239. foreach ( self::$integrations as $integration ) {
  240. if ( $integration->enabled() && method_exists( $integration, 'custom_styles' ) ) {
  241. $css = $integration->custom_styles( $css );
  242. }
  243. }
  244. return $css;
  245. }
  246. /**
  247. * Modify popup settings.
  248. *
  249. * @param array $settings
  250. * @param int $popup_id
  251. *
  252. * @return array
  253. */
  254. public static function popup_settings( $settings, $popup_id ) {
  255. if ( is_admin() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
  256. return $settings;
  257. }
  258. static $form_popup_id;
  259. /**
  260. * Checks for popup form submission.
  261. */
  262. if ( ! isset( $form_popup_id ) ) {
  263. $form_popup_id = isset( $_REQUEST['pum_form_popup_id'] ) && absint( $_REQUEST['pum_form_popup_id'] ) > 0 ? absint( $_REQUEST['pum_form_popup_id'] ) : false;
  264. }
  265. // Should it reopen? Only if all of the following are true.
  266. $should_reopen = [
  267. // Form popup was submitted and matches this popup.
  268. $form_popup_id && $popup_id == $form_popup_id,
  269. // Form reopen was not marked disable.
  270. empty( $settings['disable_form_reopen'] ) || ! $settings['disable_form_reopen'],
  271. // Close on form submission is disbaled, or has a timer larger than 0.
  272. ( empty( $settings['close_on_form_submission'] ) || ! $settings['close_on_form_submission'] || ( $settings['close_on_form_submission'] && $settings['close_on_form_submission_delay'] > 0 ) ),
  273. ];
  274. /**
  275. * If submission exists for this popup remove auto open triggers and add an admin_debug trigger to reshow the popup.
  276. */
  277. if ( ! in_array( false, $should_reopen ) ) {
  278. $triggers = ! empty( $settings['triggers'] ) ? $settings['triggers'] : array();
  279. foreach ( $triggers as $key => $trigger ) {
  280. if ( $trigger['type'] == 'auto_open' ) {
  281. unset( $triggers[ $key ] );
  282. }
  283. }
  284. $settings['triggers'][] = array(
  285. 'type' => 'admin_debug',
  286. );
  287. }
  288. return $settings;
  289. }
  290. /**
  291. * Add various extra global pum_vars js values.
  292. *
  293. * Primarily used to pass form success options for custom integrations and custom code.
  294. *
  295. * @param array $vars
  296. *
  297. * @return array
  298. */
  299. public static function pum_vars( $vars = array() ) {
  300. /**
  301. * If a form was submitted via non-ajax methods this checks if a successful submission was reported.
  302. */
  303. if ( isset( self::$form_success ) && ! empty( self::$form_success['popup_id'] ) ) {
  304. self::$form_success['settings'] = wp_parse_args( self::$form_success['settings'], array(
  305. 'openpopup' => false,
  306. 'openpopup_id' => 0,
  307. 'closepopup' => false,
  308. 'closedelay' => 0,
  309. 'redirect_enabled' => false,
  310. 'redirect' => '',
  311. 'cookie' => false,
  312. ) );
  313. if ( is_array( self::$form_success['settings']['cookie'] ) ) {
  314. self::$form_success['settings']['cookie'] = wp_parse_args( self::$form_success['settings']['cookie'], array(
  315. 'name' => 'pum-' . self::$form_success['popup_id'],
  316. 'expires' => '+1 year',
  317. ) );
  318. }
  319. $vars['form_success'] = self::$form_success;
  320. }
  321. if ( ! empty( self::$form_submission ) ) {
  322. // Remap values from PHP underscore_case to JS camelCase
  323. $vars['form_submission'] = PUM_Utils_Array::remap_keys( self::$form_submission, [
  324. 'form_provider' => 'formProvider',
  325. 'form_id' => 'formId',
  326. 'form_instance_id' => 'formInstanceId',
  327. 'popup_id' => 'popupId',
  328. ] );
  329. }
  330. return $vars;
  331. }
  332. /**
  333. * Returns array of options for a select field to select an integrated form.
  334. *
  335. * @return array
  336. */
  337. public static function get_integrated_forms_selectlist() {
  338. $enabled_form_integrations = PUM_Integrations::get_enabled_form_integrations();
  339. $options = [];
  340. foreach ( $enabled_form_integrations as $integration ) {
  341. switch ( $integration->key ) {
  342. default:
  343. $group_options = [
  344. $integration->key . '_any' => sprintf( __( 'Any %s Form', 'popup-maker' ), $integration->label() ),
  345. ];
  346. foreach ( $integration->get_form_selectlist() as $formId => $formLabel ) {
  347. // ex. ninjaforms_1, contactform7_55
  348. $group_options[ $integration->key . '_' . $formId ] = $formLabel;
  349. }
  350. $options[ $integration->label() ] = $group_options;
  351. break;
  352. }
  353. }
  354. return $options;
  355. }
  356. }