PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / trunk
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant vtrunk
2.3.1 2.3.0 2.2.8 2.2.7 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.11.0 1.11.1 1.11.2 1.6 1.7 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.10 1.9.11 1.9.12 All 59 releases
merchant / admin / classes / class-merchant-admin-preview.php

class-merchant-admin-preview.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant trunk, at admin/classes/class-merchant-admin-preview.php

319 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Merchant_Admin_Preview Class.
4 */
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit; // Exit if accessed directly
8 }
9
10 if ( ! class_exists( 'Merchant_Admin_Preview' ) ) {
11 class Merchant_Admin_Preview {
12
13 /**
14 * The preview HTML.
15 *
16 * @var string
17 */
18 protected $html;
19
20 /**
21 * Manipulators array.
22 *
23 * @var array
24 */
25 protected $manipulators;
26
27 /**
28 * The single class instance.
29 */
30 private static $instance = null;
31
32 /**
33 * Instance.
34 */
35 public static function instance() {
36 if ( is_null( self::$instance ) ) {
37 self::$instance = new self();
38 }
39
40 return self::$instance;
41 }
42
43 /**
44 * Constructor.
45 */
46 public function __construct() {
47 // Add admin preview class to body.
48 add_filter( 'admin_body_class', array( $this, 'add_admin_body_class' ) );
49 }
50
51 /**
52 * Add admin preview class to body.
53 *
54 * @param string $classes
55 *
56 * @return string
57 */
58 public function add_admin_body_class( $classes ) {
59 $classes .= ' merchant-has-admin-preview';
60
61 return $classes;
62 }
63
64 /**
65 * Set the HTML of the preview box.
66 *
67 * @param string|callable $html The HTML as a string or a callable that should echo the HTML.
68 * @param mixed ...$args Optional args to use in the callback
69 *
70 * @return void
71 */
72 public function set_html( $html, ...$args ) {
73 if ( is_callable( $html ) ) {
74 ob_start();
75 call_user_func( $html, ...$args );
76 $html = ob_get_clean();
77 }
78
79 $this->html = $html;
80 }
81
82 /**
83 * This will update the CSS variable of a selector with the value from the settings field.
84 *
85 * @param string $setting the setting ID
86 * @param string $selector the selector
87 * @param string $variable the CSS variable
88 * @param string $unit optional. set unit.
89 *
90 * @return void
91 */
92 public function set_css( $setting, $selector, $variable, $unit = '' ) {
93 $this->manipulators['css'][] = array(
94 'setting' => $setting,
95 'selector' => $selector,
96 'variable' => $variable,
97 'unit' => $unit,
98 );
99 }
100
101 /**
102 * This will update the text of a selector with the value from the settings field.
103 *
104 * @param string $setting the setting ID
105 * @param string $selector the selector
106 * @param array $replacements optional, set string replacements
107 *
108 * @return void
109 */
110 public function set_text( $setting, $selector, $replacements = array() ) {
111 $manipulator = array(
112 'setting' => $setting,
113 'selector' => $selector,
114 );
115
116 if ( ! empty( $replacements ) ) {
117 $manipulator['replacements'] = $replacements;
118 }
119
120 $this->manipulators['text'][] = $manipulator;
121 }
122
123 /**
124 * This will update an attribute of an element with the value from the settings field.
125 *
126 * @param string $setting the setting ID
127 * @param string $selector the selector
128 * @param string $attribute the attribute to update.
129 * @param array $replacements optional, set string replacements
130 *
131 * @return void
132 */
133 public function set_attribute( $setting, $selector, $attribute, $replacements = array() ) {
134 $manipulator = array(
135 'setting' => $setting,
136 'selector' => $selector,
137 'attribute' => $attribute,
138 );
139
140 if ( ! empty( $replacements ) ) {
141 $manipulator['replacements'] = $replacements;
142 }
143
144 $this->manipulators['attributes'][] = $manipulator;
145 }
146
147 /**
148 * This will update an icon elements based on icon choices.
149 *
150 * @param string $setting the setting ID
151 * @param string $selector the selector
152 *
153 * @return void
154 */
155 public function set_icon( $setting, $selector ) {
156 $manipulator = array(
157 'setting' => $setting,
158 'selector' => $selector,
159 );
160
161 $this->manipulators['icons'][] = $manipulator;
162 }
163
164 /**
165 * This will update an svg icon elements based on icon choices.
166 *
167 * @param string $setting the setting ID
168 * @param string $selector the selector
169 *
170 * @return void
171 */
172 public function set_svg_icon( $setting, $selector ) {
173 $manipulator = array(
174 'icons_lib' => Merchant_SVG_Icons::$svg_icons,
175 'setting' => $setting,
176 'selector' => $selector,
177 );
178
179 $this->manipulators['svg_icons'][] = $manipulator;
180 }
181
182 /**
183 * This will update a class of an element with the value from the settings field.
184 *
185 * @param string $setting the setting ID
186 * @param string $selector the selector
187 * @param array $remove optional, classes to remove prior to adding the new one.
188 * @param string $add optional, Toggle a pre-defined class. When this is set it will
189 * toggle this class instead of adding the value from the setting.
190 * Best used with switchers and checkboxes.
191 *
192 * @return void
193 */
194 public function set_class( $setting, $selector, $remove = array(), $add = '' ) {
195 $manipulator = array(
196 'setting' => $setting,
197 'selector' => $selector,
198 );
199
200 if ( ! empty( $remove ) ) {
201 $manipulator['remove'] = $remove;
202 }
203
204 if ( ! empty( $add ) ) {
205 $manipulator['add'] = $add;
206 }
207
208 $this->manipulators['classes'][] = $manipulator;
209 }
210
211 /**
212 * This will update the repeater content of a selector with the value from the settings field.
213 *
214 * @param string $setting the setting ID
215 * @param string $selector the selector
216 * @param string $repeater_item_selector the repeater item selector
217 * @param string $icon optional, the icon selector. Useful when the $list_item_selector contains
218 * icon HTML markup inside his content.
219 *
220 * @return void
221 */
222 public function set_repeater_content( $setting, $selector ) {
223 $manipulator = array(
224 'setting' => $setting,
225 'selector' => $selector,
226 );
227
228 $this->manipulators['repeater_content'][] = $manipulator;
229 }
230
231 /**
232 * This will update the flexible content items inside a selector with the value from the settings field.
233 *
234 * @param string $setting the setting ID
235 * @param string $selector the selector
236 * @param array $variables the args to point {variables} with settings for each layout
237 *
238 * @example array('layout' => array('{variable}' => 'setting'))
239 *
240 * @return void
241 */
242 public function set_flexible_content( $setting, $selector, $variables ) {
243 $manipulator = array(
244 'setting' => $setting,
245 'selector' => $selector,
246 'variables' => $variables,
247 );
248
249 $this->manipulators['flexible_content'][] = $manipulator;
250 }
251
252 /**
253 * This will trigger the preview on input change to load the latest changes from
254 * all inputs added to the manipulators array.
255 *
256 * @param string $setting the setting ID
257 *
258 * @return void
259 */
260 public function trigger_update( $setting ) {
261 $this->manipulators['update'][] = array(
262 'setting' => $setting,
263 );
264 }
265
266 /*****************************************
267 * Helpers
268 *****************************************/
269
270 /***
271 * Get price format to use in replacements.
272 *
273 * @return string
274 */
275 public function get_price_format() {
276 return str_replace( '0.00', '{string}', wc_price( '0' ) );
277 }
278
279 /*****************************************
280 * Static methods
281 *****************************************/
282
283 /**
284 * @return bool
285 */
286 public static function has_preview() {
287 return isset( self::instance()->html ) && ! empty( self::instance()->html );
288 }
289
290 /**
291 * @param $module_id
292 *
293 * @return void
294 */
295 public static function set_preview( $module_id ) {
296 /**
297 * Hook: merchant_module_preview
298 *
299 * @since 1.2
300 */
301 apply_filters( 'merchant_module_preview', self::instance(), $module_id );
302
303 $manipulators = isset( self::instance()->manipulators ) && ! empty( self::instance()->manipulators )
304 ? self::instance()->manipulators
305 : array();
306 $script_variable = 'var merchantPreviewManipulators = ' . wp_json_encode( $manipulators );
307
308 wp_add_inline_script( 'merchant-admin-preview', $script_variable );
309 }
310
311 /**
312 * @return string
313 */
314 public static function get_html() {
315 return self::instance()->html;
316 }
317 }
318 }
319