Base.php
289 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\RegisterActivation; |
| 4 | |
| 5 | use ProfilePress\Core\Classes\FormRepository as FR; |
| 6 | use ProfilePress\Core\Membership\DigitalProducts\UploadHandler; |
| 7 | |
| 8 | class Base |
| 9 | { |
| 10 | public static function run_install($networkwide = false) |
| 11 | { |
| 12 | if (is_multisite() && $networkwide) { |
| 13 | |
| 14 | $site_ids = get_sites(['fields' => 'ids', 'number' => 0]); |
| 15 | |
| 16 | foreach ($site_ids as $site_id) { |
| 17 | switch_to_blog($site_id); |
| 18 | self::pp_install(); |
| 19 | restore_current_blog(); |
| 20 | } |
| 21 | } else { |
| 22 | self::pp_install(); |
| 23 | } |
| 24 | |
| 25 | flush_rewrite_rules(); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Run plugin install / activation action when new blog is created in multisite setup. |
| 30 | * |
| 31 | * @param int $blog_id |
| 32 | */ |
| 33 | public static function multisite_new_blog_install($blog_id) |
| 34 | { |
| 35 | if ( ! function_exists('is_plugin_active_for_network')) { |
| 36 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 37 | } |
| 38 | |
| 39 | if (is_plugin_active_for_network('wp-user-avatar/wp-user-avatar.php')) { |
| 40 | switch_to_blog($blog_id); |
| 41 | self::pp_install(); |
| 42 | restore_current_blog(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Perform plugin activation / installation. |
| 48 | */ |
| 49 | public static function pp_install() |
| 50 | { |
| 51 | // set flag for those migrating from wp user avatar |
| 52 | if ( ! get_option('ppress_plugin_activated') && '1' == get_option('wp_user_avatar_default_avatar_updated')) { |
| 53 | add_option('ppress_is_from_wp_user_avatar', 'true'); |
| 54 | } |
| 55 | |
| 56 | if ( ! current_user_can('activate_plugins') || get_option('ppress_plugin_activated') == 'true') return; |
| 57 | |
| 58 | CreateDBTables::make(); |
| 59 | |
| 60 | UploadHandler::get_instance()->create_protection_files(true); |
| 61 | |
| 62 | self::default_settings(); |
| 63 | self::create_default_forms(); |
| 64 | self::clear_wpengine_cache(); |
| 65 | |
| 66 | add_option('ppress_install_date', current_time('mysql')); |
| 67 | add_option('ppress_plugin_activated', 'true'); |
| 68 | add_option('ppress_new_v4_install', 'true'); |
| 69 | |
| 70 | flush_rewrite_rules(); |
| 71 | } |
| 72 | |
| 73 | public static function create_default_forms() |
| 74 | { |
| 75 | FR::add_form( |
| 76 | esc_html__('Default Registration', 'wp-user-avatar'), |
| 77 | FR::REGISTRATION_TYPE, |
| 78 | 'Tulip', |
| 79 | FR::DRAG_DROP_BUILDER_TYPE |
| 80 | ); |
| 81 | |
| 82 | FR::add_form( |
| 83 | esc_html__('Default Login', 'wp-user-avatar'), |
| 84 | FR::LOGIN_TYPE, |
| 85 | 'Tulip', |
| 86 | FR::DRAG_DROP_BUILDER_TYPE |
| 87 | ); |
| 88 | |
| 89 | FR::add_form( |
| 90 | esc_html__('Default Password Reset', 'wp-user-avatar'), |
| 91 | FR::PASSWORD_RESET_TYPE, |
| 92 | 'Tulip', |
| 93 | FR::DRAG_DROP_BUILDER_TYPE |
| 94 | ); |
| 95 | |
| 96 | FR::add_form( |
| 97 | esc_html__('Default Edit Profile', 'wp-user-avatar'), |
| 98 | FR::EDIT_PROFILE_TYPE, |
| 99 | 'Tulip', |
| 100 | FR::DRAG_DROP_BUILDER_TYPE |
| 101 | ); |
| 102 | |
| 103 | FR::add_form( |
| 104 | esc_html__('Default User Profile', 'wp-user-avatar'), |
| 105 | FR::USER_PROFILE_TYPE, |
| 106 | 'DefaultTemplate', |
| 107 | FR::DRAG_DROP_BUILDER_TYPE |
| 108 | ); |
| 109 | |
| 110 | FR::add_form( |
| 111 | esc_html__('Default Member Directory', 'wp-user-avatar'), |
| 112 | FR::MEMBERS_DIRECTORY_TYPE, |
| 113 | 'DefaultTemplate', |
| 114 | FR::DRAG_DROP_BUILDER_TYPE |
| 115 | ); |
| 116 | |
| 117 | FR::add_form( |
| 118 | esc_html__('Lucid Tab Widget', 'wp-user-avatar'), |
| 119 | FR::MELANGE_TYPE, |
| 120 | 'Lucid', |
| 121 | FR::SHORTCODE_BUILDER_TYPE |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | public static function default_settings() |
| 126 | { |
| 127 | $settings = [ |
| 128 | 'login_username_email_restrict' => 'both', |
| 129 | 'myac_edit_account_endpoint' => 'edit-profile', |
| 130 | 'myac_change_password_endpoint' => 'change-password', |
| 131 | 'set_user_profile_slug' => 'profile', |
| 132 | 'set_login_redirect' => 'dashboard', |
| 133 | 'global_site_access' => 'everyone', |
| 134 | 'global_restricted_access_message' => '<p>' . esc_html__('You are unauthorized to view this page.', 'wp-user-avatar') . '</p>', |
| 135 | |
| 136 | 'admin_email_addresses' => ppress_admin_email(), |
| 137 | 'email_sender_name' => ppress_site_title(), |
| 138 | 'email_sender_email' => 'wordpress@' . ppress_site_url_without_scheme(), |
| 139 | 'email_content_type' => 'text/html', |
| 140 | 'email_template_type' => 'default', |
| 141 | |
| 142 | 'password_reset_email_enabled' => 'on', |
| 143 | 'password_reset_email_subject' => sprintf(__('[%s] Password Reset'), ppress_site_title()), |
| 144 | 'password_reset_email_content' => ppress_password_reset_content_default(), |
| 145 | |
| 146 | 'new_user_admin_email_email_enabled' => 'on', |
| 147 | 'new_user_admin_email_email_subject' => sprintf(__('[%s] New User Registration'), ppress_site_title()), |
| 148 | 'new_user_admin_email_email_content' => ppress_new_user_admin_notification_message_default(), |
| 149 | ]; |
| 150 | |
| 151 | foreach ($settings as $key => $value) { |
| 152 | ppress_update_settings($key, $value); |
| 153 | } |
| 154 | |
| 155 | self::membership_default_settings(); |
| 156 | } |
| 157 | |
| 158 | public static function membership_default_settings() |
| 159 | { |
| 160 | $settings = [ |
| 161 | // Business info |
| 162 | 'business_name' => ppress_site_title(), |
| 163 | 'business_country' => 'US', |
| 164 | // Payment settings |
| 165 | //'payment_currency' => 'USD', excluded so onboarding checklist can ask user to set currency |
| 166 | 'currency_position' => 'left', |
| 167 | 'currency_decimal_separator' => '.', |
| 168 | 'currency_thousand_separator' => ',', |
| 169 | 'currency_decimal_number' => '2', |
| 170 | 'terms_agreement_label' => sprintf(__('I have read and agree to the website %s', 'wp-user-avatar'), '[terms]'), |
| 171 | ]; |
| 172 | |
| 173 | foreach ($settings as $key => $value) { |
| 174 | ppress_update_settings($key, $value); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | public static function create_pages() |
| 179 | { |
| 180 | $pages = [ |
| 181 | 'set_login_url' => [ |
| 182 | 'post_title' => esc_html__('Log In', 'wp-user-avatar'), |
| 183 | 'post_content' => sprintf('<!-- wp:shortcode -->[profilepress-login id="%s"]<!-- /wp:shortcode -->', FR::get_form_first_id(FR::LOGIN_TYPE)), |
| 184 | ], |
| 185 | 'set_registration_url' => [ |
| 186 | 'post_title' => esc_html__('Sign Up', 'wp-user-avatar'), |
| 187 | 'post_content' => sprintf('<!-- wp:shortcode -->[profilepress-registration id="%s"]<!-- /wp:shortcode -->', FR::get_form_first_id(FR::REGISTRATION_TYPE)), |
| 188 | ], |
| 189 | 'set_lost_password_url' => [ |
| 190 | 'post_title' => esc_html__('Reset Password', 'wp-user-avatar'), |
| 191 | 'post_content' => sprintf('<!-- wp:shortcode -->[profilepress-password-reset id="%s"]<!-- /wp:shortcode -->', FR::get_form_first_id(FR::PASSWORD_RESET_TYPE)), |
| 192 | ], |
| 193 | 'edit_user_profile_url' => [ |
| 194 | 'post_title' => esc_html__('My Account', 'wp-user-avatar'), |
| 195 | 'post_content' => '<!-- wp:shortcode -->[profilepress-my-account]<!-- /wp:shortcode -->', |
| 196 | 'post_name' => 'account' |
| 197 | ], |
| 198 | 'set_user_profile_shortcode' => [ |
| 199 | 'post_title' => esc_html__('My Profile', 'wp-user-avatar'), |
| 200 | 'post_content' => sprintf('<!-- wp:shortcode -->[profilepress-user-profile id="%s"]<!-- /wp:shortcode -->', FR::get_form_first_id(FR::USER_PROFILE_TYPE)) |
| 201 | ], |
| 202 | 'member_directory' => [ |
| 203 | 'post_title' => esc_html__('Member Directory', 'wp-user-avatar'), |
| 204 | 'post_content' => sprintf('<!-- wp:shortcode -->[profilepress-member-directory id="%s"]<!-- /wp:shortcode -->', FR::get_form_first_id(FR::MEMBERS_DIRECTORY_TYPE)) |
| 205 | ] |
| 206 | ]; |
| 207 | |
| 208 | foreach ($pages as $key => $page) { |
| 209 | |
| 210 | $insert = wp_insert_post( |
| 211 | array_merge( |
| 212 | ['post_status' => 'publish', 'post_type' => 'page'], |
| 213 | $page |
| 214 | ), |
| 215 | true |
| 216 | ); |
| 217 | |
| 218 | if ($insert && ! is_wp_error($insert)) { |
| 219 | ppress_update_settings($key, $insert); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | self::create_membership_pages(); |
| 224 | } |
| 225 | |
| 226 | public static function create_membership_pages() |
| 227 | { |
| 228 | $pages = [ |
| 229 | 'checkout_page_id' => [ |
| 230 | 'post_title' => esc_html__('Checkout', 'wp-user-avatar'), |
| 231 | 'post_content' => '<!-- wp:shortcode -->[profilepress-checkout]<!-- /wp:shortcode -->' |
| 232 | ], |
| 233 | 'payment_success_page_id' => [ |
| 234 | 'post_title' => esc_html__('Order Confirmation', 'wp-user-avatar'), |
| 235 | 'post_content' => sprintf( |
| 236 | '<!-- wp:paragraph --><p>%s</p><!-- /wp:paragraph --><!-- wp:shortcode -->[profilepress-receipt]<!-- /wp:shortcode -->', |
| 237 | esc_html__('Thank you for your order!', 'wp-user-avatar') |
| 238 | ) |
| 239 | ], |
| 240 | 'payment_failure_page_id' => [ |
| 241 | 'post_title' => esc_html__('Order Failed', 'wp-user-avatar'), |
| 242 | 'post_content' => sprintf( |
| 243 | '<!-- wp:paragraph --><p>%s</p><!-- /wp:paragraph -->', |
| 244 | esc_html__('Your transaction failed, please try again or contact support.', 'wp-user-avatar') |
| 245 | ) |
| 246 | ] |
| 247 | ]; |
| 248 | |
| 249 | foreach ($pages as $key => $page) { |
| 250 | |
| 251 | $page_id = ppress_settings_by_key($key, ''); |
| 252 | $page_status = get_post_status($page_id); |
| 253 | |
| 254 | if (empty($page_id) || 'publish' !== $page_status) { |
| 255 | |
| 256 | $insert = wp_insert_post( |
| 257 | array_merge( |
| 258 | ['post_status' => 'publish', 'post_type' => 'page'], |
| 259 | $page |
| 260 | ), |
| 261 | true |
| 262 | ); |
| 263 | |
| 264 | if ($insert && ! is_wp_error($insert)) { |
| 265 | ppress_update_settings($key, $insert); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | public static function clear_wpengine_cache() |
| 272 | { |
| 273 | if ( ! class_exists('\WpeCommon')) { |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | if (method_exists('\WpeCommon', 'purge_memcached')) { |
| 278 | \WpeCommon::purge_memcached(); |
| 279 | } |
| 280 | |
| 281 | if (method_exists('\WpeCommon', 'clear_maxcdn_cache')) { |
| 282 | \WpeCommon::clear_maxcdn_cache(); |
| 283 | } |
| 284 | |
| 285 | if (method_exists('\WpeCommon', 'purge_varnish_cache')) { |
| 286 | \WpeCommon::purge_varnish_cache(); |
| 287 | } |
| 288 | } |
| 289 | } |