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

197 lines 4.3 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 * Constructor.
47 *
48 */
49 public function __construct() {
50 // Add and expose the module into the plugin dashboard.
51 add_filter( 'merchant_modules', array( $this, 'add_module' ) );
52
53 // Add module options.
54 add_filter( 'merchant_module_file_path', array( $this, 'add_module_options' ), 10, 2 );
55
56 // Add class to body to identify if module is active or not.
57 add_filter( 'admin_body_class', array( $this, 'add_module_activation_status_class' ), 10, 2 );
58
59 // Handle modules list item class.
60 add_filter( "merchant_admin_module_{$this->module_id}_list_item_class", array( $this, 'modules_list_item_class' ) );
61 }
62
63 /**
64 * Active modules class handler.
65 *
66 */
67 public function add_module_activation_status_class( $classes ) {
68 if ( ! $this->is_module_settings_page() ) {
69 return $classes;
70 }
71
72 if ( Merchant_Modules::is_module_active( $this->module_id ) ) {
73 $classes = $classes . ' merchant-module-enabled';
74 } else {
75 $classes = $classes . ' merchant-module-disabled';
76 }
77
78 return $classes;
79 }
80
81 /**
82 * Modules list item class.
83 *
84 * @param string $class
85 *
86 * @return string
87 */
88 public function modules_list_item_class( $class ) {
89 if ( $this->wc_only && ! class_exists( 'Woocommerce' ) ) {
90 $class = $class . ' merchant-module-wc-only';
91 }
92
93 return $class;
94 }
95
96 /**
97 * Is module settings page.
98 *
99 * @return bool
100 */
101 public function is_module_settings_page() {
102 return isset( $_GET['page'] ) && 'merchant' === $_GET['page'] // phpcs:ignore WordPress.Security.NonceVerification.Recommended
103 && isset( $_GET['module'] ) && $this->module_id === $_GET['module']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
104 }
105
106 /**
107 * Get module settings.
108 *
109 */
110 public function get_module_settings() {
111 $settings = get_option( 'merchant' ) ? get_option( 'merchant' ) : array();
112
113 // Default settings.
114 $defaults = $this->module_default_settings;
115
116 if ( empty( $settings[ $this->module_id ] ) ) {
117 $settings[ $this->module_id ] = $defaults;
118 }
119
120 // Parse settings with defaults.
121 $settings = wp_parse_args( $settings[ $this->module_id ], $defaults );
122
123 return $settings;
124 }
125
126 /**
127 * Get preview URL
128 *
129 * @param array $args
130 *
131 * @return string
132 */
133 public function set_module_preview_url( $args = array() ) {
134 // Mount preview url.
135 $preview_url = site_url( '/' );
136
137 // Type based preview url
138 if ( isset( $args['type'] ) ) {
139 switch ( $args['type'] ) {
140 case 'shop':
141 if ( function_exists( 'wc_get_page_id' ) ) {
142 $preview_url = get_permalink( wc_get_page_id( 'shop' ) );
143 }
144 break;
145
146 case 'product':
147 $query_args = array(
148 'post_type' => 'product',
149 'posts_per_page' => 1,
150 );
151
152 if ( isset( $args['query'] ) ) {
153 $products = ( new WP_Query( wp_parse_args( $args['query'], $query_args ) ) )->get_posts();
154
155 // If no results can be found with the custom query,
156 // then use the default args
157 if ( empty( $products ) || ! isset( $products[0] ) ) {
158 $products = ( new WP_Query( $query_args ) )->get_posts();
159 }
160 } else {
161 $products = ( new WP_Query( $query_args ) )->get_posts();
162 }
163
164 if ( ! empty( $products ) && isset( $products[0] ) ) {
165 $preview_url = get_permalink( $products[0] );
166 }
167
168 break;
169 }
170 }
171
172 return $preview_url;
173 }
174
175 /**
176 * Add module.
177 *
178 */
179 public function add_module( $modules ) {
180 $modules[ $this->module_section ]['modules'][ $this->module_id ] = $this->module_data;
181
182 return $modules;
183 }
184
185 /**
186 * Add module options.
187 *
188 */
189 public function add_module_options( $module_path, $merchant_module ) {
190 if ( $this->module_id === $merchant_module ) {
191 return $this->module_options_path;
192 }
193
194 return $module_path;
195 }
196 }
197