No Description

UI.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Admin_Shortcode_UI
  10. *
  11. * This class maintains a global set of all registered PUM shortcodes.
  12. *
  13. * @since 1.7.0
  14. */
  15. class PUM_Admin_Shortcode_UI {
  16. private static $initialized = false;
  17. /**
  18. * Here for backward compatibility with 3rd party plugins.
  19. *
  20. * @deprecated 1.7.0
  21. */
  22. public static function instance() {
  23. self::init();
  24. }
  25. public static function init() {
  26. if ( ! self::$initialized ) {
  27. add_action( 'admin_init', array( __CLASS__, 'init_editor' ), 20 );
  28. self::$initialized = true;
  29. }
  30. }
  31. /**
  32. * Initialize the editor button when needed.
  33. */
  34. public static function init_editor() {
  35. /*
  36. * Check if the logged in WordPress User can edit Posts or Pages.
  37. */
  38. if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
  39. return;
  40. }
  41. /*
  42. * Check if the logged in WordPress User has the Visual Editor enabled.
  43. */
  44. if ( get_user_option( 'rich_editing' ) !== 'true' ) {
  45. return;
  46. }
  47. /*
  48. * Check if the shortcode ui disabled.
  49. */
  50. if ( apply_filters( 'pum_disable_shortcode_ui', false ) || pum_get_option( 'disable_shortcode_ui' ) ) {
  51. return;
  52. }
  53. // Add shortcode ui button & js.
  54. add_filter( 'mce_buttons', array( __CLASS__, 'mce_buttons' ) );
  55. add_filter( 'mce_external_plugins', array( __CLASS__, 'mce_external_plugins' ) );
  56. // Add core site styles for form previews.
  57. add_editor_style( Popup_Maker::$URL . 'assets/css/pum-site.min.css' );
  58. // Process live previews.
  59. add_action( 'wp_ajax_pum_do_shortcode', array( __CLASS__, 'do_shortcode' ) );
  60. //add_action( 'wp_ajax_pum_do_shortcode', array( __CLASS__, 'wp_ajax_pum_do_shortcode' ) );
  61. }
  62. /**
  63. * Adds our tinymce button
  64. *
  65. * @param array $buttons
  66. *
  67. * @return array
  68. */
  69. public static function mce_buttons( $buttons ) {
  70. // Enqueue scripts when editor is detected.
  71. if ( ! did_action( 'admin_enqueue_scripts' ) ) {
  72. add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ), 120 ); // 120 because core styles are registered at 100 for some reason.
  73. } else {
  74. self::enqueue_scripts();
  75. }
  76. array_push( $buttons, 'pum_shortcodes' );
  77. return $buttons;
  78. }
  79. /**
  80. * Enqueues needed assets.
  81. */
  82. public static function enqueue_scripts() {
  83. // Register editor styles.
  84. add_editor_style( PUM_Admin_Assets::$css_url . 'pum-admin-editor-styles' . PUM_Admin_Assets::$suffix . '.css' );
  85. wp_enqueue_style( 'pum-admin-shortcode-ui' );
  86. wp_enqueue_script( 'pum-admin-shortcode-ui' );
  87. wp_localize_script( 'pum-admin-shortcode-ui', 'pum_shortcode_ui_vars', apply_filters( 'pum_shortcode_ui_vars', array(
  88. 'nonce' => wp_create_nonce( "pum-shortcode-ui-nonce" ),
  89. 'I10n' => array(
  90. 'insert' => __( 'Insert', 'popup-maker' ),
  91. 'cancel' => __( 'Cancel', 'popup-maker' ),
  92. 'shortcode_ui_button_tooltip' => __( 'Popup Maker Shortcodes', 'popup-maker' ),
  93. 'error_loading_shortcode_preview' => __( 'There was an error in generating the preview', 'popup-maker' ),
  94. ),
  95. 'shortcodes' => self::shortcode_ui_var(),
  96. ) ) );
  97. }
  98. /**
  99. * Generates a json object variable to pass to the Shortcode UI front end.
  100. *
  101. * @return array
  102. */
  103. public static function shortcode_ui_var() {
  104. $type = pum_typenow();
  105. $shortcodes = array();
  106. foreach ( PUM_Shortcodes::instance()->get_shortcodes() as $tag => $shortcode ) {
  107. $post_types = apply_filters( 'pum_shortcode_post_types', $shortcode->post_types(), $shortcode );
  108. /**
  109. * @var $shortcode PUM_Shortcode
  110. */
  111. if ( ! in_array( '*', $post_types ) && ! in_array( $type, $post_types ) ) {
  112. continue;
  113. }
  114. $shortcodes[ $tag ] = array(
  115. 'version' => $shortcode->version,
  116. 'label' => $shortcode->label(),
  117. 'description' => $shortcode->description(),
  118. 'tabs' => $shortcode->_tabs(),
  119. 'sections' => $shortcode->_subtabs(),
  120. 'fields' => $shortcode->_fields(),
  121. 'has_content' => $shortcode->has_content,
  122. 'ajax_rendering' => $shortcode->ajax_rendering === true,
  123. );
  124. }
  125. return $shortcodes;
  126. }
  127. /**
  128. * Adds our tinymce plugin js
  129. *
  130. * @param array $plugin_array
  131. *
  132. * @return array
  133. */
  134. public static function mce_external_plugins( $plugin_array ) {
  135. return array_merge( $plugin_array, array(
  136. 'pum_shortcodes' => add_query_arg( array( 'version'=> Popup_Maker::$VER ), PUM_Admin_Assets::$js_url . 'mce-buttons' . PUM_Admin_Assets::$suffix . '.js' ),
  137. ) );
  138. }
  139. public static function do_shortcode() {
  140. check_ajax_referer( 'pum-shortcode-ui-nonce', 'nonce' );
  141. $tag = ! empty( $_REQUEST['tag'] ) ? sanitize_key( $_REQUEST['tag'] ) : false;
  142. $shortcode = ! empty( $_REQUEST['shortcode'] ) ? stripslashes( sanitize_text_field( $_REQUEST['shortcode'] ) ) : null;
  143. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : null;
  144. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  145. return esc_html__( "You do not have access to preview this post.", 'popup-maker' );
  146. }
  147. /** @var PUM_Shortcode $shortcode */
  148. $shortcode_object = PUM_Shortcodes::instance()->get_shortcode( $tag );
  149. if ( ! defined( 'PUM_DOING_PREVIEW' ) ) {
  150. define( 'PUM_DOING_PREVIEW', true );
  151. }
  152. /**
  153. * Often the global $post is not set yet. Set it in case for proper rendering.
  154. */
  155. if ( ! empty( $post_id ) ) {
  156. global $post;
  157. $post = get_post( $post_id );
  158. setup_postdata( $post );
  159. }
  160. /** @var string $content Rendered shortcode content. */
  161. $content = PUM_Helpers::do_shortcode( $shortcode );
  162. /** If no matching tag or $content wasn't rendered die. */
  163. if ( ! $shortcode_object || $content == $shortcode ) {
  164. wp_send_json_error();
  165. }
  166. /** Generate inline styles when needed. */
  167. $styles = "<style>" . $shortcode_object->get_template_styles() . "</style>";
  168. wp_send_json_success( $styles . $content );
  169. }
  170. }