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
hustle-renderer-abstract.php
296 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Hustle_Renderer_Abstract |
| 4 | * |
| 5 | * @package Hustle |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Class Hustle_Renderer_Abstract |
| 10 | * |
| 11 | * @since 4.0 |
| 12 | */ |
| 13 | abstract class Hustle_Renderer_Abstract { |
| 14 | |
| 15 | /** |
| 16 | * A unique ID for the current module. |
| 17 | * |
| 18 | * @var array |
| 19 | */ |
| 20 | protected static $render_ids = array(); |
| 21 | |
| 22 | /** |
| 23 | * Module sub_type. |
| 24 | * Only for embedded and social sharing modules. |
| 25 | * |
| 26 | * @since 4.0 |
| 27 | * @var string |
| 28 | */ |
| 29 | protected $sub_type = null; |
| 30 | |
| 31 | /** |
| 32 | * Whether the render is for a preview. |
| 33 | * |
| 34 | * @since 4.0 |
| 35 | * @var boolean |
| 36 | */ |
| 37 | public static $is_preview = false; |
| 38 | |
| 39 | /** |
| 40 | * Is admin area? |
| 41 | * |
| 42 | * @var bool |
| 43 | */ |
| 44 | public $is_admin; |
| 45 | |
| 46 | /** |
| 47 | * Module object |
| 48 | * |
| 49 | * @var object |
| 50 | */ |
| 51 | public $module; |
| 52 | |
| 53 | /** |
| 54 | * Constructor |
| 55 | */ |
| 56 | public function __construct() { |
| 57 | $this->is_admin = is_admin(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Generate an ID for the current module |
| 62 | * represented as an integer, starting from 0. |
| 63 | * |
| 64 | * @since 4.0 |
| 65 | * |
| 66 | * @param int $id ID. |
| 67 | */ |
| 68 | public function generate_render_id( $id ) { |
| 69 | if ( ! isset( self::$render_ids[ $id ] ) ) { |
| 70 | self::$render_ids[ $id ] = 0; |
| 71 | } else { |
| 72 | ++self::$render_ids[ $id ]; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Return the markup of the module. |
| 78 | * |
| 79 | * @since 4.0 |
| 80 | * |
| 81 | * @param Hustle_Model $module Module to display. |
| 82 | * @param string $sub_type The sub_type for embedded and social sharing modules: widget, shortcode, etc. |
| 83 | * @param string $custom_classes Custom classes. |
| 84 | * @param bool $is_preview Is preview. |
| 85 | * |
| 86 | * @return string HTML code. |
| 87 | */ |
| 88 | public function display( Hustle_Model $module, $sub_type = null, $custom_classes = '', $is_preview = false ) { |
| 89 | |
| 90 | $this->module = $module; |
| 91 | $this->sub_type = $sub_type; |
| 92 | $this->generate_render_id( $this->module->module_id ); |
| 93 | |
| 94 | self::$is_preview = $is_preview; |
| 95 | |
| 96 | $avoid_static_cache = Opt_In_Utils::is_static_cache_enabled(); |
| 97 | $has_schedule_settings = ! empty( $this->module->settings->is_schedule ) && '1' === $this->module->settings->is_schedule; |
| 98 | if ( $avoid_static_cache && ! $has_schedule_settings ) { |
| 99 | $is_simple_conditions = $this->module->get_visibility()->is_simple_conditions( $module->module_type, $sub_type ); |
| 100 | if ( $is_simple_conditions ) { |
| 101 | $custom_classes .= ' hustle-show-this-module'; |
| 102 | } |
| 103 | } |
| 104 | if ( $avoid_static_cache && empty( $is_simple_conditions ) ) { |
| 105 | $display_module = $this->module->active; |
| 106 | } else { |
| 107 | $display_module = $this->module->active && $this->module->get_visibility()->is_allowed_to_display( $module->module_type, $sub_type ); |
| 108 | } |
| 109 | if ( $is_preview || $display_module ) { |
| 110 | $this->enqueue_styles(); |
| 111 | // Render form. |
| 112 | return $this->get_module( $sub_type, $custom_classes ); |
| 113 | } |
| 114 | |
| 115 | return ''; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Return markup |
| 120 | * |
| 121 | * @since 4.0 |
| 122 | * |
| 123 | * @param string $sub_type Sub type. |
| 124 | * @param string $custom_classes Custom classes. |
| 125 | * |
| 126 | * @return mixed|void |
| 127 | */ |
| 128 | public function get_module( $sub_type = null, $custom_classes = '' ) { |
| 129 | $html = ''; |
| 130 | $post_id = $this->get_post_id(); |
| 131 | $id = $this->module->module_id; |
| 132 | $module_type = $this->module->module_type; |
| 133 | // if rendered on Preview, the array is empty and sometimes PHP notices show up. |
| 134 | if ( $this->is_admin && ( empty( self::$render_ids ) || ! $id ) ) { |
| 135 | self::$render_ids[ $id ] = 0; |
| 136 | } |
| 137 | $render_id = self::$render_ids[ $id ]; |
| 138 | |
| 139 | // TODO: validate sub_types. |
| 140 | $data_type = is_null( $sub_type ) ? $this->module->module_type : $sub_type; |
| 141 | |
| 142 | do_action( 'hustle_before_module_render', $render_id, $this, $post_id, $sub_type ); |
| 143 | |
| 144 | $html .= $this->get_wrapper_main( $sub_type, $custom_classes ); |
| 145 | |
| 146 | $html .= wp_kses_post( $this->get_overlay_mask() ); |
| 147 | |
| 148 | $html .= wp_kses_post( $this->get_wrapper_content( $sub_type ) ); |
| 149 | |
| 150 | $html .= $this->get_module_body( $sub_type ); |
| 151 | |
| 152 | $html .= '</div>'; // Closing wrapper content. |
| 153 | |
| 154 | $html .= '</div>'; // Closing wrapper main. |
| 155 | |
| 156 | /** |
| 157 | * Tracking |
| 158 | */ |
| 159 | $post_id = $this->get_post_id(); |
| 160 | |
| 161 | /** |
| 162 | * Output |
| 163 | */ |
| 164 | $html = apply_filters( 'hustle_render_module_markup', $html, $this, $render_id, $sub_type, $post_id ); |
| 165 | do_action( 'hustle_after_module_render', $this, $render_id, $post_id, $sub_type ); |
| 166 | return $html; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Return post ID |
| 171 | * |
| 172 | * @since 4.0 |
| 173 | * @return int|string|bool |
| 174 | */ |
| 175 | public function get_post_id() { |
| 176 | return get_queried_object_id(); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Enqueue styles |
| 181 | */ |
| 182 | public function enqueue_styles() { |
| 183 | $disable_styles = apply_filters( 'hustle_disable_front_styles', false, $this->module, $this ); |
| 184 | |
| 185 | if ( ! $disable_styles ) { |
| 186 | $render_id = self::$render_ids[ $this->module->module_id ]; |
| 187 | |
| 188 | $style_id = 'hustle-module-' . esc_attr( $this->module->module_id ) . '-' . esc_attr( $render_id ) . '-styles'; |
| 189 | |
| 190 | if ( Hustle_Module_Inline_Style_Queue::has_inline_style( $style_id ) ) { |
| 191 | // Already enqueued. |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | $style = $this->module->get_decorated()->get_module_styles( $this->module->module_type ); // it's already escaped. |
| 196 | |
| 197 | Hustle_Module_Inline_Style_Queue::enqueue_inline_style( $style_id, $style ); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Print styles |
| 203 | */ |
| 204 | public function print_styles() { |
| 205 | |
| 206 | $disable_styles = apply_filters( 'hustle_disable_front_styles', false, $this->module, $this ); |
| 207 | |
| 208 | if ( ! $disable_styles ) { |
| 209 | $render_id = self::$render_ids[ $this->module->module_id ]; |
| 210 | $style = $this->module->get_decorated()->get_module_styles( $this->module->module_type ); // it's already escaped. |
| 211 | |
| 212 | printf( |
| 213 | '<style id="hustle-module-%1$s-%2$s-styles" class="hustle-module-styles hustle-module-styles-%3$s">%4$s</style>', |
| 214 | esc_attr( $this->module->module_id ), |
| 215 | esc_attr( $render_id ), |
| 216 | esc_attr( $this->module->module_id ), |
| 217 | $style // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 218 | ); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Loads a module via ajax. |
| 224 | * Currently used for preview only. |
| 225 | * |
| 226 | * @since 4.0.0 |
| 227 | */ |
| 228 | public static function ajax_load_module() { |
| 229 | $preview_data = filter_input( INPUT_POST, 'previewData', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); |
| 230 | |
| 231 | $id = filter_input( INPUT_POST, 'id', FILTER_VALIDATE_INT ); |
| 232 | |
| 233 | if ( ! $id && empty( $preview_data ) ) { |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | if ( empty( $preview_data['template_name'] ) ) { |
| 238 | // Previewing an already saved module. |
| 239 | $module = Hustle_Module_Collection::instance()->return_model_from_id( $id ); |
| 240 | } else { |
| 241 | // Previewing a template. |
| 242 | // Only non-ssharing modules have templates. |
| 243 | $module = Hustle_Module_Model::new_instance(); |
| 244 | |
| 245 | $template_mode = $preview_data['template_mode']; |
| 246 | $template_name = $preview_data['template_name']; |
| 247 | $module_type = $preview_data['module_type']; |
| 248 | |
| 249 | $templates_helper = new Hustle_Templates_Helper(); |
| 250 | $preview_data = $templates_helper->get_template( $template_name, $template_mode ); |
| 251 | |
| 252 | $preview_data['module_mode'] = $template_mode; |
| 253 | $preview_data['module_type'] = $module_type; |
| 254 | $preview_data['module_name'] = $template_name; |
| 255 | |
| 256 | $module->populate_module_for_template( $preview_data ); |
| 257 | } |
| 258 | |
| 259 | if ( empty( $module ) || is_wp_error( $module ) ) { |
| 260 | wp_send_json_error( esc_html__( 'Invalid module.' ), 'hustle' ); |
| 261 | } |
| 262 | |
| 263 | $view = $module->get_renderer(); |
| 264 | |
| 265 | // This might change later on. We're only using the ajax for preview at the moment. |
| 266 | $is_preview = true; |
| 267 | |
| 268 | // Add filter for Forminator to load as a preview. |
| 269 | add_filter( 'forminator_render_shortcode_is_preview', '__return_true' ); |
| 270 | |
| 271 | // Define constant for other plugins to hook in preview. |
| 272 | if ( ! defined( 'HUSTLE_RENDER_PREVIEW' ) || ! HUSTLE_RENDER_PREVIEW ) { |
| 273 | define( 'HUSTLE_RENDER_PREVIEW', $is_preview ); |
| 274 | } |
| 275 | |
| 276 | do_action( 'hustle_before_ajax_display', $module, $is_preview ); |
| 277 | |
| 278 | $response = $view->ajax_display( $module, $preview_data, $is_preview ); |
| 279 | |
| 280 | $response = apply_filters( 'hustle_ajax_display_response', $response, $module, $is_preview ); |
| 281 | |
| 282 | do_action( 'hustle_after_ajax_display', $module, $is_preview, $response ); |
| 283 | |
| 284 | wp_send_json_success( $response ); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Get overlay mask |
| 289 | * |
| 290 | * @return string |
| 291 | */ |
| 292 | protected function get_overlay_mask() { |
| 293 | return ''; |
| 294 | } |
| 295 | } |
| 296 |