non-sshare-styles
5 years ago
class-hustle-decorator-non-sshare.php
5 years ago
class-hustle-decorator-sshare.php
5 years ago
class-hustle-decorator_abstract.php
5 years ago
class-hustle-module-preview.php
5 years ago
hustle-module-front-ajax.php
5 years ago
hustle-module-front.php
5 years ago
hustle-module-renderer.php
5 years ago
hustle-renderer-abstract.php
5 years ago
hustle-renderer-sshare.php
5 years ago
hustle-renderer-abstract.php
232 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class Hustle_Renderer_Abstract |
| 5 | * |
| 6 | * @since 4.0 |
| 7 | */ |
| 8 | abstract class Hustle_Renderer_Abstract { |
| 9 | |
| 10 | /** |
| 11 | * A unique ID for the current module. |
| 12 | * |
| 13 | * @var array |
| 14 | */ |
| 15 | protected static $render_ids = array(); |
| 16 | |
| 17 | /** |
| 18 | * Module sub_type. |
| 19 | * Only for embedded and social sharing modules. |
| 20 | * |
| 21 | * @since 4.0 |
| 22 | * @var string |
| 23 | */ |
| 24 | protected $sub_type = null; |
| 25 | |
| 26 | /** |
| 27 | * Whether the render is for a preview. |
| 28 | * |
| 29 | * @since 4.0 |
| 30 | * @var boolean |
| 31 | */ |
| 32 | public static $is_preview = false; |
| 33 | |
| 34 | public function __construct() { |
| 35 | $this->is_admin = is_admin(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Generate an ID for the current module |
| 40 | * represented as an integer, starting from 0. |
| 41 | * |
| 42 | * @since 4.0 |
| 43 | * |
| 44 | * @param $id |
| 45 | */ |
| 46 | public function generate_render_id( $id ) { |
| 47 | if ( ! isset( self::$render_ids[ $id ] ) ) { |
| 48 | self::$render_ids[ $id ] = 0; |
| 49 | } else { |
| 50 | self::$render_ids[ $id ] ++; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Return the markup of the module. |
| 56 | * |
| 57 | * @since 4.0 |
| 58 | * |
| 59 | * @param Hustle_Model $module Module to display. |
| 60 | * @param string $sub_type The sub_type for embedded and social sharing modules: widget, shortcode, etc. |
| 61 | * @param string $custom_classes |
| 62 | * @param bool $is_preview |
| 63 | */ |
| 64 | public function display( Hustle_Model $module, $sub_type = null, $custom_classes = '', $is_preview = false ) { |
| 65 | |
| 66 | $this->module = $module; |
| 67 | $this->sub_type = $sub_type; |
| 68 | $this->generate_render_id( $this->module->module_id ); |
| 69 | |
| 70 | self::$is_preview = $is_preview; |
| 71 | |
| 72 | if ( $is_preview || ( $this->module->active && $this->module->get_visibility()->is_allowed_to_display( $module->module_type, $sub_type ) ) ) { |
| 73 | |
| 74 | // Render form |
| 75 | echo $this->get_module( $sub_type, $custom_classes ); // wpcs: xss ok. |
| 76 | |
| 77 | add_action( 'wp_footer', array( $this, 'print_styles' ), 9999 ); |
| 78 | } |
| 79 | |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Return markup |
| 84 | * |
| 85 | * @since 4.0 |
| 86 | * |
| 87 | * @param string $sub_type |
| 88 | * @param string $custom_classes |
| 89 | * |
| 90 | * @return mixed|void |
| 91 | */ |
| 92 | public function get_module( $sub_type = null, $custom_classes = '' ) { |
| 93 | $html = ''; |
| 94 | $post_id = $this->get_post_id(); |
| 95 | $id = $this->module->module_id; |
| 96 | $module_type = $this->module->module_type; |
| 97 | // if rendered on Preview, the array is empty and sometimes PHP notices show up |
| 98 | if ( $this->is_admin && ( empty( self::$render_ids ) || ! $id ) ) { |
| 99 | self::$render_ids[ $id ] = 0; |
| 100 | } |
| 101 | $render_id = self::$render_ids[ $id ]; |
| 102 | |
| 103 | // TODO: validate sub_types |
| 104 | $data_type = is_null( $sub_type ) ? $this->module->module_type : $sub_type; |
| 105 | |
| 106 | do_action( 'hustle_before_module_render', $render_id, $this, $post_id, $sub_type ); |
| 107 | |
| 108 | $html .= $this->get_wrapper_main( $sub_type, $custom_classes ); |
| 109 | |
| 110 | $html .= $this->get_overlay_mask(); |
| 111 | |
| 112 | $html .= $this->get_wrapper_content( $sub_type ); |
| 113 | |
| 114 | $html .= $this->get_module_body( $sub_type ); |
| 115 | |
| 116 | $html .= '</div>'; // Closing wrapper content. |
| 117 | |
| 118 | $html .= '</div>'; // Closing wrapper main. |
| 119 | |
| 120 | /** |
| 121 | * Tracking |
| 122 | */ |
| 123 | //$form_view = Hustle_Tracking_Model::get_instance(); |
| 124 | $post_id = $this->get_post_id(); |
| 125 | |
| 126 | /** |
| 127 | * Output |
| 128 | */ |
| 129 | $html = apply_filters( 'hustle_render_module_markup', $html, $this, $render_id, $sub_type, $post_id ); |
| 130 | do_action( 'hustle_after_module_render', $this, $render_id, $post_id, $sub_type ); |
| 131 | return $html; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Return post ID |
| 136 | * |
| 137 | * @since 4.0 |
| 138 | * @return int|string|bool |
| 139 | */ |
| 140 | public function get_post_id() { |
| 141 | return get_queried_object_id(); |
| 142 | } |
| 143 | |
| 144 | public function print_styles() { |
| 145 | |
| 146 | $disable_styles = apply_filters( 'hustle_disable_front_styles', false, $this->module, $this ); |
| 147 | |
| 148 | if ( ! $disable_styles ) { |
| 149 | $render_id = self::$render_ids[ $this->module->module_id ]; |
| 150 | $style = $this->module->get_decorated()->get_module_styles( $this->module->module_type ); |
| 151 | |
| 152 | printf( |
| 153 | '<style type="text/css" id="hustle-module-%1$s-%2$s-styles" class="hustle-module-styles hustle-module-styles-%3$s">%4$s</style>', |
| 154 | esc_attr( $this->module->module_id ), |
| 155 | esc_attr( $render_id ), |
| 156 | esc_attr( $this->module->module_id ), |
| 157 | wp_strip_all_tags( $style ) |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Loads a module via ajax. |
| 165 | * Currently used for preview only. |
| 166 | * |
| 167 | * @since 4.0.0 |
| 168 | */ |
| 169 | public static function ajax_load_module() { |
| 170 | $preview_data = filter_input( INPUT_POST, 'previewData', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); |
| 171 | |
| 172 | $id = filter_input( INPUT_POST, 'id', FILTER_VALIDATE_INT ); |
| 173 | |
| 174 | if ( ! $id && empty( $preview_data ) ) { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | if ( empty( $preview_data['template_name'] ) ) { |
| 179 | // Previewing an already saved module. |
| 180 | $module = Hustle_Module_Collection::instance()->return_model_from_id( $id ); |
| 181 | } else { |
| 182 | // Previewing a template. |
| 183 | // Only non-ssharing modules have templates. |
| 184 | $module = new Hustle_Module_Model(); |
| 185 | |
| 186 | $template_mode = $preview_data['template_mode']; |
| 187 | $template_name = $preview_data['template_name']; |
| 188 | $module_type = $preview_data['module_type']; |
| 189 | |
| 190 | $templates_helper = new Hustle_Templates_Helper(); |
| 191 | $preview_data = $templates_helper->get_template( $template_name, $template_mode ); |
| 192 | |
| 193 | $preview_data['module_mode'] = $template_mode; |
| 194 | $preview_data['module_type'] = $module_type; |
| 195 | $preview_data['module_name'] = $template_name; |
| 196 | |
| 197 | $module->populate_module_for_template( $preview_data ); |
| 198 | } |
| 199 | |
| 200 | if ( empty( $module ) || is_wp_error( $module ) ) { |
| 201 | wp_send_json_error( __( 'Invalid module.' ), 'hustle' ); |
| 202 | } |
| 203 | |
| 204 | $view = $module->get_renderer(); |
| 205 | |
| 206 | // This might change later on. We're only using the ajax for preview at the moment. |
| 207 | $is_preview = true; |
| 208 | |
| 209 | // Add filter for Forminator to load as a preview. |
| 210 | add_filter( 'forminator_render_shortcode_is_preview', '__return_true' ); |
| 211 | |
| 212 | // Define constant for other plugins to hook in preview. |
| 213 | if ( ! defined( 'HUSTLE_RENDER_PREVIEW' ) || ! HUSTLE_RENDER_PREVIEW ) { |
| 214 | define( 'HUSTLE_RENDER_PREVIEW', $is_preview ); |
| 215 | } |
| 216 | |
| 217 | do_action( 'hustle_before_ajax_display', $module, $is_preview ); |
| 218 | |
| 219 | $response = $view->ajax_display( $module, $preview_data, $is_preview ); |
| 220 | |
| 221 | $response = apply_filters( 'hustle_ajax_display_response', $response, $module, $is_preview ); |
| 222 | |
| 223 | do_action( 'hustle_after_ajax_display', $module, $is_preview, $response ); |
| 224 | |
| 225 | wp_send_json_success( $response ); |
| 226 | } |
| 227 | |
| 228 | protected function get_overlay_mask() { |
| 229 | return ''; |
| 230 | } |
| 231 | } |
| 232 |