PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/df_home/static/test/portalbkd/wp-content/plugins/wordpress-popup/inc/class-popup-public.php

https://gitlab.com/darmawan.fatria/df-skp-2014
PHP | 407 lines | 182 code | 61 blank | 164 comment | 25 complexity | 56f5476e646b19f6df4fc39837efba1d MD5 | raw file
  1. <?php
  2. // Load dependencies.
  3. require_once PO_INC_DIR . 'class-popup-base.php';
  4. /**
  5. * Defines the popup class for front end pages
  6. *
  7. * @since 4.6
  8. */
  9. class IncPopup extends IncPopupBase {
  10. /**
  11. * Data added to the page via wp_localize_script()
  12. * @var array
  13. */
  14. protected $script_data = array();
  15. /**
  16. * Lists all popups that have been enqueued via the footer loading method.
  17. * @var array
  18. */
  19. protected $enqueued = array();
  20. /**
  21. * Returns the singleton instance of the popup (front end) class.
  22. *
  23. * @since 4.6
  24. */
  25. static public function instance() {
  26. static $Inst = null;
  27. // In theory the "public" class does not need this, but to avoid
  28. // unexpected problems we initialize only after current user is known...
  29. if ( ! did_action( 'set_current_user' ) ) {
  30. add_action( 'set_current_user', array( __CLASS__, 'instance' ) );
  31. return null;
  32. }
  33. if ( null === $Inst ) {
  34. $Inst = new IncPopup();
  35. }
  36. return $Inst;
  37. }
  38. /**
  39. * Private constructor (singleton)
  40. *
  41. * @since 4.6
  42. */
  43. protected function __construct() {
  44. parent::__construct();
  45. // Init loading-process of the PopUp.
  46. add_action(
  47. 'wp', // "wp", not "init"!
  48. array( $this, 'init_public' )
  49. );
  50. /**
  51. * Experimental hook to dynamically create popups on your site.
  52. * Call the action hook `wdev-popup` with param 1 being the content and
  53. * param 2 is an optional array of popup options.
  54. *
  55. * Note that currently this plays nice with existing popups if FOOTER
  56. * loading method is used, but all other loading methods will interfere
  57. * with this hook and only show either the dynamic or predefined popups.
  58. *
  59. * @since 4.7.1
  60. * @param string $content The popup contents (HTML code allowed).
  61. * @param array $options Optional. List of popup configuration options.
  62. */
  63. add_action(
  64. 'wdev-popup',
  65. array( $this, 'show_popup' ),
  66. 10, 2
  67. );
  68. }
  69. /**
  70. * Initialize the public part of the plugin on every front-end page:
  71. * Determine how the popup is loaded.
  72. *
  73. * @since 4.6
  74. */
  75. public function init_public() {
  76. // Load plugin settings.
  77. $settings = IncPopupDatabase::get_settings();
  78. // Initialize javascript-data.
  79. $this->script_data['ajaxurl'] = '';
  80. $this->script_data['do'] = 'get_data';
  81. $this->script_data['ajax_data'] = array();
  82. // Find the current loading method.
  83. $cur_method = isset( $settings['loadingmethod'] ) ? $settings['loadingmethod'] : 'ajax';
  84. if ( empty( $cur_method ) ) { $cur_method = 'ajax'; }
  85. if ( isset( $_POST['_po_method_'] ) ) { $cur_method = $_POST['_po_method_']; }
  86. /*
  87. * Apply the specific loading method to include the popup on the page.
  88. * Details to the loading methods are documented in the header comment
  89. * of the "load_method_xyz()" functions.
  90. */
  91. switch ( $cur_method ) {
  92. case 'ajax': // former 'external'
  93. $this->load_method_ajax();
  94. break;
  95. case 'front': // former 'frontloading'
  96. $this->load_method_front();
  97. if ( isset( $_GET['action'] )
  98. && 'inc_popup' == $_GET['action']
  99. ) {
  100. $this->ajax_load_popup();
  101. }
  102. break;
  103. case 'footer':
  104. $this->load_method_footer();
  105. break;
  106. case 'raw': // Set via form field "_po_method_"
  107. $this->load_method_raw();
  108. break;
  109. default:
  110. /**
  111. * Custom loading handler can be processed by an add-on.
  112. */
  113. do_action( 'popup-init-loading-method', $cur_method, $this );
  114. break;
  115. }
  116. }
  117. /**
  118. * Action handler that allows us to add a popup via WordPress hook.
  119. *
  120. * @since 4.7.1
  121. * @param string $contents The PopUp contents.
  122. * @param array $options PopUp options.
  123. */
  124. public function show_popup( $contents, $options = array() ) {
  125. $this->script_data['popup'] = lib2()->array->get( $this->script_data['popup'] );
  126. $popup = new IncPopupItem();
  127. $data = lib2()->array->get( $options );
  128. $data['content'] = $contents;
  129. $popup->populate( $data );
  130. $popup->script_data['manual'] = true;
  131. // 1. Add the popup to the global popup-list.
  132. $this->popups[] = $popup;
  133. // 2. Enqueue the popup in the page footer.
  134. $item = $popup->get_script_data( false );
  135. unset( $item['html'] );
  136. unset( $item['styles'] );
  137. $this->script_data['popup'][] = $item;
  138. $this->load_scripts();
  139. $this->enqueue_footer();
  140. }
  141. /**
  142. * Enqueues the PopUp javascripts and data.
  143. *
  144. * @since 4.6
  145. */
  146. public function load_scripts() {
  147. static $Loaded = false;
  148. if ( ! did_action( 'wp' ) ) {
  149. // We have to make sure that wp is fully initialized:
  150. // Some rules that use filter 'popup-ajax-data' depend on this.
  151. add_action(
  152. 'wp',
  153. array( $this, 'load_scripts' )
  154. );
  155. return;
  156. }
  157. if ( ! $Loaded ) {
  158. if ( is_array( $this->script_data ) && ! empty( $this->script_data ) ) {
  159. $popup_data = apply_filters( 'popup-ajax-data', $this->script_data );
  160. lib2()->ui->data( '_popup_data', $popup_data, 'front' );
  161. $popup_data['popup'] = lib2()->array->get( $popup_data['popup'] );
  162. foreach ( $popup_data['popup'] as $item ) {
  163. $this->enqueued[] = $item['html_id'];
  164. }
  165. }
  166. lib2()->ui->add( PO_JS_URL . 'public.min.js', 'front' );
  167. lib2()->ui->add( PO_CSS_URL . 'animate.min.css', 'front' );
  168. } else {
  169. if ( is_array( $this->script_data ) && is_array( $this->script_data['popup'] ) ) {
  170. foreach ( $this->script_data['popup'] as $popup ) {
  171. if ( in_array( $popup['html_id'], $this->enqueued ) ) {
  172. continue;
  173. }
  174. $script = 'window._popup_data.popup.push(' . json_encode( $popup ) . ')';
  175. lib2()->ui->script( $script );
  176. $this->enqueued[] = $popup['html_id'];
  177. }
  178. }
  179. }
  180. $Loaded = true;
  181. }
  182. /*==================================*\
  183. ======================================
  184. == ==
  185. == LOAD METHODS ==
  186. == ==
  187. ======================================
  188. \*==================================*/
  189. /**
  190. * Load-Method: External
  191. *
  192. * IS AJAX
  193. * IS ADMIN
  194. *
  195. * PopUp data is loaded via a normal WordPress ajax request, directed at
  196. * the admin-ajax.php handler.
  197. *
  198. * @since 4.6
  199. */
  200. protected function load_method_ajax() {
  201. global $pagenow;
  202. if ( ! in_array( $pagenow, array( 'wp-login.php', 'wp-register.php' ) ) ) {
  203. // Data is loaded via a normal WordPress ajax request.
  204. $this->script_data['ajaxurl'] = admin_url( 'admin-ajax.php' );
  205. $this->script_data['ajax_data']['orig_request_uri'] = $_SERVER['REQUEST_URI'];
  206. $this->load_scripts();
  207. }
  208. }
  209. /**
  210. * Load-Method: Front/Frontloading
  211. *
  212. * NOT AJAX
  213. * NOT ADMIN
  214. *
  215. * PopUp data is loaded in an ajax request. The ajax request is directed
  216. * at the same URL that is currently displayed, but a few URL-parameters are
  217. * added to instruct the plugin to return popup-data instead the the normal
  218. * webpage.
  219. *
  220. * @since 4.6
  221. */
  222. protected function load_method_front() {
  223. global $pagenow;
  224. if ( ! in_array( $pagenow, array( 'wp-login.php', 'wp-register.php' ) ) ) {
  225. /*
  226. * Data is loaded via the public URL of the page, simply by adding
  227. * some URL parameters.
  228. */
  229. $this->script_data['ajaxurl'] = '';
  230. $this->script_data['ajax_data']['request_uri'] = $_SERVER['REQUEST_URI'];
  231. $this->load_scripts();
  232. }
  233. }
  234. /**
  235. * Load-Method: Footer
  236. *
  237. * NOT AJAX
  238. * NOT ADMIN
  239. *
  240. * The PopUp styles and html is directly injected into the webpage header
  241. * and footer. The PopUp is ready when the page is loaded. No ajax request
  242. * is made.
  243. *
  244. * @since 4.6
  245. */
  246. protected function load_method_footer() {
  247. /**
  248. * Set up the rquest information from here.
  249. * These values are used by some rules and need to be set manually here
  250. * In an ajax request they would already be defined by the ajax url.
  251. */
  252. $_REQUEST['thereferrer'] = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '';
  253. $_REQUEST['thefrom'] = lib2()->net->current_url();
  254. // Populates $this->popups
  255. $this->select_popup();
  256. if ( empty( $this->popups ) ) { return; }
  257. $data = $this->get_popup_data();
  258. foreach ( $data as $item ) {
  259. if ( ! empty( $item['manual'] ) ) { continue; }
  260. if ( in_array( $item['html_id'], $this->enqueued ) ) { continue; }
  261. unset( $item['html'] );
  262. unset( $item['styles'] );
  263. $this->script_data['popup'][] = $item;
  264. }
  265. $this->load_scripts();
  266. $this->enqueue_footer();
  267. }
  268. /**
  269. * Load-Method: Raw
  270. *
  271. * This is used when a form is submitted inside a PopUp - it means that we
  272. * should only return the contents of the PopUp(s) and not the whole page.
  273. * Set via form field "_po_method_".
  274. *
  275. * @since 4.6.1.2
  276. */
  277. protected function load_method_raw() {
  278. /**
  279. * Set up the rquest information from here.
  280. * These values are used by some rules and need to be set manually here
  281. * In an ajax request they would already be defined by the ajax url.
  282. */
  283. $_REQUEST['thereferrer'] = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '';
  284. $_REQUEST['thefrom'] = lib2()->net->current_url();
  285. // Populates $this->popups
  286. $this->select_popup();
  287. if ( empty( $this->popups ) ) { die(); }
  288. echo '<div>';
  289. $this->show_footer();
  290. echo '</div>';
  291. die();
  292. }
  293. /*======================================*\
  294. ==========================================
  295. == ==
  296. == HELPER FUNCTIONS ==
  297. == ==
  298. ==========================================
  299. \*======================================*/
  300. /**
  301. * Adds the wp_header/wp_footer actions to the action queue.
  302. *
  303. * @since 4.7.1
  304. */
  305. protected function enqueue_footer() {
  306. static $Did_Enqueue = false;
  307. if ( $Did_Enqueue ) { return; }
  308. $Did_Enqueue = true;
  309. add_action(
  310. 'wp_head',
  311. array( $this, 'show_header')
  312. );
  313. add_action(
  314. 'wp_footer',
  315. array( $this, 'show_footer')
  316. );
  317. }
  318. /**
  319. * Used by "load_method_footer" to print the popup CSS styles.
  320. *
  321. * @since 4.6
  322. */
  323. public function show_header() {
  324. if ( empty( $this->popups ) ) { return; }
  325. $code = '';
  326. $data = $this->get_popup_data();
  327. foreach ( $data as $ind => $item ) {
  328. $code .= $item['styles'];
  329. }
  330. echo '<style type="text/css">' . $code . '</style>';
  331. }
  332. /**
  333. * Used by "load_method_footer" to print the popup HTML code.
  334. *
  335. * @since 4.6
  336. */
  337. public function show_footer() {
  338. if ( empty( $this->popups ) ) { return; }
  339. $code = '';
  340. $data = $this->get_popup_data();
  341. foreach ( $data as $ind => $item ) {
  342. $code .= $item['html'];
  343. }
  344. echo $code;
  345. }
  346. };