non-sshare-styles
1 month ago
class-hustle-decorator-non-sshare.php
3 months ago
class-hustle-decorator-sshare.php
3 months ago
class-hustle-decorator_abstract.php
3 years ago
class-hustle-module-preview.php
1 year ago
hustle-module-front-ajax.php
1 month ago
hustle-module-front.php
1 month ago
hustle-module-inline-style-queue.php
3 months ago
hustle-module-renderer.php
1 month ago
hustle-renderer-abstract.php
3 months ago
hustle-renderer-sshare.php
5 months ago
class-hustle-module-preview.php
158 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Module's preview page handler. |
| 4 | * |
| 5 | * @package Hustle |
| 6 | * @since 4.3.1 |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Hustle_Module_Preview class. |
| 11 | * |
| 12 | * @since 4.3.1 |
| 13 | */ |
| 14 | class Hustle_Module_Preview { |
| 15 | |
| 16 | /** |
| 17 | * Hustle_Module_Preview constructor. |
| 18 | * |
| 19 | * @since 4.3.1 |
| 20 | */ |
| 21 | public function __construct() { |
| 22 | add_action( 'wp_enqueue_scripts', array( $this, 'register_preview_scripts' ) ); |
| 23 | |
| 24 | add_action( 'wp_footer', array( $this, 'wp_footer' ) ); |
| 25 | |
| 26 | add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); |
| 27 | |
| 28 | add_filter( 'the_title', array( $this, 'the_title' ) ); |
| 29 | |
| 30 | if ( 'posts' === get_option( 'show_on_front' ) ) { |
| 31 | add_filter( 'the_excerpt', array( $this, 'show_after_page_post_content' ) ); |
| 32 | } else { |
| 33 | add_filter( 'the_content', array( $this, 'show_after_page_post_content' ) ); |
| 34 | } |
| 35 | |
| 36 | // With a priority of 20 to override possible WC's filter. |
| 37 | add_filter( 'show_admin_bar', '__return_false', 20 ); |
| 38 | |
| 39 | // Remove WordPress emoji - it generates JS error in Mozilla https://core.trac.wordpress.org/ticket/53529 . |
| 40 | remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); |
| 41 | remove_action( 'wp_print_styles', 'print_emoji_styles' ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Registers the script used in the preview iframe. |
| 46 | * |
| 47 | * @since 4.3.1 |
| 48 | */ |
| 49 | public function register_preview_scripts() { |
| 50 | Hustle_Module_Front::add_hui_scripts(); |
| 51 | |
| 52 | wp_register_script( |
| 53 | 'hustle_preview_script', |
| 54 | Opt_In::$plugin_url . 'assets/js/preview.min.js', |
| 55 | array( 'jquery' ), |
| 56 | Opt_In::VERSION, |
| 57 | true |
| 58 | ); |
| 59 | |
| 60 | $vars = array( |
| 61 | 'ajaxurl' => admin_url( 'admin-ajax.php', is_ssl() ? 'https' : 'http' ), |
| 62 | 'nonce' => wp_create_nonce( 'hustle-preview' ), |
| 63 | 'days_and_months' => array( |
| 64 | 'days_full' => Hustle_Time_Helper::get_week_days(), |
| 65 | 'days_short' => Hustle_Time_Helper::get_week_days( 'short' ), |
| 66 | 'days_min' => Hustle_Time_Helper::get_week_days( 'min' ), |
| 67 | 'months_full' => Hustle_Time_Helper::get_months(), |
| 68 | 'months_short' => Hustle_Time_Helper::get_months( 'short' ), |
| 69 | ), |
| 70 | ); |
| 71 | |
| 72 | wp_localize_script( |
| 73 | 'hustle_preview_script', |
| 74 | 'hustleVars', |
| 75 | $vars |
| 76 | ); |
| 77 | wp_enqueue_script( 'hustle_preview_script' ); |
| 78 | wp_enqueue_script( 'jquery-ui-datepicker' ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Set of actions to run on the wp_footer. |
| 83 | * |
| 84 | * @since 4.3.1 |
| 85 | */ |
| 86 | public function wp_footer() { |
| 87 | Hustle_Module_Front::print_front_styles(); |
| 88 | $this->render_non_inline_preview_container(); |
| 89 | $this->maybe_print_forminator_scripts(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Print forminator scripts for preview. |
| 94 | * Used by Dashboard, Wizards, and Listings. |
| 95 | * |
| 96 | * @since 4.3.1 |
| 97 | */ |
| 98 | private function maybe_print_forminator_scripts() { |
| 99 | // Add Forminator's front styles and scripts for preview. |
| 100 | if ( defined( 'FORMINATOR_VERSION' ) ) { |
| 101 | forminator_print_front_styles( FORMINATOR_VERSION ); |
| 102 | forminator_print_front_scripts( FORMINATOR_VERSION ); |
| 103 | |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Set the amount of posts to 1 per page. |
| 109 | * Useful when the first page is one containing posts. |
| 110 | * |
| 111 | * @since 4.3.1 |
| 112 | * |
| 113 | * @param WP_Query $query The WP_Query instance. |
| 114 | */ |
| 115 | public function pre_get_posts( $query ) { |
| 116 | $query->set( 'posts_per_page', 1 ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Adds a custom title for the page/post. |
| 121 | * |
| 122 | * @since 4.3.1 |
| 123 | * |
| 124 | * @param string $title Title to be displayed. |
| 125 | * @return string |
| 126 | */ |
| 127 | public function the_title( $title ) { |
| 128 | if ( ! in_the_loop() ) { |
| 129 | return $title; |
| 130 | } |
| 131 | /* translators: Plugin name */ |
| 132 | return esc_html( sprintf( __( '%s Preview', 'hustle' ), Opt_In_Utils::get_plugin_name() ) ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Replaces the_content by a container to render inline modules in. |
| 137 | * Used for rendering embeds. |
| 138 | * |
| 139 | * @since 4.3.1 |
| 140 | * |
| 141 | * @param string $content Current post/page content. |
| 142 | * @return string |
| 143 | */ |
| 144 | public function show_after_page_post_content( $content ) { |
| 145 | return '<div id="module-preview-inline-container"></div>' . $content; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Adds a container at the bottom of the page to render non-inline modules in. |
| 150 | * Used for popups and slide-ins. |
| 151 | * |
| 152 | * @since 4.3.1 |
| 153 | */ |
| 154 | private function render_non_inline_preview_container() { |
| 155 | echo '<div id="module-preview-container"></div>'; |
| 156 | } |
| 157 | } |
| 158 |