=' ) ); if ( ! defined( 'GRAVITY_MANAGER_URL' ) ) { define( 'GRAVITY_MANAGER_URL', 'https://www.gravityhelp.com/wp-content/plugins/gravitymanager' ); } if ( ! defined( 'GRAVITY_MANAGER_PROXY_URL' ) ) { define( 'GRAVITY_MANAGER_PROXY_URL', 'http://proxy.gravityplugins.com' ); } require_once( plugin_dir_path( __FILE__ ) . 'common.php' ); require_once( plugin_dir_path( __FILE__ ) . 'forms_model.php' ); require_once( plugin_dir_path( __FILE__ ) . 'widget.php' ); require_once( plugin_dir_path( __FILE__ ) . 'includes/api.php' ); require_once( plugin_dir_path( __FILE__ ) . 'includes/webapi/webapi.php' ); require_once( plugin_dir_path( __FILE__ ) . 'includes/fields/class-gf-fields.php' ); // GFCommon::$version is deprecated, set it to current version for backwards compatibility GFCommon::$version = GFForms::$version; add_action( 'init', array( 'RGForms', 'init' ) ); add_action( 'wp', array( 'RGForms', 'maybe_process_form' ), 9 ); add_action( 'admin_init', array( 'RGForms', 'maybe_process_form' ), 9 ); add_action( 'wp', array( 'RGForms', 'process_exterior_pages' ) ); add_filter( 'upgrader_pre_install', array( 'GFForms', 'validate_upgrade' ), 10, 2 ); add_filter( 'tiny_mce_before_init', array( 'GFForms', 'modify_tiny_mce_4' ), 20 ); add_filter( 'user_has_cap', array( 'RGForms', 'user_has_cap' ), 10, 3 ); //Hooks for no-conflict functionality if ( is_admin() && ( RGForms::is_gravity_page() || RGForms::is_gravity_ajax_action() ) ) { add_action( 'wp_print_scripts', array( 'RGForms', 'no_conflict_mode_script' ), 1000 ); add_action( 'admin_print_footer_scripts', array( 'RGForms', 'no_conflict_mode_script' ), 9 ); add_action( 'wp_print_styles', array( 'RGForms', 'no_conflict_mode_style' ), 1000 ); add_action( 'admin_print_styles', array( 'RGForms', 'no_conflict_mode_style' ), 1 ); add_action( 'admin_print_footer_scripts', array( 'RGForms', 'no_conflict_mode_style' ), 1 ); add_action( 'admin_footer', array( 'RGForms', 'no_conflict_mode_style' ), 1 ); } add_action( 'plugins_loaded', array( 'GFForms', 'loaded' ) ); class GFForms { public static $version = '1.9.13'; public static function loaded() { /** * Fires when Gravity Forms has loaded. When developing Add-Ons, use this hook to initialize any functionality that depends on Gravity Forms functionality */ do_action( 'gform_loaded' ); //initializing Add-Ons if necessary if ( class_exists( 'GFAddOn' ) ) { GFAddOn::init_addons(); } } public static function has_members_plugin() { return function_exists( 'members_get_capabilities' ); } //Plugin starting point. Will load appropriate files public static function init() { //load text domains GFCommon::load_gf_text_domain( 'gravityforms' ); add_filter( 'gform_logging_supported', array( 'RGForms', 'set_logging_supported' ) ); add_action( 'admin_head', array( 'GFCommon', 'maybe_output_gf_vars' ) ); self::register_scripts(); //Maybe set up Gravity Forms: only on admin requests for single site installation and always for multisite if ( ( IS_ADMIN && false === ( defined( 'DOING_AJAX' ) && true === DOING_AJAX ) ) || is_multisite() ) { self::setup(); } //Plugin update actions add_filter( 'transient_update_plugins', array( 'GFForms', 'check_update' ) ); add_filter( 'site_transient_update_plugins', array( 'GFForms', 'check_update' ) ); add_filter( 'auto_update_plugin', array( 'GFForms', 'maybe_auto_update' ), 10, 2 ); if ( IS_ADMIN ) { global $current_user; //Members plugin integration. Adding Gravity Forms roles to the checkbox list if ( self::has_members_plugin() ) { add_filter( 'members_get_capabilities', array( 'RGForms', 'members_get_capabilities' ) ); } if ( is_multisite() ) { add_filter( 'wpmu_drop_tables', array( 'GFFormsModel', 'mu_drop_tables' ) ); } add_action( 'admin_enqueue_scripts', array( 'GFForms', 'enqueue_admin_scripts' ) ); add_action( 'print_media_templates', array( 'GFForms', 'action_print_media_templates' ) ); if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { add_action( 'admin_footer', array( 'GFForms', 'deprecate_add_on_methods' ) ); } //Loading Gravity Forms if user has access to any functionality if ( GFCommon::current_user_can_any( GFCommon::all_caps() ) ) { require_once( GFCommon::get_base_path() . '/export.php' ); GFExport::maybe_export(); //imports theme forms if configured to be automatic imported self::maybe_import_theme_forms(); //creates the "Forms" left menu add_action( 'admin_menu', array( 'RGForms', 'create_menu' ) ); if ( GF_SUPPORTED_WP_VERSION ) { add_action( 'admin_footer', array( 'RGForms', 'check_upload_folder' ) ); add_action( 'wp_dashboard_setup', array( 'RGForms', 'dashboard_setup' ) ); // Support modifying the admin page title for settings add_filter( 'admin_title', array( __class__, 'modify_admin_title' ), 10, 2 ); //Adding "embed form" button add_action( 'media_buttons', array( 'RGForms', 'add_form_button' ), 20 ); require_once( GFCommon::get_base_path() . '/includes/locking/locking.php' ); if ( self::page_supports_add_form_button() ) { add_action( 'admin_footer', array( 'RGForms', 'add_mce_popup' ) ); } if ( self::is_gravity_page() ) { require_once( GFCommon::get_base_path() . '/tooltips.php' ); } else if ( RG_CURRENT_PAGE == 'media-upload.php' ) { require_once( GFCommon::get_base_path() . '/entry_list.php' ); } else if ( in_array( RG_CURRENT_PAGE, array( 'admin.php', 'admin-ajax.php' ) ) ) { add_action( 'wp_ajax_rg_save_form', array( 'RGForms', 'save_form' ) ); add_action( 'wp_ajax_rg_change_input_type', array( 'RGForms', 'change_input_type' ) ); add_action( 'wp_ajax_rg_refresh_field_preview', array( 'RGForms', 'refresh_field_preview' ) ); add_action( 'wp_ajax_rg_add_field', array( 'RGForms', 'add_field' ) ); add_action( 'wp_ajax_rg_duplicate_field', array( 'RGForms', 'duplicate_field' ) ); add_action( 'wp_ajax_rg_delete_field', array( 'RGForms', 'delete_field' ) ); add_action( 'wp_ajax_rg_delete_file', array( 'RGForms', 'delete_file' ) ); add_action( 'wp_ajax_rg_select_export_form', array( 'RGForms', 'select_export_form' ) ); add_action( 'wp_ajax_rg_start_export', array( 'RGForms', 'start_export' ) ); add_action( 'wp_ajax_gf_upgrade_license', array( 'RGForms', 'upgrade_license' ) ); add_action( 'wp_ajax_gf_delete_custom_choice', array( 'RGForms', 'delete_custom_choice' ) ); add_action( 'wp_ajax_gf_save_custom_choice', array( 'RGForms', 'save_custom_choice' ) ); add_action( 'wp_ajax_gf_get_post_categories', array( 'RGForms', 'get_post_category_values' ) ); add_action( 'wp_ajax_gf_get_notification_post_categories', array( 'RGForms', 'get_notification_post_category_values' ) ); add_action( 'wp_ajax_gf_save_confirmation', array( 'RGForms', 'save_confirmation' ) ); add_action( 'wp_ajax_gf_delete_confirmation', array( 'RGForms', 'delete_confirmation' ) ); add_action( 'wp_ajax_gf_save_new_form', array( 'RGForms', 'save_new_form' ) ); //entry list ajax operations add_action( 'wp_ajax_rg_update_lead_property', array( 'RGForms', 'update_lead_property' ) ); add_action( 'wp_ajax_delete-gf_entry', array( 'RGForms', 'update_lead_status' ) ); //form list ajax operations add_action( 'wp_ajax_rg_update_form_active', array( 'RGForms', 'update_form_active' ) ); //notification list ajax operations add_action( 'wp_ajax_rg_update_notification_active', array( 'RGForms', 'update_notification_active' ) ); //confirmation list ajax operations add_action( 'wp_ajax_rg_update_confirmation_active', array( 'RGForms', 'update_confirmation_active' ) ); //dynamic captcha image add_action( 'wp_ajax_rg_captcha_image', array( 'RGForms', 'captcha_image' ) ); //dashboard message "dismiss upgrade" link add_action( 'wp_ajax_rg_dismiss_upgrade', array( 'RGForms', 'dashboard_dismiss_upgrade' ) ); // entry detail: resend notifications add_action( 'wp_ajax_gf_resend_notifications', array( 'RGForms', 'resend_notifications' ) ); // Shortcode UI add_action( 'wp_ajax_gf_do_shortcode', array( 'GFForms', 'handle_ajax_do_shortcode' ) ); } add_filter( 'plugins_api', array( 'RGForms', 'get_addon_info' ), 100, 3 ); add_action( 'after_plugin_row_gravityforms/gravityforms.php', array( 'RGForms', 'plugin_row' ) ); add_action( 'install_plugins_pre_plugin-information', array( 'RGForms', 'display_changelog' ) ); add_filter( 'plugin_action_links', array( 'RGForms', 'plugin_settings_link' ), 10, 2 ); } } add_action( 'admin_init', array( 'RGForms', 'ajax_parse_request' ), 10 ); } else { add_action( 'wp_enqueue_scripts', array( 'RGForms', 'enqueue_scripts' ), 11 ); add_action( 'wp', array( 'RGForms', 'ajax_parse_request' ), 10 ); } // Add "Form" to the "New" menu in WP admin bar add_action( 'wp_before_admin_bar_render', array( 'GFForms', 'admin_bar' ) ); add_shortcode( 'gravityform', array( 'RGForms', 'parse_shortcode' ) ); add_shortcode( 'gravityforms', array( 'RGForms', 'parse_shortcode' ) ); // ManageWP premium update filters add_filter( 'mwp_premium_update_notification', array( 'RGForms', 'premium_update_push' ) ); add_filter( 'mwp_premium_perform_update', array( 'RGForms', 'premium_update' ) ); // Push Gravity Forms to the top of the list of plugins to make sure it's loaded before any add-ons add_action( 'activated_plugin', array( 'GFForms', 'load_first' ) ); } public static function load_first() { $plugin_path = basename( dirname( __FILE__ ) ) . '/gravityforms.php'; $active_plugins = get_option( 'active_plugins' ); $key = array_search( $plugin_path, $active_plugins ); if ( $key > 0 ) { array_splice( $active_plugins, $key, 1 ); array_unshift( $active_plugins, $plugin_path ); update_option( 'active_plugins', $active_plugins ); } } public static function set_logging_supported( $plugins ) { $plugins['gravityforms'] = 'Gravity Forms Core'; return $plugins; } public static function maybe_process_form() { $form_id = isset( $_POST['gform_submit'] ) ? absint( $_POST['gform_submit'] ) : 0; if ( $form_id ) { $form_info = RGFormsModel::get_form( $form_id ); $is_valid_form = $form_info && $form_info->is_active; if ( $is_valid_form ) { require_once( GFCommon::get_base_path() . '/form_display.php' ); GFFormDisplay::process_form( $form_id ); } } elseif ( isset( $_POST['gform_send_resume_link'] ) ) { require_once( GFCommon::get_base_path() . '/form_display.php' ); GFFormDisplay::process_send_resume_link(); } } public static function process_exterior_pages() { if ( rgempty( 'gf_page', $_GET ) ) { return; } $page = rgget( 'gf_page' ); $is_legacy_upload_page = $_SERVER['REQUEST_METHOD'] == 'POST' && $page == 'upload'; if ( $is_legacy_upload_page && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) { _doing_it_wrong( 'gf_page=upload', 'gf_page=upload is now deprecated. Use GFCommon::get_upload_page_slug() instead', '1.9.6.13' ); } $is_upload_page = $_SERVER['REQUEST_METHOD'] == 'POST' && $page == GFCommon::get_upload_page_slug(); if ( $is_upload_page || $is_legacy_upload_page ) { require_once( GFCommon::get_base_path() . '/includes/upload.php' ); exit(); } //ensure users are logged in if ( ! is_user_logged_in() ) { auth_redirect(); } switch ( $page ) { case 'preview': require_once( GFCommon::get_base_path() . '/preview.php' ); break; case 'print-entry' : require_once( GFCommon::get_base_path() . '/print-entry.php' ); break; case 'select_columns' : require_once( GFCommon::get_base_path() . '/select_columns.php' ); break; } exit(); } public static function check_update( $update_plugins_option ) { if ( ! class_exists( 'GFCommon' ) ) { require_once( 'common.php' ); } return GFCommon::check_update( $update_plugins_option, true ); } //Creates or updates database tables. Will only run when version changes public static function setup( $force_setup = false ) { $current_version = get_option( 'rg_form_version' ); if ( $current_version === false ){ // Turn background updates on by default for all new installations. update_option( 'gform_pending_installation', true ); update_option( 'gform_enable_background_updates', true ); } $has_version_changed = $current_version != GFCommon::$version; if ( $has_version_changed ) { //Making sure version has really changed. Gets around aggressive caching issue on some sites that cause setup to run multiple times. $has_version_changed = self::get_wp_option( 'rg_form_version' ) != GFCommon::$version; } if ( $has_version_changed || $force_setup ) { $blog_id = get_current_blog_id(); GFCommon::log_debug( "GFForms::setup(): Blog {$blog_id} - Beginning of setup. From version " . get_option( 'rg_form_version' ) . ' to version ' . GFCommon::$version ); //setting up database structure self::setup_database(); //auto-setting and auto-validating license key based on value configured via the GF_LICENSE_KEY constant or the gf_license_key variable //auto-populating reCAPTCHA keys base on constant self::maybe_populate_keys(); //Auto-importing forms based on GF_IMPORT_FILE AND GF_THEME_IMPORT_FILE self::maybe_import_forms(); self::add_security_files(); self::do_self_healing(); //The format the version info changed to JSON. Make sure the old format is not cached. if ( version_compare( get_option( 'rg_form_version' ), '1.8.0.3', '<' ) ) { delete_transient( 'gform_update_info' ); } update_option( 'rg_form_version', GFCommon::$version ); GFCommon::log_debug( "GFForms::setup(): Blog {$blog_id} - End of setup." ); } } public static function setup_database() { global $wpdb; require_once( ABSPATH . '/wp-admin/includes/upgrade.php' ); if ( ! empty( $wpdb->charset ) ) { $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; } if ( ! empty( $wpdb->collate ) ) { $charset_collate .= " COLLATE $wpdb->collate"; } //Fixes issue with dbDelta lower-casing table names, which cause problems on case sensitive DB servers. add_filter( 'dbdelta_create_queries', array( 'RGForms', 'dbdelta_fix_case' ) ); /* * Indexes have a maximum size of 767 bytes. Historically, we haven't need to be concerned about that. * As of 4.2, however, WP core moved to utf8mb4, which uses 4 bytes per character. This means that an index which * used to have room for floor(767/3) = 255 characters, now only has room for floor(767/4) = 191 characters. */ $max_index_length = 191; //------ FORM ----------------------------------------------- $form_table_name = RGFormsModel::get_form_table_name(); $sql = 'CREATE TABLE ' . $form_table_name . " ( id mediumint(8) unsigned not null auto_increment, title varchar(150) not null, date_created datetime not null, is_active tinyint(1) not null default 1, is_trash tinyint(1) not null default 0, PRIMARY KEY (id) ) $charset_collate;"; dbDelta( $sql ); //droping table that was created by mistake in version 1.6.3.2 $wpdb->query( 'DROP TABLE IF EXISTS A' . $form_table_name ); //------ META ----------------------------------------------- $meta_table_name = RGFormsModel::get_meta_table_name(); $sql = 'CREATE TABLE ' . $meta_table_name . " ( form_id mediumint(8) unsigned not null, display_meta longtext, entries_grid_meta longtext, confirmations longtext, notifications longtext, PRIMARY KEY (form_id) ) $charset_collate;"; dbDelta( $sql ); //droping outdated form_id index (if one exists) self::drop_index( $meta_table_name, 'form_id' ); //------ FORM VIEW ----------------------------------------------- $form_view_table_name = RGFormsModel::get_form_view_table_name(); $sql = 'CREATE TABLE ' . $form_view_table_name . " ( id bigint(20) unsigned not null auto_increment, form_id mediumint(8) unsigned not null, date_created datetime not null, ip char(15), count mediumint(8) unsigned not null default 1, PRIMARY KEY (id), KEY form_id (form_id) ) $charset_collate;"; dbDelta( $sql ); //------ LEAD ----------------------------------------------- $lead_table_name = RGFormsModel::get_lead_table_name(); $sql = 'CREATE TABLE ' . $lead_table_name . " ( id int(10) unsigned not null auto_increment, form_id mediumint(8) unsigned not null, post_id bigint(20) unsigned, date_created datetime not null, is_starred tinyint(1) not null default 0, is_read tinyint(1) not null default 0, ip varchar(39) not null, source_url varchar(200) not null default '', user_agent varchar(250) not null default '', currency varchar(5), payment_status varchar(15), payment_date datetime, payment_amount decimal(19,2), payment_method varchar(30), transaction_id varchar(50), is_fulfilled tinyint(1), created_by bigint(20) unsigned, transaction_type tinyint(1), status varchar(20) not null default 'active', PRIMARY KEY (id), KEY form_id (form_id), KEY status (status) ) $charset_collate;"; dbDelta( $sql ); //------ LEAD NOTES ------------------------------------------ $lead_notes_table_name = RGFormsModel::get_lead_notes_table_name(); $sql = 'CREATE TABLE ' . $lead_notes_table_name . " ( id int(10) unsigned not null auto_increment, lead_id int(10) unsigned not null, user_name varchar(250), user_id bigint(20), date_created datetime not null, value longtext, note_type varchar(50), PRIMARY KEY (id), KEY lead_id (lead_id), KEY lead_user_key (lead_id,user_id) ) $charset_collate;"; dbDelta( $sql ); //------ LEAD DETAIL ----------------------------------------- $lead_detail_table_name = RGFormsModel::get_lead_details_table_name(); $sql = 'CREATE TABLE ' . $lead_detail_table_name . ' ( id bigint(20) unsigned not null auto_increment, lead_id int(10) unsigned not null, form_id mediumint(8) unsigned not null, field_number float not null, value varchar(' . GFORMS_MAX_FIELD_LENGTH . "), PRIMARY KEY (id), KEY form_id (form_id), KEY lead_id (lead_id), KEY lead_field_number (lead_id,field_number) ) $charset_collate;"; dbDelta( $sql ); //------ LEAD DETAIL LONG ----------------------------------- $lead_detail_long_table_name = RGFormsModel::get_lead_details_long_table_name(); $sql = 'CREATE TABLE ' . $lead_detail_long_table_name . " ( lead_detail_id bigint(20) unsigned not null, value longtext, PRIMARY KEY (lead_detail_id) ) $charset_collate;"; dbDelta( $sql ); // dropping outdated form_id index (if one exists) self::drop_index( $lead_detail_long_table_name, 'lead_detail_key' ); //------ LEAD META ------------------------------------------ $lead_meta_table_name = RGFormsModel::get_lead_meta_table_name(); // dropping meta_key and form_id_meta_key (if they exist) to prevent duplicate keys error on upgrade if ( version_compare( get_option( 'rg_form_version' ), '1.9.8.12', '<' ) ) { self::drop_index( $lead_meta_table_name, 'meta_key' ); self::drop_index( $lead_meta_table_name, 'form_id_meta_key' ); } $sql = 'CREATE TABLE ' . $lead_meta_table_name . " ( id bigint(20) unsigned not null auto_increment, form_id mediumint(8) unsigned not null default 0, lead_id bigint(20) unsigned not null, meta_key varchar(255), meta_value longtext, PRIMARY KEY (id), KEY meta_key (meta_key($max_index_length)), KEY lead_id (lead_id), KEY form_id_meta_key (form_id,meta_key($max_index_length)) ) $charset_collate;"; dbDelta( $sql ); //------ INCOMPLETE SUBMISSIONS ------------------------------- $incomplete_submissions_table_name = RGFormsModel::get_incomplete_submissions_table_name(); $sql = 'CREATE TABLE ' . $incomplete_submissions_table_name . " ( uuid char(32) not null, email varchar(255), form_id mediumint(8) unsigned not null, date_created datetime not null, ip varchar(39) not null, source_url longtext not null, submission longtext not null, PRIMARY KEY (uuid), KEY form_id (form_id) ) $charset_collate;"; dbDelta( $sql ); remove_filter( 'dbdelta_create_queries', array( 'RGForms', 'dbdelta_fix_case' ) ); //fix form_id value needed to update from version 1.6.11 self::fix_lead_meta_form_id_values(); //fix checkbox value. needed for version 1.0 and below but won't hurt for higher versions self::fix_checkbox_value(); //fix leading and trailing spaces in Form objects and entry values if ( version_compare( get_option( 'rg_form_version' ), '1.8.3.1', '<' ) ) { self::fix_leading_and_trailing_spaces(); } } public static function add_security_files() { $upload_root = GFFormsModel::get_upload_root(); if ( ! is_dir( $upload_root ) ) { return; } GFCommon::recursive_add_index_file( $upload_root ); GFCommon::add_htaccess_file(); } private static function do_self_healing() { $flag_security_alert = self::heal_wp_upload_dir(); $gf_upload_root = GFFormsModel::get_upload_root(); if ( ! is_dir( $gf_upload_root ) ) { return; } $flag_security_alert = self::rename_suspicious_files_recursive( $gf_upload_root, $flag_security_alert ); if ( $flag_security_alert ) { update_option( 'gform_security_alert', $flag_security_alert ); } } /** * Renames files with a .bak extension if they have a file extension that is not allowed in the Gravity Forms uploads folder. */ private static function rename_suspicious_files_recursive( $dir, $flag_security_alert = false ) { if ( ! is_dir( $dir ) || is_link( $dir ) ) { return; } if ( ! ( $dir_handle = opendir( $dir ) ) ) { return; } // ignores all errors set_error_handler( create_function( '', 'return 0;' ), E_ALL ); while ( false !== ( $file = readdir( $dir_handle ) ) ) { if ( is_dir( $dir . DIRECTORY_SEPARATOR . $file ) && $file != '.' && $file != '..' ) { $flag_security_alert = self::rename_suspicious_files_recursive( $dir . DIRECTORY_SEPARATOR . $file, $flag_security_alert ); } elseif ( GFCommon::file_name_has_disallowed_extension( $file ) && ! GFCommon::match_file_extension( $file, array( 'htaccess', 'bak', 'html' ) ) ) { $mini_hash = substr( wp_hash( $file ), 0, 6 ); $newName = sprintf( '%s/%s.%s.bak', $dir, $file, $mini_hash ); rename( $dir . '/' . $file, $newName ); $flag_security_alert = true; } } closedir( $dir_handle ); return $flag_security_alert; } private static function heal_wp_upload_dir(){ $wp_upload_dir = wp_upload_dir(); $wp_upload_path = $wp_upload_dir['basedir']; if ( ! is_dir( $wp_upload_path ) ) { return; } $flag_security_alert = false; // ignores all errors set_error_handler( create_function( '', 'return 0;' ), E_ALL ); foreach ( glob( $wp_upload_path . DIRECTORY_SEPARATOR . '*_input_*.{php,php5}', GLOB_BRACE ) as $filename ) { $mini_hash = substr( wp_hash( $filename ), 0, 6 ); $newName = sprintf( '%s.%s.bak', $filename, $mini_hash ); rename( $filename, $newName ); $flag_security_alert = true; } return $flag_security_alert; } private static function fix_leading_and_trailing_spaces() { global $wpdb; $meta_table_name = GFFormsModel::get_meta_table_name(); $lead_details_table = GFFormsModel::get_lead_details_table_name(); $lead_details_long_table = GFFormsModel::get_lead_details_long_table_name(); $result = $wpdb->query( "UPDATE $lead_details_table SET value = TRIM(value)" ); $result = $wpdb->query( "UPDATE $lead_details_long_table SET value = TRIM(value)" ); $results = $wpdb->get_results( "SELECT form_id, display_meta, confirmations, notifications FROM {$meta_table_name}", ARRAY_A ); foreach ( $results as &$result ) { $form_id = $result['form_id']; $form = GFFormsModel::unserialize( $result['display_meta'] ); $form_updated = false; $form = GFFormsModel::trim_form_meta_values( $form, $form_updated ); if ( $form_updated ) { GFFormsModel::update_form_meta( $form_id, $form ); } $confirmations = GFFormsModel::unserialize( $result['confirmations'] ); $confirmations_updated = false; $confirmations = GFFormsModel::trim_conditional_logic_values( $confirmations, $form, $confirmations_updated ); if ( $confirmations_updated ) { GFFormsModel::update_form_meta( $form_id, $confirmations, 'confirmations' ); } $notifications = GFFormsModel::unserialize( $result['notifications'] ); $notifications_updated = false; $notifications = GFFormsModel::trim_conditional_logic_values( $notifications, $form, $notifications_updated ); if ( $notifications_updated ) { GFFormsModel::update_form_meta( $form_id, $notifications, 'notifications' ); } } return $results; } private static function maybe_import_forms() { if ( defined( 'GF_IMPORT_FILE' ) && ! get_option( 'gf_imported_file' ) ) { require_once( GFCommon::get_base_path() . '/export.php' ); GFExport::import_file( GF_IMPORT_FILE ); update_option( 'gf_imported_file', true ); } } private static function maybe_import_theme_forms() { //Import theme specific forms if configured. Will only import forms once per theme. if ( defined( 'GF_THEME_IMPORT_FILE' ) ) { $themes = get_option( 'gf_imported_theme_file' ); if ( ! is_array( $themes ) ) { $themes = array(); } //if current theme has already imported it's forms, don't import again $theme = get_template(); if ( ! isset( $themes[ $theme ] ) ) { require_once( GFCommon::get_base_path() . '/export.php' ); //importing forms GFExport::import_file( get_stylesheet_directory() . '/' . GF_THEME_IMPORT_FILE ); //adding current theme to the list of imported themes. So that forms are not imported again for it. $themes[ $theme ] = true; update_option( 'gf_imported_theme_file', $themes ); } } } private static function maybe_populate_keys() { global $gf_license_key; $license_key = defined( 'GF_LICENSE_KEY' ) && empty( $gf_license_key ) ? GF_LICENSE_KEY : $gf_license_key; if ( ! empty( $license_key ) ) { RGFormsModel::save_key( $license_key ); GFCommon::cache_remote_message(); GFCommon::get_version_info( false ); } //auto-setting recaptcha keys based on value configured via the constant or global variable global $gf_recaptcha_public_key, $gf_recaptcha_private_key; $private_key = defined( 'GF_RECAPTCHA_PRIVATE_KEY' ) && empty( $gf_recaptcha_private_key ) ? GF_RECAPTCHA_PRIVATE_KEY : $gf_recaptcha_private_key; if ( ! empty( $private_key ) ) { update_option( 'rg_gforms_captcha_private_key', $private_key ); } $public_key = defined( 'GF_RECAPTCHA_PUBLIC_KEY' ) && empty( $gf_recaptcha_public_key ) ? GF_RECAPTCHA_PUBLIC_KEY : $gf_recaptcha_public_key; if ( ! empty( $public_key ) ) { update_option( 'rg_gforms_captcha_public_key', $public_key ); } } public static function get_wp_option( $option_name ) { global $wpdb; return $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM {$wpdb->prefix}options WHERE option_name=%s", $option_name ) ); } //Changes form_id values from default value "0" to the correct value. Needed when upgrading users from 1.6.11 private static function fix_lead_meta_form_id_values() { global $wpdb; $lead_meta_table_name = RGFormsModel::get_lead_meta_table_name(); $lead_table_name = RGFormsModel::get_lead_table_name(); $sql = "UPDATE $lead_meta_table_name lm,$lead_table_name l SET lm.form_id = l.form_id WHERE lm.form_id=0 AND lm.lead_id = l.id; "; $wpdb->get_results( $sql ); } public static function dbdelta_fix_case( $cqueries ) { foreach ( $cqueries as $table => $qry ) { $table_name = $table; if ( preg_match( "|CREATE TABLE ([^ ]*)|", $qry, $matches ) ) { $query_table_name = trim( $matches[1], '`' ); //fix table names that are different just by their casing if ( strtolower( $query_table_name ) == $table ) { $table_name = $query_table_name; } } $queries[ $table_name ] = $qry; } return $queries; } public static function no_conflict_mode_style() { if ( ! get_option( 'gform_enable_noconflict' ) ) { return; } global $wp_styles; $wp_required_styles = array( 'admin-bar', 'colors', 'ie', 'wp-admin', 'editor-style' ); $gf_required_styles = array( 'common' => array(), 'gf_edit_forms' => array( 'thickbox', 'editor-buttons', 'wp-jquery-ui-dialog', 'media-views', 'buttons', 'wp-pointer' ), 'gf_edit_forms_notification' => array( 'thickbox', 'editor-buttons', 'wp-jquery-ui-dialog', 'media-views', 'buttons' ), 'gf_new_form' => array( 'thickbox' ), 'gf_entries' => array( 'thickbox' ), 'gf_settings' => array(), 'gf_export' => array(), 'gf_help' => array() ); self::no_conflict_mode( $wp_styles, $wp_required_styles, $gf_required_styles, 'styles' ); } public static function no_conflict_mode_script() { if ( ! get_option( 'gform_enable_noconflict' ) ) { return; } global $wp_scripts; $wp_required_scripts = array( 'admin-bar', 'common', 'jquery-color', 'utils', 'svg-painter' ); $gf_required_scripts = array( 'common' => array( 'gform_tooltip_init', 'sack' ), 'gf_edit_forms' => array( 'backbone', 'editor', 'gform_floatmenu', 'gform_forms', 'gform_form_admin', 'gform_form_editor', 'gform_gravityforms', 'gform_json', 'gform_menu', 'gform_placeholder', 'jquery-ui-autocomplete', 'jquery-ui-core', 'jquery-ui-datepicker', 'jquery-ui-sortable', 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-tabs', 'json2', 'media-editor', 'media-models', 'media-upload', 'media-views', 'plupload', 'plupload-flash', 'plupload-html4', 'plupload-html5', 'quicktags', 'rg_currency', 'thickbox', 'word-count', 'wp-plupload', 'wpdialogs-popup', 'wplink', 'wp-pointer' ), 'gf_edit_forms_notification' => array( 'editor', 'word-count', 'quicktags', 'wpdialogs-popup', 'media-upload', 'wplink', 'backbone', 'jquery-ui-sortable', 'json2', 'media-editor', 'media-models', 'media-views', 'plupload', 'plupload-flash', 'plupload-html4', 'plupload-html5', 'plupload-silverlight', 'wp-plupload', 'gform_placeholder', 'gform_json', 'jquery-ui-autocomplete' ), 'gf_new_form' => array( 'thickbox', 'jquery-ui-core', 'jquery-ui-sortable', 'jquery-ui-tabs', 'rg_currency', 'gform_gravityforms', 'gform_json', 'gform_form_admin' ), 'gf_entries' => array( 'thickbox', 'gform_gravityforms', 'wp-lists', 'gform_json', 'gform_field_filter', 'plupload-all' ), 'gf_settings' => array(), 'gf_export' => array( 'gform_form_admin', 'jquery-ui-datepicker', 'gform_field_filter' ), 'gf_help' => array(), ); self::no_conflict_mode( $wp_scripts, $wp_required_scripts, $gf_required_scripts, 'scripts' ); } private static function no_conflict_mode( &$wp_objects, $wp_required_objects, $gf_required_objects, $type = 'scripts' ) { $current_page = trim( strtolower( rgget( 'page' ) ) ); if ( empty( $current_page ) ) { $current_page = trim( strtolower( rgget( 'gf_page' ) ) ); } if ( empty( $current_page ) ) { $current_page = RG_CURRENT_PAGE; } $view = rgempty( 'view', $_GET ) ? 'default' : rgget( 'view' ); $page_objects = isset( $gf_required_objects[ $current_page . '_' . $view ] ) ? $gf_required_objects[ $current_page . '_' . $view ] : rgar( $gf_required_objects, $current_page ); //disable no-conflict if $page_objects is false if ( $page_objects === false ) { return; } if ( ! is_array( $page_objects ) ) { $page_objects = array(); } //merging wp scripts with gravity forms scripts $required_objects = array_merge( $wp_required_objects, $gf_required_objects['common'], $page_objects ); //allowing addons or other products to change the list of no conflict scripts $required_objects = apply_filters( "gform_noconflict_{$type}", $required_objects ); $queue = array(); foreach ( $wp_objects->queue as $object ) { if ( in_array( $object, $required_objects ) ) { $queue[] = $object; } } $wp_objects->queue = $queue; $required_objects = self::add_script_dependencies( $wp_objects->registered, $required_objects ); //unregistering scripts $registered = array(); foreach ( $wp_objects->registered as $script_name => $script_registration ) { if ( in_array( $script_name, $required_objects ) ) { $registered[ $script_name ] = $script_registration; } } $wp_objects->registered = $registered; } private static function add_script_dependencies( $registered, $scripts ) { //gets all dependent scripts linked to the $scripts array passed do { $dependents = array(); foreach ( $scripts as $script ) { $deps = isset( $registered[ $script ] ) && is_array( $registered[ $script ]->deps ) ? $registered[ $script ]->deps : array(); foreach ( $deps as $dep ) { if ( ! in_array( $dep, $scripts ) && ! in_array( $dep, $dependents ) ) { $dependents[] = $dep; } } } $scripts = array_merge( $scripts, $dependents ); } while ( ! empty( $dependents ) ); return $scripts; } //Integration with ManageWP public static function premium_update_push( $premium_update ) { if ( ! function_exists( 'get_plugin_data' ) ) { include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); } $update = GFCommon::get_version_info(); if ( rgar( $update, 'is_valid_key' ) == true && version_compare( GFCommon::$version, $update['version'], '<' ) ) { $gforms = get_plugin_data( __FILE__ ); $gforms['type'] = 'plugin'; $gforms['slug'] = 'gravityforms/gravityforms.php'; $gforms['new_version'] = ! rgempty( 'version', $update ) ? $update['version'] : false; $premium_update[] = $gforms; } return $premium_update; } //Integration with ManageWP public static function premium_update( $premium_update ) { if ( ! function_exists( 'get_plugin_data' ) ) { include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); } $update = GFCommon::get_version_info(); if ( rgar( $update, 'is_valid_key' ) == true && version_compare( GFCommon::$version, $update['version'], '<' ) ) { $gforms = get_plugin_data( __FILE__ ); $gforms['slug'] = 'gravityforms/gravityforms.php'; // If not set by default, always pass theme template $gforms['type'] = 'plugin'; $gforms['url'] = ! rgempty( 'url', $update ) ? $update['url'] : false; // OR provide your own callback function for managing the update array_push( $premium_update, $gforms ); } return $premium_update; } public static function drop_index( $table, $index ) { global $wpdb; if ( ! GFFormsModel::is_valid_table( $table ) || ! GFFormsModel::is_valid_index( $index ) ) { return; } // check first if the table exists to prevent errors on first install $has_table = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ); if ( $has_table ) { $has_index = $wpdb->get_var( $wpdb->prepare( "SHOW INDEX FROM {$table} WHERE Key_name=%s", $index ) ); if ( $has_index ) { $wpdb->query( "DROP INDEX {$index} ON {$table}" ); } } } public static function validate_upgrade( $do_upgrade, $hook_extra ) { if ( rgar( $hook_extra, 'plugin' ) == 'gravityforms/gravityforms.php' && ! GFForms::has_database_permission( $error ) ) { return new WP_Error( 'no_db_permission', $error ); } return true; } private static function has_database_permission( &$error ) { global $wpdb; $wpdb->hide_errors(); $has_permission = true; $sql = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}rg_test ( col1 int )"; $wpdb->query( $sql ); $error = 'Current database user does not have necessary permissions to create tables. Gravity Forms requires that the database user has CREATE and ALTER permissions. If you need assistance in changing database user permissions, contact your hosting provider.'; if ( ! empty( $wpdb->last_error ) ) { $has_permission = false; } if ( $has_permission ) { $sql = "ALTER TABLE {$wpdb->prefix}rg_test ADD COLUMN a" . uniqid() . ' int'; $wpdb->query( $sql ); $error = 'Current database user does not have necessary permissions to modify (ALTER) tables. Gravity Forms requires that the database user has CREATE and ALTER permissions. If you need assistance in changing database user permissions, contact your hosting provider.'; if ( ! empty( $wpdb->last_error ) ) { $has_permission = false; } $sql = "DROP TABLE {$wpdb->prefix}rg_test"; $wpdb->query( $sql ); } $wpdb->show_errors(); return $has_permission; } //Changes checkbox entry values from "!" to the current choice text. Needed when upgrading users from 1.0 private static function fix_checkbox_value() { global $wpdb; $table_name = RGFormsModel::get_lead_details_table_name(); $sql = "select * from $table_name where value= '!'"; $results = $wpdb->get_results( $sql ); foreach ( $results as $result ) { $form = RGFormsModel::get_form_meta( $result->form_id ); $field = RGFormsModel::get_field( $form, $result->field_number ); if ( $field->type == 'checkbox' ) { $input = GFCommon::get_input( $field, $result->field_number ); $wpdb->update( $table_name, array( 'value' => $input['label'] ), array( 'id' => $result->id ) ); } } } public static function user_has_cap( $all_caps, $cap, $args ) { $gf_caps = GFCommon::all_caps(); $capability = rgar( $cap, 0 ); if ( $capability != 'gform_full_access' ) { return $all_caps; } if ( ! self::has_members_plugin() ) { //give full access to administrators if the members plugin is not installed if ( current_user_can( 'administrator' ) || is_super_admin() ) { $all_caps['gform_full_access'] = true; } } else if ( current_user_can( 'administrator' ) || is_super_admin() ) { //checking if user has any GF permission. $has_gf_cap = false; foreach ( $gf_caps as $gf_cap ) { if ( rgar( $all_caps, $gf_cap ) ) { $has_gf_cap = true; } } if ( ! $has_gf_cap ) { //give full access to administrators if none of the GF permissions are active by the Members plugin $all_caps['gform_full_access'] = true; } } return $all_caps; } //Target of Member plugin filter. Provides the plugin with Gravity Forms lists of capabilities public static function members_get_capabilities( $caps ) { return array_merge( $caps, GFCommon::all_caps() ); } //Tests if the upload folder is writable and displays an error message if not public static function check_upload_folder() { //check if upload folder is writable $folder = RGFormsModel::get_upload_root(); if ( empty( $folder ) ) { echo "