PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 1.9.11
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v1.9.11
2.3.2 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 All 60 releases
merchant / inc / modules / class-add-module.php

class-add-module.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 1.9.11, at inc/modules/class-add-module.php

277 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 class Merchant_Add_Module {
8
9 /**
10 * WooCommerce only.
11 *
12 */
13 public $wc_only = false;
14
15 /**
16 * Module section.
17 *
18 */
19 public $module_section = '';
20
21 /**
22 * Module id.
23 *
24 */
25 public $module_id = '';
26
27 /**
28 * Module default settings.
29 *
30 */
31 public $module_default_settings = array();
32
33 /**
34 * Module data.
35 *
36 */
37 public $module_data = array();
38
39 /**
40 * Module options.
41 *
42 */
43 public $module_options_path = '';
44
45 /**
46 * Whether the module has a shortcode or not.
47 *
48 * @var bool
49 */
50 public $has_shortcode = false;
51
52 /**
53 * Constructor.
54 *
55 */
56 public function __construct() {
57 // Add and expose the module into the plugin dashboard.
58 add_filter( 'merchant_modules', array( $this, 'add_module' ) );
59
60 // Add module options.
61 add_filter( 'merchant_module_file_path', array( $this, 'add_module_options' ), 10, 2 );
62
63 // Add class to body to identify if module is active or not.
64 add_filter( 'admin_body_class', array( $this, 'add_module_activation_status_class' ), 10, 2 );
65
66 // Handle modules list item class.
67 add_filter( "merchant_admin_module_{$this->module_id}_list_item_class", array( $this, 'modules_list_item_class' ) );
68
69 if ( $this->has_shortcode ) {
70 add_shortcode( 'merchant_module_' . str_replace( '-', '_', $this->module_id ), array( $this, 'shortcode_handler' ) );
71 }
72 }
73
74 /**
75 * Active modules class handler.
76 *
77 */
78 public function add_module_activation_status_class( $classes ) {
79 if ( ! $this->is_module_settings_page() ) {
80 return $classes;
81 }
82
83 if ( Merchant_Modules::is_module_active( $this->module_id ) ) {
84 $classes = $classes . ' merchant-module-enabled';
85 } else {
86 $classes = $classes . ' merchant-module-disabled';
87 }
88
89 return $classes;
90 }
91
92 /**
93 * Modules list item class.
94 *
95 * @param string $module_class
96 *
97 * @return string
98 */
99 public function modules_list_item_class( $module_class ) {
100 if ( $this->wc_only && ! class_exists( 'Woocommerce' ) ) {
101 $module_class = $module_class . ' merchant-module-wc-only';
102 }
103
104 return $module_class;
105 }
106
107 /**
108 * Is module settings page.
109 *
110 * @return bool
111 */
112 public function is_module_settings_page() {
113 return isset( $_GET['page'] ) && 'merchant' === $_GET['page'] // phpcs:ignore WordPress.Security.NonceVerification.Recommended
114 && isset( $_GET['module'] ) && $this->module_id === $_GET['module']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
115 }
116
117 /**
118 * Get module settings.
119 *
120 */
121 public function get_module_settings() {
122 $settings = get_option( 'merchant' ) ? get_option( 'merchant' ) : array();
123
124 // Default settings.
125 $defaults = $this->module_default_settings;
126
127 if ( empty( $settings[ $this->module_id ] ) ) {
128 $settings[ $this->module_id ] = $defaults;
129 }
130
131 // Parse settings with defaults.
132 // Todo: check if recursive_parse_args() works for all modules and remove the condition.
133 $settings = $this->module_id === 'product-labels' ? $this->recursive_parse_args( $settings[ $this->module_id ], $defaults ) : wp_parse_args( $settings[ $this->module_id ], $defaults );
134
135 return $settings;
136 }
137
138 /**
139 * Get preview URL
140 *
141 * @param array $args
142 *
143 * @return string
144 */
145 public function set_module_preview_url( $args = array() ) {
146 // Mount preview url.
147 $preview_url = site_url( '/' );
148
149 // Type based preview url
150 if ( isset( $args['type'] ) ) {
151 switch ( $args['type'] ) {
152 case 'shop':
153 if ( function_exists( 'wc_get_page_id' ) ) {
154 $preview_url = get_permalink( wc_get_page_id( 'shop' ) );
155 }
156 break;
157
158 case 'product':
159 $query_args = array(
160 'post_type' => 'product',
161 'posts_per_page' => 1,
162 );
163
164 if ( isset( $args['query'] ) ) {
165 $products = ( new WP_Query( wp_parse_args( $args['query'], $query_args ) ) )->get_posts();
166
167 // If no results can be found with the custom query,
168 // then use the default args
169 if ( empty( $products ) || ! isset( $products[0] ) ) {
170 $products = ( new WP_Query( $query_args ) )->get_posts();
171 }
172 } else {
173 $products = ( new WP_Query( $query_args ) )->get_posts();
174 }
175
176 if ( ! empty( $products ) && isset( $products[0] ) ) {
177 $preview_url = get_permalink( $products[0] );
178 }
179
180 break;
181 }
182 }
183
184 return $preview_url;
185 }
186
187 /**
188 * Add module.
189 *
190 */
191 public function add_module( $modules ) {
192 $modules[ $this->module_section ]['modules'][ $this->module_id ] = $this->module_data;
193
194 return $modules;
195 }
196
197 /**
198 * Add module options.
199 *
200 */
201 public function add_module_options( $module_path, $merchant_module ) {
202 if ( $this->module_id === $merchant_module ) {
203 return $this->module_options_path;
204 }
205
206 return $module_path;
207 }
208
209 /**
210 * Display error message if the shortcode is placed in the wrong place.
211 *
212 * @return mixed|null
213 */
214 public function shortcode_placement_error() {
215 /*
216 * translators: %s: module id
217 */
218 $message = __( 'The shortcode <strong>[merchant_module_%s]</strong> can only be used on single product pages.', 'merchant' );
219 $message = sprintf( $message, str_replace( '-', '_', $this->module_id ) );
220 $message = wp_kses( $message, array(
221 'strong' => array(),
222 ) );
223
224 /**
225 * Filter the shortcode error message html content.
226 *
227 * @param string $message_content
228 * @param string $module_id
229 *
230 * @since 1.8
231 */
232 return apply_filters( 'merchant_module_shortcode_error_message_html',
233 '<div class="merchant-shortcode-wrong-placement">' .
234 $message
235 . '</div>',
236 $this->module->module_id );
237 }
238
239 /**
240 * Check if shortcode is enabled.
241 *
242 * @return bool
243 */
244 public function is_shortcode_enabled() {
245
246 /**
247 * Hook 'merchant_{$this->module_id}_is_shortcode_enabled'
248 *
249 * @since 1.9.3
250 */
251 return apply_filters( "merchant_{$this->module_id}_is_shortcode_enabled", Merchant_Admin_Options::get( $this->module_id, 'use_shortcode', false ) );
252 }
253
254 /**
255 * Recursively merges user-defined arguments into default arguments.
256 *
257 * @param $args
258 * @param $defaults
259 *
260 * @return mixed
261 */
262 private function recursive_parse_args( $args, $defaults ) {
263 $result = $defaults;
264
265 foreach ( $args as $key => $value ) {
266 // If the value is an array and the corresponding default is also an array, merge them recursively.
267 if ( is_array( $value ) && isset( $result[ $key ] ) && is_array( $result[ $key ] ) ) {
268 $result[ $key ] = $this->recursive_parse_args( $value, $result[ $key ] );
269 } else {
270 $result[ $key ] = $value;
271 }
272 }
273
274 return $result;
275 }
276 }
277