| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) |
| 3 |
exit; |
| 4 |
|
| 5 |
|
| 6 |
function xyz_ips_network_install($networkwide) { |
| 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(); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
function xyz_ips_install(){ |
| 27 |
global $wpdb; |
| 28 |
|
| 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 |
} |
| 58 |
|
| 59 |
if(get_option('xyz_ips_sort_field_name')==''){ |
| 60 |
add_option('xyz_ips_sort_field_name','id'); |
| 61 |
} |
| 62 |
|
| 63 |
if(get_option('xyz_credit_link') == ""){ |
| 64 |
add_option("xyz_credit_link",0); |
| 65 |
} |
| 66 |
|
| 67 |
if(get_option('xyz_ips_credit_dismiss') == ""){ |
| 68 |
add_option("xyz_ips_credit_dismiss",0); |
| 69 |
} |
| 70 |
|
| 71 |
if(get_option('xyz_ips_premium_version_ads')==""){ |
| 72 |
add_option('xyz_ips_premium_version_ads',1); |
| 73 |
} |
| 74 |
|
| 75 |
if(get_option('xyz_ips_auto_insert')==""){ |
| 76 |
add_option('xyz_ips_auto_insert',1); |
| 77 |
} |
| 78 |
|
| 79 |
if(get_option('xyz_ips_auto_exception')==""){ |
| 80 |
add_option('xyz_ips_auto_exception',1); |
| 81 |
} |
| 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 |
?> |
| 157 |
|