← Back
Editing: class-GD-cORe-activator.php
<?php /** * Fired during plugin activation. * * @since 3.0.0 */ class GD_cORe_Activator { /** * Creates custom database tables needed by the plugin * * @since 4.0.0 */ public static function create_database_tables() { global $wpdb; $charset_collate = $wpdb->get_charset_collate(); // Load Audits class to get the table name constant if (!class_exists('GD_cORe\handlers\Audits')) { require_once plugin_dir_path(dirname(__FILE__)) . 'admin/handlers/class-main-handler.php'; require_once plugin_dir_path(dirname(__FILE__)) . 'admin/handlers/class-audit-handler.php'; } // Create Lighthouse reports table $table_name = $wpdb->prefix . GD_cORe\handlers\Audits::LIGHTHOUSE_TABLE; $sql = "CREATE TABLE $table_name ( id bigint(20) NOT NULL AUTO_INCREMENT, report_id varchar(255) NOT NULL, url varchar(255) NOT NULL, date_created datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, report_data longtext NOT NULL, PRIMARY KEY (id), KEY report_id (report_id) ) $charset_collate;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; // Use dbDelta to create/update the table $result = dbDelta($sql); // Log any errors if (!empty($wpdb->last_error)) { error_log('GD cORe: Database table creation error: ' . $wpdb->last_error); } return $result; } /** * Short Description. ( use period ). * * Long Description. * * @since 3.0.0 */ public static function activate() { self::create_database_tables(); // Set the database version option update_option('gd_core_db_version', GD_cORe_VERSION); $rules = []; $rules[] = '<IfModule mod_rewrite.c>'; $rules[] = 'RewriteEngine On'; $rules[] = 'RewriteCond %{DOCUMENT_ROOT}/wp-content/smush-webp/disable_smush_webp !-f'; $rules[] = 'RewriteCond %{HTTP_ACCEPT} image/webp'; $rules[] = 'RewriteCond %{DOCUMENT_ROOT}/wp-content/smush-webp/$1.webp -f'; $rules[] = 'RewriteRule wp-content/uploads/(.*.(?:png|jpe?g))$ wp-content/smush-webp/$1.webp [NC,T=image/webp]'; $rules[] = '</IfModule>'; $rules[] = '<IfModule mod_headers.c>'; $rules[] = 'Header append Vary Accept env=WEBP_image'; $rules[] = '</IfModule>'; $rules[] = '<IfModule mod_mime.c>'; $rules[] = 'AddType image/webp .webp'; $rules[] = '</IfModule>'; insert_with_markers( get_home_path().'.htaccess', 'GD_cORe', $rules ); if ( get_option( 'gd_core_dct_data' ) === false) { update_option( 'gd_core_dct_data', '' ); } // handles upgrade from pre 3.0.0 core versions if ( get_option( 'pwscore_setup' ) && !get_option( 'gd_core_setup' ) ) { update_option( 'gd_core_setup', get_option( 'pwscore_setup' ) ); delete_option( 'pwscore_setup' ); } // No longer need to store access tokens if ( get_option( 'pwscore_setup' ) || empty( get_option( 'pwscore_setup' ) ) ) { delete_option( 'pwscore_setup' ); } // Clean up old authentication tokens delete_option( 'pwscore_at' ); delete_option( 'gd_core_at' ); // leaving in-case rollback is needed // delete_option( 'pwscore_sk' ); // delete_option( 'gd_core_sk' ); if ( get_option( 'pwscore_required' ) || empty( get_option( 'pwscore_required' ) ) ) { delete_option( 'pwscore_required' ); } if ( get_option( 'pwsCoreWpmudevKEY' ) || empty( get_option( 'pwsCoreWpmudevKEY' ) ) ) { delete_option( 'pwsCoreWpmudevKEY' ); } if ( class_exists( 'WPaaS\API' ) ) { $wpaas_api = new WPaaS\API(); $wpaas_api->toggle_rum( true ); } if ( 'en_CA' === get_option( 'WPLANG' ) ) { update_option( 'WPLANG', '' ); } GD_cORe_Activator::uninstall_default_plugins(); GD_cORe_Activator::delete_default_posts(); } /** * Uninstalls all default posts. * * @since 3.3.1 **/ public static function delete_default_posts() { $default_post_ids = [ 1, 2, 3 ]; $default_posts_keyword_list = [ 'sample', 'privacy', 'hello world', 'Hello world!', 'Sample Page', 'Privacy Policy' ]; $default_statuses = [ 'publish', 'draft', 'auto-draft' ]; $post_and_page_args = array( 'include' => $default_post_ids, 'post_status' => $default_statuses ); $current_posts = self::get_pages_and_posts( $post_and_page_args ); // Iterate posts. foreach ( $current_posts as $post ) { if ( in_array( $post->post_title, $default_posts_keyword_list ) && self::is_post_unmodified( $post ) ) { wp_delete_post( $post->ID, true ); } } } /** * returns all pages and posts given a set of args * * @since 3.5.2 **/ public static function get_pages_and_posts( $args ) { return array_merge( get_posts( $args ), get_pages( $args ) ); } /** * checks if a posts modified date is the same as its creation date. * * @since 3.5.2 **/ public static function is_post_unmodified( $post ) { if ( strtotime( $post->post_date ) === strtotime( $post->post_modified ) ) { return true; } return false; } /** * Uninstalls all default plugins. * * @since 3.3.1 **/ public static function uninstall_default_plugins() { $current_plugins = get_plugins(); $default_plugins_list = [ 'hello-dolly/hello.php' ]; foreach ( $default_plugins_list as $plugin ) { if ( !array_key_exists( $plugin, $current_plugins ) ) { continue; } deactivate_plugins( [ $plugin ] ); delete_plugins( [ $plugin ] ); } } }
Save File
Cancel