PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 1.9.0
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v1.9.0
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.0, at inc/modules/class-add-module.php

247 lines 5.7 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 $settings = wp_parse_args( $settings[ $this->module_id ], $defaults );
133
134 return $settings;
135 }
136
137 /**
138 * Get preview URL
139 *
140 * @param array $args
141 *
142 * @return string
143 */
144 public function set_module_preview_url( $args = array() ) {
145 // Mount preview url.
146 $preview_url = site_url( '/' );
147
148 // Type based preview url
149 if ( isset( $args['type'] ) ) {
150 switch ( $args['type'] ) {
151 case 'shop':
152 if ( function_exists( 'wc_get_page_id' ) ) {
153 $preview_url = get_permalink( wc_get_page_id( 'shop' ) );
154 }
155 break;
156
157 case 'product':
158 $query_args = array(
159 'post_type' => 'product',
160 'posts_per_page' => 1,
161 );
162
163 if ( isset( $args['query'] ) ) {
164 $products = ( new WP_Query( wp_parse_args( $args['query'], $query_args ) ) )->get_posts();
165
166 // If no results can be found with the custom query,
167 // then use the default args
168 if ( empty( $products ) || ! isset( $products[0] ) ) {
169 $products = ( new WP_Query( $query_args ) )->get_posts();
170 }
171 } else {
172 $products = ( new WP_Query( $query_args ) )->get_posts();
173 }
174
175 if ( ! empty( $products ) && isset( $products[0] ) ) {
176 $preview_url = get_permalink( $products[0] );
177 }
178
179 break;
180 }
181 }
182
183 return $preview_url;
184 }
185
186 /**
187 * Add module.
188 *
189 */
190 public function add_module( $modules ) {
191 $modules[ $this->module_section ]['modules'][ $this->module_id ] = $this->module_data;
192
193 return $modules;
194 }
195
196 /**
197 * Add module options.
198 *
199 */
200 public function add_module_options( $module_path, $merchant_module ) {
201 if ( $this->module_id === $merchant_module ) {
202 return $this->module_options_path;
203 }
204
205 return $module_path;
206 }
207
208 /**
209 * Display error message if the shortcode is placed in the wrong place.
210 *
211 * @return mixed|null
212 */
213 public function shortcode_placement_error() {
214 /*
215 * translators: %s: module id
216 */
217 $message = __( 'The shortcode <strong>[merchant_module_%s]</strong> can only be used on single product pages.', 'merchant' );
218 $message = sprintf( $message, str_replace( '-', '_', $this->module_id ) );
219 $message = wp_kses( $message, array(
220 'strong' => array(),
221 ) );
222
223 /**
224 * Filter the shortcode error message html content.
225 *
226 * @param string $message_content
227 * @param string $module_id
228 *
229 * @since 1.8
230 */
231 return apply_filters( 'merchant_module_shortcode_error_message_html',
232 '<div class="merchant-shortcode-wrong-placement">' .
233 $message
234 . '</div>',
235 $this->module->module_id );
236 }
237
238 /**
239 * Check if shortcode is enabled.
240 *
241 * @return bool
242 */
243 public function is_shortcode_enabled() {
244 return Merchant_Admin_Options::get( $this->module_id, 'use_shortcode', false );
245 }
246 }
247