Files
ultimatemember/includes/core/class-setup.php
T

357 lines
9.5 KiB
PHP
Raw Normal View History

<?php
namespace um\core;
2023-12-19 11:47:41 +02:00
use WP_User_Query;
2019-10-24 01:01:04 +03:00
2023-12-19 11:47:41 +02:00
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
2019-10-24 01:01:04 +03:00
2018-03-26 01:27:46 +03:00
if ( ! class_exists( 'um\core\Setup' ) ) {
2018-03-20 13:24:38 +02:00
/**
* Class Setup
2019-10-24 01:01:04 +03:00
*
2018-03-20 13:24:38 +02:00
* @package um\core
*/
class Setup {
2018-03-20 13:24:38 +02:00
/**
2023-12-19 11:47:41 +02:00
* Run setup.
2018-03-20 13:24:38 +02:00
*/
2023-12-19 11:47:41 +02:00
public function run_setup() {
2019-12-20 15:06:58 +02:00
$this->create_db();
2018-03-20 13:24:38 +02:00
$this->install_basics();
$this->install_default_forms();
$this->set_default_settings();
$this->set_default_role_meta();
2022-06-20 17:29:31 +03:00
$this->set_default_user_status();
2018-03-20 13:24:38 +02:00
}
2019-12-20 15:06:58 +02:00
/**
2023-12-19 11:47:41 +02:00
* Create custom DB tables.
2019-12-20 15:06:58 +02:00
*/
2023-12-19 11:47:41 +02:00
public function create_db() {
2019-12-20 15:06:58 +02:00
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE {$wpdb->prefix}um_metadata (
umeta_id bigint(20) unsigned NOT NULL auto_increment,
user_id bigint(20) unsigned NOT NULL default '0',
um_key varchar(255) default NULL,
um_value longtext default NULL,
PRIMARY KEY (umeta_id),
KEY user_id_indx (user_id),
KEY meta_key_indx (um_key),
KEY meta_value_indx (um_value(191))
) $charset_collate;";
2023-12-19 11:47:41 +02:00
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
2019-12-20 15:06:58 +02:00
dbDelta( $sql );
}
2018-03-20 13:24:38 +02:00
/**
2023-12-19 11:47:41 +02:00
* Basics.
2018-03-20 13:24:38 +02:00
*/
2023-12-19 11:47:41 +02:00
public function install_basics() {
if ( ! get_option( '__ultimatemember_sitekey' ) ) {
2023-12-19 11:47:41 +02:00
update_option( '__ultimatemember_sitekey', str_replace( array( 'http://', 'https://' ), '', sanitize_user( get_bloginfo( 'url' ) ) ) . '-' . wp_generate_password( 20, false ) );
}
2018-03-20 13:24:38 +02:00
}
2018-03-20 13:24:38 +02:00
/**
2023-12-19 11:47:41 +02:00
* Default Forms.
2018-03-20 13:24:38 +02:00
*/
2023-12-19 11:47:41 +02:00
public function install_default_forms() {
2018-03-20 13:24:38 +02:00
if ( current_user_can( 'manage_options' ) && ! get_option( 'um_is_installed' ) ) {
$options = get_option( 'um_options', array() );
2018-03-20 13:24:38 +02:00
update_option( 'um_is_installed', 1 );
2018-03-20 13:24:38 +02:00
//Install default options
foreach ( UM()->config()->settings_defaults as $key => $value ) {
2023-12-19 11:47:41 +02:00
$options[ $key ] = $value;
2018-03-20 13:24:38 +02:00
}
2023-12-19 11:47:41 +02:00
// Install Core Forms.
2018-03-20 13:24:38 +02:00
foreach ( UM()->config()->core_forms as $id ) {
2023-12-19 11:47:41 +02:00
// If page does not exist - create it.
2018-03-20 13:24:38 +02:00
$page_exists = UM()->query()->find_post_id( 'um_form', '_um_core', $id );
if ( ! $page_exists ) {
2023-12-19 11:47:41 +02:00
if ( 'register' === $id ) {
2018-03-20 13:24:38 +02:00
$title = 'Default Registration';
2023-12-19 11:47:41 +02:00
} elseif ( 'login' === $id ) {
2018-03-20 13:24:38 +02:00
$title = 'Default Login';
} else {
$title = 'Default Profile';
}
$form = array(
2023-12-19 11:47:41 +02:00
'post_type' => 'um_form',
'post_title' => $title,
'post_status' => 'publish',
'post_author' => get_current_user_id(),
2018-03-20 13:24:38 +02:00
);
$form_id = wp_insert_post( $form );
2023-12-19 11:47:41 +02:00
foreach ( UM()->config()->core_form_meta[ $id ] as $key => $value ) {
if ( '_um_custom_fields' === $key ) {
$array = maybe_unserialize( $value );
2018-03-20 13:24:38 +02:00
update_post_meta( $form_id, $key, $array );
} else {
update_post_meta( $form_id, $key, $value );
}
}
$core_forms[ $id ] = $form_id;
2018-03-20 13:24:38 +02:00
}
/** DONE **/
}
if ( isset( $core_forms ) ) {
2018-03-20 13:24:38 +02:00
update_option( 'um_core_forms', $core_forms );
}
2018-03-20 13:24:38 +02:00
2023-12-19 11:47:41 +02:00
// Install Core Directories.
2018-03-20 13:24:38 +02:00
foreach ( UM()->config()->core_directories as $id ) {
2023-12-19 11:47:41 +02:00
// If page does not exist - create it.
2018-03-20 13:24:38 +02:00
$page_exists = UM()->query()->find_post_id( 'um_directory', '_um_core', $id );
if ( ! $page_exists ) {
$title = 'Members';
$form = array(
2023-12-19 11:47:41 +02:00
'post_type' => 'um_directory',
'post_title' => $title,
'post_status' => 'publish',
'post_author' => get_current_user_id(),
2018-03-20 13:24:38 +02:00
);
$form_id = wp_insert_post( $form );
foreach ( UM()->config()->core_directory_meta[ $id ] as $key => $value ) {
2023-12-19 11:47:41 +02:00
if ( '_um_custom_fields' === $key ) {
$array = maybe_unserialize( $value );
2018-03-20 13:24:38 +02:00
update_post_meta( $form_id, $key, $array );
} else {
2023-12-19 11:47:41 +02:00
update_post_meta( $form_id, $key, $value );
2018-03-20 13:24:38 +02:00
}
}
$core_directories[ $id ] = $form_id;
2018-03-20 13:24:38 +02:00
}
/** DONE **/
}
if ( isset( $core_directories ) ) {
update_option( 'um_core_directories', $core_directories );
}
2018-03-20 13:24:38 +02:00
update_option( 'um_options', $options );
}
}
2018-03-20 13:24:38 +02:00
/**
2023-12-19 11:47:41 +02:00
* Install Pre-defined pages with shortcodes.
*/
2023-08-09 16:24:22 +03:00
public function install_default_pages() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
2018-03-20 13:24:38 +02:00
2023-08-09 16:24:22 +03:00
$core_forms = get_option( 'um_core_forms', array() );
$core_directories = get_option( 'um_core_directories', array() );
2018-03-20 13:24:38 +02:00
$setup_shortcodes = array_merge( $core_forms, $core_directories );
2018-03-20 13:24:38 +02:00
2023-12-19 11:47:41 +02:00
// Install Core Pages.
$core_pages = array();
foreach ( UM()->config()->core_pages as $slug => $array ) {
2018-03-20 13:24:38 +02:00
$page_exists = UM()->query()->find_post_id( 'page', '_um_core', $slug );
if ( $page_exists ) {
$core_pages[ $slug ] = $page_exists;
continue;
2018-03-20 13:24:38 +02:00
}
2017-07-28 16:58:35 +03:00
2023-12-19 11:47:41 +02:00
// If page does not exist - create it.
$content = '';
2023-08-09 16:24:22 +03:00
if ( 'logout' === $slug ) {
$content = '';
2023-08-09 16:24:22 +03:00
} elseif ( 'account' === $slug ) {
$content = '[ultimatemember_account]';
2023-08-09 16:24:22 +03:00
} elseif ( 'password-reset' === $slug ) {
$content = '[ultimatemember_password]';
2023-08-09 16:24:22 +03:00
} elseif ( 'user' === $slug ) {
$content = '[ultimatemember form_id="' . $setup_shortcodes['profile'] . '"]';
} elseif ( ! empty( $setup_shortcodes[ $slug ] ) ) {
$content = '[ultimatemember form_id="' . $setup_shortcodes[ $slug ] . '"]';
2018-03-20 13:24:38 +02:00
}
2024-02-07 17:38:36 +02:00
/** This filter is documented in includes/core/class-setup.php */
2019-09-09 12:39:45 +03:00
$content = apply_filters( 'um_setup_predefined_page_content', $content, $slug );
$user_page = array(
2023-08-09 16:24:22 +03:00
'post_title' => $array['title'],
'post_content' => $content,
'post_name' => $slug,
'post_type' => 'page',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'comment_status' => 'closed',
);
$post_id = wp_insert_post( $user_page );
update_post_meta( $post_id, '_um_core', $slug );
$core_pages[ $slug ] = $post_id;
}
$options = get_option( 'um_options', array() );
foreach ( $core_pages as $slug => $page_id ) {
2023-08-09 16:24:22 +03:00
$key = UM()->options()->get_core_page_id( $slug );
$options[ $key ] = $page_id;
2018-03-20 13:24:38 +02:00
}
update_option( 'um_options', $options );
2019-10-24 01:01:04 +03:00
// reset rewrite rules after first install of core pages
UM()->rewrite()->reset_rules();
2018-03-20 13:24:38 +02:00
}
2024-02-07 17:38:36 +02:00
public function predefined_page( $slug, $with_rewrite = true ) {
$page_exists = UM()->query()->find_post_id( 'page', '_um_core', $slug );
if ( $page_exists ) {
return;
}
$predefined_pages = UM()->config()->get( 'predefined_pages' );
if ( empty( $predefined_pages ) || ! array_key_exists( $slug, $predefined_pages ) ) {
return;
}
$data = $predefined_pages[ $slug ];
if ( empty( $data['title'] ) ) {
return;
}
$content = ! empty( $data['content'] ) ? $data['content'] : '';
/**
* Filters Ultimate Member predefined pages content when set up the predefined page.
*
* @param {string} $content Predefined page content.
* @param {string} $slug Predefined page slug (key).
*
* @return {string} Predefined page content.
*
* @since 2.1.0
* @hook um_setup_predefined_page_content
*
* @example <caption>Set Ultimate Member predefined pages content with key = 'my_page_key'.</caption>
* function my_um_setup_predefined_page_content( $content, $slug ) {
* // your code here
* if ( 'my_page_key' === $slug ) {
* $content = __( 'My Page content', 'my-translate-key' );
* }
* return $pages;
* }
* add_filter( 'um_setup_predefined_page_content', 'my_um_setup_predefined_page_content' );
*/
$content = apply_filters( 'um_setup_predefined_page_content', $content, $slug );
$user_page = array(
'post_title' => $data['title'],
'post_content' => $content,
'post_name' => $slug,
'post_type' => 'page',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'comment_status' => 'closed',
);
$post_id = wp_insert_post( $user_page );
if ( empty( $post_id ) || is_wp_error( $post_id ) ) {
return;
}
update_post_meta( $post_id, '_um_core', $slug );
UM()->options()->update( UM()->options()->get_predefined_page_option_key( $slug ), $post_id );
if ( $with_rewrite ) {
// Reset rewrite rules after page creation and option upgrade.
UM()->rewrite()->reset_rules();
}
}
2018-03-20 13:24:38 +02:00
/**
2023-12-19 11:47:41 +02:00
* Set default UM settings.
2018-03-20 13:24:38 +02:00
*/
2023-12-19 11:47:41 +02:00
public function set_default_settings() {
2019-10-24 01:01:04 +03:00
$options = get_option( 'um_options', array() );
2018-03-20 13:24:38 +02:00
foreach ( UM()->config()->settings_defaults as $key => $value ) {
//set new options to default
2019-10-24 01:01:04 +03:00
if ( ! isset( $options[ $key ] ) ) {
2018-07-05 18:07:22 +03:00
$options[ $key ] = $value;
2019-10-24 01:01:04 +03:00
}
2018-03-20 13:24:38 +02:00
}
update_option( 'um_options', $options );
}
/**
2023-12-19 11:47:41 +02:00
* Set UM roles meta to Default WP roles.
2018-03-20 13:24:38 +02:00
*/
2023-12-19 11:47:41 +02:00
public function set_default_role_meta() {
2018-03-20 13:24:38 +02:00
foreach ( UM()->config()->default_roles_metadata as $role => $meta ) {
2018-07-05 18:07:22 +03:00
add_option( "um_role_{$role}_meta", $meta );
2018-03-20 13:24:38 +02:00
}
}
2022-06-20 17:29:31 +03:00
/**
2023-12-19 11:47:41 +02:00
* Set accounts without account_status meta to 'approved' status.
2022-06-20 17:29:31 +03:00
*
* @since 2.4.2
*/
2023-12-19 11:47:41 +02:00
public function set_default_user_status() {
2022-06-20 17:29:31 +03:00
$result = get_transient( 'um_count_users_unassigned' );
if ( false === $result ) {
$args = array(
'fields' => 'ids',
'number' => 0,
'meta_query' => array(
array(
'key' => 'account_status',
'compare' => 'NOT EXISTS',
),
),
'um_custom_user_query' => true,
);
2023-12-19 11:47:41 +02:00
$users = new WP_User_Query( $args );
2022-06-20 17:29:31 +03:00
if ( empty( $users ) || is_wp_error( $users ) ) {
$result = array();
} else {
$result = $users->get_results();
}
set_transient( 'um_count_users_unassigned', $result, DAY_IN_SECONDS );
2022-06-20 17:29:31 +03:00
}
if ( empty( $result ) ) {
return;
}
foreach ( $result as $user_id ) {
update_user_meta( $user_id, 'account_status', 'approved' );
}
}
2018-03-20 13:24:38 +02:00
}
2022-06-20 17:29:31 +03:00
}