PluginActiveEvents.php
3 weeks ago
PluginDeactivateEvents.php
3 weeks ago
PluginInitEvents.php
1 month ago
PluginLoadedEvents.php
3 weeks ago
PluginShortcode.php
2 months ago
TemplateRedirection.php
1 week ago
PluginActiveEvents.php
266 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin active events handler |
| 4 | * |
| 5 | * @package kirki |
| 6 | */ |
| 7 | |
| 8 | namespace Kirki\Manager; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; // Exit if accessed directly. |
| 12 | } |
| 13 | use Kirki\HelperFunctions; |
| 14 | |
| 15 | // use function Kirki\Framework\migrator; |
| 16 | |
| 17 | /** |
| 18 | * Do some task during plugin activation |
| 19 | */ |
| 20 | class PluginActiveEvents { |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * Initilize the class |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | public function __construct() { |
| 29 | $installed = get_option('kirki_installed' ); |
| 30 | if ( ! $installed ) { |
| 31 | update_option('kirki_installed', time() ); |
| 32 | } |
| 33 | |
| 34 | HelperFunctions::set_kirki_version_in_db(); |
| 35 | $this->deactivate_droip_plugin(); |
| 36 | |
| 37 | self::update_rbac(); |
| 38 | self::create_custom_tables(); |
| 39 | |
| 40 | // migrator()->run(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Deactivate droip/droip.php plugin to prevent conflicts |
| 45 | * |
| 46 | * @return void |
| 47 | */ |
| 48 | private function deactivate_droip_plugin() { |
| 49 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 50 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 51 | } |
| 52 | |
| 53 | $droip_plugin = 'droip/droip.php'; |
| 54 | |
| 55 | if ( is_plugin_active( $droip_plugin ) ) { |
| 56 | deactivate_plugins( $droip_plugin ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Create custom tables |
| 62 | * |
| 63 | * @return void |
| 64 | */ |
| 65 | public static function create_custom_tables() { |
| 66 | self::create_forms_table(); |
| 67 | self::create_collaboration_table(); |
| 68 | self::create_cm_reference_table(); |
| 69 | self::create_comments_table(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Update role manager data |
| 74 | * |
| 75 | * @return void |
| 76 | */ |
| 77 | public static function update_rbac() { |
| 78 | global $wp_roles; |
| 79 | $role_names = $wp_roles ? $wp_roles->role_names : []; |
| 80 | |
| 81 | if ( is_array( $role_names ) && count( $role_names ) ) { |
| 82 | foreach ( $role_names as $key => $value ) { |
| 83 | $exist = get_option('kirki_' . $key ); |
| 84 | if ( ! $exist ) { |
| 85 | if ( in_array( $key, KIRKI_USERS_DEFAULT_FULL_ACCESS, true ) ) { |
| 86 | add_option('kirki_' . $key, KIRKI_ACCESS_LEVELS['FULL_ACCESS'] ); |
| 87 | } else { |
| 88 | add_option('kirki_' . $key, KIRKI_ACCESS_LEVELS['NO_ACCESS'] ); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Create forms table |
| 97 | * |
| 98 | * @return void |
| 99 | */ |
| 100 | private static function create_forms_table() { |
| 101 | global $wpdb; |
| 102 | $charset_collate = $wpdb->get_charset_collate(); |
| 103 | |
| 104 | $form_table = $wpdb->prefix . KIRKI_FORM_TABLE; |
| 105 | $form_table_query = "CREATE TABLE IF NOT EXISTS $form_table ( |
| 106 | id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, |
| 107 | post_id BIGINT UNSIGNED NOT NULL, |
| 108 | form_ele_id VARCHAR(255) NOT NULL, |
| 109 | name VARCHAR(255) DEFAULT 'My Kirki Form', |
| 110 | created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 111 | updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
| 112 | PRIMARY KEY (id) |
| 113 | ) $charset_collate;"; |
| 114 | |
| 115 | $form_data_table = $wpdb->prefix . KIRKI_FORM_DATA_TABLE; |
| 116 | $form_data_table_query = "CREATE TABLE IF NOT EXISTS $form_data_table ( |
| 117 | id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, |
| 118 | form_id BIGINT UNSIGNED NOT NULL, |
| 119 | user_id BIGINT UNSIGNED DEFAULT NULL, |
| 120 | session_id VARCHAR(255) NOT NULL, |
| 121 | timestamp BIGINT NOT NULL, |
| 122 | input_key VARCHAR(255) NOT NULL, |
| 123 | input_value VARCHAR(2000) NOT NULL, |
| 124 | input_type VARCHAR(100) NOT NULL DEFAULT 'text', |
| 125 | created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 126 | updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
| 127 | PRIMARY KEY (id), |
| 128 | KEY form_id (form_id) |
| 129 | -- FOREIGN KEY constraints are not handled by dbDelta reliably |
| 130 | ) $charset_collate;"; |
| 131 | |
| 132 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 133 | dbDelta( $form_table_query ); |
| 134 | dbDelta( $form_data_table_query ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Create forms table |
| 139 | * |
| 140 | * @return void |
| 141 | */ |
| 142 | private static function create_collaboration_table() { |
| 143 | global $wpdb; |
| 144 | $charset_collate = $wpdb->get_charset_collate(); |
| 145 | |
| 146 | $table_name = $wpdb->prefix . KIRKI_COLLABORATION_TABLE; |
| 147 | $sql1 = "CREATE TABLE IF NOT EXISTS $table_name ( |
| 148 | id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY, |
| 149 | user_id bigint(20) NOT NULL, |
| 150 | session_id varchar(50) NOT NULL, |
| 151 | parent varchar(255) NOT NULL COMMENT 'table name', |
| 152 | parent_id bigint(20) COMMENT 'table row id', |
| 153 | data longtext COMMENT 'json data', |
| 154 | status int(1) NOT NULL COMMENT '1=active, 2=sent, 0=expire', |
| 155 | created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 156 | updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP |
| 157 | ) $charset_collate;"; |
| 158 | |
| 159 | $table_name = $wpdb->prefix . KIRKI_COLLABORATION_TABLE . '_connected'; |
| 160 | $sql2 = "CREATE TABLE IF NOT EXISTS $table_name ( |
| 161 | id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY, |
| 162 | user_id bigint(20) NOT NULL, |
| 163 | post_id bigint(20) NOT NULL, |
| 164 | session_id varchar(50) NOT NULL, |
| 165 | created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 166 | updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP |
| 167 | ) $charset_collate;"; |
| 168 | |
| 169 | $table_name = $wpdb->prefix . KIRKI_COLLABORATION_TABLE . '_sent'; |
| 170 | $sql3 = "CREATE TABLE IF NOT EXISTS $table_name ( |
| 171 | id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY, |
| 172 | collaboration_id bigint(20) NOT NULL, |
| 173 | session_id varchar(50) NOT NULL, |
| 174 | created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 175 | updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP |
| 176 | ) $charset_collate;"; |
| 177 | |
| 178 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 179 | dbDelta( $sql1 ); |
| 180 | dbDelta( $sql2 ); |
| 181 | dbDelta( $sql3 ); |
| 182 | } |
| 183 | |
| 184 | public static function add_post_field_in_collaboaration_connected_table() { |
| 185 | global $wpdb; |
| 186 | $table_name = $wpdb->prefix . KIRKI_COLLABORATION_TABLE . '_connected'; |
| 187 | $columns = $wpdb->get_results( "SHOW COLUMNS FROM $table_name LIKE 'post_id'" ); |
| 188 | |
| 189 | if ( empty( $columns ) ) { |
| 190 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 191 | $wpdb->query( |
| 192 | "ALTER TABLE $table_name ADD COLUMN post_id bigint(20) NOT NULL AFTER user_id" |
| 193 | ); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Kirki reference table |
| 199 | * |
| 200 | * @return void |
| 201 | */ |
| 202 | private static function create_cm_reference_table() { |
| 203 | /** |
| 204 | * table name -> wp_kirki_cm_reference |
| 205 | * table key -> post_id (foreign key) -> wp_posts -> ID (cascade) |
| 206 | * table key -> field_meta_key (foreign key) -> wp_postmeta -> meta_key (cascade) |
| 207 | * table key -> ref_post_id (foreign key) -> wp_posts -> ID (cascade) |
| 208 | */ |
| 209 | global $wpdb; |
| 210 | $charset_collate = $wpdb->get_charset_collate(); |
| 211 | |
| 212 | $table_name = $wpdb->prefix . KIRKI_CM_REFERENCE_TABLE; |
| 213 | $sql = "CREATE TABLE IF NOT EXISTS $table_name ( |
| 214 | id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 215 | post_id BIGINT(20) UNSIGNED NOT NULL, |
| 216 | field_meta_key VARCHAR(255) NOT NULL, |
| 217 | ref_post_id BIGINT(20) UNSIGNED NOT NULL, |
| 218 | PRIMARY KEY (id), |
| 219 | INDEX (post_id), |
| 220 | INDEX (ref_post_id), |
| 221 | FOREIGN KEY (post_id) REFERENCES {$wpdb->prefix}posts(ID) ON DELETE CASCADE, |
| 222 | FOREIGN KEY (ref_post_id) REFERENCES {$wpdb->prefix}posts(ID) ON DELETE CASCADE |
| 223 | ) ENGINE=InnoDB $charset_collate;"; |
| 224 | |
| 225 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 226 | dbDelta( $sql ); |
| 227 | } |
| 228 | |
| 229 | private static function create_comments_table() { |
| 230 | global $wpdb; |
| 231 | $charset_collate = $wpdb->get_charset_collate(); |
| 232 | $table_suffix = KIRKI_COMMENTS_TABLE; |
| 233 | $table_name = $wpdb->prefix . $table_suffix; |
| 234 | |
| 235 | $sql = "CREATE TABLE IF NOT EXISTS $table_name ( |
| 236 | id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 237 | user_id BIGINT(20) UNSIGNED NOT NULL, |
| 238 | post_id BIGINT(20) UNSIGNED NOT NULL, |
| 239 | parent_id BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, |
| 240 | comment TEXT NOT NULL, |
| 241 | meta_data VARCHAR(255) NOT NULL, |
| 242 | status INT(1) NOT NULL COMMENT '1=active, 2=resolved, 0=deleted', |
| 243 | created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 244 | updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
| 245 | PRIMARY KEY (id), |
| 246 | KEY post_id (post_id), |
| 247 | FOREIGN KEY (post_id) REFERENCES {$wpdb->prefix}posts(ID) ON DELETE CASCADE |
| 248 | ) ENGINE=InnoDB $charset_collate;"; |
| 249 | |
| 250 | $seen_table_name = $wpdb->prefix . $table_suffix . '_seen'; |
| 251 | $sql2 = "CREATE TABLE IF NOT EXISTS $seen_table_name ( |
| 252 | id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 253 | comment_id BIGINT(20) UNSIGNED NOT NULL, |
| 254 | user_id BIGINT(20) UNSIGNED NOT NULL, |
| 255 | PRIMARY KEY (id), |
| 256 | UNIQUE KEY user_comment_unique (user_id, comment_id), |
| 257 | FOREIGN KEY (comment_id) REFERENCES $table_name(id) ON DELETE CASCADE |
| 258 | ) ENGINE=InnoDB $charset_collate;"; |
| 259 | |
| 260 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 261 | dbDelta( $sql ); |
| 262 | dbDelta( $sql2 ); |
| 263 | } |
| 264 | |
| 265 | } |
| 266 |