Nessuna descrizione

Previews.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Previews
  10. *
  11. * This class sets up the necessary changes to allow admins & editors to preview popups on the front end.
  12. */
  13. class PUM_Previews {
  14. /**
  15. * Initiator method.
  16. */
  17. public static function init() {
  18. // add_filter( 'template_include', array( __CLASS__, 'template_include' ), 1000, 2 );
  19. add_filter( 'pum_popup_is_loadable', array( __CLASS__, 'is_loadable' ), 1000, 2 );
  20. add_filter( 'pum_popup_data_attr', array( __CLASS__, 'data_attr' ), 1000, 2 );
  21. add_filter( 'pum_popup_get_public_settings', array( __CLASS__, 'get_public_settings' ), 1000, 2 );
  22. }
  23. /**
  24. * This changes the template to a blank one to prevent duplicate content issues.
  25. *
  26. * @param $template
  27. *
  28. * @return string
  29. */
  30. public static function template_include( $template ) {
  31. if ( ! is_singular( 'popup' ) ) {
  32. return $template;
  33. }
  34. return POPMAKE_DIR . 'templates/single-popup.php';
  35. }
  36. /**
  37. * For popup previews this will force only the correct popup to load.
  38. *
  39. * @param bool $loadable
  40. * @param int $popup_id
  41. *
  42. * @return bool
  43. */
  44. public static function is_loadable( $loadable, $popup_id ) {
  45. return self::should_preview_popup( $popup_id ) ? true : $loadable;
  46. }
  47. /**
  48. * Sets the Popup Post Type public arg to true for content editors.
  49. *
  50. * This enables them to use the built in preview links.
  51. *
  52. * @param int $popup_id
  53. *
  54. * @return bool
  55. */
  56. public static function should_preview_popup( $popup_id = 0 ) {
  57. if ( defined( "DOING_AJAX" ) && DOING_AJAX ) {
  58. return false;
  59. }
  60. if ( isset( $_GET['popup_preview'] ) && $_GET['popup_preview'] && isset( $_GET['popup'] ) ) {
  61. static $popup;
  62. if ( ! isset( $popup ) ) {
  63. if ( is_numeric( $_GET['popup'] ) && absint( $_GET['popup'] ) > 0 ) {
  64. $popup = absint( $_GET['popup'] );
  65. } else {
  66. $post = get_page_by_path( sanitize_text_field( $_GET['popup'] ), OBJECT, 'popup' );
  67. $popup = $post->ID;
  68. }
  69. }
  70. if ( $popup_id == $popup && current_user_can( 'edit_post', $popup ) ) {
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. /**
  77. * On popup previews add an admin debug trigger.
  78. *
  79. * @param $data_attr
  80. * @param $popup_id
  81. *
  82. * @return mixed
  83. */
  84. public static function data_attr( $data_attr, $popup_id ) {
  85. if ( ! self::should_preview_popup( $popup_id ) ) {
  86. return $data_attr;
  87. }
  88. $data_attr['triggers'] = array(
  89. array(
  90. 'type' => 'admin_debug',
  91. ),
  92. );
  93. return $data_attr;
  94. }
  95. /**
  96. * On popup previews add an admin debug trigger.
  97. *
  98. * @param array $settings
  99. * @param PUM_Model_Popup $popup
  100. *
  101. * @return array
  102. */
  103. public static function get_public_settings( $settings, $popup ) {
  104. if ( ! self::should_preview_popup( $popup->ID ) ) {
  105. return $settings;
  106. }
  107. $settings['triggers'] = array(
  108. array(
  109. 'type' => 'admin_debug',
  110. ),
  111. );
  112. return $settings;
  113. }
  114. }