| @@ -1,65 +1,156 @@ | ||
| 1 | 1 | <?php |
| 2 | +if ( ! defined( 'ABSPATH' ) ) | |
| 3 | + exit; | |
| 2 | 4 | |
| 5 | + | |
| 3 | 6 | function xyz_ips_network_install($networkwide) { |
| 4 | - global $wpdb; | |
| 5 | - | |
| 6 | - if (function_exists('is_multisite') && is_multisite()) { | |
| 7 | - // check if it is a network activation - if so, run the activation function for each blog id | |
| 8 | - if ($networkwide) { | |
| 9 | - $old_blog = $wpdb->blogid; | |
| 10 | - // Get all blog ids | |
| 11 | - $blogids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); | |
| 12 | - foreach ($blogids as $blog_id) { | |
| 13 | - switch_to_blog($blog_id); | |
| 14 | - xyz_ips_install(); | |
| 15 | - } | |
| 16 | - switch_to_blog($old_blog); | |
| 17 | - return; | |
| 18 | - } | |
| 19 | - } | |
| 20 | - xyz_ips_install(); | |
| 7 | + global $wpdb; | |
| 8 | + if (function_exists('is_multisite') && is_multisite()) { | |
| 9 | + // check if it is a network activation - if so, run the activation function for each blog id | |
| 10 | + if ($networkwide) { | |
| 11 | + $old_blog = $wpdb->blogid; | |
| 12 | + // Get all blog ids | |
| 13 | + $blogids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); | |
| 14 | + foreach ($blogids as $blog_id) { | |
| 15 | + switch_to_blog($blog_id); | |
| 16 | + xyz_ips_install(); | |
| 17 | + } | |
| 18 | + switch_to_blog($old_blog); | |
| 19 | + return; | |
| 20 | + } | |
| 21 | + } | |
| 22 | + xyz_ips_install(); | |
| 21 | 23 | } |
| 22 | 24 | |
| 23 | 25 | |
| 24 | 26 | function xyz_ips_install(){ |
| 25 | - | |
| 26 | - global $wpdb; | |
| 27 | - //global $current_user; get_currentuserinfo(); | |
| 28 | - | |
| 27 | + global $wpdb; | |
| 29 | 28 | |
| 30 | - if(get_option('xyz_ips_sort_order')=='') | |
| 31 | - { | |
| 32 | - add_option('xyz_ips_sort_order','desc'); | |
| 33 | - } | |
| 34 | - if(get_option('xyz_ips_sort_field_name')=='') | |
| 35 | - { | |
| 36 | - add_option('xyz_ips_sort_field_name','id'); | |
| 37 | - } | |
| 38 | - | |
| 39 | - | |
| 40 | - | |
| 41 | - | |
| 42 | - | |
| 43 | - if(get_option('xyz_credit_link') == "") | |
| 44 | - { | |
| 45 | - add_option("xyz_credit_link",0); | |
| 46 | - } | |
| 29 | + $plugin_name = 'xyz-wp-insert-code-snippet/xyz-wp-insert-code-snippet.php'; | |
| 30 | + if ( is_plugin_active( $plugin_name ) ) { | |
| 31 | + | |
| 32 | + wp_die( | |
| 33 | + sprintf( | |
| 34 | + /* translators: 1: Plugin name, 2: Deactivate target, 3: Link to plugins page */ | |
| 35 | + esc_html__( 'The plugin %1$s cannot be activated unless the %2$s is deactivated. Back to %3$s.', 'insert-php-code-snippet' ), | |
| 36 | + '<strong>Insert PHP Code Snippet</strong>', | |
| 37 | + '<strong>premium version</strong>', | |
| 38 | + sprintf( | |
| 39 | + '<a href="%s">%s</a>', | |
| 40 | + esc_url( admin_url( 'plugins.php' ) ), | |
| 41 | + esc_html__( 'Plugin Installation', 'insert-php-code-snippet' ) | |
| 42 | + ) | |
| 43 | + ) | |
| 44 | + ); | |
| 45 | + } | |
| 46 | +if ( version_compare( PHP_VERSION, '7.0.0', '<' ) ) { | |
| 47 | + wp_die( | |
| 48 | + sprintf( | |
| 49 | + 'This plugin requires PHP version 7.0 or higher. You are using PHP %s. <a href="%s">Go back to Plugins page</a>.', | |
| 50 | + PHP_VERSION, | |
| 51 | + esc_url( admin_url( 'plugins.php' ) ) | |
| 52 | + ) | |
| 53 | + ); | |
| 54 | +} | |
| 55 | + if(get_option('xyz_ips_sort_order')==''){ | |
| 56 | + add_option('xyz_ips_sort_order','desc'); | |
| 57 | + } | |
| 47 | 58 | |
| 48 | - add_option('xyz_ips_limit',20); | |
| 49 | - $queryInsertPhp = "CREATE TABLE IF NOT EXISTS ".$wpdb->prefix."xyz_ips_short_code ( | |
| 50 | - `id` int NOT NULL AUTO_INCREMENT, | |
| 51 | - `title` varchar(1000) COLLATE utf8_unicode_ci NOT NULL, | |
| 52 | - `content` longtext COLLATE utf8_unicode_ci NOT NULL, | |
| 53 | - `short_code` varchar(2000) COLLATE utf8_unicode_ci NOT NULL, | |
| 54 | - `status` int NOT NULL, | |
| 55 | - PRIMARY KEY (`id`) | |
| 56 | - ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1"; | |
| 57 | - $wpdb->query($queryInsertPhp); | |
| 58 | -} | |
| 59 | + if(get_option('xyz_ips_sort_field_name')==''){ | |
| 60 | + add_option('xyz_ips_sort_field_name','id'); | |
| 61 | + } | |
| 59 | 62 | |
| 60 | -register_activation_hook( XYZ_INSERT_PHP_PLUGIN_FILE ,'xyz_ips_network_install'); | |
| 63 | + if(get_option('xyz_credit_link') == ""){ | |
| 64 | + add_option("xyz_credit_link",0); | |
| 65 | + } | |
| 61 | 66 | |
| 67 | + if(get_option('xyz_ips_credit_dismiss') == ""){ | |
| 68 | + add_option("xyz_ips_credit_dismiss",0); | |
| 69 | + } | |
| 62 | 70 | |
| 71 | + if(get_option('xyz_ips_premium_version_ads')==""){ | |
| 72 | + add_option('xyz_ips_premium_version_ads',1); | |
| 73 | + } | |
| 63 | 74 | |
| 75 | + if(get_option('xyz_ips_auto_insert')==""){ | |
| 76 | + add_option('xyz_ips_auto_insert',1); | |
| 77 | + } | |
| 64 | 78 | |
| 79 | + if(get_option('xyz_ips_auto_exception')==""){ | |
| 80 | + add_option('xyz_ips_auto_exception',1); | |
| 81 | + } | |
| 65 | 82 | |
| 83 | + $xyz_ips_installed_date = get_option('xyz_ips_installed_date'); | |
| 84 | + if ($xyz_ips_installed_date=="") { | |
| 85 | + $xyz_ips_installed_date = time(); | |
| 86 | + update_option('xyz_ips_installed_date', $xyz_ips_installed_date); | |
| 87 | + } | |
| 88 | + add_option('xyz_ips_limit',20); | |
| 89 | + add_option('xyz_ips_exception_email',"0"); | |
| 90 | + add_option('xyz_ips_exec_in_editor','0'); | |
| 91 | + $charset_collate = $wpdb->get_charset_collate(); | |
| 92 | + $queryInsertPhp = "CREATE TABLE IF NOT EXISTS ".$wpdb->prefix."xyz_ips_short_code ( | |
| 93 | +`id` int NOT NULL AUTO_INCREMENT, | |
| 94 | +`title` varchar(1000) NOT NULL, | |
| 95 | + `description` TEXT NULL , | |
| 96 | +`content` longtext NOT NULL, | |
| 97 | +`short_code` varchar(2000) NOT NULL, | |
| 98 | +`status` int NOT NULL, | |
| 99 | +PRIMARY KEY (`id`) | |
| 100 | +) ENGINE=InnoDB ".$charset_collate." AUTO_INCREMENT=1"; | |
| 101 | + $wpdb->query($queryInsertPhp); | |
| 102 | + | |
| 103 | + | |
| 104 | + $tblcolums = $wpdb->get_col("SHOW COLUMNS FROM ".$wpdb->prefix."xyz_ips_short_code"); | |
| 105 | + if(!(in_array("insertionMethod", $tblcolums))) | |
| 106 | + $wpdb->query("ALTER TABLE ".$wpdb->prefix."xyz_ips_short_code ADD insertionMethod int NOT NULL default 2"); | |
| 107 | + if(!(in_array("insertionLocation", $tblcolums))) | |
| 108 | + $wpdb->query("ALTER TABLE ".$wpdb->prefix."xyz_ips_short_code ADD insertionLocation int NOT NULL default 0"); | |
| 109 | + if(!(in_array("insertionLocationType", $tblcolums))) | |
| 110 | + $wpdb->query("ALTER TABLE ".$wpdb->prefix."xyz_ips_short_code ADD insertionLocationType int NOT NULL default 0"); | |
| 111 | + if(!(in_array("description", $tblcolums))) | |
| 112 | + $wpdb->query("ALTER TABLE ".$wpdb->prefix."xyz_ips_short_code ADD description TEXT NULL "); | |
| 113 | + $table_name = $wpdb->prefix . 'xyz_ips_usage'; | |
| 114 | + $charset_collate = $wpdb->get_charset_collate(); | |
| 115 | + $sql = "CREATE TABLE {$table_name} ( | |
| 116 | + post_id BIGINT(20) UNSIGNED NOT NULL, | |
| 117 | + snippet_id BIGINT(20) UNSIGNED NOT NULL, | |
| 118 | + post_type VARCHAR(20) NOT NULL, | |
| 119 | + PRIMARY KEY (post_id, snippet_id), | |
| 120 | + KEY post_id (post_id), | |
| 121 | + KEY snippet_id (snippet_id) | |
| 122 | + ) {$charset_collate};"; | |
| 123 | + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; | |
| 124 | + dbDelta($sql); | |
| 125 | + // Set sync flag only if not already set | |
| 126 | + if (get_option('xyz_ips_sync_needed') === false) { | |
| 127 | + add_option('xyz_ips_sync_needed', 1); | |
| 128 | + } | |
| 129 | + add_option('xyz_ips_show_snippet_usage',1);//default enable | |
| 130 | + //preview page | |
| 131 | + $slug = 'xyz-ics-preview-page'; | |
| 132 | + $title = 'Snippet Preview'; | |
| 133 | + // Use an option to store the ID | |
| 134 | + $preview_page_id = get_option('xyz_ips_preview_page_id'); | |
| 135 | + // Verify the stored ID actually exists in the DB | |
| 136 | + if (!$preview_page_id || get_post_status($preview_page_id) === false) { | |
| 137 | + // Final safety check: see if a page with this slug exists but isn't in our options | |
| 138 | + $existing_id = xyz_ips_page_exists_by_slug($slug); | |
| 139 | + if (!$existing_id) { | |
| 140 | + $post_id = wp_insert_post(array( | |
| 141 | + 'post_author' => get_current_user_id() ?: 1, // Avoid author 0 | |
| 142 | + 'post_name' => $slug, | |
| 143 | + 'post_title' => 'Snippet Preview', | |
| 144 | + 'post_content' => '', | |
| 145 | + 'post_status' => 'draft', | |
| 146 | + 'post_type' => 'page' | |
| 147 | + )); | |
| 148 | + update_option('xyz_ips_preview_page_id', $post_id); | |
| 149 | + } else { | |
| 150 | + // Page exists but we didn't have the ID saved; save it now. | |
| 151 | + update_option('xyz_ips_preview_page_id', $existing_id); | |
| 152 | + } | |
| 153 | + } | |
| 154 | +} | |
| 155 | +register_activation_hook( XYZ_INSERT_PHP_PLUGIN_FILE ,'xyz_ips_network_install'); | |
| 156 | +?> | |