Nessuna descrizione

Cookies.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Cookies
  10. */
  11. class PUM_Cookies {
  12. /**
  13. * @var PUM_Cookies
  14. */
  15. public static $instance;
  16. /**
  17. * @var bool
  18. */
  19. public $preload_posts = false;
  20. /**
  21. * @var array
  22. */
  23. public $cookies;
  24. /**
  25. *
  26. */
  27. public static function init() {
  28. self::instance();
  29. }
  30. /**
  31. * @return PUM_Cookies
  32. */
  33. public static function instance() {
  34. if ( ! isset( self::$instance ) ) {
  35. self::$instance = new self;
  36. self::$instance->preload_posts = pum_is_popup_editor();
  37. }
  38. return self::$instance;
  39. }
  40. /**
  41. * @param null $cookie
  42. *
  43. * @return mixed|null
  44. */
  45. public function get_cookie( $cookie = null ) {
  46. $cookies = $this->get_cookies();
  47. return isset( $cookies[ $cookie ] ) ? $cookies[ $cookie ] : null;
  48. }
  49. /**
  50. * @return array
  51. */
  52. public function get_cookies() {
  53. if ( ! isset( $this->cookies ) ) {
  54. $this->register_cookies();
  55. }
  56. return $this->cookies;
  57. }
  58. /**
  59. * Registers all known cookies when called.
  60. */
  61. public function register_cookies() {
  62. $cookies = apply_filters( 'pum_registered_cookies', array(
  63. 'on_popup_close' => array(
  64. 'name' => __( 'On Popup Close', 'popup-maker' ),
  65. ),
  66. 'on_popup_open' => array(
  67. 'name' => __( 'On Popup Open', 'popup-maker' ),
  68. ),
  69. 'form_submission' => [
  70. 'name' => __( 'Form Submission', 'popup-maker' ),
  71. 'fields' => array_merge_recursive( $this->cookie_fields(), [
  72. 'general' => [
  73. 'form' => [
  74. 'type' => 'select',
  75. 'label' => __( 'Form', 'popup-maker' ),
  76. 'options' => $this->preload_posts ? array_merge( [
  77. 'any' => __( 'Any Supported Form*', 'popup-maker' ),
  78. __( 'Popup Maker', 'popup-maker' ) => [
  79. 'pumsubform' => __( 'Subscription Form', 'popup-maker' ),
  80. ],
  81. ], PUM_Integrations::get_integrated_forms_selectlist() ) : array(),
  82. 'pri' => - 1,
  83. 'std' => 'any',
  84. ],
  85. 'only_in_popup' => [
  86. 'type' => 'checkbox',
  87. 'label' => __( 'Only in this popup', 'popup-maker' ),
  88. 'std' => '1',
  89. ],
  90. ],
  91. ] ),
  92. ],
  93. 'pum_sub_form_success' => array(
  94. 'name' => __( 'Subscription Form: Successful', 'popup-maker' ),
  95. ),
  96. 'pum_sub_form_already_subscribed' => array(
  97. 'name' => __( 'Subscription Form: Already Subscribed', 'popup-maker' ),
  98. ),
  99. 'manual' => array(
  100. 'name' => __( 'Manual', 'popup-maker' ),
  101. 'settings_column' => '<pre class="manual-cookie-shortcode"><code>[popup_cookie name="{{data.name}}" expires="{{data.time}}" sitewide="{{data.path ? 1 : 0}}"]</code></pre>',
  102. ),
  103. ) );
  104. // @deprecated filter.
  105. $cookies = apply_filters( 'pum_get_cookies', $cookies );
  106. $this->add_cookies( $cookies );
  107. }
  108. /**
  109. * @param array $cookies
  110. */
  111. public function add_cookies( $cookies = array() ) {
  112. foreach ( $cookies as $key => $cookie ) {
  113. if ( empty( $cookie['id'] ) && ! is_numeric( $key ) ) {
  114. $cookie['id'] = $key;
  115. }
  116. $this->add_cookie( $cookie );
  117. }
  118. }
  119. /**
  120. * @param null $cookie
  121. */
  122. public function add_cookie( $cookie = null ) {
  123. if ( ! empty( $cookie['id'] ) && ! isset ( $this->cookies[ $cookie['id'] ] ) ) {
  124. $cookie = wp_parse_args( $cookie, array(
  125. 'id' => '',
  126. 'name' => '',
  127. 'modal_title' => __( 'Cookie Settings', 'popup-maker' ),
  128. 'settings_column' => sprintf( '%s%s%s', '<# if (typeof data.session === "undefined" || data.session !== "1") { print(data.time); } else { print("', __( 'Sessions', 'popup-maker' ), '"); } #>' ),
  129. 'priority' => 10,
  130. 'tabs' => $this->get_tabs(),
  131. 'fields' => $this->cookie_fields(),
  132. ) );
  133. // Here for backward compatibility to merge in labels properly.
  134. if ( ! empty( $cookie['labels'] ) ) {
  135. foreach ( $cookie['labels'] as $key => $value ) {
  136. $cookie[ $key ] = $value;
  137. unset( $cookie['labels'][ $key ] );
  138. }
  139. unset( $cookie['labels'] );
  140. }
  141. // Add cookie fields for all cookies automatically.
  142. if ( empty( $cookie['fields'] ) ) {
  143. $cookie['fields'] = $this->cookie_fields();
  144. }
  145. $cookie['fields'] = PUM_Admin_Helpers::parse_tab_fields( $cookie['fields'], array(
  146. 'has_subtabs' => false,
  147. 'name' => '%s',
  148. ) );
  149. $this->cookies[ $cookie['id'] ] = $cookie;
  150. }
  151. return;
  152. }
  153. /**
  154. * Returns an array of section labels for all triggers.
  155. *
  156. * Use the filter pum_get_trigger_section_labels to add or modify labels.
  157. *
  158. * @return array
  159. */
  160. public function get_tabs() {
  161. /**
  162. * Filter the array of trigger section labels.
  163. *
  164. * @param array $to_do The list of trigger section labels.
  165. */
  166. return apply_filters( 'pum_get_trigger_tabs', array(
  167. 'general' => __( 'General', 'popup-maker' ),
  168. 'advanced' => __( 'Advanced', 'popup-maker' ),
  169. ) );
  170. }
  171. /**
  172. * Returns the cookie fields used for cookie options.
  173. *
  174. * @return array
  175. *
  176. * @uses filter pum_get_cookie_fields
  177. *
  178. */
  179. public function cookie_fields() {
  180. return apply_filters( 'pum_get_cookie_fields', array(
  181. 'general' => array(
  182. 'name' => array(
  183. 'label' => __( 'Cookie Name', 'popup-maker' ),
  184. 'placeholder' => __( 'Cookie Name ex. popmaker-123', 'popup-maker' ),
  185. 'desc' => __( 'The name that will be used when checking for or saving this cookie.', 'popup-maker' ),
  186. 'std' => '',
  187. 'priority' => 1,
  188. ),
  189. 'time' => array(
  190. 'label' => __( 'Cookie Time', 'popup-maker' ),
  191. 'placeholder' => __( '364 days 23 hours 59 minutes 59 seconds', 'popup-maker' ),
  192. 'desc' => __( 'Enter a plain english time before cookie expires.', 'popup-maker' ),
  193. 'std' => '1 month',
  194. 'priority' => 2,
  195. ),
  196. ),
  197. 'advanced' => array(
  198. 'session' => array(
  199. 'label' => __( 'Use Session Cookie?', 'popup-maker' ),
  200. 'desc' => __( 'Session cookies expire when the user closes their browser.', 'popup-maker' ) . ' ' . sprintf( __( '%sNote%s: Modern browsers that reopen your last browser session\'s tabs do not properly clear session cookies', 'popup-maker' ), '<strong>', '</strong>' ),
  201. 'type' => 'checkbox',
  202. 'std' => false,
  203. 'priority' => 1,
  204. ),
  205. 'path' => array(
  206. 'label' => __( 'Sitewide Cookie', 'popup-maker' ),
  207. 'desc' => __( 'This will prevent the popup from triggering on all pages until the cookie expires.', 'popup-maker' ),
  208. 'type' => 'checkbox',
  209. 'std' => true,
  210. 'priority' => 2,
  211. ),
  212. 'key' => array(
  213. 'label' => __( 'Cookie Key', 'popup-maker' ),
  214. 'desc' => __( 'Changing this will cause all existing cookies to be invalid.', 'popup-maker' ),
  215. 'type' => 'cookie_key',
  216. 'std' => '',
  217. 'priority' => 3,
  218. ),
  219. ),
  220. ) );
  221. }
  222. /**
  223. * @return array
  224. */
  225. public function get_labels() {
  226. static $labels;
  227. if ( ! isset( $labels ) ) {
  228. /**
  229. * Filter the array of cookie labels.
  230. *
  231. * @param array $to_do The list of cookie labels.
  232. */
  233. $labels = apply_filters( 'pum_get_cookie_labels', array() );
  234. }
  235. return $labels;
  236. }
  237. /**
  238. * @param null $cookie
  239. * @param array $settings
  240. *
  241. * @return array
  242. * @deprecated
  243. *
  244. */
  245. public function validate_cookie( $cookie = null, $settings = array() ) {
  246. return $settings;
  247. }
  248. /**
  249. * @return array
  250. */
  251. public function dropdown_list() {
  252. $_cookies = $this->get_cookies();
  253. $cookies = array();
  254. foreach ( $_cookies as $id => $cookie ) {
  255. $cookies[ $id ] = $cookie['name'];
  256. }
  257. return $cookies;
  258. }
  259. }