abstracts
4 years ago
admin
4 years ago
elementor
4 years ago
export
4 years ago
fields
4 years ago
interfaces
8 years ago
libraries
7 years ago
log-handlers
4 years ago
shortcodes
4 years ago
templates
5 years ago
class-everest-forms.php
4 years ago
class-evf-ajax.php
4 years ago
class-evf-autoloader.php
7 years ago
class-evf-background-updater.php
7 years ago
class-evf-cache-helper.php
6 years ago
class-evf-deprecated-action-hooks.php
6 years ago
class-evf-deprecated-filter-hooks.php
5 years ago
class-evf-emails.php
5 years ago
class-evf-fields.php
6 years ago
class-evf-form-block.php
4 years ago
class-evf-form-handler.php
4 years ago
class-evf-form-task.php
4 years ago
class-evf-forms-features.php
4 years ago
class-evf-frontend-scripts.php
4 years ago
class-evf-install.php
5 years ago
class-evf-integrations.php
7 years ago
class-evf-log-levels.php
8 years ago
class-evf-logger.php
5 years ago
class-evf-post-types.php
5 years ago
class-evf-privacy.php
6 years ago
class-evf-session-handler.php
7 years ago
class-evf-shortcodes.php
4 years ago
class-evf-smart-tags.php
4 years ago
class-evf-template-loader.php
4 years ago
class-evf-validation.php
6 years ago
evf-conditional-functions.php
6 years ago
evf-core-functions.php
4 years ago
evf-deprecated-functions.php
6 years ago
evf-entry-functions.php
4 years ago
evf-formatting-functions.php
4 years ago
evf-notice-functions.php
4 years ago
evf-template-functions.php
4 years ago
evf-template-hooks.php
7 years ago
evf-update-functions.php
5 years ago
class-evf-install.php
745 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Installation related functions and actions. |
| 4 | * |
| 5 | * @package EverestForms\Classes |
| 6 | * @version 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_Install Class. |
| 13 | */ |
| 14 | class EVF_Install { |
| 15 | |
| 16 | /** |
| 17 | * DB updates and callbacks that need to be run per version. |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | private static $db_updates = array( |
| 22 | '1.0.0' => array( |
| 23 | 'evf_update_100_db_version', |
| 24 | ), |
| 25 | '1.0.1' => array( |
| 26 | 'evf_update_101_db_version', |
| 27 | ), |
| 28 | '1.0.2' => array( |
| 29 | 'evf_update_102_db_version', |
| 30 | ), |
| 31 | '1.0.3' => array( |
| 32 | 'evf_update_103_db_version', |
| 33 | ), |
| 34 | '1.1.0' => array( |
| 35 | 'evf_update_110_update_forms', |
| 36 | 'evf_update_110_db_version', |
| 37 | ), |
| 38 | '1.1.6' => array( |
| 39 | 'evf_update_116_delete_options', |
| 40 | 'evf_update_116_db_version', |
| 41 | ), |
| 42 | '1.2.0' => array( |
| 43 | 'evf_update_120_db_rename_options', |
| 44 | 'evf_update_120_db_version', |
| 45 | ), |
| 46 | '1.3.0' => array( |
| 47 | 'evf_update_130_db_version', |
| 48 | ), |
| 49 | '1.4.0' => array( |
| 50 | 'evf_update_140_db_multiple_email', |
| 51 | 'evf_update_140_db_version', |
| 52 | ), |
| 53 | '1.4.4' => array( |
| 54 | 'evf_update_144_delete_options', |
| 55 | 'evf_update_144_db_version', |
| 56 | ), |
| 57 | '1.4.9' => array( |
| 58 | 'evf_update_149_db_rename_options', |
| 59 | 'evf_update_149_no_payment_options', |
| 60 | 'evf_update_149_db_version', |
| 61 | ), |
| 62 | '1.5.0' => array( |
| 63 | 'evf_update_150_field_datetime_type', |
| 64 | 'evf_update_150_db_version', |
| 65 | ), |
| 66 | '1.6.0' => array( |
| 67 | 'evf_update_160_db_version', |
| 68 | ), |
| 69 | '1.7.5' => array( |
| 70 | 'evf_update_175_remove_capabilities', |
| 71 | 'evf_update_175_restore_draft_forms', |
| 72 | 'evf_update_175_db_version', |
| 73 | ), |
| 74 | ); |
| 75 | |
| 76 | /** |
| 77 | * Background update class. |
| 78 | * |
| 79 | * @var object |
| 80 | */ |
| 81 | private static $background_updater; |
| 82 | |
| 83 | /** |
| 84 | * Hook in tabs. |
| 85 | */ |
| 86 | public static function init() { |
| 87 | add_action( 'init', array( __CLASS__, 'check_version' ), 5 ); |
| 88 | add_action( 'init', array( __CLASS__, 'init_background_updater' ), 5 ); |
| 89 | add_action( 'admin_init', array( __CLASS__, 'install_actions' ) ); |
| 90 | add_filter( 'map_meta_cap', array( __CLASS__, 'filter_map_meta_cap' ), 10, 4 ); |
| 91 | add_filter( 'plugin_action_links_' . EVF_PLUGIN_BASENAME, array( __CLASS__, 'plugin_action_links' ) ); |
| 92 | add_filter( 'plugin_row_meta', array( __CLASS__, 'plugin_row_meta' ), 10, 2 ); |
| 93 | add_filter( 'wpmu_drop_tables', array( __CLASS__, 'wpmu_drop_tables' ) ); |
| 94 | add_filter( 'cron_schedules', array( __CLASS__, 'cron_schedules' ) ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Init background updates |
| 99 | */ |
| 100 | public static function init_background_updater() { |
| 101 | include_once dirname( __FILE__ ) . '/class-evf-background-updater.php'; |
| 102 | self::$background_updater = new EVF_Background_Updater(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Check EverestForms version and run the updater is required. |
| 107 | * |
| 108 | * This check is done on all requests and runs if the versions do not match. |
| 109 | */ |
| 110 | public static function check_version() { |
| 111 | if ( ! defined( 'IFRAME_REQUEST' ) && version_compare( get_option( 'everest_forms_version' ), evf()->version, '<' ) ) { |
| 112 | self::install(); |
| 113 | do_action( 'everest_forms_updated' ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Install actions when a update button is clicked within the admin area. |
| 119 | * |
| 120 | * This function is hooked into admin_init to affect admin only. |
| 121 | */ |
| 122 | public static function install_actions() { |
| 123 | if ( ! empty( $_GET['do_update_everest_forms'] ) ) { |
| 124 | check_admin_referer( 'evf_db_update', 'evf_db_update_nonce' ); |
| 125 | self::update(); |
| 126 | EVF_Admin_Notices::add_notice( 'update' ); |
| 127 | } |
| 128 | if ( ! empty( $_GET['force_update_everest_forms'] ) ) { |
| 129 | do_action( 'wp_' . get_current_blog_id() . '_evf_updater_cron' ); |
| 130 | wp_safe_redirect( admin_url( 'admin.php?page=evf-settings' ) ); |
| 131 | exit; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Install EVF. |
| 137 | */ |
| 138 | public static function install() { |
| 139 | if ( ! is_blog_installed() ) { |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | // Check if we are not already running this routine. |
| 144 | if ( 'yes' === get_transient( 'evf_installing' ) ) { |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | // If we made it till here nothing is running yet, lets set the transient now. |
| 149 | set_transient( 'evf_installing', 'yes', MINUTE_IN_SECONDS * 10 ); |
| 150 | evf_maybe_define_constant( 'EVF_INSTALLING', true ); |
| 151 | |
| 152 | self::remove_admin_notices(); |
| 153 | self::create_options(); |
| 154 | self::create_tables(); |
| 155 | self::create_roles(); |
| 156 | self::setup_environment(); |
| 157 | self::create_cron_jobs(); |
| 158 | self::create_files(); |
| 159 | self::create_forms(); |
| 160 | self::maybe_set_activation_transients(); |
| 161 | self::update_evf_version(); |
| 162 | self::maybe_update_db_version(); |
| 163 | self::maybe_add_activated_date(); |
| 164 | |
| 165 | delete_transient( 'evf_installing' ); |
| 166 | |
| 167 | do_action( 'everest_forms_flush_rewrite_rules' ); |
| 168 | do_action( 'everest_forms_installed' ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Reset any notices added to admin. |
| 173 | */ |
| 174 | private static function remove_admin_notices() { |
| 175 | include_once dirname( __FILE__ ) . '/admin/class-evf-admin-notices.php'; |
| 176 | EVF_Admin_Notices::remove_all_notices(); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Setup EVF environment - post types, taxonomies, endpoints. |
| 181 | */ |
| 182 | private static function setup_environment() { |
| 183 | EVF_Post_Types::register_post_types(); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Is this a brand new EVF install? |
| 188 | * |
| 189 | * @return boolean |
| 190 | */ |
| 191 | private static function is_new_install() { |
| 192 | return is_null( get_option( 'everest_forms_version', null ) ) && is_null( get_option( 'everest_forms_db_version', null ) ); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Is a DB update needed? |
| 197 | * |
| 198 | * @return boolean |
| 199 | */ |
| 200 | public static function needs_db_update() { |
| 201 | $current_db_version = get_option( 'everest_forms_db_version', null ); |
| 202 | $updates = self::get_db_update_callbacks(); |
| 203 | $update_versions = array_keys( $updates ); |
| 204 | usort( $update_versions, 'version_compare' ); |
| 205 | |
| 206 | return ! is_null( $current_db_version ) && version_compare( $current_db_version, end( $update_versions ), '<' ); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * See if we need to set redirect transients for activation or not. |
| 211 | */ |
| 212 | private static function maybe_set_activation_transients() { |
| 213 | if ( self::is_new_install() ) { |
| 214 | set_transient( '_evf_activation_redirect', 1, 30 ); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * See if we need to show or run database updates during install. |
| 220 | */ |
| 221 | private static function maybe_update_db_version() { |
| 222 | if ( self::needs_db_update() ) { |
| 223 | if ( apply_filters( 'everest_forms_enable_auto_update_db', false ) ) { |
| 224 | self::init_background_updater(); |
| 225 | self::update(); |
| 226 | } else { |
| 227 | EVF_Admin_Notices::add_notice( 'update' ); |
| 228 | } |
| 229 | } else { |
| 230 | self::update_db_version(); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Store the initial plugin activation date during install. |
| 236 | */ |
| 237 | private static function maybe_add_activated_date() { |
| 238 | $activated_date = get_option( 'everest_forms_activated', '' ); |
| 239 | |
| 240 | if ( empty( $activated_date ) ) { |
| 241 | update_option( 'everest_forms_activated', time() ); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Update EVF version to current. |
| 247 | */ |
| 248 | private static function update_evf_version() { |
| 249 | delete_option( 'everest_forms_version' ); |
| 250 | add_option( 'everest_forms_version', evf()->version ); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Get list of DB update callbacks. |
| 255 | * |
| 256 | * @return array |
| 257 | */ |
| 258 | public static function get_db_update_callbacks() { |
| 259 | return self::$db_updates; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Push all needed DB updates to the queue for processing. |
| 264 | */ |
| 265 | private static function update() { |
| 266 | $current_db_version = get_option( 'everest_forms_db_version' ); |
| 267 | $logger = evf_get_logger(); |
| 268 | $update_queued = false; |
| 269 | |
| 270 | foreach ( self::get_db_update_callbacks() as $version => $update_callbacks ) { |
| 271 | if ( version_compare( $current_db_version, $version, '<' ) ) { |
| 272 | foreach ( $update_callbacks as $update_callback ) { |
| 273 | $logger->info( |
| 274 | sprintf( 'Queuing %s - %s', $version, $update_callback ), |
| 275 | array( 'source' => 'evf_db_updates' ) |
| 276 | ); |
| 277 | self::$background_updater->push_to_queue( $update_callback ); |
| 278 | $update_queued = true; |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | if ( $update_queued ) { |
| 284 | self::$background_updater->save()->dispatch(); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Update DB version to current. |
| 290 | * |
| 291 | * @param string|null $version New EverestForms DB version or null. |
| 292 | */ |
| 293 | public static function update_db_version( $version = null ) { |
| 294 | delete_option( 'everest_forms_db_version' ); |
| 295 | add_option( 'everest_forms_db_version', is_null( $version ) ? evf()->version : $version ); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Add more cron schedules. |
| 300 | * |
| 301 | * @param array $schedules List of WP scheduled cron jobs. |
| 302 | * @return array |
| 303 | */ |
| 304 | public static function cron_schedules( $schedules ) { |
| 305 | $schedules['monthly'] = array( |
| 306 | 'interval' => 2635200, |
| 307 | 'display' => __( 'Monthly', 'everest-forms' ), |
| 308 | ); |
| 309 | return $schedules; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Create cron jobs (clear them first). |
| 314 | */ |
| 315 | private static function create_cron_jobs() { |
| 316 | wp_clear_scheduled_hook( 'everest_forms_cleanup_logs' ); |
| 317 | wp_clear_scheduled_hook( 'everest_forms_cleanup_sessions' ); |
| 318 | wp_schedule_event( time() + ( 3 * HOUR_IN_SECONDS ), 'daily', 'everest_forms_cleanup_logs' ); |
| 319 | wp_schedule_event( time() + ( 6 * HOUR_IN_SECONDS ), 'twicedaily', 'everest_forms_cleanup_sessions' ); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Default options. |
| 324 | * |
| 325 | * Sets up the default options used on the settings page. |
| 326 | */ |
| 327 | private static function create_options() { |
| 328 | // Include settings so that we can run through defaults. |
| 329 | include_once dirname( __FILE__ ) . '/admin/class-evf-admin-settings.php'; |
| 330 | |
| 331 | $settings = EVF_Admin_Settings::get_settings_pages(); |
| 332 | |
| 333 | foreach ( $settings as $section ) { |
| 334 | if ( ! method_exists( $section, 'get_settings' ) ) { |
| 335 | continue; |
| 336 | } |
| 337 | $subsections = array_unique( array_merge( array( '' ), array_keys( $section->get_sections() ) ) ); |
| 338 | |
| 339 | foreach ( $subsections as $subsection ) { |
| 340 | foreach ( $section->get_settings( $subsection ) as $value ) { |
| 341 | if ( isset( $value['default'] ) && isset( $value['id'] ) ) { |
| 342 | $autoload = isset( $value['autoload'] ) ? (bool) $value['autoload'] : true; |
| 343 | add_option( $value['id'], $value['default'], '', ( $autoload ? 'yes' : 'no' ) ); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Set up the database tables which the plugin needs to function. |
| 352 | */ |
| 353 | private static function create_tables() { |
| 354 | global $wpdb; |
| 355 | |
| 356 | $wpdb->hide_errors(); |
| 357 | |
| 358 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 359 | |
| 360 | /** |
| 361 | * Before updating with DBDELTA, add fields column to entries table schema. |
| 362 | */ |
| 363 | if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}evf_entries';" ) ) { |
| 364 | if ( ! $wpdb->get_var( "SHOW COLUMNS FROM `{$wpdb->prefix}evf_entries` LIKE 'fields';" ) ) { |
| 365 | $wpdb->query( "ALTER TABLE {$wpdb->prefix}evf_entries ADD `fields` longtext NULL AFTER `referer`;" ); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Change wp_evf_sessions schema to use a bigint auto increment field |
| 371 | * instead of char(32) field as the primary key. Doing this change primarily |
| 372 | * as it should reduce the occurrence of deadlocks, but also because it is |
| 373 | * not a good practice to use a char(32) field as the primary key of a table. |
| 374 | */ |
| 375 | if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}evf_sessions'" ) ) { |
| 376 | if ( ! $wpdb->get_var( "SHOW KEYS FROM {$wpdb->prefix}evf_sessions WHERE Key_name = 'PRIMARY' AND Column_name = 'session_id'" ) ) { |
| 377 | $wpdb->query( |
| 378 | "ALTER TABLE `{$wpdb->prefix}evf_sessions` DROP PRIMARY KEY, DROP KEY `session_id`, ADD PRIMARY KEY(`session_id`), ADD UNIQUE KEY(`session_key`)" |
| 379 | ); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | dbDelta( self::get_schema() ); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Get Table schema. |
| 388 | * |
| 389 | * When adding or removing a table, make sure to update the list of tables in EVF_Install::get_tables(). |
| 390 | * |
| 391 | * @return string |
| 392 | */ |
| 393 | private static function get_schema() { |
| 394 | global $wpdb; |
| 395 | |
| 396 | $charset_collate = ''; |
| 397 | |
| 398 | if ( $wpdb->has_cap( 'collation' ) ) { |
| 399 | $charset_collate = $wpdb->get_charset_collate(); |
| 400 | } |
| 401 | |
| 402 | $tables = " |
| 403 | CREATE TABLE {$wpdb->prefix}evf_entries ( |
| 404 | entry_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, |
| 405 | form_id BIGINT UNSIGNED NOT NULL, |
| 406 | user_id BIGINT UNSIGNED NOT NULL, |
| 407 | user_device varchar(100) NOT NULL, |
| 408 | user_ip_address VARCHAR(100) NULL DEFAULT '', |
| 409 | referer text NOT NULL, |
| 410 | fields longtext NULL, |
| 411 | status varchar(20) NOT NULL, |
| 412 | viewed tinyint(1) NOT NULL DEFAULT '0', |
| 413 | starred tinyint(1) NOT NULL DEFAULT '0', |
| 414 | date_created datetime NOT NULL DEFAULT '0000-00-00 00:00:00', |
| 415 | PRIMARY KEY (entry_id), |
| 416 | KEY form_id (form_id) |
| 417 | ) $charset_collate; |
| 418 | CREATE TABLE {$wpdb->prefix}evf_entrymeta ( |
| 419 | meta_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, |
| 420 | entry_id BIGINT UNSIGNED NOT NULL, |
| 421 | meta_key varchar(255) default NULL, |
| 422 | meta_value longtext NULL, |
| 423 | PRIMARY KEY (meta_id), |
| 424 | KEY entry_id (entry_id), |
| 425 | KEY meta_key (meta_key(32)) |
| 426 | ) $charset_collate; |
| 427 | CREATE TABLE {$wpdb->prefix}evf_sessions ( |
| 428 | session_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, |
| 429 | session_key char(32) NOT NULL, |
| 430 | session_value longtext NOT NULL, |
| 431 | session_expiry BIGINT UNSIGNED NOT NULL, |
| 432 | PRIMARY KEY (session_id), |
| 433 | UNIQUE KEY session_key (session_key) |
| 434 | ) $charset_collate; |
| 435 | "; |
| 436 | |
| 437 | return $tables; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Return a list of EverestForms tables. Used to make sure all UM tables are dropped when uninstalling the plugin |
| 442 | * in a single site or multi site environment. |
| 443 | * |
| 444 | * @return array UM tables. |
| 445 | */ |
| 446 | public static function get_tables() { |
| 447 | global $wpdb; |
| 448 | |
| 449 | $tables = array( |
| 450 | "{$wpdb->prefix}evf_entries", |
| 451 | "{$wpdb->prefix}evf_entrymeta", |
| 452 | "{$wpdb->prefix}evf_sessions", |
| 453 | ); |
| 454 | |
| 455 | return $tables; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Drop EverestForms tables. |
| 460 | */ |
| 461 | public static function drop_tables() { |
| 462 | global $wpdb; |
| 463 | |
| 464 | $tables = self::get_tables(); |
| 465 | |
| 466 | foreach ( $tables as $table ) { |
| 467 | $wpdb->query( "DROP TABLE IF EXISTS {$table}" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Uninstall tables when MU blog is deleted. |
| 473 | * |
| 474 | * @param array $tables List of tables that will be deleted by WP. |
| 475 | * @return string[] |
| 476 | */ |
| 477 | public static function wpmu_drop_tables( $tables ) { |
| 478 | return array_merge( $tables, self::get_tables() ); |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Create roles and capabilities. |
| 483 | */ |
| 484 | public static function create_roles() { |
| 485 | global $wp_roles; |
| 486 | |
| 487 | if ( ! class_exists( 'WP_Roles' ) ) { |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | if ( ! isset( $wp_roles ) ) { |
| 492 | $wp_roles = new WP_Roles(); // @codingStandardsIgnoreLine |
| 493 | } |
| 494 | |
| 495 | $capabilities = self::get_core_capabilities(); |
| 496 | |
| 497 | foreach ( $capabilities as $cap_group ) { |
| 498 | foreach ( $cap_group as $cap ) { |
| 499 | $wp_roles->add_cap( 'administrator', $cap ); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Get the core capabilities. |
| 506 | * |
| 507 | * Core capabilities are assigned to admin during installation or reset. |
| 508 | * |
| 509 | * @since 1.0.0 |
| 510 | * @since 1.7.5 Removed unused post type capabilities and added supported ones. |
| 511 | * |
| 512 | * @return array $capabilities Core capabilities. |
| 513 | */ |
| 514 | private static function get_core_capabilities() { |
| 515 | $capabilities = array(); |
| 516 | |
| 517 | $capabilities['core'] = array( |
| 518 | 'manage_everest_forms', |
| 519 | ); |
| 520 | |
| 521 | $capability_types = array( 'forms', 'entries' ); |
| 522 | |
| 523 | foreach ( $capability_types as $capability_type ) { |
| 524 | if ( 'forms' === $capability_type ) { |
| 525 | $capabilities[ $capability_type ][] = "everest_forms_create_{$capability_type}"; |
| 526 | } |
| 527 | |
| 528 | foreach ( array( 'view', 'edit', 'delete' ) as $context ) { |
| 529 | $capabilities[ $capability_type ][] = "everest_forms_{$context}_{$capability_type}"; |
| 530 | $capabilities[ $capability_type ][] = "everest_forms_{$context}_others_{$capability_type}"; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return $capabilities; |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Get the meta capabilities. |
| 539 | * |
| 540 | * @since 1.7.5 |
| 541 | * |
| 542 | * @param string $cap Capability name to get. |
| 543 | * @return array $meta_caps Meta capabilities. |
| 544 | */ |
| 545 | private static function get_meta_caps( $cap = '' ) { |
| 546 | $meta_caps = array(); |
| 547 | $meta_cap_types = array( 'form', 'form_entries', 'entry' ); |
| 548 | |
| 549 | foreach ( $meta_cap_types as $meta_cap_type ) { |
| 550 | if ( $cap && $cap !== $meta_cap_type ) { |
| 551 | continue; |
| 552 | } |
| 553 | |
| 554 | foreach ( array( 'view', 'edit', 'delete' ) as $context ) { |
| 555 | $meta_caps[ "everest_forms_{$context}_{$meta_cap_type}" ] = array( |
| 556 | 'own' => 'form' === $meta_cap_type ? "everest_forms_{$context}_forms" : "everest_forms_{$context}_entries", |
| 557 | 'others' => 'form' === $meta_cap_type ? "everest_forms_{$context}_others_forms" : "everest_forms_{$context}_others_entries", |
| 558 | ); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | return $meta_caps; |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Remove EverestForms roles. |
| 567 | */ |
| 568 | public static function remove_roles() { |
| 569 | global $wp_roles; |
| 570 | |
| 571 | if ( ! class_exists( 'WP_Roles' ) ) { |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | if ( ! isset( $wp_roles ) ) { |
| 576 | $wp_roles = new WP_Roles(); // @codingStandardsIgnoreLine |
| 577 | } |
| 578 | |
| 579 | $capabilities = self::get_core_capabilities(); |
| 580 | |
| 581 | foreach ( $capabilities as $cap_group ) { |
| 582 | foreach ( $cap_group as $cap ) { |
| 583 | $wp_roles->remove_cap( 'administrator', $cap ); |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * Create default contact form. |
| 590 | */ |
| 591 | public static function create_forms() { |
| 592 | $forms_count = wp_count_posts( 'everest_form' ); |
| 593 | |
| 594 | if ( empty( $forms_count->publish ) ) { |
| 595 | include_once dirname( __FILE__ ) . '/templates/contact.php'; |
| 596 | |
| 597 | // Create a form. |
| 598 | $form_id = wp_insert_post( |
| 599 | array( |
| 600 | 'post_title' => esc_html__( 'Contact Form', 'everest-forms' ), |
| 601 | 'post_status' => 'publish', |
| 602 | 'post_type' => 'everest_form', |
| 603 | 'post_content' => '{}', |
| 604 | ) |
| 605 | ); |
| 606 | |
| 607 | if ( $form_id ) { |
| 608 | wp_update_post( |
| 609 | array( |
| 610 | 'ID' => $form_id, |
| 611 | 'post_content' => evf_encode( array_merge( array( 'id' => $form_id ), $form_template['contact'] ) ), |
| 612 | ) |
| 613 | ); |
| 614 | } |
| 615 | |
| 616 | update_option( 'everest_forms_default_form_page_id', $form_id ); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Create files/directories. |
| 622 | */ |
| 623 | private static function create_files() { |
| 624 | // Bypass if filesystem is read-only and/or non-standard upload system is used. |
| 625 | if ( apply_filters( 'everest_forms_install_skip_create_files', false ) ) { |
| 626 | return; |
| 627 | } |
| 628 | |
| 629 | // Install files and folders for uploading files and prevent hotlinking. |
| 630 | $files = array( |
| 631 | array( |
| 632 | 'base' => EVF_LOG_DIR, |
| 633 | 'file' => '.htaccess', |
| 634 | 'content' => 'deny from all', |
| 635 | ), |
| 636 | array( |
| 637 | 'base' => EVF_LOG_DIR, |
| 638 | 'file' => 'index.html', |
| 639 | 'content' => '', |
| 640 | ), |
| 641 | ); |
| 642 | |
| 643 | foreach ( $files as $file ) { |
| 644 | if ( wp_mkdir_p( $file['base'] ) && ! file_exists( trailingslashit( $file['base'] ) . $file['file'] ) ) { |
| 645 | $file_handle = @fopen( trailingslashit( $file['base'] ) . $file['file'], 'w' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_read_fopen |
| 646 | if ( $file_handle ) { |
| 647 | fwrite( $file_handle, $file['content'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fwrite |
| 648 | fclose( $file_handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Filter user's capabilities for the given primitive or meta capability. |
| 656 | * |
| 657 | * @since 1.7.5 |
| 658 | * |
| 659 | * @param string[] $caps Array of the user's capabilities. |
| 660 | * @param string $cap Capability being checked. |
| 661 | * @param int $user_id The user ID. |
| 662 | * @param array $args Adds context to the capability check, typically the object ID. |
| 663 | * |
| 664 | * @return string[] Array of required capabilities for the requested action. |
| 665 | */ |
| 666 | public static function filter_map_meta_cap( $caps, $cap, $user_id, $args ) { |
| 667 | $meta_caps = self::get_meta_caps(); |
| 668 | $entry_caps = self::get_meta_caps( 'entry' ); |
| 669 | |
| 670 | // Check if meta cap is valid to proceed. |
| 671 | if ( in_array( $cap, array_keys( $meta_caps ), true ) ) { |
| 672 | $id = isset( $args[0] ) ? (int) $args[0] : 0; |
| 673 | |
| 674 | // Check if meta cap requires form ID from entry. |
| 675 | if ( in_array( $cap, array_keys( $entry_caps ), true ) ) { |
| 676 | $entry = evf_get_entry( $id, false, array( 'cap' => false ) ); |
| 677 | if ( ! $entry ) { |
| 678 | return $caps; |
| 679 | } |
| 680 | |
| 681 | $id = isset( $entry->form_id ) ? (int) $entry->form_id : 0; |
| 682 | } |
| 683 | |
| 684 | $form = evf()->form->get( $id, array( 'cap' => false ) ); |
| 685 | if ( ! $form ) { |
| 686 | return $caps; |
| 687 | } |
| 688 | |
| 689 | if ( ! is_a( $form, 'WP_Post' ) ) { |
| 690 | return $caps; |
| 691 | } |
| 692 | |
| 693 | if ( 'everest_form' !== $form->post_type ) { |
| 694 | return $caps; |
| 695 | } |
| 696 | |
| 697 | // If the post author is set and the user is the author... |
| 698 | if ( $form->post_author && $user_id === (int) $form->post_author ) { |
| 699 | $caps = isset( $meta_caps[ $cap ]['own'] ) ? array( $meta_caps[ $cap ]['own'] ) : array( 'do_not_allow' ); |
| 700 | } else { |
| 701 | // The user is trying someone else's form. |
| 702 | $caps = isset( $meta_caps[ $cap ]['others'] ) ? array( $meta_caps[ $cap ]['others'] ) : array( 'do_not_allow' ); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | return $caps; |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * Display action links in the Plugins list table. |
| 711 | * |
| 712 | * @param array $actions Plugin Action links. |
| 713 | * @return array |
| 714 | */ |
| 715 | public static function plugin_action_links( $actions ) { |
| 716 | $new_actions = array( |
| 717 | 'settings' => '<a href="' . admin_url( 'admin.php?page=evf-settings' ) . '" aria-label="' . esc_attr__( 'View Everest Forms Settings', 'everest-forms' ) . '">' . esc_html__( 'Settings', 'everest-forms' ) . '</a>', |
| 718 | ); |
| 719 | |
| 720 | return array_merge( $new_actions, $actions ); |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Display row meta in the Plugins list table. |
| 725 | * |
| 726 | * @param array $plugin_meta Plugin Row Meta. |
| 727 | * @param string $plugin_file Plugin Row Meta. |
| 728 | * @return array |
| 729 | */ |
| 730 | public static function plugin_row_meta( $plugin_meta, $plugin_file ) { |
| 731 | if ( EVF_PLUGIN_BASENAME === $plugin_file ) { |
| 732 | $new_plugin_meta = array( |
| 733 | 'docs' => '<a href="' . esc_url( apply_filters( 'everest_forms_docs_url', 'https://docs.wpeverest.com/documentation/plugins/everest-forms/' ) ) . '" aria-label="' . esc_attr__( 'View Everest Forms documentation', 'everest-forms' ) . '">' . esc_html__( 'Docs', 'everest-forms' ) . '</a>', |
| 734 | 'support' => '<a href="' . esc_url( apply_filters( 'everest_forms_support_url', 'https://wordpress.org/support/plugin/everest-forms/' ) ) . '" aria-label="' . esc_attr__( 'Visit free customer support', 'everest-forms' ) . '">' . esc_html__( 'Free support', 'everest-forms' ) . '</a>', |
| 735 | ); |
| 736 | |
| 737 | return array_merge( $plugin_meta, $new_plugin_meta ); |
| 738 | } |
| 739 | |
| 740 | return (array) $plugin_meta; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | EVF_Install::init(); |
| 745 |