PluginProbe
Database Reset / 3.1
Database Reset v3.1
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.1, at lib/helpers.php

144 lines 4.4 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( $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 update_option( 'blogname', $blog_title );
86 update_option( 'admin_email', $user_email );
87 update_option( 'blog_public', $public );
88
89 if ( $language ) {
90 update_option( 'WPLANG', $language );
91 }
92
93 $guessurl = ( wp_guess_url() !== 'http:' ) ? wp_guess_url() : $site_url;
94
95 update_option( 'siteurl', $guessurl );
96 update_option( 'home', $guessurl );
97
98 // If not a public blog, don't ping.
99 if ( ! $public )
100 update_option( 'default_pingback_flag', 0 );
101
102 /*
103 * Create default user. If the user already exists, the user tables are
104 * being shared among blogs. Just set the role in that case.
105 */
106 $user_id = username_exists( $user_name );
107 $user_password = trim( $user_password );
108 $email_password = false;
109 if ( ! $user_id && empty( $user_password ) ) {
110 $user_password = wp_generate_password( 12, false );
111 $message = __( '<strong><em>Note that password</em></strong> carefully! It is a <em>random</em> password that was generated just for you.' );
112 $user_id = wp_create_user( $user_name, $user_password, $user_email );
113 $email_password = true;
114 } elseif ( ! $user_id ) {
115 // Password has been provided
116 $message = '<em>'.__( 'Your chosen password.' ).'</em>';
117 $user_id = wp_create_user( $user_name, $user_password, $user_email );
118 } else {
119 $message = __( 'User already exists. Password inherited.' );
120 }
121
122 $user = new WP_User( $user_id );
123 $user->set_role( 'administrator' );
124
125 wp_install_defaults( $user_id );
126
127 wp_install_maybe_enable_pretty_permalinks();
128
129 flush_rewrite_rules();
130
131 wp_cache_flush();
132
133 /**
134 * Fires after a site is fully installed.
135 *
136 * @since 3.9.0
137 *
138 * @param WP_User $user The site owner.
139 */
140 do_action( 'wp_install', $user );
141
142 return array( 'url' => $guessurl, 'user_id' => $user_id, 'password' => $user_password, 'password_message' => $message );
143 }
144