| 1 |
<?php |
| 2 |
/** |
| 3 |
* Create LoginPress Page. |
| 4 |
* |
| 5 |
* @package LoginPress |
| 6 |
* @author WPBrigade |
| 7 |
* @since 1.1.3 |
| 8 |
* @version 1.1.25 |
| 9 |
*/ |
| 10 |
|
| 11 |
// Exit if accessed directly. |
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Create a LoginPress page for Multisite. |
| 18 |
*/ |
| 19 |
|
| 20 |
if ( ! class_exists( 'LoginPress_Page_Create' ) ) : |
| 21 |
|
| 22 |
class LoginPress_Page_Create { |
| 23 |
|
| 24 |
function __construct() |
| 25 |
{ |
| 26 |
$this->_init(); |
| 27 |
$this->_hooks(); |
| 28 |
} |
| 29 |
|
| 30 |
function _hooks(){ |
| 31 |
add_action( 'wpmu_new_blog', array( $this, 'loginpress_new_site_created' ), 10, 6 ); |
| 32 |
} |
| 33 |
|
| 34 |
public function _init(){ |
| 35 |
global $wpdb; |
| 36 |
|
| 37 |
if ( ! current_user_can( 'activate_plugins' ) ) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
if ( is_multisite() ) { |
| 42 |
|
| 43 |
foreach ( $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs LIMIT 100" ) as $blog_id ) { |
| 44 |
switch_to_blog( $blog_id ); |
| 45 |
$this->loginpress_run_install(); |
| 46 |
restore_current_blog(); |
| 47 |
} |
| 48 |
|
| 49 |
} else { |
| 50 |
$this->loginpress_run_install(); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Run the LoginPress install process |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function loginpress_run_install() { |
| 60 |
|
| 61 |
/* translators: 1: Name of this plugin */ |
| 62 |
$post_content = sprintf( __( '<p>This page is used by %1$s to preview the login page in the Customizer.</p>', 'loginpress' ), 'LoginPress' ); |
| 63 |
|
| 64 |
$pages = apply_filters( |
| 65 |
'loginpress_create_pages', array( |
| 66 |
'loginpress' => array( |
| 67 |
'name' => _x( 'loginpress', 'Page slug', 'loginpress' ), |
| 68 |
'title' => _x( 'LoginPress', 'Page title', 'loginpress' ), |
| 69 |
'content' => $post_content, |
| 70 |
), |
| 71 |
) |
| 72 |
); |
| 73 |
|
| 74 |
foreach ( $pages as $key => $page ) { |
| 75 |
$this->loginpress_create_page( esc_sql( $page['name'] ), 'loginpress_page', $page['title'], $page['content'] ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Create a page and store the ID in an option. |
| 81 |
* |
| 82 |
* @param mixed $slug Slug for the new page. |
| 83 |
* @param string $option Option name to store the page's ID. |
| 84 |
* @param string $page_title (default: '') Title for the new page. |
| 85 |
* @param string $page_content (default: '') Content for the new page. |
| 86 |
* @return int page ID |
| 87 |
*/ |
| 88 |
public function loginpress_create_page( $slug, $option = '', $page_title = '', $page_content = '' ) { |
| 89 |
global $wpdb; |
| 90 |
|
| 91 |
// Set up options. |
| 92 |
$options = array(); |
| 93 |
|
| 94 |
// Pull options from WP. |
| 95 |
$loginpress_setting = get_option( 'loginpress_setting', array() ); |
| 96 |
if ( ! is_array( $loginpress_setting ) && empty( $loginpress_setting ) ) { |
| 97 |
$loginpress_setting = array(); |
| 98 |
} |
| 99 |
$option_value = array_key_exists( 'loginpress_page', $loginpress_setting ) ? $loginpress_setting['loginpress_page'] : false; |
| 100 |
|
| 101 |
if ( $option_value > 0 && ( $page_object = get_post( $option_value ) ) ) { |
| 102 |
if ( 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array( 'pending', 'trash', 'future', 'auto-draft' ), true ) ) { |
| 103 |
// Valid page is already in place. |
| 104 |
return $page_object->ID; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// Search for an existing page with the specified page slug. |
| 109 |
$loginpress_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_name = %s LIMIT 1;", $slug ) ); |
| 110 |
|
| 111 |
$loginpress_page_found = apply_filters( 'loginpress_create_page_id', $loginpress_page_found, $slug, $page_content ); |
| 112 |
|
| 113 |
if ( $loginpress_page_found ) { |
| 114 |
|
| 115 |
if ( $option ) { |
| 116 |
|
| 117 |
$options['loginpress_page'] = $loginpress_page_found; |
| 118 |
$loginpress_page_found = isset( $page_id ) ? $loginpress_page_found : $option_value; |
| 119 |
$merged_options = array_merge( $loginpress_setting, $options ); |
| 120 |
$loginpress_setting = $merged_options; |
| 121 |
|
| 122 |
update_option( 'loginpress_setting', $loginpress_setting ); |
| 123 |
} |
| 124 |
return $loginpress_page_found; |
| 125 |
} |
| 126 |
|
| 127 |
// Search for an existing page with the specified page slug. |
| 128 |
$loginpress_trashed_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_name = %s LIMIT 1;", $slug ) ); |
| 129 |
|
| 130 |
if ( $loginpress_trashed_found ) { |
| 131 |
$page_id = $loginpress_trashed_found; |
| 132 |
$page_data = array( |
| 133 |
'ID' => $page_id, |
| 134 |
'post_status' => 'publish', |
| 135 |
); |
| 136 |
|
| 137 |
wp_update_post( $page_data ); |
| 138 |
|
| 139 |
} else { |
| 140 |
|
| 141 |
$page_data = array( |
| 142 |
'post_status' => 'publish', |
| 143 |
'post_type' => 'page', |
| 144 |
'post_author' => 1, |
| 145 |
'post_name' => $slug, |
| 146 |
'post_title' => $page_title, |
| 147 |
'post_content' => $page_content, |
| 148 |
'comment_status' => 'closed', |
| 149 |
); |
| 150 |
|
| 151 |
$page_id = wp_insert_post( $page_data ); |
| 152 |
} |
| 153 |
|
| 154 |
if ( $option ) { |
| 155 |
|
| 156 |
$options['loginpress_page'] = $page_id; |
| 157 |
$page_id = isset( $page_id ) ? $page_id : $option_value; |
| 158 |
$merged_options = array_merge( $loginpress_setting, $options ); |
| 159 |
$loginpress_setting = $merged_options; |
| 160 |
|
| 161 |
update_option( 'loginpress_setting', $loginpress_setting ); |
| 162 |
} |
| 163 |
|
| 164 |
// Assign the LoginPress template. |
| 165 |
$this->loginpress_attach_template_to_page( $page_id, 'template-loginpress.php' ); |
| 166 |
|
| 167 |
return $page_id; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Attaches the specified template to the page identified by the specified name. |
| 172 |
* |
| 173 |
* @param int|int $page The id of the page to attach the template. |
| 174 |
* @param int|int $template The template's filename (assumes .php' is specified). |
| 175 |
* |
| 176 |
* @returns -1 if the page does not exist; otherwise, the ID of the page. |
| 177 |
*/ |
| 178 |
public function loginpress_attach_template_to_page( $page, $template ) { |
| 179 |
|
| 180 |
// Only attach the template if the page exists. |
| 181 |
if ( -1 !== $page ) { |
| 182 |
update_post_meta( $page, '_wp_page_template', $template ); |
| 183 |
} |
| 184 |
|
| 185 |
return $page; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* When a new Blog is created in multisite, check if LoginPress is network activated, and run the installer |
| 190 |
* |
| 191 |
* @param int|int $blog_id The Blog ID created. |
| 192 |
* @param int|int $user_id The User ID set as the admin. |
| 193 |
* @param string $domain The URL. |
| 194 |
* @param string $path Site Path. |
| 195 |
* @param int|int $site_id The Site ID. |
| 196 |
* @param array|array $meta Blog Meta. |
| 197 |
* @return void |
| 198 |
*/ |
| 199 |
public function loginpress_new_site_created( $blog_id, $user_id, $domain, $path, $site_id, $meta ) { |
| 200 |
|
| 201 |
if ( is_plugin_active_for_network( plugin_basename( LOGINPRESS_ROOT_FILE ) ) ) { |
| 202 |
|
| 203 |
switch_to_blog( $blog_id ); |
| 204 |
$this->_init(); |
| 205 |
restore_current_blog(); |
| 206 |
|
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
endif; |
| 212 |
?> |
| 213 |
|