PluginProbe
LoginPress | wp-login Custom Login Page Customizer / trunk
LoginPress | wp-login Custom Login Page Customizer vtrunk
6.2.5 6.2.4 6.2.3 6.2.2 6.2.1 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.3 1.0.4 All 118 releases
loginpress / include / class-loginpress-page-create.php

class-loginpress-page-create.php in LoginPress | wp-login Custom Login Page Customizer trunk, at include/class-loginpress-page-create.php

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