admin
1 week ago
auto-insert
4 months ago
conditional-logic
6 months ago
execute
5 months ago
generator
6 months ago
lite
9 months ago
capabilities.php
2 years ago
class-wpcode-abilities-api.php
1 week ago
class-wpcode-admin-bar-info.php
2 years ago
class-wpcode-auto-insert.php
4 months ago
class-wpcode-capabilities.php
3 years ago
class-wpcode-conditional-logic.php
4 months ago
class-wpcode-error.php
2 years ago
class-wpcode-file-cache.php
1 year ago
class-wpcode-file-logger.php
1 year ago
class-wpcode-generator.php
4 months ago
class-wpcode-install.php
1 year ago
class-wpcode-library-auth.php
1 year ago
class-wpcode-library.php
1 week ago
class-wpcode-settings.php
6 months ago
class-wpcode-smart-tags.php
11 months ago
class-wpcode-snippet-cache.php
4 months ago
class-wpcode-snippet-execute.php
1 year ago
class-wpcode-snippet.php
1 year ago
compat.php
2 years ago
global-output.php
1 year ago
helpers.php
1 year ago
icons.php
5 months ago
ihaf.php
3 years ago
legacy.php
3 years ago
pluggable.php
2 years ago
post-type.php
1 week ago
safe-mode.php
11 months ago
shortcode.php
2 years ago
class-wpcode-install.php
252 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Logic to run on plugin install. |
| 4 | * |
| 5 | * @package WPCode |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Class WPCode_Install. |
| 14 | */ |
| 15 | class WPCode_Install { |
| 16 | |
| 17 | /** |
| 18 | * WPCode_Install constructor. |
| 19 | */ |
| 20 | public function __construct() { |
| 21 | register_activation_hook( WPCODE_FILE, array( $this, 'activate' ) ); |
| 22 | add_action( 'admin_init', array( $this, 'maybe_run_install' ) ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Activation hook. |
| 27 | * |
| 28 | * @return void |
| 29 | */ |
| 30 | public function activate() { |
| 31 | // Add capabilities on activation as deleting the plugin removes them |
| 32 | // but the option used in the `maybe_run_install` method below is not |
| 33 | // removed so the capabilities are not added back. |
| 34 | WPCode_Capabilities::add_capabilities(); |
| 35 | |
| 36 | // Use an action to have a single activation hook plugin-wide. |
| 37 | do_action( 'wpcode_plugin_activation' ); |
| 38 | |
| 39 | set_transient( 'wpcode_just_activated', class_exists( 'WPCode_License' ) ? 'pro' : 'lite', 60 ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Install routine to run on plugin activation. |
| 44 | * Runs on admin_init so that we also handle updates. |
| 45 | * The ihaf_activated option was used by IHAF 1.6 and the key "lite" is for the activation date |
| 46 | * of that version of the plugin. In the WPCode plugin we use the "wpcode" key, so we have the update date |
| 47 | * and install the demo data. |
| 48 | * |
| 49 | * @return void |
| 50 | */ |
| 51 | public function maybe_run_install() { |
| 52 | if ( ! is_admin() ) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | $activated = get_option( 'ihaf_activated', array() ); |
| 57 | |
| 58 | if ( ! is_array( $activated ) ) { |
| 59 | $activated = array(); |
| 60 | } |
| 61 | |
| 62 | if ( empty( $activated['wpcode'] ) ) { |
| 63 | $activated['wpcode'] = time(); |
| 64 | |
| 65 | update_option( 'ihaf_activated', $activated ); |
| 66 | |
| 67 | // Add custom capabilities. |
| 68 | WPCode_Capabilities::add_capabilities(); |
| 69 | |
| 70 | // The option was empty so let's add the demo data. |
| 71 | $this->add_demo_data(); |
| 72 | |
| 73 | do_action( 'wpcode_install' ); |
| 74 | } |
| 75 | |
| 76 | // Maybe run manually just one time. |
| 77 | $install = get_option( 'wpcode_install', false ); |
| 78 | |
| 79 | if ( ! empty( $install ) ) { |
| 80 | $this->activate(); |
| 81 | delete_option( 'wpcode_install' ); |
| 82 | } |
| 83 | |
| 84 | // Let's run an upgrade routine. |
| 85 | if ( empty( $activated['version'] ) ) { |
| 86 | $this->update_2_1_0(); |
| 87 | } |
| 88 | |
| 89 | if ( isset( $activated['version'] ) && version_compare( $activated['version'], WPCODE_VERSION, '=' ) ) { |
| 90 | // If the version is identical just skip. |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | if ( isset( $activated['version'] ) && version_compare( $activated['version'], '2.1.4', '<' ) ) { |
| 95 | $this->update_2_1_4(); |
| 96 | } |
| 97 | |
| 98 | if ( isset( $activated['version'] ) && version_compare( $activated['version'], '2.1.5', '<' ) ) { |
| 99 | $this->update_2_1_5(); |
| 100 | } |
| 101 | |
| 102 | // Give other plugins a chance to run an upgrade routine. |
| 103 | do_action( 'wpcode_before_version_update', $activated ); |
| 104 | |
| 105 | $activated['version'] = WPCODE_VERSION; |
| 106 | update_option( 'ihaf_activated', $activated ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Upgrade routine for 2.1.0. |
| 111 | * Empty the library cache to get the new snippets. |
| 112 | * |
| 113 | * @return void |
| 114 | */ |
| 115 | public function update_2_1_0() { |
| 116 | if ( isset( wpcode()->library ) ) { |
| 117 | wpcode()->library->delete_cache(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Upgrade routine for 2.1.4. |
| 123 | * Convert recently deactivated to the new error system. |
| 124 | * |
| 125 | * @return void |
| 126 | */ |
| 127 | public function update_2_1_4() { |
| 128 | // Let's find all the snippets with the _wpcode_recently_deactivated meta key set. |
| 129 | $snippets = get_posts( |
| 130 | array( |
| 131 | 'post_type' => 'wpcode', |
| 132 | 'posts_per_page' => - 1, |
| 133 | 'post_status' => 'any', |
| 134 | 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 135 | array( |
| 136 | 'key' => '_wpcode_recently_deactivated', |
| 137 | 'compare' => 'EXISTS', |
| 138 | ), |
| 139 | ), |
| 140 | ) |
| 141 | ); |
| 142 | // Let's loop through the snippets and convert them to the new error system. |
| 143 | foreach ( $snippets as $snippet ) { |
| 144 | $snippet = new WPCode_Snippet( $snippet->ID ); |
| 145 | $error = array( |
| 146 | 'message' => esc_html__( 'Snippet was deactivated due to an error.', 'insert-headers-and-footers' ), |
| 147 | 'time' => get_post_meta( $snippet->get_id(), '_wpcode_recently_deactivated', true ), |
| 148 | 'wpc_type' => 'deactivated', |
| 149 | ); |
| 150 | $error_line = get_post_meta( $snippet->get_id(), '_wpcode_recently_deactivated_error_line', true ); |
| 151 | if ( ! empty( $error_line ) ) { |
| 152 | $error['error_line'] = $error_line; |
| 153 | } |
| 154 | $snippet->set_last_error( $error ); |
| 155 | |
| 156 | // Remove the old meta keys. |
| 157 | delete_post_meta( $snippet->get_id(), '_wpcode_recently_deactivated' ); |
| 158 | delete_post_meta( $snippet->get_id(), '_wpcode_recently_deactivated_error_line' ); |
| 159 | |
| 160 | wpcode()->error->clear_snippets_errors(); |
| 161 | } |
| 162 | |
| 163 | $this->add_columns_to_hidden( array( 'id', 'code_type', 'shortcode' ) ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Upgrade routine for 2.1.5. |
| 168 | * Add the priority column to the hidden columns. |
| 169 | * |
| 170 | * @return void |
| 171 | */ |
| 172 | public function update_2_1_5() { |
| 173 | $this->add_columns_to_hidden( array( 'priority' ) ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Add columns to the hidden columns for users that set their screen settings. |
| 178 | * |
| 179 | * @param array $new_columns The columns to add. |
| 180 | * |
| 181 | * @return void |
| 182 | */ |
| 183 | public function add_columns_to_hidden( $new_columns ) { |
| 184 | // Let's add the new columns to the hidden array for users that set their screen settings. |
| 185 | $meta_key = 'managetoplevel_page_wpcodecolumnshidden'; |
| 186 | |
| 187 | $users = get_users( |
| 188 | array( |
| 189 | 'meta_key' => $meta_key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 190 | 'meta_compare' => 'EXISTS', |
| 191 | 'fields' => 'ID', |
| 192 | ) |
| 193 | ); |
| 194 | |
| 195 | foreach ( $users as $user_id ) { |
| 196 | $columns = get_user_meta( $user_id, $meta_key, true ); |
| 197 | if ( ! is_array( $columns ) ) { |
| 198 | $columns = array(); |
| 199 | } |
| 200 | $columns = array_merge( $columns, $new_columns ); |
| 201 | update_user_meta( $user_id, $meta_key, $columns ); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Add some example snippets in a new installation. |
| 207 | * |
| 208 | * @return void |
| 209 | */ |
| 210 | public function add_demo_data() { |
| 211 | $snippets = array( |
| 212 | array( |
| 213 | 'title' => __( 'Display a message after the 1st paragraph of posts', 'insert-headers-and-footers' ), |
| 214 | 'code' => 'Thank you for reading this post, don\'t forget to subscribe!', |
| 215 | 'code_type' => 'text', |
| 216 | 'auto_insert' => 1, |
| 217 | 'location' => 'after_paragraph', |
| 218 | 'insert_number' => 1, |
| 219 | 'tags' => array( |
| 220 | 'sample', |
| 221 | 'message', |
| 222 | ), |
| 223 | ), |
| 224 | array( |
| 225 | 'title' => __( 'Completely Disable Comments', 'insert-headers-and-footers' ), |
| 226 | 'code' => "add_action('admin_init', function () {\r\n \/\/ Redirect any user trying to access comments page\r\n global \$pagenow;\r\n \r\n if (\$pagenow === 'edit-comments.php') {\r\n wp_safe_redirect(admin_url());\r\n exit;\r\n }\r\n\r\n \/\/ Remove comments metabox from dashboard\r\n remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');\r\n\r\n \/\/ Disable support for comments and trackbacks in post types\r\n foreach (get_post_types() as \$post_type) {\r\n if (post_type_supports(\$post_type, 'comments')) {\r\n remove_post_type_support(\$post_type, 'comments');\r\n remove_post_type_support(\$post_type, 'trackbacks');\r\n }\r\n }\r\n});\r\n\r\n\/\/ Close comments on the front-end\r\nadd_filter('comments_open', '__return_false', 20, 2);\r\nadd_filter('pings_open', '__return_false', 20, 2);\r\n\r\n\/\/ Hide existing comments\r\nadd_filter('comments_array', '__return_empty_array', 10, 2);\r\n\r\n\/\/ Remove comments page in menu\r\nadd_action('admin_menu', function () {\r\n remove_menu_page('edit-comments.php');\r\n});\r\n\r\n\/\/ Remove comments links from admin bar\r\nadd_action('init', function () {\r\n if (is_admin_bar_showing()) {\r\n remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);\r\n }\r\n});", |
| 227 | 'code_type' => 'php', |
| 228 | 'auto_insert' => 1, |
| 229 | 'location' => 'everywhere', |
| 230 | 'tags' => array( |
| 231 | 'sample', |
| 232 | 'disable', |
| 233 | 'comments', |
| 234 | ), |
| 235 | 'library_id' => 12, |
| 236 | ), |
| 237 | ); |
| 238 | |
| 239 | // The activation hook runs after `init` so our plugin's custom |
| 240 | // post type and custom taxonomies didn't have a chance to be registered. |
| 241 | wpcode_register_post_type(); |
| 242 | wpcode_register_taxonomies(); |
| 243 | |
| 244 | foreach ( $snippets as $snippet ) { |
| 245 | $new_snippet = new WPCode_Snippet( $snippet ); |
| 246 | $new_snippet->save(); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | new WPCode_Install(); |
| 252 |