PluginProbe
Database Reset / 3.24
Database Reset v3.24
2.3.2 3.0 3.0.1 3.0.2 3.1 3.15 3.16 3.17 3.18 3.19 3.20 3.21 3.22 3.23 3.24 3.25 trunk 1.0 1.2 1.2.1 1.2.2 1.3 1.4 2.0 2.1 All 27 releases
wordpress-database-reset / lib / helpers.php

helpers.php in Database Reset 3.24, at lib/helpers.php

147 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Check for command line constant
5 */
6 function is_command_line() {
7 return ( defined( 'WP_CLI' ) && WP_CLI );
8 }
9
10 /**
11 * Activation process
12 * @return bool Should we proceed with plugin activation?
13 */
14 function db_reset_activate() {
15 $status = db_reset_activation_checks();
16
17 if ( is_string( $status ) ) {
18 db_reset_cancel_activation( $status );
19 }
20 }
21
22 /**
23 * Check for minimum plugin requirements
24 * @return string|bool Error message if fails or boolean true if passes
25 */
26 function db_reset_activation_checks() {
27 if ( ! function_exists( 'spl_autoload_register' ) &&
28 version_compare( phpversion(), '5.3', '<' ) ) {
29 return __( 'The WordPress Database Reset plugin requires at least PHP 5.3!', 'wordpress-database-reset' );
30 }
31
32 if ( version_compare( get_bloginfo( 'version' ), '3.0', '<' ) ) {
33 return __( 'The WordPress Database Reset plugin requires at least WordPress 3.0!', 'wordpress-database-reset' );
34 }
35
36 return true;
37 }
38
39 /**
40 * Cancel activation and kill process
41 * @param string $message Error message
42 * @return void
43 */
44 function db_reset_cancel_activation( $message ) {
45 deactivate_plugins( __FILE__ );
46 wp_die( esc_html($message) );
47 }
48
49 // Ewww. Still need it though.
50 require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
51
52 /**
53 * Installs the site.
54 *
55 * This is a copied and pasted version from /wp-admin/includes/upgrade.php.
56 * Why am I doing this? Because there's no way of hooking into the WP install
57 * method to stop the 'new blog' email from firing.
58 *
59 * Also, through the command line, the call to wp_guess_url() doesn't work
60 * and returns an empty value. We must pass the siteurl option to this
61 * function in order to be able to reset the options table without it
62 * completely breaking the site.
63 *
64 * @since 2.1.0
65 *
66 * @param string $blog_title Blog title.
67 * @param string $user_name User's username.
68 * @param string $user_email User's email.
69 * @param bool $public Whether blog is public.
70 * @param string $deprecated Optional. Not used.
71 * @param string $user_password Optional. User's chosen password. Default empty (random password).
72 * @param string $language Optional. Language chosen. Default empty.
73 * @return array Array keys 'url', 'user_id', 'password', and 'password_message'.
74 */
75 function db_reset_install( $blog_title, $user_name, $user_email, $public, $site_url, $deprecated = '', $user_password = '', $language = '' ) {
76 if ( ! empty( $deprecated ) )
77 _deprecated_argument( __FUNCTION__, '2.6' );
78
79 wp_check_mysql_version();
80 wp_cache_flush();
81 make_db_current_silent();
82 populate_options();
83 populate_roles();
84
85 remove_all_actions('update_option_blogname');
86 remove_all_actions('update_option_blogdescription');
87
88 update_option( 'blogname', $blog_title );
89 update_option( 'admin_email', $user_email );
90 update_option( 'blog_public', $public );
91
92 if ( $language ) {
93 update_option( 'WPLANG', $language );
94 }
95
96 $guessurl = ( wp_guess_url() !== 'http:' ) ? wp_guess_url() : $site_url;
97
98 update_option( 'siteurl', $guessurl );
99 update_option( 'home', $guessurl );
100
101 // If not a public blog, don't ping.
102 if ( ! $public )
103 update_option( 'default_pingback_flag', 0 );
104
105 /*
106 * Create default user. If the user already exists, the user tables are
107 * being shared among blogs. Just set the role in that case.
108 */
109 $user_id = username_exists( $user_name );
110 $user_password = trim( $user_password );
111 $email_password = false;
112 if ( ! $user_id && empty( $user_password ) ) {
113 $user_password = wp_generate_password( 12, false );
114 $message = __( '<strong><em>Note that password</em></strong> carefully! It is a <em>random</em> password that was generated just for you.' );
115 $user_id = wp_create_user( $user_name, $user_password, $user_email );
116 $email_password = true;
117 } elseif ( ! $user_id ) {
118 // Password has been provided
119 $message = '<em>'.__( 'Your chosen password.' ).'</em>';
120 $user_id = wp_create_user( $user_name, $user_password, $user_email );
121 } else {
122 $message = __( 'User already exists. Password inherited.' );
123 }
124
125 $user = new WP_User( $user_id );
126 $user->set_role( 'administrator' );
127
128 wp_install_defaults( $user_id );
129
130 wp_install_maybe_enable_pretty_permalinks();
131
132 flush_rewrite_rules();
133
134 wp_cache_flush();
135
136 /**
137 * Fires after a site is fully installed.
138 *
139 * @since 3.9.0
140 *
141 * @param WP_User $user The site owner.
142 */
143 do_action( 'wp_install', $user );
144
145 return array( 'url' => $guessurl, 'user_id' => $user_id, 'password' => $user_password, 'password_message' => $message );
146 }
147