| 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 |
* @return void |
| 218 |
*/ |
| 219 |
public static function create_plugin_table() { |
| 220 |
global $wpdb; |
| 221 |
$table_name = $wpdb->prefix . 'disco_campaigns'; |
| 222 |
$charset_collate = $wpdb->get_charset_collate(); |
| 223 |
|
| 224 |
$sql = "CREATE TABLE $table_name ( |
| 225 |
id int(11) NOT NULL AUTO_INCREMENT, |
| 226 |
intent varchar(20) NOT NULL default '', |
| 227 |
status varchar(20) NOT NULL default '', |
| 228 |
priority int(5) NOT NULL default '0', |
| 229 |
data longtext NOT NULL, |
| 230 |
PRIMARY KEY (id) |
| 231 |
) $charset_collate;"; |
| 232 |
|
| 233 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 234 |
dbDelta( $sql ); |
| 235 |
|
| 236 |
add_option( 'DISCO_DB_VERSION', DISCO_DB_VERSION ); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Fired for each blog when the plugin is activated. |
| 241 |
* |
| 242 |
* @return void |
| 243 |
* @since 1.0.0 |
| 244 |
*/ |
| 245 |
private static function single_activate() { |
| 246 |
// Install DB |
| 247 |
self::create_plugin_table(); |
| 248 |
|
| 249 |
if ( ! \get_option( 'disco_install_time' ) ) { |
| 250 |
\update_option( 'disco_install_time', time() ); |
| 251 |
} |
| 252 |
|
| 253 |
// Add a custom roles add_role( 'advanced', __( 'Advanced' ) ); |
| 254 |
self::add_capabilities(); |
| 255 |
self::upgrade_procedure(); |
| 256 |
// Clear the permalinks |
| 257 |
\flush_rewrite_rules(); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Fired for each blog when the plugin is deactivated. |
| 262 |
* |
| 263 |
* @return void |
| 264 |
* @since 1.0.0 |
| 265 |
*/ |
| 266 |
private static function single_deactivate() { |
| 267 |
self::remove_capabilities(); |
| 268 |
// Clear the permalinks |
| 269 |
\flush_rewrite_rules(); |
| 270 |
} |
| 271 |
|
| 272 |
} |
| 273 |
|