Açıklama Yok

Theme.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Model_Theme
  10. *
  11. * @since 1.8
  12. */
  13. class PUM_Model_Theme extends PUM_Abstract_Model_Post {
  14. /** @var string */
  15. protected $required_post_type = 'popup_theme';
  16. /** @var array */
  17. public $settings;
  18. /** @var bool */
  19. public $doing_passive_migration = false;
  20. /**
  21. * The current model version.
  22. *
  23. * 1 - v1.0.0
  24. * 2 - v1.3.0
  25. * 3 - v1.8.0
  26. *
  27. * @var int
  28. */
  29. public $model_version = 3;
  30. /**
  31. * The version of the data currently stored for the current item.
  32. *
  33. * 1 - v1.0.0
  34. * 2 - v1.3.0
  35. * 3 - v1.8.0
  36. *
  37. * @var int
  38. */
  39. public $data_version;
  40. /**
  41. * Returns array of all theme settings.
  42. *
  43. * @return array
  44. */
  45. public function get_settings() {
  46. $this->settings = $this->get_meta( 'popup_theme_settings' );
  47. if ( ! is_array( $this->settings ) ) {
  48. $this->settings = array();
  49. }
  50. return apply_filters( 'pum_theme_settings', $this->settings, $this->ID );
  51. }
  52. /**
  53. * Returns a specific theme setting with optional default value when not found.
  54. *
  55. * @param $key
  56. * @param bool $default
  57. *
  58. * @return bool|mixed
  59. */
  60. public function get_setting( $key, $default = false ) {
  61. $settings = $this->get_settings();
  62. return isset( $settings[ $key ] ) ? $settings[ $key ] : $default;
  63. }
  64. /**
  65. * @param string $key
  66. * @param mixed $value
  67. *
  68. * @return bool|int
  69. */
  70. public function update_setting( $key, $value ) {
  71. $settings = $this->get_settings();
  72. $settings[ $key ] = $value;
  73. return $this->update_meta( 'popup_theme_settings', $settings );
  74. }
  75. /**
  76. * @param array $merge_settings
  77. *
  78. * @return bool|int
  79. */
  80. public function update_settings( $merge_settings = array() ) {
  81. $settings = $this->get_settings();
  82. foreach ( $merge_settings as $key => $value ) {
  83. $settings[ $key ] = $value;
  84. }
  85. return $this->update_meta( 'popup_theme_settings', $settings );
  86. }
  87. /**
  88. * Returns array of all google font variations used for this theme.
  89. *
  90. * @return array
  91. */
  92. public function get_google_fonts_used() {
  93. $fonts_used = array();
  94. $settings = $this->get_settings();
  95. $google_fonts = PUM_Integration_GoogleFonts::fetch_fonts();
  96. if ( ! empty( $settings['title_font_family'] ) && is_string( $settings['title_font_family'] ) && array_key_exists( $settings['title_font_family'], $google_fonts ) ) {
  97. $variant = ! empty( $settings['title_font_weight'] ) && $settings['title_font_weight'] != 'normal' ? $settings['title_font_weight'] : '';
  98. if ( isset( $settings['title_font_style'] ) && $settings['title_font_style'] == 'italic' ) {
  99. $variant .= 'italic';
  100. }
  101. $fonts_used[ $settings['title_font_family'] ][ $variant ] = $variant;
  102. }
  103. if ( ! empty( $settings['content_font_family'] ) && is_string( $settings['content_font_family'] ) && array_key_exists( $settings['content_font_family'], $google_fonts ) ) {
  104. $variant = ! empty( $settings['content_font_weight'] ) && $settings['content_font_weight'] != 'normal' ? $settings['content_font_weight'] : '';
  105. if ( isset( $settings['content_font_style'] ) && $settings['content_font_style'] == 'italic' ) {
  106. $variant .= 'italic';
  107. }
  108. $fonts_used[ $settings['content_font_family'] ][ $variant ] = $variant;
  109. }
  110. if ( ! empty( $settings['close_font_family'] ) && is_string( $settings['close_font_family'] ) && array_key_exists( $settings['close_font_family'], $google_fonts ) ) {
  111. $variant = ! empty( $settings['close_font_weight'] ) && $settings['close_font_weight'] != 'normal' ? $settings['close_font_weight'] : '';
  112. if ( isset( $settings['close_font_style'] ) && $settings['close_font_style'] == 'italic' ) {
  113. $variant .= 'italic';
  114. }
  115. $fonts_used[ $settings['close_font_family'] ][ $variant ] = $variant;
  116. }
  117. return $fonts_used;
  118. }
  119. /**
  120. * @return array
  121. */
  122. public function get_generated_styles() {
  123. $styles = array(
  124. 'overlay' => array(),
  125. 'container' => array(),
  126. 'title' => array(),
  127. 'content' => array(),
  128. 'close' => array(),
  129. );
  130. /*
  131. * Overlay Styles
  132. */
  133. if ( $this->get_setting( 'overlay_background_color' ) ) {
  134. $styles['overlay']['background-color'] = PUM_Utils_CSS::hex2rgba( $this->get_setting( 'overlay_background_color' ), $this->get_setting( 'overlay_background_opacity' ) );
  135. }
  136. /*
  137. * Container Styles
  138. */
  139. $styles['container'] = array(
  140. 'padding' => "{$this->get_setting('container_padding')}px",
  141. 'border-radius' => "{$this->get_setting('container_border_radius')}px",
  142. 'border' => PUM_Utils_CSS::border_style( $this->get_setting( 'container_border_width' ), $this->get_setting( 'container_border_style' ), $this->get_setting( 'container_border_color' ) ),
  143. 'box-shadow' => PUM_Utils_CSS::box_shadow_style( $this->get_setting( 'container_boxshadow_horizontal' ), $this->get_setting( 'container_boxshadow_vertical' ), $this->get_setting( 'container_boxshadow_blur' ), $this->get_setting( 'container_boxshadow_spread' ), $this->get_setting( 'container_boxshadow_color' ), $this->get_setting( 'container_boxshadow_opacity' ), $this->get_setting( 'container_boxshadow_inset' ) ),
  144. );
  145. if ( $this->get_setting( 'container_background_color' ) ) {
  146. $styles['container']['background-color'] = PUM_Utils_CSS::hex2rgba( $this->get_setting( 'container_background_color' ), $this->get_setting( 'container_background_opacity' ) );
  147. }
  148. /*
  149. * Title Styles
  150. */
  151. $styles['title'] = array(
  152. 'color' => $this->get_setting( 'title_font_color' ),
  153. 'text-align' => $this->get_setting( 'title_text_align' ),
  154. 'text-shadow' => PUM_Utils_CSS::text_shadow_style( $this->get_setting( 'title_textshadow_horizontal' ), $this->get_setting( 'title_textshadow_vertical' ), $this->get_setting( 'title_textshadow_blur' ), $this->get_setting( 'title_textshadow_color' ), $this->get_setting( 'title_textshadow_opacity' ) ),
  155. 'font-family' => $this->get_setting( 'title_font_family' ),
  156. 'font-weight' => $this->get_setting( 'title_font_weight' ),
  157. 'font-size' => "{$this->get_setting( 'title_font_size' )}px",
  158. 'font-style' => $this->get_setting( 'title_font_style' ),
  159. 'line-height' => "{$this->get_setting( 'title_line_height' )}px",
  160. );
  161. /*
  162. * Content Styles
  163. */
  164. $styles['content'] = array(
  165. 'color' => $this->get_setting( 'content_font_color' ),
  166. 'font-family' => $this->get_setting( 'content_font_family' ),
  167. 'font-weight' => $this->get_setting( 'content_font_weight' ),
  168. 'font-style' => $this->get_setting( 'content_font_style' ),
  169. );
  170. /*
  171. * Close Styles
  172. */
  173. $styles['close'] = array(
  174. 'position' => $this->get_setting( 'close_position_outside' ) ? 'fixed' : 'absolute',
  175. 'height' => ! $this->get_setting( 'close_height' ) || $this->get_setting( 'close_height' ) <= 0 ? 'auto' : "{$this->get_setting('close_height')}px",
  176. 'width' => ! $this->get_setting( 'close_width' ) || $this->get_setting( 'close_width' ) <= 0 ? 'auto' : "{$this->get_setting('close_width')}px",
  177. 'left' => 'auto',
  178. 'right' => 'auto',
  179. 'bottom' => 'auto',
  180. 'top' => 'auto',
  181. 'padding' => "{$this->get_setting('close_padding')}px",
  182. 'color' => $this->get_setting( 'close_font_color' ),
  183. 'font-family' => $this->get_setting( 'close_font_family' ),
  184. 'font-weight' => $this->get_setting( 'close_font_weight' ),
  185. 'font-size' => "{$this->get_setting('close_font_size')}px",
  186. 'font-style' => $this->get_setting( 'close_font_style' ),
  187. 'line-height' => "{$this->get_setting('close_line_height')}px",
  188. 'border' => PUM_Utils_CSS::border_style( $this->get_setting( 'close_border_width' ), $this->get_setting( 'close_border_style' ), $this->get_setting( 'close_border_color' ) ),
  189. 'border-radius' => "{$this->get_setting('close_border_radius')}px",
  190. 'box-shadow' => PUM_Utils_CSS::box_shadow_style( $this->get_setting( 'close_boxshadow_horizontal' ), $this->get_setting( 'close_boxshadow_vertical' ), $this->get_setting( 'close_boxshadow_blur' ), $this->get_setting( 'close_boxshadow_spread' ), $this->get_setting( 'close_boxshadow_color' ), $this->get_setting( 'close_boxshadow_opacity' ), $this->get_setting( 'close_boxshadow_inset' ) ),
  191. 'text-shadow' => PUM_Utils_CSS::text_shadow_style( $this->get_setting( 'close_textshadow_horizontal' ), $this->get_setting( 'close_textshadow_vertical' ), $this->get_setting( 'close_textshadow_blur' ), $this->get_setting( 'close_textshadow_color' ), $this->get_setting( 'close_textshadow_opacity' ) ),
  192. );
  193. if ( $this->get_setting( 'close_background_color' ) ) {
  194. $styles['close']['background-color'] = PUM_Utils_CSS::hex2rgba( $this->get_setting( 'close_background_color' ), $this->get_setting( 'close_background_opacity' ) );
  195. }
  196. $top = "{$this->get_setting('close_position_top')}px";
  197. $left = "{$this->get_setting('close_position_left')}px";
  198. $right = "{$this->get_setting('close_position_right')}px";
  199. $bottom = "{$this->get_setting('close_position_bottom')}px";
  200. switch ( $this->get_setting( 'close_location' ) ) {
  201. case "topleft":
  202. $styles['close']['top'] = $top;
  203. $styles['close']['left'] = $left;
  204. break;
  205. case "topcenter":
  206. $styles['close']['top'] = $top;
  207. $styles['close']['left'] = "50%";
  208. $styles['close']['transform'] = "translateX(-50%)";
  209. break;
  210. case "topright":
  211. $styles['close']['top'] = $top;
  212. $styles['close']['right'] = $right;
  213. break;
  214. case 'middleleft':
  215. $styles['close']['top'] = "50%";
  216. $styles['close']['left'] = $left;
  217. $styles['close']['transform'] = "translate(0, -50%)";
  218. break;
  219. case 'middleright':
  220. $styles['close']['top'] = "50%";
  221. $styles['close']['right'] = $right;
  222. $styles['close']['transform'] = "translate(0, -50%)";
  223. break;
  224. case "bottomleft":
  225. $styles['close']['bottom'] = $bottom;
  226. $styles['close']['left'] = $left;
  227. break;
  228. case "bottomcenter":
  229. $styles['close']['bottom'] = $bottom;
  230. $styles['close']['left'] = "50%";
  231. $styles['close']['transform'] = "translateX(-50%)";
  232. break;
  233. case "bottomright":
  234. $styles['close']['bottom'] = $bottom;
  235. $styles['close']['right'] = $right;
  236. break;
  237. }
  238. /** @deprecated 1.8.0 filter */
  239. $styles = (array) apply_filters( 'popmake_generate_theme_styles', (array) $styles, $this->ID, $this->get_deprecated_settings() );
  240. return (array) apply_filters( 'pum_theme_get_generated_styles', (array) $styles, $this->ID );
  241. }
  242. public function get_deprecated_settings() {
  243. return array(
  244. 'overlay' => $this->_dep_get_settings_group( 'overlay' ),
  245. 'container' => $this->_dep_get_settings_group( 'container' ),
  246. 'title' => $this->_dep_get_settings_group( 'title' ),
  247. 'content' => $this->_dep_get_settings_group( 'content' ),
  248. 'close' => $this->_dep_get_settings_group( 'close' ),
  249. );
  250. }
  251. /**
  252. * Retrieve settings in the form of deprecated grouped arrays.
  253. *
  254. * @param $group
  255. * @param null $key
  256. *
  257. * @return mixed
  258. */
  259. public function _dep_get_settings_group( $group, $key = null ) {
  260. if ( ! isset( $this->$group ) ) {
  261. /**
  262. * Remap old meta settings to new settings location for v1.7. This acts as a passive migration when needed.
  263. */
  264. $remapped_keys = $this->remapped_meta_settings_keys( $group );
  265. // This will only return data from extensions as core data has been migrated already.
  266. $group_values = $this->get_meta( "popup_theme_$group" );
  267. if ( ! $group_values || ! is_array( $group_values ) ) {
  268. $group_values = array();
  269. }
  270. // Data manipulation begins here. We don't want any of this saved, only returned for backward compatibility.
  271. foreach ( $remapped_keys as $old_key => $new_key ) {
  272. $group_values[ $old_key ] = $this->get_setting( $new_key );
  273. }
  274. $deprecated_values = pum_get_theme_v1_meta( $group, $this->ID );
  275. if ( ! empty( $deprecated_values ) ) {
  276. foreach ( $deprecated_values as $old_key => $value ) {
  277. if ( ! isset( $group_values[ $old_key ] ) ) {
  278. $group_values[ $old_key ] = $value;
  279. }
  280. }
  281. }
  282. $this->$group = $group_values;
  283. }
  284. $values = apply_filters( "pum_theme_get_$group", $this->$group, $this->ID );
  285. if ( ! $key ) {
  286. return $values;
  287. }
  288. $value = isset ( $values[ $key ] ) ? $values[ $key ] : null;
  289. if ( ! isset( $value ) ) {
  290. $value = $this->get_meta( "popup_theme_{$group}_{$key}" );
  291. }
  292. return apply_filters( "pum_theme_get_{$group}_" . $key, $value, $this->ID );
  293. }
  294. /**
  295. * @param $group
  296. *
  297. * @return array|mixed
  298. */
  299. public function remapped_meta_settings_keys( $group ) {
  300. $remapped_meta_settings_keys = array(
  301. 'overlay' => array(
  302. 'background_color' => 'overlay_background_color',
  303. 'background_opacity' => 'overlay_background_opacity',
  304. ),
  305. 'container' => array(
  306. 'padding' => 'container_padding',
  307. 'background_color' => 'container_background_color',
  308. 'background_opacity' => 'container_background_opacity',
  309. 'border_style' => 'container_border_style',
  310. 'border_color' => 'container_border_color',
  311. 'border_width' => 'container_border_width',
  312. 'border_radius' => 'container_border_radius',
  313. 'boxshadow_inset' => 'container_boxshadow_inset',
  314. 'boxshadow_horizontal' => 'container_boxshadow_horizontal',
  315. 'boxshadow_vertical' => 'container_boxshadow_vertical',
  316. 'boxshadow_blur' => 'container_boxshadow_blur',
  317. 'boxshadow_spread' => 'container_boxshadow_spread',
  318. 'boxshadow_color' => 'container_boxshadow_color',
  319. 'boxshadow_opacity' => 'container_boxshadow_opacity',
  320. ),
  321. 'title' => array(
  322. 'font_color' => 'title_font_color',
  323. 'line_height' => 'title_line_height',
  324. 'font_size' => 'title_font_size',
  325. 'font_family' => 'title_font_family',
  326. 'font_weight' => 'title_font_weight',
  327. 'font_style' => 'title_font_style',
  328. 'text_align' => 'title_text_align',
  329. 'textshadow_horizontal' => 'title_textshadow_horizontal',
  330. 'textshadow_vertical' => 'title_textshadow_vertical',
  331. 'textshadow_blur' => 'title_textshadow_blur',
  332. 'textshadow_color' => 'title_textshadow_color',
  333. 'textshadow_opacity' => 'title_textshadow_opacity',
  334. ),
  335. 'content' => array(
  336. 'font_color' => 'content_font_color',
  337. 'font_family' => 'content_font_family',
  338. 'font_weight' => 'content_font_weight',
  339. 'font_style' => 'content_font_style',
  340. ),
  341. 'close' => array(
  342. 'text' => 'close_text',
  343. 'location' => 'close_location',
  344. 'position_top' => 'close_position_top',
  345. 'position_left' => 'close_position_left',
  346. 'position_bottom' => 'close_position_bottom',
  347. 'position_right' => 'close_position_right',
  348. 'padding' => 'close_padding',
  349. 'height' => 'close_height',
  350. 'width' => 'close_width',
  351. 'background_color' => 'close_background_color',
  352. 'background_opacity' => 'close_background_opacity',
  353. 'font_color' => 'close_font_color',
  354. 'line_height' => 'close_line_height',
  355. 'font_size' => 'close_font_size',
  356. 'font_family' => 'close_font_family',
  357. 'font_weight' => 'close_font_weight',
  358. 'font_style' => 'close_font_style',
  359. 'border_style' => 'close_border_style',
  360. 'border_color' => 'close_border_color',
  361. 'border_width' => 'close_border_width',
  362. 'border_radius' => 'close_border_radius',
  363. 'boxshadow_inset' => 'close_boxshadow_inset',
  364. 'boxshadow_horizontal' => 'close_boxshadow_horizontal',
  365. 'boxshadow_vertical' => 'close_boxshadow_vertical',
  366. 'boxshadow_blur' => 'close_boxshadow_blur',
  367. 'boxshadow_spread' => 'close_boxshadow_spread',
  368. 'boxshadow_color' => 'close_boxshadow_color',
  369. 'boxshadow_opacity' => 'close_boxshadow_opacity',
  370. 'textshadow_horizontal' => 'close_textshadow_horizontal',
  371. 'textshadow_vertical' => 'close_textshadow_vertical',
  372. 'textshadow_blur' => 'close_textshadow_blur',
  373. 'textshadow_color' => 'close_textshadow_color',
  374. 'textshadow_opacity' => 'close_textshadow_opacity',
  375. ),
  376. );
  377. return isset( $remapped_meta_settings_keys[ $group ] ) ? $remapped_meta_settings_keys[ $group ] : array();
  378. }
  379. /**
  380. * @param WP_Post $post
  381. */
  382. public function setup( $post ) {
  383. parent::setup( $post );
  384. if ( ! $this->is_valid() ) {
  385. return;
  386. }
  387. if ( $this->ID === 5 ) {
  388. $test = '1';
  389. }
  390. if ( ! isset( $this->data_version ) ) {
  391. $this->data_version = (int) $this->get_meta( 'popup_theme_data_version' );
  392. if ( ! $this->data_version ) {
  393. $theme_overlay_v1 = $this->get_meta( 'popup_theme_overlay_background_color' );
  394. $theme_overlay_v2 = $this->get_meta( 'popup_theme_overlay' );
  395. // If there are existing settings set the data version to 1/2 so they can be updated.
  396. // Otherwise set to the current version as this is a new popup.
  397. if ( ! empty( $theme_overlay_v1 ) ) {
  398. $this->data_version = 1;
  399. } else if ( ! empty( $theme_overlay_v2 ) && is_array( $theme_overlay_v2 ) ) {
  400. $this->data_version = 2;
  401. } else {
  402. $this->data_version = $this->model_version;
  403. }
  404. $this->update_meta( 'popup_theme_data_version', $this->data_version );
  405. }
  406. }
  407. if ( $this->data_version < $this->model_version && pum_passive_theme_upgrades_enabled() ) {
  408. /**
  409. * Process passive settings migration as each popup is loaded. The will only run each migration routine once for each popup.
  410. */
  411. $this->passive_migration();
  412. }
  413. }
  414. /**
  415. * Allows for passive migration routines based on the current data version.
  416. */
  417. public function passive_migration() {
  418. $this->doing_passive_migration = true;
  419. for ( $i = $this->data_version; $this->data_version < $this->model_version; $i ++ ) {
  420. // Process migration for current version. ex. current version is 2, runs pum_theme_passive_migration_2.
  421. do_action_ref_array( 'pum_theme_passive_migration_' . $this->data_version, array( &$this ) );
  422. $this->data_version ++;
  423. /**
  424. * Update the themes data version.
  425. */
  426. $this->update_meta( 'popup_theme_data_version', $this->data_version );
  427. }
  428. do_action_ref_array( 'pum_theme_passive_migration', array( &$this, $this->data_version ) );
  429. $this->doing_passive_migration = false;
  430. }
  431. }