class-code-manager-i18n.php
7 months ago
class-code-manager-loader.php
7 months ago
class-code-manager-switch.php
7 months ago
class-code-manager.php
7 months ago
class-code-manager-switch.php
114 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Code Manager switch |
| 4 | * |
| 5 | * @package . |
| 6 | */ |
| 7 | |
| 8 | use Code_Manager\Code_Manager_Model; |
| 9 | |
| 10 | /** |
| 11 | * Class Code_Manager_Switch |
| 12 | * |
| 13 | * Switch to: |
| 14 | * + activate plugin {@see Code_Manager_Switch::activate()} |
| 15 | * + deactive plugin {@see Code_Manager_Switch::deactivate()} |
| 16 | * |
| 17 | * @author Peter Schulz |
| 18 | * @since 1.0.0 |
| 19 | * |
| 20 | * @see Code_Manager_Switch::activate() |
| 21 | * @see Code_Manager_Switch::deactivate() |
| 22 | */ |
| 23 | class Code_Manager_Switch { |
| 24 | |
| 25 | /** |
| 26 | * Activate Code Manager |
| 27 | * |
| 28 | * The user must have the appropriate privileges to perform this operation. |
| 29 | * |
| 30 | * For single site installation {@see Code_Manager_Switch::activate_blog()} will be called. For multi site |
| 31 | * installations {@see Code_Manager_Switch::activate_blog()} must be called for every blog. |
| 32 | * |
| 33 | * IMPORTANT!!! |
| 34 | * |
| 35 | * For blogs installed on multi site installations after activation of the plugin, activation of the plugin for |
| 36 | * that blog will not be performed if the plugin is network activated. In that case the admin user of the blog |
| 37 | * will receive a message when viewing a plugin page with an option to follow these steps manually. |
| 38 | * |
| 39 | * @since 1.0.0 |
| 40 | * |
| 41 | * @see Code_Manager_Switch::activate_blog() |
| 42 | */ |
| 43 | public static function activate() { |
| 44 | if ( current_user_can( 'activate_plugins' ) ) { |
| 45 | // Activate plugin. |
| 46 | if ( is_multisite() ) { |
| 47 | global $wpdb; |
| 48 | // Multisite installation. |
| 49 | $blogids = $wpdb->get_col( "select blog_id from $wpdb->blogs" ); // db call ok; no-cache ok. |
| 50 | foreach ( $blogids as $blog_id ) { |
| 51 | // Uninstall blog. |
| 52 | switch_to_blog( $blog_id ); |
| 53 | self::activate_blog(); |
| 54 | restore_current_blog(); |
| 55 | } |
| 56 | } else { |
| 57 | // Single site installation. |
| 58 | self::activate_blog(); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Activate blog |
| 65 | * |
| 66 | * The user must have the appropriate privileges to perform this operation. |
| 67 | * |
| 68 | * Creates plugin table (if not found) and updates plugin version. This action is performed on the |
| 69 | * 'active WordPress blog'. On single site there is only one blog. On multisite installations it must be |
| 70 | * executed for every blog. |
| 71 | * |
| 72 | * @since 1.0.0 |
| 73 | */ |
| 74 | protected static function activate_blog() { |
| 75 | if ( current_user_can( 'activate_plugins' ) ) { |
| 76 | if ( CODE_MANAGER_VERSION !== get_option( 'code_manager_version' ) ) { |
| 77 | $code_manager_model = new Code_Manager_Model(); |
| 78 | if ( ! $code_manager_model::table_exists() ) { |
| 79 | global $wpdb; |
| 80 | |
| 81 | $create_table = |
| 82 | 'CREATE TABLE {wp_prefix}code_manager |
| 83 | ( code_id mediumint(9) NOT NULL AUTO_INCREMENT |
| 84 | , code_name varchar(100) NOT NULL |
| 85 | , code_type varchar(30) NOT NULL |
| 86 | , code_enabled tinyint(1) NOT NULL DEFAULT 0 |
| 87 | , code text |
| 88 | , code_author varchar(100) |
| 89 | , code_description text |
| 90 | , PRIMARY KEY (code_id) |
| 91 | , UNIQUE KEY (code_name) |
| 92 | )'; |
| 93 | $wpdb->query( str_replace( '{wp_prefix}', $wpdb->prefix, $create_table ) ); |
| 94 | } |
| 95 | |
| 96 | update_option( 'code_manager_version', CODE_MANAGER_VERSION ); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Deactivate plugin WP Data Access |
| 103 | * |
| 104 | * On deactivation we leave the repository and options as they are in case the user wants to reactivate the |
| 105 | * plugin later again. Tables and options are deleted when the plugin is uninstalled. |
| 106 | * |
| 107 | * @since 1.0.0 |
| 108 | */ |
| 109 | public static function deactivate() { |
| 110 | // Add future deactivation code here. |
| 111 | } |
| 112 | |
| 113 | } |
| 114 |