PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.0.3
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.0.3
1.4.15 1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 All 178 releases
disco / backend / ActDeact.php

ActDeact.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.0.3, at backend/ActDeact.php

268 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Disco
5 *
6 * @package Disco
7 * @author Ohidul Islam <wahid0003@gmail.com>
8 * @link http://domain.tld
9 * @license GPL 2.0+
10 * @copyright 2022 WebAppick
11 */
12
13 namespace Disco\Backend;
14
15 use Disco\Engine\Base;
16
17 /**
18 * Activate and deactivate method of the plugin and relates.
19 */
20 class ActDeact extends Base {
21
22 /**
23 * Initialize the class.
24 *
25 * @return void|bool
26 */
27 public function initialize() {
28 if ( ! parent::initialize() ) {
29 return;
30 }
31
32 // Activate plugin when new blog is added
33 \add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) );
34
35 \add_action( 'admin_init', array( $this, 'upgrade_procedure' ) );
36 }
37
38 /**
39 * Fired when a new site is activated with a WPMU environment.
40 *
41 * @param int $blog_id ID of the new blog.
42 * @return void
43 * @since 1.0.0
44 */
45 public function activate_new_site( int $blog_id ) {
46 if ( 1 !== \did_action( 'wpmu_new_blog' ) ) {
47 return;
48 }
49
50 \switch_to_blog( $blog_id );
51 self::single_activate();
52 \restore_current_blog();
53 }
54
55 /**
56 * Fired when the plugin is activated.
57 *
58 * @param bool|null $network_wide True if active in a multiset, false if classic site.
59 * @return void
60 * @since 1.0.0
61 */
62 public static function activate( $network_wide ) {
63 if ( \function_exists( 'is_multisite' ) && \is_multisite() ) {
64 if ( $network_wide ) {
65 // Get all blog ids
66 /** @var array<\WP_Site> $blogs */
67 $blogs = \get_sites();
68
69 foreach ( $blogs as $blog ) {
70 \switch_to_blog( (int) $blog->blog_id );
71 self::single_activate();
72 \restore_current_blog();
73 }
74
75 return;
76 }
77 }
78
79 self::single_activate();
80 }
81
82 /**
83 * Fired when the plugin is deactivated.
84 *
85 * @param bool $network_wide True if WPMU super admin uses
86 * "Network Deactivate" action, false if
87 * WPMU is disabled or plugin is
88 * deactivated on an individual blog.
89 * @return void
90 * @since 1.0.0
91 */
92 public static function deactivate( bool $network_wide ) {
93 if ( \function_exists( 'is_multisite' ) && \is_multisite() ) {
94 if ( $network_wide ) {
95 // Get all blog ids
96 /** @var array<\WP_Site> $blogs */
97 $blogs = \get_sites();
98
99 foreach ( $blogs as $blog ) {
100 \switch_to_blog( (int) $blog->blog_id );
101 self::single_deactivate();
102 \restore_current_blog();
103 }
104
105 return;
106 }
107 }
108
109 self::single_deactivate();
110 }
111
112 /**
113 * Add admin capabilities
114 *
115 * @return void
116 */
117 public static function add_capabilities() {
118 // Add the capabilities to all the roles
119 $caps = array(
120 'create_plugins',
121 'read_demo',
122 'read_private_demoes',
123 'edit_demo',
124 'edit_demoes',
125 'edit_private_demoes',
126 'edit_published_demoes',
127 'edit_others_demoes',
128 'publish_demoes',
129 'delete_demo',
130 'delete_demoes',
131 'delete_private_demoes',
132 'delete_published_demoes',
133 'delete_others_demoes',
134 'manage_demoes',
135 );
136 $roles = array(
137 \get_role( 'administrator' ),
138 \get_role( 'editor' ),
139 \get_role( 'author' ),
140 \get_role( 'contributor' ),
141 \get_role( 'subscriber' ),
142 );
143
144 foreach ( $roles as $role ) {
145 foreach ( $caps as $cap ) {
146 if ( \is_null( $role ) ) {
147 continue;
148 }
149
150 $role->add_cap( $cap );
151 }
152 }
153 }
154
155 /**
156 * Remove capabilities to specific roles
157 *
158 * @return void
159 */
160 public static function remove_capabilities() {
161 // Remove capabilities to specific roles
162 $bad_caps = array(
163 'create_demoes',
164 'read_private_demoes',
165 'edit_demo',
166 'edit_demoes',
167 'edit_private_demoes',
168 'edit_published_demoes',
169 'edit_others_demoes',
170 'publish_demoes',
171 'delete_demo',
172 'delete_demoes',
173 'delete_private_demoes',
174 'delete_published_demoes',
175 'delete_others_demoes',
176 'manage_demoes',
177 );
178 $roles = array(
179 \get_role( 'author' ),
180 \get_role( 'contributor' ),
181 \get_role( 'subscriber' ),
182 );
183
184 foreach ( $roles as $role ) {
185 foreach ( $bad_caps as $cap ) {
186 if ( \is_null( $role ) ) {
187 continue;
188 }
189
190 $role->remove_cap( $cap );
191 }
192 }
193 }
194
195 /**
196 * Upgrade procedure
197 *
198 * @return void
199 */
200 public static function upgrade_procedure() {
201 if ( ! \is_admin() ) {
202 return;
203 }
204
205 $version = \strval( \get_option( 'disco-version' ) );
206
207 if ( ! \version_compare( DISCO_VERSION, $version, '>' ) ) {
208 return;
209 }
210
211 \update_option( 'disco-version', DISCO_VERSION );
212 \delete_option( DISCO_TEXTDOMAIN . '_fake-meta' );
213 }
214
215 /**
216 * Create Plugin Table
217 */
218 public static function create_plugin_table() {
219 global $wpdb;
220 $table_name = $wpdb->prefix . 'disco_campaigns';
221 $charset_collate = $wpdb->get_charset_collate();
222
223 $sql = "CREATE TABLE $table_name (
224 id int(11) NOT NULL AUTO_INCREMENT,
225 intent varchar(20) NOT NULL default '',
226 status varchar(20) NOT NULL default '',
227 priority int(5) NOT NULL default '0',
228 data longtext NOT NULL,
229 PRIMARY KEY (id)
230 ) $charset_collate;";
231
232 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
233 dbDelta( $sql );
234
235 add_option( 'DISCO_DB_VERSION', DISCO_DB_VERSION );
236 }
237
238 /**
239 * Fired for each blog when the plugin is activated.
240 *
241 * @return void
242 * @since 1.0.0
243 */
244 private static function single_activate() {
245 // Install DB
246 self::create_plugin_table();
247
248 // Add a custom roles add_role( 'advanced', __( 'Advanced' ) );
249 self::add_capabilities();
250 self::upgrade_procedure();
251 // Clear the permalinks
252 \flush_rewrite_rules();
253 }
254
255 /**
256 * Fired for each blog when the plugin is deactivated.
257 *
258 * @return void
259 * @since 1.0.0
260 */
261 private static function single_deactivate() {
262 self::remove_capabilities();
263 // Clear the permalinks
264 \flush_rewrite_rules();
265 }
266
267 }
268