- ' . __("Add Form", "gravityforms") . ' ';
- }
- }
-
-
- //Action target that displays the popup to insert a form to a post/page
- public static function add_mce_popup(){
- ?>
-
-
-
-
- ' . __( 'Settings', 'gravityforms' ) . '');
-
- return $links;
- }
-
- //Displays message on Plugin's page
- public static function plugin_row($plugin_name){
- $key = GFCommon::get_key();
- $version_info = GFCommon::get_version_info();
-
- if(!rgar($version_info, "is_valid_key")){
-
- $plugin_name = "gravityforms/gravityforms.php";
-
- $new_version = version_compare(GFCommon::$version, $version_info["version"], '<') ? __('There is a new version of Gravity Forms available.', 'gravityforms') .' '. sprintf(__('View version %s Details', 'gravityforms'), $version_info["version"]) . ' . ' : '';
-
- echo '' . $new_version . __('
Register your copy of Gravity Forms to receive access to automatic upgrades and support. Need a license key?
Purchase one now .', 'gravityforms') . '
';
-
- }
- }
-
- //Displays current version details on Plugin's page
- public static function display_changelog(){
- if($_REQUEST["plugin"] != "gravityforms")
- return;
-
- $page_text = self::get_changelog();
- echo $page_text;
-
- exit;
- }
-
- public static function get_changelog(){
- $key = GFCommon::get_key();
- $body = "key=$key";
- $options = array('method' => 'POST', 'timeout' => 3, 'body' => $body);
- $options['headers'] = array(
- 'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset'),
- 'Content-Length' => strlen($body),
- 'User-Agent' => 'WordPress/' . get_bloginfo("version"),
- 'Referer' => get_bloginfo("url")
- );
-
- $raw_response = GFCommon::post_to_manager("changelog.php", GFCommon::get_remote_request_params(), $options);
- if ( is_wp_error( $raw_response ) || 200 != $raw_response['response']['code']){
- $page_text = __("Oops!! Something went wrong. Please try again or contact us .", 'gravityforms');
- }
- else{
- $page_text = $raw_response['body'];
- if(substr($page_text, 0, 10) != "")
- $page_text = "";
- }
- return stripslashes($page_text);
- }
-
-//------------------------------------------------------
-//-------------- DASHBOARD PAGE -------------------------
-
- //Registers the dashboard widget
- public static function dashboard_setup(){
- $dashboard_title = apply_filters("gform_dashboard_title", __("Forms", "gravityforms"));
- wp_add_dashboard_widget('rg_forms_dashboard', $dashboard_title, array('RGForms', 'dashboard'));
- }
-
- //Displays the dashboard UI
- public static function dashboard(){
- $forms = RGFormsModel::get_form_summary();
-
- if(sizeof($forms) > 0){
- ?>
-
-
-
-
-
-
-
-
-
- ', ''); ?>
-
- ", "");
- }*/
- $message = sprintf(__("There is an update available for Gravity Forms. %sView Details%s %s", "gravityforms"), "", " ", $auto_upgrade);
- ?>
-
-
- __( 'Next >', "gravityforms" ),
- 'prev' => __( '< Prev', "gravityforms" ),
- 'image' => __( 'Image', "gravityforms" ),
- 'of' => __( 'of', "gravityforms" ),
- 'close' => __( 'Close', "gravityforms" ),
- 'noiframes' => __( 'This feature requires inline frames. You have iframes disabled or your browser does not support them.' , 'gravityforms'),
- 'loadingAnimation' => includes_url( 'js/thickbox/loadingAnimation.gif' ),
- 'closeImage' => includes_url( 'js/thickbox/tb-close.png' )
- ) );
- }
-
- public static function enqueue_admin_scripts() {
-
- $scripts = array();
- $page = self::get_page();
-
- switch($page) {
- case 'new_form' :
- case 'form_list':
- $scripts = array(
- 'gform_gravityforms',
- 'gform_json',
- 'gform_form_admin',
- 'thickbox'
- );
- break;
-
- case 'form_settings':
- $scripts = array(
- 'gform_gravityforms',
- 'gform_forms',
- 'gform_json',
- 'gform_form_admin',
- 'gform_placeholder',
- 'jquery-ui-datepicker',
- 'gform_masked_input',
- 'jquery-ui-sortable'
- );
- break;
-
- case 'form_editor':
- $thickbox = !GFCommon::is_wp_version("3.3") ? 'gf_thickbox' : 'thickbox';
- $scripts = array(
- $thickbox,
- 'jquery-ui-core',
- 'jquery-ui-sortable',
- 'jquery-ui-tabs',
- 'sack',
- 'gform_gravityforms',
- 'gform_forms',
- 'gform_json',
- 'gform_form_admin',
- 'gform_floatmenu',
- 'gform_menu',
- 'gform_placeholder',
- 'jquery-ui-autocomplete'
- );
-
- if ( wp_is_mobile() )
- $scripts[] = 'jquery-touch-punch';
-
- break;
-
- case 'entry_detail':
- $scripts = array('gform_json');
- break;
-
- case 'entry_detail_edit':
- $scripts = array('gform_gravityforms','plupload-all');
- break;
-
- case 'entry_list':
- $scripts = array(
- 'wp-lists',
- 'wp-ajax-response',
- 'thickbox',
- 'gform_json',
- 'thickbox',
- 'gform_field_filter'
- );
- break;
-
- case 'notification_list':
- $scripts = array(
- 'gform_forms',
- 'gform_json',
- 'gform_form_admin'
- );
- break;
-
- case 'notification_new':
- case 'notification_edit':
- $scripts = array(
- 'jquery-ui-autocomplete',
- 'gform_gravityforms',
- 'gform_placeholder',
- 'gform_form_admin',
- 'gform_forms',
- 'gform_json'
- );
- break;
-
- case 'confirmation':
- $scripts = array(
- 'gform_form_admin',
- 'gform_forms',
- 'gform_gravityforms',
- 'gform_placeholder',
- 'gform_json',
- 'wp-pointer'
- );
- break;
-
- case 'addons':
- $scripts = array('thickbox');
- break;
-
- case 'export_entry':
- $scripts = array(
- 'jquery-ui-datepicker',
- 'gform_form_admin',
- 'gform_field_filter'
- );
- break;
-
- }
-
- if(empty($scripts))
- return;
-
- foreach($scripts as $script){
- wp_enqueue_script($script);
- }
-
-
- GFCommon::localize_gform_gravityforms_multifile();
-
-
- }
-
- public static function get_page() {
-
- /**
- * Page names:
- *
- * new_form
- * form_list
- * form_editor
- * form_settings
- * confirmation
- * notification_list
- * notification_new
- * notification_edit
- * entry_list
- * entry_detail
- * entry_detail_edit
- * settings
- * addons
- * export_entry
- * export_form
- * import_form
- */
-
- if( rgget('page') == 'gf_new_form' )
- return 'new_form';
-
- if( rgget('page') == 'gf_edit_forms' && !rgget('id') )
- return 'form_list';
-
- if( rgget('page') == 'gf_edit_forms' && !rgget('view') )
- return 'form_editor';
-
- if( rgget('page') == 'gf_edit_forms' && rgget('view') == 'settings' && ( !rgget('subview') || rgget('subview') == 'settings') )
- return 'form_settings';
-
- if( rgget('page') == 'gf_edit_forms' && rgget('view') == 'settings' && rgget('subview') == 'confirmation' )
- return 'confirmation';
-
- if( rgget('page') == 'gf_edit_forms' && rgget('view') == 'settings' && rgget('subview') == 'notification' && rgget('nid') )
- return 'notification_edit';
-
- if( rgget('page') == 'gf_edit_forms' && rgget('view') == 'settings' && rgget('subview') == 'notification' && isset($_GET['nid']) )
- return 'notification_edit';
-
- if( rgget('page') == 'gf_edit_forms' && rgget('view') == 'settings' && rgget('subview') == 'notification' )
- return 'notification_list';
-
- if( rgget('page') == 'gf_entries' && (!rgget('view') || rgget('view') == 'entries' ) )
- return 'entry_list';
-
- if( rgget('page') == 'gf_entries' && rgget('view') == 'entry' && isset($_POST['screen_mode']) && rgpost('screen_mode') != 'view')
- return 'entry_detail_edit';
-
- if( rgget('page') == 'gf_entries' && rgget('view') == 'entry')
- return 'entry_detail';
-
- if( rgget('page') == 'gf_settings')
- return 'settings';
-
- if(rgget('page') == 'gf_addons')
- return 'addons';
-
- if(rgget('page') == 'gf_export' && ( rgget('view') == 'export_entry' || !isset($_GET['view']) ) )
- return 'export_entry';
-
- if(rgget('page') == 'gf_export' && rgget('view') == 'export_form')
- return 'export_form';
-
- if(rgget('page') == 'gf_export' && rgget('view') == 'import_form')
- return 'import_form';
-
- return false;
- }
-
- public static function get_form($form_id, $display_title=true, $display_description=true, $force_display=false, $field_values=null, $ajax=false, $tabindex = 1){
- require_once(GFCommon::get_base_path() . "/form_display.php");
- return GFFormDisplay::get_form($form_id, $display_title, $display_description, $force_display, $field_values, $ajax, $tabindex);
- }
-
- public static function new_form(){
- self::form_list_page();
- }
-
- public static function enqueue_scripts(){
- require_once(GFCommon::get_base_path() . "/form_display.php");
- GFFormDisplay::enqueue_scripts();
- }
-
- public static function print_form_scripts($form, $ajax){
- require_once(GFCommon::get_base_path() . "/form_display.php");
- GFFormDisplay::print_form_scripts($form, $ajax);
- }
-
- public static function forms_page($form_id){
- require_once(GFCommon::get_base_path() . "/form_detail.php");
- GFFormDetail::forms_page($form_id);
- }
-
- public static function settings_page(){
- require_once(GFCommon::get_base_path() . "/settings.php");
- GFSettings::settings_page();
- }
-
- public static function add_settings_page( $name, $handle = '', $icon_path = '' ){
- require_once(GFCommon::get_base_path() . "/settings.php");
- GFSettings::add_settings_page($name, $handle, $icon_path);
- }
-
- public static function help_page(){
- require_once(GFCommon::get_base_path() . "/help.php");
- GFHelp::help_page();
- }
-
- public static function export_page(){
- require_once(GFCommon::get_base_path() . "/export.php");
- GFExport::export_page();
- }
-
- public static function update_page(){
- require_once(GFCommon::get_base_path() . "/update.php");
- GFUpdate::update_page();
- }
-
- public static function addons_page(){
-
- wp_print_styles(array("thickbox"));
-
- $plugins = get_plugins();
- $installed_plugins = array();
- foreach($plugins as $key => $plugin){
- $is_active = is_plugin_active($key);
- $installed_plugin = array("plugin" => $key, "name" => $plugin["Name"], "is_active"=>$is_active);
- $installed_plugin["activation_url"] = $is_active ? "" : wp_nonce_url("plugins.php?action=activate&plugin={$key}", "activate-plugin_{$key}");
- $installed_plugin["deactivation_url"] = !$is_active ? "" : wp_nonce_url("plugins.php?action=deactivate&plugin={$key}", "deactivate-plugin_{$key}");
-
- $installed_plugins[] = $installed_plugin;
- }
-
- $nonces = self::get_addon_nonces();
-
- $body = array("plugins" => urlencode(serialize($installed_plugins)), "nonces" => urlencode(serialize($nonces)), "key" => GFCommon::get_key());
- $options = array('body' => $body, 'headers' => array('Referer' => get_bloginfo("url")), 'timeout' => 15);
-
- $raw_response = GFCommon::post_to_manager("api.php", "op=plugin_browser&{$_SERVER["QUERY_STRING"]}", $options);
-
- if ( is_wp_error( $raw_response ) || $raw_response['response']['code'] != 200){
- echo "" . __("Add-On browser is currently unavailable. Please try again later.", "gravityforms") . "
";
- }
- else{
- echo GFCommon::get_remote_message();
- echo $raw_response["body"];
- }
- }
-
- public static function get_addon_info($api, $action, $args){
-
- if($action == "plugin_information" && empty($api) && ( !rgempty("rg", $_GET) || $args->slug == 'gravityforms')){
- $raw_response = GFCommon::post_to_manager("api.php", "op=get_plugin&slug={$args->slug}", array());
-
- if ( is_wp_error( $raw_response ) || $raw_response['response']['code'] != 200)
- return false;
-
- $plugin = unserialize($raw_response["body"]);
-
- $api = new stdClass();
- $api->name = $plugin["title"];
- $api->version = $plugin["version"];
- $api->download_link = $plugin["download_url"];
- $api->tested = '10.0';
- }
- return $api;
- }
-
- public static function get_addon_nonces(){
- $raw_response = GFCommon::post_to_manager("api.php", "op=get_plugins", array());
-
- if ( is_wp_error( $raw_response ) || $raw_response['response']['code'] != 200)
- return false;
-
- $addons = unserialize($raw_response["body"]);
- $nonces = array();
- foreach($addons as $addon){
- $nonces[$addon["key"]] = wp_create_nonce("install-plugin_{$addon["key"]}");
- }
-
- return $nonces;
- }
-
- public static function start_export(){
- require_once(GFCommon::get_base_path() . "/export.php");
- GFExport::start_export();
- }
-
- public static function get_post_category_values(){
- require_once(GFCommon::get_base_path() . "/form_detail.php");
- GFFormDetail::get_post_category_values();
- }
-
- public static function get_notification_post_category_values(){
- require_once(GFCommon::get_base_path() . "/notification.php");
- GFNotification::get_post_category_values();
- }
-
- public static function all_leads_page(){
-
- $view = rgget("view");
- $lead_id = rgget('lid');
-
- if($view == 'entry' && (rgget('lid') || !rgblank(rgget('pos')))) {
- require_once(GFCommon::get_base_path() . "/entry_detail.php");
- GFEntryDetail::lead_detail_page();
- } else if ($view == 'entries' || empty($view)){
- require_once(GFCommon::get_base_path() . "/entry_list.php");
- GFEntryList::all_leads_page();
- } else {
- $form_id = rgget('id');
- do_action("gform_entries_view", $view, $form_id, $lead_id);
- }
-
- }
-
- public static function form_list_page(){
- require_once(GFCommon::get_base_path() . "/form_list.php");
- GFFormList::form_list_page();
- }
-
- public static function forms(){
- if(!GFCommon::ensure_wp_version())
- return;
-
- $id = RGForms::get("id");
- $view = RGForms::get("view");
-
- if($view == "entries"){
- require_once(GFCommon::get_base_path() . "/entry_list.php");
- GFEntryList::leads_page($id);
- } else if($view == "entry"){
- require_once(GFCommon::get_base_path() . "/entry_detail.php");
- GFEntryDetail::lead_detail_page();
- } else if($view == "notification"){
- require_once(GFCommon::get_base_path() . "/notification.php");
- //GFNotification::notification_page($id);
- } else if($view == 'settings') {
- require_once(GFCommon::get_base_path() . "/form_settings.php");
- GFFormSettings::form_settings_page($id);
- } else if(empty($view)){
- if(is_numeric($id)){
- self::forms_page($id);
- } else{
- self::form_list_page();
- }
- }
-
- do_action("gform_view", $view, $id);
-
- }
-
- public static function get($name, $array=null){
- if(!$array)
- $array = $_GET;
-
- if(isset($array[$name]))
- return $array[$name];
-
- return "";
- }
-
- public static function post($name){
- if(isset($_POST[$name]))
- return $_POST[$name];
-
- return "";
- }
-
- // AJAX Function
- public static function resend_notifications(){
-
- check_admin_referer('gf_resend_notifications', 'gf_resend_notifications');
- $form_id = rgpost('formId');
- $leads = rgpost('leadIds'); // may be a single ID or an array of IDs
- if(0 == $leads){
- // get all the lead ids for the current filter / search
- $filter = rgpost("filter");
- $search = rgpost("search");
- $star = $filter == "star" ? 1 : null;
- $read = $filter == "unread" ? 0 : null;
- $status = in_array($filter, array("trash", "spam")) ? $filter : "active";
-
- $search_criteria["status"] = $status;
-
- if($star)
- $search_criteria["field_filters"][] = array("key" => "is_starred", "value" => (bool) $star );
- if(!is_null($read))
- $search_criteria["field_filters"][] = array("key" => "is_read", "value" => (bool) $read );
-
- $search_field_id = rgpost("fieldId");
-
- if(isset($_POST["fieldId"]) && $_POST["fieldId"] !== ''){
- $key = $search_field_id;
- $val = $search;
- $strpos_row_key = strpos($search_field_id, "|");
- if ($strpos_row_key !== false) { //multi-row
- $key_array = explode("|", $search_field_id);
- $key = $key_array[0];
- $val = $key_array[1] . ":" . $val;
- }
- $search_criteria["field_filters"][] = array(
- "key" => $key,
- "operator" => rgempty("operator", $_POST) ? "is" : rgpost("operator"),
- "value" => $val
- );
- }
-
- $leads = GFFormsModel::search_lead_ids($form_id, $search_criteria);
- } else {
- $leads = !is_array($leads) ? array($leads) : $leads;
- }
-
- $form = apply_filters("gform_before_resend_notifications_{$form_id}", apply_filters('gform_before_resend_notifications', RGFormsModel::get_form_meta($form_id), $leads), $leads);
-
- if(empty($leads) || empty($form)) {
- _e("There was an error while resending the notifications.", "gravityforms");
- die();
- };
-
- $notifications = json_decode(rgpost('notifications'));
- if(!is_array($notifications))
- die(__("No notifications have been selected. Please select a notification to be sent.", "gravityforms"));
-
- if( ! rgempty( 'sendTo', $_POST ) && ! GFCommon::is_valid_email_list(rgpost('sendTo')))
- die(__("The Send To email address provided is not valid.", "gravityforms"));
-
- foreach($leads as $lead_id){
-
- $lead = RGFormsModel::get_lead($lead_id);
- foreach($notifications as $notification_id){
- $notification = $form["notifications"][$notification_id];
- if(!$notification)
- continue;
-
- //overriding To email if one was specified
- if(rgpost('sendTo')){
- $notification["to"] = rgpost('sendTo');
- $notification["toType"] = "email";
- }
-
- GFCommon::send_notification($notification, $form, $lead);
- }
- }
-
- die();
- }
-
-//-------------------------------------------------
-//----------- AJAX CALLS --------------------------
- //captcha image
-
- public static function captcha_image(){
- $field = array("simpleCaptchaSize" => $_GET["size"], "simpleCaptchaFontColor"=> $_GET["fg"], "simpleCaptchaBackgroundColor"=>$_GET["bg"]);
- if($_GET["type"] == "math")
- $captcha = GFCommon::get_math_captcha($field, $_GET["pos"]);
- else
- $captcha = GFCommon::get_captcha($field);
-
- @ini_set('memory_limit', '256M');
- $image = imagecreatefrompng($captcha["path"]);
-
- include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
- wp_stream_image($image, "image/png", 0);
- imagedestroy($image);
- die();
- }
-
-
- public static function update_form_active(){
- check_ajax_referer('rg_update_form_active','rg_update_form_active');
- RGFormsModel::update_form_active($_POST["form_id"], $_POST["is_active"]);
- }
- public static function update_notification_active(){
- check_ajax_referer('rg_update_notification_active','rg_update_notification_active');
- RGFormsModel::update_notification_active($_POST["form_id"], $_POST["notification_id"], $_POST["is_active"]);
- }
- public static function update_confirmation_active(){
- check_ajax_referer('rg_update_confirmation_active','rg_update_confirmation_active');
- RGFormsModel::update_confirmation_active($_POST["form_id"], $_POST["confirmation_id"], $_POST["is_active"]);
- }
- public static function update_lead_property(){
- check_ajax_referer('rg_update_lead_property','rg_update_lead_property');
- RGFormsModel::update_lead_property($_POST["lead_id"], $_POST["name"], $_POST["value"]);
- }
-
- public static function update_lead_status(){
- check_ajax_referer('gf_delete_entry');
- $status = rgpost("status");
- $lead_id = rgpost("entry");
-
- switch($status){
- case "unspam" :
- //TODO: call akismet and set entry as not spam.
- RGFormsModel::update_lead_property($lead_id, "status", "active");
- break;
-
- case "delete" :
- if(GFCommon::current_user_can_any("gravityforms_delete_entries")){
- RGFormsModel::delete_lead($lead_id);
- }
- break;
-
- default :
- RGFormsModel::update_lead_property($lead_id, "status", $status);
- break;
- }
- header("Content-Type: text/xml");
- echo " ";
- exit();
-
- }
-
- //settings
- public static function upgrade_license(){
- require_once(GFCommon::get_base_path() . "/settings.php");
- GFSettings::upgrade_license();
- }
-
- //form detail
- public static function save_form(){
- require_once(GFCommon::get_base_path() . "/form_detail.php");
- GFFormDetail::save_form();
- }
- public static function add_field(){
- require_once(GFCommon::get_base_path() . "/form_detail.php");
- GFFormDetail::add_field();
- }
- public static function duplicate_field(){
- require_once(GFCommon::get_base_path() . "/form_detail.php");
- GFFormDetail::duplicate_field();
- }
- public static function delete_field(){
- require_once(GFCommon::get_base_path() . "/form_detail.php");
- GFFormDetail::delete_field();
- }
- public static function change_input_type(){
- require_once(GFCommon::get_base_path() . "/form_detail.php");
- GFFormDetail::change_input_type();
- }
- public static function delete_custom_choice(){
- require_once(GFCommon::get_base_path() . "/form_detail.php");
- GFFormDetail::delete_custom_choice();
- }
- public static function save_custom_choice(){
- require_once(GFCommon::get_base_path() . "/form_detail.php");
- GFFormDetail::save_custom_choice();
- }
-
- //entry detail
- public static function delete_file(){
- check_ajax_referer("rg_delete_file", "rg_delete_file");
- $lead_id = intval($_POST["lead_id"]);
- $field_id = intval($_POST["field_id"]);
- $file_index = intval($_POST["file_index"]);
-
- RGFormsModel::delete_file($lead_id, $field_id, $file_index);
- die("EndDeleteFile($field_id, $file_index);");
- }
-
- //export
- public static function select_export_form(){
- check_ajax_referer("rg_select_export_form", "rg_select_export_form");
- $form_id = intval($_POST["form_id"]);
- $form = RGFormsModel::get_form_meta($form_id);
-
- $form = apply_filters("gform_form_export_page_{$form_id}", apply_filters("gform_form_export_page", $form));
-
- $filter_settings = GFCommon::get_field_filter_settings($form);
- $filter_settings_json = json_encode($filter_settings);
- $fields = array();
-
- $form = GFExport::add_default_export_fields($form);
-
- if(is_array($form["fields"])){
- foreach($form["fields"] as $field){
- if(is_array(rgar($field,"inputs"))){
- foreach($field["inputs"] as $input)
- $fields[] = array($input["id"], GFCommon::get_label($field, $input["id"]));
- }
- else if(!rgar($field,"displayOnly")){
- $fields[] = array($field["id"], GFCommon::get_label($field));
- }
- }
- }
- $field_json = GFCommon::json_encode($fields);
-
- die("EndSelectExportForm($field_json, $filter_settings_json);");
- }
-
- // form settings
- public static function save_confirmation() {
- require_once(GFCommon::get_base_path() . '/form_settings.php');
- GFFormSettings::save_confirmation();
- }
-
- public static function delete_confirmation() {
- require_once(GFCommon::get_base_path() . '/form_settings.php');
- GFFormSettings::delete_confirmation();
- }
-
- // form list
- public static function save_new_form() {
- require_once(GFCommon::get_base_path() . '/form_list.php');
- GFFormList::save_new_form();
- }
-
- public static function top_toolbar(){
-
- $forms = RGFormsModel::get_forms(null, "title");
- $id = rgempty("id", $_GET) ? count($forms) > 0 ? $forms[0]->id : "0" : rgget("id");
-
- ?>
-
-
-
-
-
- $menu_item){
- $priorities[$k] = rgar($menu_item,"priority");
- }
-
- array_multisort($priorities, SORT_DESC, $menu_items);
-
- $keys = array_keys($menu_items);
- $last_key = array_pop($keys); // array_pop(array_keys($menu_items)) causes a Strict Standards warning in WP 3.6 on PHP 5.4
-
- foreach($menu_items as $key => $menu_item){
- if(is_array($menu_item)){
- if(GFCommon::current_user_can_any(rgar($menu_item, "capabilities"))){
- $sub_menu_str = "";
- $count_sub_menu_items = 0;
- $sub_menu_items = rgar($menu_item, "sub_menu_items");
- if (is_array($sub_menu_items)){
- foreach($sub_menu_items as $k => $val){
- if(false === GFCommon::current_user_can_any(rgar($sub_menu_items[$k], "capabilities")))
- unset($sub_menu_items[$k]);
- }
- $sub_menu_items = array_values($sub_menu_items); //reset numeric keys
- $count_sub_menu_items = count($sub_menu_items);
- }
-
- $menu_class = rgar($menu_item, "menu_class");
-
- if ($count_sub_menu_items == 1){
- $label = $compact ? rgar($menu_item, "label") : rgar($sub_menu_items[0], "label");
- $menu_item = $sub_menu_items[0];
- } else {
- $label = rgar($menu_item, "label");
- $sub_menu_str = self::toolbar_sub_menu_items($sub_menu_items, $compact);
- }
- $link_class = rgar($menu_item, "link_class");
- $icon = rgar($menu_item, "icon");
- $url = rgar($menu_item, "url");
- $title = rgar($menu_item, "title");
- $onclick = rgar($menu_item, "onclick");
-
- $target = rgar($menu_item, "target");
- $link = "{$icon} {$label} " . $sub_menu_str;
- if($compact){
- if ($key == "delete")
- $link = apply_filters("gform_form_delete_link", $link);
- $divider = $key == $last_key ? '' : " | ";
- if($count_sub_menu_items > 0)
- $menu_class .= " gf_form_action_has_submenu";
- $output .= '';
- } else {
-
- $output .= "";
- }
-
- }
- } elseif($compact) {
- //for backwards compatibility <1.7: form actions only
- $divider = $key == $last_key ? '' : " | ";
- $output .= ''. $menu_item . $divider . ' ';
- }
- }
-
- return $output;
- }
-
- public static function get_toolbar_menu_items($form_id, $compact = false){
- $menu_items = array();
-
- //---- Form Editor ----
- $edit_capabilities = array("gravityforms_edit_forms");
-
- $menu_items['edit'] = array(
- 'label' => $compact ? __("Edit", "gravityforms") : __("Form Editor", "gravityforms"),
- 'icon' => ' ',
- 'title' => __('Edit this form', 'gravityforms'),
- 'url' => '?page=gf_edit_forms&id=' . $form_id,
- 'menu_class' => 'gf_form_toolbar_editor',
- 'link_class' => self::toolbar_class("editor"),
- 'capabilities' => $edit_capabilities,
- 'priority' => 1000
- );
-
- //---- Form Settings ----
-
- $sub_menu_items = self::get_form_settings_sub_menu_items($form_id);
-
- $menu_items['settings'] = array(
- 'label' => $compact ? __("Settings", "gravityforms") : __("Form Settings", "gravityforms"),
- 'icon' => ' ',
- 'title' => __('Edit settings for this form', 'gravityforms'),
- 'url' => '?page=gf_edit_forms&view=settings&id=' . $form_id,
- 'menu_class' => 'gf_form_toolbar_settings',
- 'link_class' => self::toolbar_class("settings"),
- 'sub_menu_items' => $sub_menu_items,
- 'capabilities' => $edit_capabilities,
- 'priority' => 900
- );
-
-
- //---- Entries ----
-
- $entries_capabilities = array('gravityforms_view_entries','gravityforms_edit_entries','gravityforms_delete_entries');
-
- $menu_items['entries'] = array(
- 'label' => __("Entries", "gravityforms"),
- 'icon' => '',
- 'title' => __('View entries generated by this form', 'gravityforms'),
- 'url' => '?page=gf_entries&id=' . $form_id,
- 'menu_class' => 'gf_form_toolbar_entries',
- 'link_class' => self::toolbar_class("entries"),
- 'capabilities' => $entries_capabilities,
- 'priority' => 800
- );
-
- //---- Preview ----
-
- $preview_capabilities = array("gravityforms_edit_forms", "gravityforms_create_form", "gravityforms_preview_forms");
-
- $menu_items['preview'] = array(
- 'label' => __("Preview", "gravityforms"),
- 'icon' => ' ',
- 'title' => __('Preview this form', 'gravityforms'),
- 'url' => trailingslashit(site_url()) . '?gf_page=preview&id=' . $form_id,
- 'menu_class' => 'gf_form_toolbar_preview',
- 'link_class' => self::toolbar_class("preview"),
- 'target' => '_blank',
- 'capabilities' => $preview_capabilities,
- 'priority' => 700
- );
-
-
- return $menu_items;
- }
-
- public static function toolbar_sub_menu_items($menu_items, $compact = false){
- if (empty($menu_items))
- return "";
-
- $sub_menu_items_string = "";
- foreach ($menu_items as $menu_item){
- if(GFCommon::current_user_can_any(rgar($menu_item, "capabilities"))){
- $menu_class = rgar($menu_item, "menu_class");
- $link_class = rgar($menu_item, "link_class");
- $url = rgar($menu_item, "url");
- $label = rgar($menu_item, "label");
- $target = rgar($menu_item, "target");
- $sub_menu_items_string .= "";
- }
- }
- if($compact){
- $sub_menu_items_string = '';
- }else{
- $sub_menu_items_string = '';
- }
-
- return $sub_menu_items_string;
- }
-
- public static function get_form_settings_sub_menu_items($form_id) {
- require_once(GFCommon::get_base_path() . '/form_settings.php');
-
- $sub_menu_items = array();
- $tabs = GFFormSettings::get_tabs($form_id);
-
- foreach($tabs as $tab) {
-
- if($tab['name'] == 'settings')
- $form_setting_menu_item['label'] = 'Settings';
-
- $sub_menu_items[] = array(
- 'url' => admin_url("admin.php?page=gf_edit_forms&view=settings&subview={$tab['name']}&id={$form_id}"),
- 'label' => $tab['label'],
- 'capabilities' => array("gravityforms_edit_forms")
- );
-
- }
-
- return $sub_menu_items;
- }
-
- private static function toolbar_class($item){
-
- switch($item){
-
- case "editor":
- if(in_array(rgget("page"), array("gf_edit_forms", "gf_new_form")) && rgempty("view", $_GET))
- return "gf_toolbar_active";
- break;
-
- case "settings":
- if(rgget('view') == 'settings')
- return "gf_toolbar_active";
- break;
-
- case "notifications" :
- if(rgget("page") == "gf_new_form")
- return "gf_toolbar_disabled";
- else if(rgget("page") == "gf_edit_forms" && rgget("view") == "notification")
- return "gf_toolbar_active";
- break;
-
- case "entries" :
- if(rgget("page") == "gf_new_form")
- return "gf_toolbar_disabled";
- else if(rgget("page") == "gf_entries" && rgempty("view", $_GET))
- return "gf_toolbar_active";
-
- break;
-
- case "preview" :
- if(rgget("page") == "gf_new_form")
- return "gf_toolbar_disabled";
-
- break;
- }
-
- return "";
- }
-
- public static function admin_bar() {
- global $wp_admin_bar;
-
- if(!GFCommon::current_user_can_any("gravityforms_create_form"))
- return;
-
- $wp_admin_bar->add_menu(
- array(
- 'id' => 'gravityforms-new-form',
- 'parent' => 'new-content',
- 'title' => esc_attr__( 'Form', 'gravityforms' ),
- 'href' => admin_url( 'admin.php?page="gf_new_form' )
- )
- );
-
- }
-
-}
-
-class RGForms extends GFForms { }
-
-//Main function call. Should be used to insert a Gravity Form from code.
-function gravity_form($id, $display_title=true, $display_description=true, $display_inactive=false, $field_values=null, $ajax=false, $tabindex = 1, $echo = true){
- if (!$echo)
- return RGForms::get_form($id, $display_title, $display_description, $display_inactive, $field_values, $ajax, $tabindex);
-
- echo RGForms::get_form($id, $display_title, $display_description, $display_inactive, $field_values, $ajax, $tabindex);
-}
-
-//Enqueues the appropriate scripts for the specified form
-function gravity_form_enqueue_scripts($form_id, $is_ajax=false){
- if(!is_admin()){
- require_once(GFCommon::get_base_path() . "/form_display.php");
- $form = RGFormsModel::get_form_meta($form_id);
- GFFormDisplay::enqueue_form_scripts($form, $is_ajax);
- }
-}
-
-if(!function_exists("rgget")){
-function rgget($name, $array=null){
- if(!isset($array))
- $array = $_GET;
-
- if(isset($array[$name]))
- return $array[$name];
-
- return "";
-}
-}
-
-if(!function_exists("rgpost")){
-function rgpost($name, $do_stripslashes=true){
- if(isset($_POST[$name]))
- return $do_stripslashes ? stripslashes_deep($_POST[$name]) : $_POST[$name];
-
- return "";
-}
-}
-
-if(!function_exists("rgar")){
-function rgar($array, $name){
- if(isset($array[$name]))
- return $array[$name];
-
- return '';
-}
-}
-
-if(!function_exists("rgars")){
-function rgars($array, $name){
- $names = explode("/", $name);
- $val = $array;
- foreach($names as $current_name){
- $val = rgar($val, $current_name);
- }
- return $val;
-}
-}
-
-if( ! function_exists( 'rgempty' ) ) {
-function rgempty( $name, $array = null ) {
-
- if( is_array( $name ) )
- return empty( $name );
-
- if( ! $array )
- $array = $_POST;
-
- $val = rgar( $array, $name );
-
- return empty( $val );
-}
-}
-
-if(!function_exists("rgblank")){
-function rgblank($text){
- return empty($text) && strval($text) != "0";
-}
-}
-
-if(!function_exists("rgobj")){
-function rgobj($obj, $name){
- if(isset($obj->$name))
- return $obj->$name;
-
- return '';
-}
-}
-if(!function_exists("rgexplode")){
-function rgexplode($sep, $string, $count){
- $ary = explode($sep, $string);
- while(count($ary) < $count)
- $ary[] = "";
-
- return $ary;
-}
-}
+=' ) );
+
+if ( ! defined( 'GRAVITY_MANAGER_URL' ) ) {
+ define( 'GRAVITY_MANAGER_URL', 'http://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 compat
+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';
+
+ public static function loaded() {
+
+ 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() {
+
+ // Initializing translations. Translation files in the WP_LANG_DIR folder have a higher priority.
+ $locale = apply_filters( 'plugin_locale', get_locale(), 'gravityforms' );
+ load_textdomain( 'gravityforms', WP_LANG_DIR . '/gravityforms/gravityforms-' . $locale . '.mo' );
+ load_plugin_textdomain( 'gravityforms', false, '/gravityforms/languages' );
+
+ 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' );
+ add_action( 'admin_print_scripts', array( 'RGForms', 'print_scripts' ) );
+ } 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' ) );
+
+ // Shortocde 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;
+ }
+
+ if ( 'upload' === rgget( 'gf_page' ) ) {
+ require_once( GFCommon::get_base_path() . '/includes/upload.php' );
+ exit();
+ }
+
+
+ //ensure users are logged in
+ if ( ! is_user_logged_in() ) {
+ auth_redirect();
+ }
+
+ switch ( rgget( 'gf_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 ) {
+ global $wpdb;
+
+ $has_version_changed = get_option( 'rg_form_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();
+
+ //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' ) );
+
+ //------ 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 );
+
+ //droping 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();
+ $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),
+ KEY lead_id (lead_id),
+ KEY form_id_meta_key (form_id,meta_key)
+ ) $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 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' );
+ $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;
+ $has_index = $wpdb->get_var( "SHOW INDEX FROM {$table} WHERE Key_name='{$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 "Upload folder is not writable. Export and file upload features will not be functional.
";
+ }
+ }
+
+ //Prints common admin scripts
+ public static function print_scripts() {
+ wp_enqueue_script( 'sack' );
+ wp_print_scripts();
+ }
+
+ public static function is_gravity_ajax_action() {
+ //Gravity Forms AJAX requests
+ $current_action = self::post( 'action' );
+ $gf_ajax_actions = array(
+ 'rg_save_form', 'rg_change_input_type', 'rg_refresh_field_preview', 'rg_add_field', 'rg_duplicate_field',
+ 'rg_delete_field', 'rg_select_export_form', 'rg_start_export', 'gf_upgrade_license',
+ 'gf_delete_custom_choice', 'gf_save_custom_choice', 'gf_get_notification_post_categories',
+ 'rg_update_lead_property', 'delete-gf_entry', 'rg_update_form_active', 'rg_update_notification_active',
+ 'rg_update_confirmation_active', 'gf_resend_notifications', 'rg_dismiss_upgrade', 'gf_save_confirmation',
+ );
+
+ if ( defined( 'DOING_AJAX' ) && DOING_AJAX && in_array( $current_action, $gf_ajax_actions ) ) {
+ return true;
+ }
+
+ //not a gravity forms ajax request.
+ return false;
+ }
+
+ //Returns true if the current page is one of Gravity Forms pages. Returns false if not
+ public static function is_gravity_page() {
+
+ //Gravity Forms pages
+ $current_page = trim( strtolower( self::get( 'page' ) ) );
+ $gf_pages = array( 'gf_edit_forms', 'gf_new_form', 'gf_entries', 'gf_settings', 'gf_export', 'gf_help' );
+
+ return in_array( $current_page, $gf_pages );
+ }
+
+ //Creates "Forms" left nav
+ public static function create_menu() {
+
+ $has_full_access = current_user_can( 'gform_full_access' );
+ $min_cap = GFCommon::current_user_can_which( GFCommon::all_caps() );
+ if ( empty( $min_cap ) ) {
+ $min_cap = 'gform_full_access';
+ }
+
+ $addon_menus = array();
+ $addon_menus = apply_filters( 'gform_addon_navigation', $addon_menus );
+
+ $parent_menu = self::get_parent_menu( $addon_menus );
+
+ // Add a top-level left nav
+ $update_icon = GFCommon::has_update() && current_user_can( 'install_plugins' ) ? "1 " : '';
+
+ $admin_icon = self::get_admin_icon_b64();
+
+ add_menu_page( __( 'Forms', 'gravityforms' ), __( 'Forms', 'gravityforms' ) . $update_icon, $has_full_access ? 'gform_full_access' : $min_cap, $parent_menu['name'], $parent_menu['callback'], $admin_icon, apply_filters( 'gform_menu_position', '16.9' ) );
+
+ // Adding submenu pages
+ add_submenu_page( $parent_menu['name'], __( 'Forms', 'gravityforms' ), __( 'Forms', 'gravityforms' ), $has_full_access ? 'gform_full_access' : 'gravityforms_edit_forms', 'gf_edit_forms', array( 'RGForms', 'forms' ) );
+
+ add_submenu_page( $parent_menu['name'], __( 'New Form', 'gravityforms' ), __( 'New Form', 'gravityforms' ), $has_full_access ? 'gform_full_access' : 'gravityforms_create_form', 'gf_new_form', array( 'RGForms', 'new_form' ) );
+
+ add_submenu_page( $parent_menu['name'], __( 'Entries', 'gravityforms' ), __( 'Entries', 'gravityforms' ), $has_full_access ? 'gform_full_access' : 'gravityforms_view_entries', 'gf_entries', array( 'RGForms', 'all_leads_page' ) );
+
+ if ( is_array( $addon_menus ) ) {
+ foreach ( $addon_menus as $addon_menu ) {
+ add_submenu_page( esc_html( $parent_menu['name'] ), esc_html( $addon_menu['label'] ), esc_html( $addon_menu['label'] ), $has_full_access ? 'gform_full_access' : $addon_menu['permission'], esc_html( $addon_menu['name'] ), $addon_menu['callback'] );
+ }
+ }
+
+ add_submenu_page( $parent_menu['name'], __( 'Settings', 'gravityforms' ), __( 'Settings', 'gravityforms' ), $has_full_access ? 'gform_full_access' : 'gravityforms_view_settings', 'gf_settings', array( 'RGForms', 'settings_page' ) );
+
+ add_submenu_page( $parent_menu['name'], __( 'Import/Export', 'gravityforms' ), __( 'Import/Export', 'gravityforms' ), $has_full_access ? 'gform_full_access' : 'gravityforms_export_entries', 'gf_export', array( 'RGForms', 'export_page' ) );
+
+ if ( current_user_can( 'install_plugins' ) ) {
+ add_submenu_page( $parent_menu['name'], __( 'Updates', 'gravityforms' ), __( 'Updates', 'gravityforms' ), $has_full_access ? 'gform_full_access' : 'gravityforms_view_updates', 'gf_update', array( 'RGForms', 'update_page' ) );
+ add_submenu_page( $parent_menu['name'], __( 'Add-Ons', 'gravityforms' ), __( 'Add-Ons', 'gravityforms' ), $has_full_access ? 'gform_full_access' : 'gravityforms_view_addons', 'gf_addons', array( 'RGForms', 'addons_page' ) );
+ }
+
+ add_submenu_page( $parent_menu['name'], __( 'Help', 'gravityforms' ), __( 'Help', 'gravityforms' ), $has_full_access ? 'gform_full_access' : $min_cap, 'gf_help', array( 'RGForms', 'help_page' ) );
+
+ }
+
+ public static function get_admin_icon_b64(){
+ return 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxheWVyXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSItMTUgNzcgNTgxIDY0MCIgZW5hYmxlLWJhY2tncm91bmQ9Im5ldyAtMTUgNzcgNTgxIDY0MCIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+PGcgaWQ9IkxheWVyXzIiPjxwYXRoIGZpbGw9IiM5OTk5OTkiIGQ9Ik00ODkuNSwyMjdMNDg5LjUsMjI3TDMxNS45LDEyNi44Yy0yMi4xLTEyLjgtNTguNC0xMi44LTgwLjUsMEw2MS44LDIyN2MtMjIuMSwxMi44LTQwLjMsNDQuMi00MC4zLDY5Ljd2MjAwLjVjMCwyNS42LDE4LjEsNTYuOSw0MC4zLDY5LjdsMTczLjYsMTAwLjJjMjIuMSwxMi44LDU4LjQsMTIuOCw4MC41LDBMNDg5LjUsNTY3YzIyLjItMTIuOCw0MC4zLTQ0LjIsNDAuMy02OS43VjI5Ni44QzUyOS44LDI3MS4yLDUxMS43LDIzOS44LDQ4OS41LDIyN3ogTTQwMSwzMDAuNHY1OS4zSDI0MXYtNTkuM0g0MDF6IE0xNjMuMyw0OTAuOWMtMTYuNCwwLTI5LjYtMTMuMy0yOS42LTI5LjZjMC0xNi40LDEzLjMtMjkuNiwyOS42LTI5LjZzMjkuNiwxMy4zLDI5LjYsMjkuNkMxOTIuOSw0NzcuNiwxNzkuNiw0OTAuOSwxNjMuMyw0OTAuOXogTTE2My4zLDM1OS43Yy0xNi40LDAtMjkuNi0xMy4zLTI5LjYtMjkuNnMxMy4zLTI5LjYsMjkuNi0yOS42czI5LjYsMTMuMywyOS42LDI5LjZTMTc5LjYsMzU5LjcsMTYzLjMsMzU5Ljd6IE0yNDEsNDkwLjl2LTU5LjNoMTYwdjU5LjNIMjQxeiIvPjwvZz48L3N2Zz4=';
+ }
+
+ //Returns the parent menu item. It needs to be the same as the first sub-menu (otherwise WP will duplicate the main menu as a sub-menu)
+ public static function get_parent_menu( $addon_menus ) {
+
+ if ( GFCommon::current_user_can_any( 'gravityforms_edit_forms' ) ) {
+ $parent = array( 'name' => 'gf_edit_forms', 'callback' => array( 'RGForms', 'forms' ) );
+ } else if ( GFCommon::current_user_can_any( 'gravityforms_create_form' ) ) {
+ $parent = array( 'name' => 'gf_new_form', 'callback' => array( 'RGForms', 'new_form' ) );
+ } else if ( GFCommon::current_user_can_any( 'gravityforms_view_entries' ) ) {
+ $parent = array( 'name' => 'gf_entries', 'callback' => array( 'RGForms', 'all_leads_page' ) );
+ } else if ( is_array( $addon_menus ) && sizeof( $addon_menus ) > 0 ) {
+ foreach ( $addon_menus as $addon_menu ) {
+ if ( GFCommon::current_user_can_any( $addon_menu['permission'] ) ) {
+ $parent = array( 'name' => $addon_menu['name'], 'callback' => $addon_menu['callback'] );
+ break;
+ }
+ }
+ } else if ( GFCommon::current_user_can_any( 'gravityforms_view_settings' ) ) {
+ $parent = array( 'name' => 'gf_settings', 'callback' => array( 'RGForms', 'settings_page' ) );
+ } else if ( GFCommon::current_user_can_any( 'gravityforms_export_entries' ) ) {
+ $parent = array( 'name' => 'gf_export', 'callback' => array( 'RGForms', 'export_page' ) );
+ } else if ( GFCommon::current_user_can_any( 'gravityforms_view_updates' ) ) {
+ $parent = array( 'name' => 'gf_update', 'callback' => array( 'RGForms', 'update_page' ) );
+ } else if ( GFCommon::current_user_can_any( 'gravityforms_view_addons' ) ) {
+ $parent = array( 'name' => 'gf_addons', 'callback' => array( 'RGForms', 'addons_page' ) );
+ } else if ( GFCommon::current_user_can_any( GFCommon::all_caps() ) ) {
+ $parent = array( 'name' => 'gf_help', 'callback' => array( 'RGForms', 'help_page' ) );
+ }
+
+ return $parent;
+ }
+
+ public static function modify_admin_title( $admin_title, $title ) {
+
+ $subview = rgget( 'subview' );
+ $form_id = rgget( 'id' );
+
+ if ( ! $form_id || rgget( 'page' ) != 'gf_edit_forms' || rgget( 'view' ) != 'settings' ) {
+ return $admin_title;
+ }
+
+ require_once( GFCommon::get_base_path() . '/form_settings.php' );
+
+ $setting_tabs = GFFormSettings::get_tabs( $form_id );
+ $page_title = '';
+
+ foreach ( $setting_tabs as $tab ) {
+ if ( $tab['name'] == $subview ) {
+ $page_title = $tab['label'];
+ }
+ }
+
+ if ( $page_title ) {
+ $admin_title = sprintf( __( '%1$s ‹ %2$s — WordPress', 'gravityforms' ), $page_title, $admin_title );
+ }
+
+ return $admin_title;
+ }
+
+ //Parses the [gravityform shortcode and returns the front end form UI
+ public static function parse_shortcode( $attributes, $content = null ) {
+
+ extract(
+ shortcode_atts(
+ array(
+ 'title' => true,
+ 'description' => true,
+ 'id' => 0,
+ 'name' => '',
+ 'field_values' => '',
+ 'ajax' => false,
+ 'tabindex' => 1,
+ 'action' => 'form',
+ ), $attributes, 'gravityforms'
+ )
+ );
+
+ $shortcode_string = '';
+
+ switch ( $action ) {
+ case 'conditional':
+ $shortcode_string = GFCommon::conditional_shortcode( $attributes, $content );
+ break;
+
+ case 'form' :
+ //displaying form
+ $title = strtolower( $title ) == 'false' ? false : true;
+ $description = strtolower( $description ) == 'false' ? false : true;
+ $field_values = htmlspecialchars_decode( $field_values );
+ $field_values = str_replace( '&', '&', $field_values );
+
+ $ajax = strtolower( $ajax ) == 'true' ? true : false;
+
+ //using name to lookup form if id is not specified
+ if ( empty( $id ) ) {
+ $id = $name;
+ }
+
+ parse_str( $field_values, $field_value_array ); //parsing query string like string for field values and placing them into an associative array
+ $field_value_array = stripslashes_deep( $field_value_array );
+
+ $shortcode_string = self::get_form( $id, $title, $description, false, $field_value_array, $ajax, $tabindex );
+
+ break;
+ }
+
+ $shortcode_string = apply_filters( "gform_shortcode_{$action}", $shortcode_string, $attributes, $content );
+
+ return $shortcode_string;
+ }
+
+ public static function include_addon_framework() {
+ require_once( GFCommon::get_base_path() . '/includes/addon/class-gf-addon.php' );
+ }
+
+ public static function include_feed_addon_framework() {
+ require_once( GFCommon::get_base_path() . '/includes/addon/class-gf-feed-addon.php' );
+ }
+
+ public static function include_payment_addon_framework() {
+ require_once( GFCommon::get_base_path() . '/includes/addon/class-gf-payment-addon.php' );
+ }
+
+
+ //-------------------------------------------------
+ //----------- AJAX --------------------------------
+
+ public static function ajax_parse_request( $wp ) {
+
+ if ( isset( $_POST['gform_ajax'] ) ) {
+ parse_str( $_POST['gform_ajax'] );
+ $tabindex = isset( $tabindex ) ? absint( $tabindex ) : 1;
+ require_once( GFCommon::get_base_path() . '/form_display.php' );
+
+ $result = GFFormDisplay::get_form( $form_id, $title, $description, false, $_POST['gform_field_values'], true, $tabindex );
+ die( $result );
+ }
+ }
+
+ //------------------------------------------------------
+ //------------- PAGE/POST EDIT PAGE ---------------------
+
+ public static function page_supports_add_form_button() {
+ $is_post_edit_page = in_array( RG_CURRENT_PAGE, array( 'post.php', 'page.php', 'page-new.php', 'post-new.php' ) );
+
+ $display_add_form_button = apply_filters( 'gform_display_add_form_button', $is_post_edit_page );
+
+ return $display_add_form_button;
+ }
+
+ //Action target that adds the 'Insert Form' button to the post/page edit screen
+ public static function add_form_button() {
+
+ $is_add_form_page = self::page_supports_add_form_button();
+ if ( ! $is_add_form_page ) {
+ return;
+ }
+
+ // do a version check for the new 3.5 UI
+ $version = get_bloginfo( 'version' );
+
+ if ( $version < 3.5 ) {
+ // show button for v 3.4 and below
+ $image_btn = GFCommon::get_base_url() . '/images/form-button.png';
+ echo ' ';
+ } else {
+ // display button matching new UI
+ echo '
+
' . __( 'Add Form', 'gravityforms' ) . '
';
+ }
+ }
+
+
+ //Action target that displays the popup to insert a form to a post/page
+ public static function add_mce_popup() {
+ ?>
+
+
+
+
+ ' . __( 'Settings', 'gravityforms' ) . '' );
+
+ return $links;
+ }
+
+ //Displays message on Plugin's page
+ public static function plugin_row( $plugin_name ) {
+ $key = GFCommon::get_key();
+ $version_info = GFCommon::get_version_info();
+
+ if ( ! rgar( $version_info, 'is_valid_key' ) ) {
+
+ $plugin_name = 'gravityforms/gravityforms.php';
+
+ $new_version = version_compare( GFCommon::$version, $version_info['version'], '<' ) ? __( 'There is a new version of Gravity Forms available.', 'gravityforms' ) . ' ' . sprintf( __( 'View version %s Details', 'gravityforms' ), $version_info['version'] ) . ' . ' : '';
+
+ echo '' . $new_version . __( sprintf( '%sRegister%s your copy of Gravity Forms to receive access to automatic upgrades and support. Need a license key? %sPurchase one now%s.', '
', ' ', '
', ' ' ), 'gravityforms' ) . '
';
+ }
+ }
+
+ //Displays current version details on Plugin's page
+ public static function display_changelog() {
+ if ( $_REQUEST['plugin'] != 'gravityforms' ) {
+ return;
+ }
+
+ $page_text = self::get_changelog();
+ echo $page_text;
+
+ exit;
+ }
+
+ public static function get_changelog() {
+ $key = GFCommon::get_key();
+ $body = "key=$key";
+ $options = array( 'method' => 'POST', 'timeout' => 3, 'body' => $body );
+ $options['headers'] = array(
+ 'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' ),
+ 'Content-Length' => strlen( $body ),
+ 'User-Agent' => 'WordPress/' . get_bloginfo( 'version' ),
+ 'Referer' => get_bloginfo( 'url' )
+ );
+
+ $raw_response = GFCommon::post_to_manager( 'changelog.php', GFCommon::get_remote_request_params(), $options );
+
+ if ( is_wp_error( $raw_response ) || 200 != $raw_response['response']['code'] ) {
+ $page_text = __( "Oops!! Something went wrong. Please try again or contact us .", 'gravityforms' );
+ } else {
+ $page_text = $raw_response['body'];
+ if ( substr( $page_text, 0, 10 ) != '' ) {
+ $page_text = '';
+ }
+ }
+
+ return stripslashes( $page_text );
+ }
+
+ //------------------------------------------------------
+ //-------------- DASHBOARD PAGE -------------------------
+
+ //Registers the dashboard widget
+ public static function dashboard_setup() {
+ $dashboard_title = apply_filters( 'gform_dashboard_title', __( 'Forms', 'gravityforms' ) );
+ wp_add_dashboard_widget( 'rg_forms_dashboard', $dashboard_title, array( 'RGForms', 'dashboard' ) );
+ }
+
+ //Displays the dashboard UI
+ public static function dashboard() {
+ $forms = RGFormsModel::get_form_summary();
+
+ if ( sizeof( $forms ) > 0 ) {
+ ?>
+
+
+
+
+
+
+
+
+
+ ', '' ); ?>
+
+ ", '');
+ }*/
+ $message = sprintf( __( 'There is an update available for Gravity Forms. %sView Details%s %s', 'gravityforms' ), "", ' ', $auto_upgrade );
+ ?>
+
+
+ __( 'Next >', 'gravityforms' ),
+ 'prev' => __( '< Prev', 'gravityforms' ),
+ 'image' => __( 'Image', 'gravityforms' ),
+ 'of' => __( 'of', 'gravityforms' ),
+ 'close' => __( 'Close', 'gravityforms' ),
+ 'noiframes' => __( 'This feature requires inline frames. You have iframes disabled or your browser does not support them.', 'gravityforms' ),
+ 'loadingAnimation' => includes_url( 'js/thickbox/loadingAnimation.gif' ),
+ 'closeImage' => includes_url( 'js/thickbox/tb-close.png' )
+ )
+ );
+ }
+
+ public static function enqueue_admin_scripts( $hook ) {
+
+ $scripts = array();
+ $page = self::get_page();
+
+ switch ( $page ) {
+ case 'new_form' :
+ case 'form_list':
+ $scripts = array(
+ 'gform_gravityforms',
+ 'gform_json',
+ 'gform_form_admin',
+ 'thickbox',
+ );
+ break;
+
+ case 'form_settings':
+ $scripts = array(
+ 'gform_gravityforms',
+ 'gform_forms',
+ 'gform_json',
+ 'gform_form_admin',
+ 'gform_placeholder',
+ 'jquery-ui-datepicker',
+ 'gform_masked_input',
+ 'jquery-ui-sortable',
+ );
+ break;
+
+ case 'form_editor':
+ $thickbox = ! GFCommon::is_wp_version( '3.3' ) ? 'gf_thickbox' : 'thickbox';
+ $scripts = array(
+ $thickbox,
+ 'jquery-ui-core',
+ 'jquery-ui-sortable',
+ 'jquery-ui-draggable',
+ 'jquery-ui-droppable',
+ 'jquery-ui-tabs',
+ 'sack',
+ 'gform_gravityforms',
+ 'gform_forms',
+ 'gform_json',
+ 'gform_form_admin',
+ 'gform_floatmenu',
+ 'gform_menu',
+ 'gform_placeholder',
+ 'jquery-ui-autocomplete',
+ );
+
+ if ( wp_is_mobile() ) {
+ $scripts[] = 'jquery-touch-punch';
+ }
+
+ break;
+
+ case 'entry_detail':
+ $scripts = array( 'gform_json' );
+ break;
+
+ case 'entry_detail_edit':
+ $scripts = array( 'gform_gravityforms', 'plupload-all' );
+ break;
+
+ case 'entry_list':
+ $scripts = array(
+ 'wp-lists',
+ 'wp-ajax-response',
+ 'thickbox',
+ 'gform_json',
+ 'thickbox',
+ 'gform_field_filter',
+ );
+ break;
+
+ case 'notification_list':
+ $scripts = array(
+ 'gform_forms',
+ 'gform_json',
+ 'gform_form_admin',
+ );
+ break;
+
+ case 'notification_new':
+ case 'notification_edit':
+ $scripts = array(
+ 'jquery-ui-autocomplete',
+ 'gform_gravityforms',
+ 'gform_placeholder',
+ 'gform_form_admin',
+ 'gform_forms',
+ 'gform_json',
+ );
+ break;
+
+ case 'confirmation':
+ $scripts = array(
+ 'gform_form_admin',
+ 'gform_forms',
+ 'gform_gravityforms',
+ 'gform_placeholder',
+ 'gform_json',
+ 'wp-pointer',
+ );
+ break;
+
+ case 'addons':
+ $scripts = array( 'thickbox' );
+ break;
+
+ case 'export_entry':
+ $scripts = array(
+ 'jquery-ui-datepicker',
+ 'gform_form_admin',
+ 'gform_field_filter',
+ );
+ break;
+ case 'updates' :
+ $scripts = array(
+ 'thickbox'
+ );
+
+ }
+
+ if ( in_array( $hook, array( 'post.php', 'post-new.php' ) ) ) {
+ //add_filter( 'gform_shortcode_preview_disabled', '__return_false' );
+ require_once( GFCommon::get_base_path() . '/tooltips.php' );
+ wp_enqueue_script( 'gform_shortcode_ui' );
+ wp_enqueue_style( 'gform_shortcode_ui' );
+ wp_localize_script( 'gform_shortcode_ui', 'gfShortcodeUIData', array(
+ 'shortcodes' => self::get_shortcodes(),
+ 'previewNonce' => wp_create_nonce( 'gf-shortcode-ui-preview' ),
+ 'previewDisabled' => apply_filters( 'gform_shortcode_preview_disabled', true ),
+ 'strings' => array(
+ 'pleaseSelectAForm' => __( 'Please select a form.', 'gravityforms' ),
+ )
+ ) );
+ }
+
+ if ( empty( $scripts ) ) {
+ return;
+ }
+
+ foreach ( $scripts as $script ) {
+ wp_enqueue_script( $script );
+ }
+
+
+ GFCommon::localize_gform_gravityforms_multifile();
+
+ }
+
+ /**
+ * Gets current page name
+ *
+ * @return bool|string Page name or false
+ * Page names:
+ *
+ * new_form
+ * form_list
+ * form_editor
+ * form_settings
+ * confirmation
+ * notification_list
+ * notification_new
+ * notification_edit
+ * entry_list
+ * entry_detail
+ * entry_detail_edit
+ * settings
+ * addons
+ * export_entry
+ * export_form
+ * import_form
+ * updates
+ */
+ public static function get_page() {
+
+ if ( rgget( 'page' ) == 'gf_new_form' ) {
+ return 'new_form';
+ }
+
+ if ( rgget( 'page' ) == 'gf_edit_forms' && ! rgget( 'id' ) ) {
+ return 'form_list';
+ }
+
+ if ( rgget( 'page' ) == 'gf_edit_forms' && ! rgget( 'view' ) ) {
+ return 'form_editor';
+ }
+
+ if ( rgget( 'page' ) == 'gf_edit_forms' && rgget( 'view' ) == 'settings' && ( ! rgget( 'subview' ) || rgget( 'subview' ) == 'settings' ) ) {
+ return 'form_settings';
+ }
+
+ if ( rgget( 'page' ) == 'gf_edit_forms' && rgget( 'view' ) == 'settings' && rgget( 'subview' ) == 'confirmation' ) {
+ return 'confirmation';
+ }
+
+ if ( rgget( 'page' ) == 'gf_edit_forms' && rgget( 'view' ) == 'settings' && rgget( 'subview' ) == 'notification' && rgget( 'nid' ) ) {
+ return 'notification_edit';
+ }
+
+ if ( rgget( 'page' ) == 'gf_edit_forms' && rgget( 'view' ) == 'settings' && rgget( 'subview' ) == 'notification' && isset( $_GET['nid'] ) ) {
+ return 'notification_edit';
+ }
+
+ if ( rgget( 'page' ) == 'gf_edit_forms' && rgget( 'view' ) == 'settings' && rgget( 'subview' ) == 'notification' ) {
+ return 'notification_list';
+ }
+
+ if ( rgget( 'page' ) == 'gf_entries' && ( ! rgget( 'view' ) || rgget( 'view' ) == 'entries' ) ) {
+ return 'entry_list';
+ }
+
+ if ( rgget( 'page' ) == 'gf_entries' && rgget( 'view' ) == 'entry' && isset( $_POST['screen_mode'] ) && $_POST['screen_mode'] == 'edit' ) {
+ return 'entry_detail_edit';
+ }
+
+ if ( rgget( 'page' ) == 'gf_entries' && rgget( 'view' ) == 'entry' ){
+ return 'entry_detail';
+ }
+
+ if ( rgget( 'page' ) == 'gf_settings' ) {
+ return 'settings';
+ }
+
+ if ( rgget( 'page' ) == 'gf_addons' ) {
+ return 'addons';
+ }
+
+ if ( rgget( 'page' ) == 'gf_export' && ( rgget( 'view' ) == 'export_entry' || ! isset( $_GET['view'] ) ) ) {
+ return 'export_entry';
+ }
+
+ if ( rgget( 'page' ) == 'gf_export' && rgget( 'view' ) == 'export_form' ) {
+ return 'export_form';
+ }
+
+ if ( rgget( 'page' ) == 'gf_export' && rgget( 'view' ) == 'import_form' ) {
+ return 'import_form';
+ }
+
+ if ( rgget( 'page' ) == 'gf_update' ) {
+ return 'updates';
+ }
+
+ return false;
+ }
+
+ public static function get_form( $form_id, $display_title = true, $display_description = true, $force_display = false, $field_values = null, $ajax = false, $tabindex = 1 ) {
+ require_once( GFCommon::get_base_path() . '/form_display.php' );
+
+ return GFFormDisplay::get_form( $form_id, $display_title, $display_description, $force_display, $field_values, $ajax, $tabindex );
+ }
+
+ public static function new_form() {
+ self::form_list_page();
+ }
+
+ public static function enqueue_scripts() {
+ require_once( GFCommon::get_base_path() . '/form_display.php' );
+ GFFormDisplay::enqueue_scripts();
+ }
+
+ public static function print_form_scripts( $form, $ajax ) {
+ require_once( GFCommon::get_base_path() . '/form_display.php' );
+ GFFormDisplay::print_form_scripts( $form, $ajax );
+ }
+
+ public static function forms_page( $form_id ) {
+ require_once( GFCommon::get_base_path() . '/form_detail.php' );
+ GFFormDetail::forms_page( $form_id );
+ }
+
+ public static function settings_page() {
+ require_once( GFCommon::get_base_path() . '/settings.php' );
+ GFSettings::settings_page();
+ }
+
+ public static function add_settings_page( $name, $handle = '', $icon_path = '' ) {
+ require_once( GFCommon::get_base_path() . '/settings.php' );
+ GFSettings::add_settings_page( $name, $handle, $icon_path );
+ }
+
+ public static function help_page() {
+ require_once( GFCommon::get_base_path() . '/help.php' );
+ GFHelp::help_page();
+ }
+
+ public static function export_page() {
+ require_once( GFCommon::get_base_path() . '/export.php' );
+ GFExport::export_page();
+ }
+
+ public static function update_page() {
+ require_once( GFCommon::get_base_path() . '/update.php' );
+ GFUpdate::update_page();
+ }
+
+ public static function addons_page() {
+
+ wp_print_styles( array( 'thickbox' ) );
+
+ $plugins = get_plugins();
+ $installed_plugins = array();
+ foreach ( $plugins as $key => $plugin ) {
+ $is_active = is_plugin_active( $key );
+ $installed_plugin = array( 'plugin' => $key, 'name' => $plugin['Name'], 'is_active' => $is_active );
+ $installed_plugin['activation_url'] = $is_active ? '' : wp_nonce_url( "plugins.php?action=activate&plugin={$key}", "activate-plugin_{$key}" );
+ $installed_plugin['deactivation_url'] = ! $is_active ? '' : wp_nonce_url( "plugins.php?action=deactivate&plugin={$key}", "deactivate-plugin_{$key}" );
+
+ $installed_plugins[] = $installed_plugin;
+ }
+
+ $nonces = self::get_addon_nonces();
+
+ $body = array( 'plugins' => urlencode( serialize( $installed_plugins ) ), 'nonces' => urlencode( serialize( $nonces ) ), 'key' => GFCommon::get_key() );
+ $options = array( 'body' => $body, 'headers' => array( 'Referer' => get_bloginfo( 'url' ) ), 'timeout' => 15 );
+
+ $raw_response = GFCommon::post_to_manager( 'api.php', "op=plugin_browser&{$_SERVER['QUERY_STRING']}", $options );
+
+ if ( is_wp_error( $raw_response ) || $raw_response['response']['code'] != 200 ) {
+ echo "" . __( 'Add-On browser is currently unavailable. Please try again later.', 'gravityforms' ) . '
';
+ } else {
+ echo GFCommon::get_remote_message();
+ echo $raw_response['body'];
+ }
+ }
+
+ public static function get_addon_info( $api, $action, $args ) {
+
+ if ( $action == 'plugin_information' && empty( $api ) && ( ! rgempty( 'rg', $_GET ) || $args->slug == 'gravityforms' ) ) {
+
+ $raw_response = GFCommon::post_to_manager( 'api.php', "op=get_plugin&slug={$args->slug}", array() );
+
+ if ( is_wp_error( $raw_response ) || $raw_response['response']['code'] != 200 ) {
+ return false;
+ }
+
+ $plugin = unserialize( $raw_response['body'] );
+
+ $api = new stdClass();
+ $api->name = $plugin['title'];
+ $api->version = $plugin['version'];
+ $api->download_link = $plugin['download_url'];
+ $api->tested = '10.0';
+
+ }
+
+ return $api;
+ }
+
+ public static function get_addon_nonces() {
+
+ $raw_response = GFCommon::post_to_manager( 'api.php', 'op=get_plugins', array() );
+
+ if ( is_wp_error( $raw_response ) || $raw_response['response']['code'] != 200 ) {
+ return false;
+ }
+
+ $addons = unserialize( $raw_response['body'] );
+ $nonces = array();
+ foreach ( $addons as $addon ) {
+ $nonces[ $addon['key'] ] = wp_create_nonce( "install-plugin_{$addon['key']}" );
+ }
+
+ return $nonces;
+ }
+
+ public static function start_export() {
+ require_once( GFCommon::get_base_path() . '/export.php' );
+ GFExport::start_export();
+ }
+
+ public static function get_post_category_values() {
+ require_once( GFCommon::get_base_path() . '/form_detail.php' );
+ GFFormDetail::get_post_category_values();
+ }
+
+ public static function get_notification_post_category_values() {
+ require_once( GFCommon::get_base_path() . '/notification.php' );
+ GFNotification::get_post_category_values();
+ }
+
+ public static function all_leads_page() {
+
+ $view = rgget( 'view' );
+ $lead_id = rgget( 'lid' );
+
+ if ( $view == 'entry' && ( rgget( 'lid' ) || ! rgblank( rgget( 'pos' ) ) ) ) {
+ require_once( GFCommon::get_base_path() . '/entry_detail.php' );
+ GFEntryDetail::lead_detail_page();
+ } else if ( $view == 'entries' || empty( $view ) ) {
+ require_once( GFCommon::get_base_path() . '/entry_list.php' );
+ GFEntryList::all_leads_page();
+ } else {
+ $form_id = rgget( 'id' );
+ do_action( 'gform_entries_view', $view, $form_id, $lead_id );
+ }
+
+ }
+
+ public static function form_list_page() {
+ require_once( GFCommon::get_base_path() . '/form_list.php' );
+ GFFormList::form_list_page();
+ }
+
+ public static function forms() {
+ if ( ! GFCommon::ensure_wp_version() ) {
+ return;
+ }
+
+ $id = RGForms::get( 'id' );
+ $view = RGForms::get( 'view' );
+
+ if ( $view == 'entries' ) {
+ require_once( GFCommon::get_base_path() . '/entry_list.php' );
+ GFEntryList::leads_page( $id );
+ } else if ( $view == 'entry' ) {
+ require_once( GFCommon::get_base_path() . '/entry_detail.php' );
+ GFEntryDetail::lead_detail_page();
+ } else if ( $view == 'notification' ) {
+ require_once( GFCommon::get_base_path() . '/notification.php' );
+ //GFNotification::notification_page($id);
+ } else if ( $view == 'settings' ) {
+ require_once( GFCommon::get_base_path() . '/form_settings.php' );
+ GFFormSettings::form_settings_page( $id );
+ } else if ( empty( $view ) ) {
+ if ( is_numeric( $id ) ) {
+ self::forms_page( $id );
+ } else {
+ self::form_list_page();
+ }
+ }
+
+ do_action( 'gform_view', $view, $id );
+
+ }
+
+ public static function get( $name, $array = null ) {
+ if ( ! $array ) {
+ $array = $_GET;
+ }
+
+ if ( isset( $array[ $name ] ) ) {
+ return $array[ $name ];
+ }
+
+ return '';
+ }
+
+ public static function post( $name ) {
+ if ( isset( $_POST[ $name ] ) ) {
+ return $_POST[ $name ];
+ }
+
+ return '';
+ }
+
+ // AJAX Function
+ public static function resend_notifications() {
+
+ check_admin_referer( 'gf_resend_notifications', 'gf_resend_notifications' );
+ $form_id = rgpost( 'formId' );
+ $leads = rgpost( 'leadIds' ); // may be a single ID or an array of IDs
+ if ( 0 == $leads ) {
+ // get all the lead ids for the current filter / search
+ $filter = rgpost( 'filter' );
+ $search = rgpost( 'search' );
+ $star = $filter == 'star' ? 1 : null;
+ $read = $filter == 'unread' ? 0 : null;
+ $status = in_array( $filter, array( 'trash', 'spam' ) ) ? $filter : 'active';
+
+ $search_criteria['status'] = $status;
+
+ if ( $star ) {
+ $search_criteria['field_filters'][] = array( 'key' => 'is_starred', 'value' => (bool) $star );
+ }
+ if ( ! is_null( $read ) ) {
+ $search_criteria['field_filters'][] = array( 'key' => 'is_read', 'value' => (bool) $read );
+ }
+
+ $search_field_id = rgpost( 'fieldId' );
+
+ if ( isset( $_POST['fieldId'] ) && $_POST['fieldId'] !== '' ) {
+ $key = $search_field_id;
+ $val = $search;
+ $strpos_row_key = strpos( $search_field_id, '|' );
+ if ( $strpos_row_key !== false ) { //multi-row
+ $key_array = explode( '|', $search_field_id );
+ $key = $key_array[0];
+ $val = $key_array[1] . ':' . $val;
+ }
+ $search_criteria['field_filters'][] = array(
+ 'key' => $key,
+ 'operator' => rgempty( 'operator', $_POST ) ? 'is' : rgpost( 'operator' ),
+ 'value' => $val,
+ );
+ }
+
+ $leads = GFFormsModel::search_lead_ids( $form_id, $search_criteria );
+ } else {
+ $leads = ! is_array( $leads ) ? array( $leads ) : $leads;
+ }
+
+ $form = apply_filters( "gform_before_resend_notifications_{$form_id}", apply_filters( 'gform_before_resend_notifications', RGFormsModel::get_form_meta( $form_id ), $leads ), $leads );
+
+ if ( empty( $leads ) || empty( $form ) ) {
+ _e( 'There was an error while resending the notifications.', 'gravityforms' );
+ die();
+ };
+
+ $notifications = json_decode( rgpost( 'notifications' ) );
+ if ( ! is_array( $notifications ) ) {
+ die( __( 'No notifications have been selected. Please select a notification to be sent.', 'gravityforms' ) );
+ }
+
+ if ( ! rgempty( 'sendTo', $_POST ) && ! GFCommon::is_valid_email_list( rgpost( 'sendTo' ) ) ) {
+ die( __( 'The Send To email address provided is not valid.', 'gravityforms' ) );
+ }
+
+ foreach ( $leads as $lead_id ) {
+
+ $lead = RGFormsModel::get_lead( $lead_id );
+ foreach ( $notifications as $notification_id ) {
+ $notification = $form['notifications'][ $notification_id ];
+ if ( ! $notification ) {
+ continue;
+ }
+
+ //overriding To email if one was specified
+ if ( rgpost( 'sendTo' ) ) {
+ $notification['to'] = rgpost( 'sendTo' );
+ $notification['toType'] = 'email';
+ }
+
+ GFCommon::send_notification( $notification, $form, $lead );
+ }
+ }
+
+ die();
+ }
+
+ //-------------------------------------------------
+ //----------- AJAX CALLS --------------------------
+ //captcha image
+
+ public static function captcha_image() {
+ $field_properties = array( 'type' => 'captcha', 'simpleCaptchaSize' => $_GET['size'], 'simpleCaptchaFontColor' => $_GET['fg'], 'simpleCaptchaBackgroundColor' => $_GET['bg'] );
+ /* @var GF_Field_CAPTCHA $field */
+ $field = GF_Fields::create( $field_properties );
+ if ( $_GET['type'] == 'math' ) {
+ $captcha = $field->get_math_captcha( $_GET['pos'] );
+ } else {
+ $captcha = $field->get_captcha();
+ }
+
+ @ini_set( 'memory_limit', '256M' );
+ $image = imagecreatefrompng( $captcha['path'] );
+
+ include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
+ wp_stream_image( $image, 'image/png', 0 );
+ imagedestroy( $image );
+ die();
+ }
+
+ public static function update_form_active() {
+ check_ajax_referer( 'rg_update_form_active', 'rg_update_form_active' );
+ RGFormsModel::update_form_active( $_POST['form_id'], $_POST['is_active'] );
+ }
+
+ public static function update_notification_active() {
+ check_ajax_referer( 'rg_update_notification_active', 'rg_update_notification_active' );
+ RGFormsModel::update_notification_active( $_POST['form_id'], $_POST['notification_id'], $_POST['is_active'] );
+ }
+
+ public static function update_confirmation_active() {
+ check_ajax_referer( 'rg_update_confirmation_active', 'rg_update_confirmation_active' );
+ RGFormsModel::update_confirmation_active( $_POST['form_id'], $_POST['confirmation_id'], $_POST['is_active'] );
+ }
+
+ public static function update_lead_property() {
+ check_ajax_referer( 'rg_update_lead_property', 'rg_update_lead_property' );
+ RGFormsModel::update_lead_property( $_POST['lead_id'], $_POST['name'], $_POST['value'] );
+ }
+
+ public static function update_lead_status() {
+ check_ajax_referer( 'gf_delete_entry' );
+ $status = rgpost( 'status' );
+ $lead_id = rgpost( 'entry' );
+
+ switch ( $status ) {
+ case 'unspam' :
+ RGFormsModel::update_lead_property( $lead_id, 'status', 'active' );
+ break;
+
+ case 'delete' :
+ if ( GFCommon::current_user_can_any( 'gravityforms_delete_entries' ) ) {
+ RGFormsModel::delete_lead( $lead_id );
+ }
+ break;
+
+ default :
+ RGFormsModel::update_lead_property( $lead_id, 'status', $status );
+ break;
+ }
+ header( 'Content-Type: text/xml' );
+ echo " ";
+ exit();
+
+ }
+
+ //settings
+ public static function upgrade_license() {
+ require_once( GFCommon::get_base_path() . '/settings.php' );
+ GFSettings::upgrade_license();
+ }
+
+ //form detail
+ public static function save_form() {
+ require_once( GFCommon::get_base_path() . '/form_detail.php' );
+ GFFormDetail::save_form();
+ }
+
+ public static function add_field() {
+ require_once( GFCommon::get_base_path() . '/form_detail.php' );
+ GFFormDetail::add_field();
+ }
+
+ public static function duplicate_field() {
+ require_once( GFCommon::get_base_path() . '/form_detail.php' );
+ GFFormDetail::duplicate_field();
+ }
+
+ public static function delete_field() {
+ require_once( GFCommon::get_base_path() . '/form_detail.php' );
+ GFFormDetail::delete_field();
+ }
+
+ public static function change_input_type() {
+ require_once( GFCommon::get_base_path() . '/form_detail.php' );
+ GFFormDetail::change_input_type();
+ }
+
+ public static function refresh_field_preview() {
+ require_once( GFCommon::get_base_path() . '/form_detail.php' );
+ GFFormDetail::refresh_field_preview();
+ }
+
+ public static function delete_custom_choice() {
+ require_once( GFCommon::get_base_path() . '/form_detail.php' );
+ GFFormDetail::delete_custom_choice();
+ }
+
+ public static function save_custom_choice() {
+ require_once( GFCommon::get_base_path() . '/form_detail.php' );
+ GFFormDetail::save_custom_choice();
+ }
+
+ //entry detail
+ public static function delete_file() {
+ check_ajax_referer( 'rg_delete_file', 'rg_delete_file' );
+ $lead_id = intval( $_POST['lead_id'] );
+ $field_id = intval( $_POST['field_id'] );
+ $file_index = intval( $_POST['file_index'] );
+
+ RGFormsModel::delete_file( $lead_id, $field_id, $file_index );
+ die( "EndDeleteFile($field_id, $file_index);" );
+ }
+
+ //export
+ public static function select_export_form() {
+ check_ajax_referer( 'rg_select_export_form', 'rg_select_export_form' );
+ $form_id = intval( $_POST['form_id'] );
+ $form = RGFormsModel::get_form_meta( $form_id );
+
+ $form = apply_filters( "gform_form_export_page_{$form_id}", apply_filters( 'gform_form_export_page', $form ) );
+
+ $filter_settings = GFCommon::get_field_filter_settings( $form );
+ $filter_settings_json = json_encode( $filter_settings );
+ $fields = array();
+
+ $form = GFExport::add_default_export_fields( $form );
+
+ if ( is_array( $form['fields'] ) ) {
+ /* @var GF_Field $field */
+ foreach ( $form['fields'] as $field ) {
+ $inputs = $field->get_entry_inputs();
+ if ( is_array( $inputs ) ) {
+ foreach ( $inputs as $input ) {
+ $fields[] = array( $input['id'], GFCommon::get_label( $field, $input['id'] ) );
+ }
+ } else if ( ! $field->displayOnly ) {
+ $fields[] = array( $field->id, GFCommon::get_label( $field ) );
+ }
+ }
+ }
+ $field_json = GFCommon::json_encode( $fields );
+
+ die( "EndSelectExportForm($field_json, $filter_settings_json);" );
+ }
+
+ // form settings
+ public static function save_confirmation() {
+ require_once( GFCommon::get_base_path() . '/form_settings.php' );
+ GFFormSettings::save_confirmation();
+ }
+
+ public static function delete_confirmation() {
+ require_once( GFCommon::get_base_path() . '/form_settings.php' );
+ GFFormSettings::delete_confirmation();
+ }
+
+ // form list
+ public static function save_new_form() {
+ require_once( GFCommon::get_base_path() . '/form_list.php' );
+ GFFormList::save_new_form();
+ }
+
+ public static function top_toolbar() {
+
+ $forms = RGFormsModel::get_forms( null, 'title' );
+ $id = rgempty( 'id', $_GET ) ? count( $forms ) > 0 ? $forms[0]->id : '0' : rgget( 'id' );
+
+ ?>
+
+
+
+
+
+ $menu_item ) {
+ $priorities[ $k ] = rgar( $menu_item, 'priority' );
+ }
+
+ array_multisort( $priorities, SORT_DESC, $menu_items );
+
+ $keys = array_keys( $menu_items );
+ $last_key = array_pop( $keys ); // array_pop(array_keys($menu_items)) causes a Strict Standards warning in WP 3.6 on PHP 5.4
+
+ foreach ( $menu_items as $key => $menu_item ) {
+ if ( is_array( $menu_item ) ) {
+ if ( GFCommon::current_user_can_any( rgar( $menu_item, 'capabilities' ) ) ) {
+ $sub_menu_str = '';
+ $count_sub_menu_items = 0;
+ $sub_menu_items = rgar( $menu_item, 'sub_menu_items' );
+ if ( is_array( $sub_menu_items ) ) {
+ foreach ( $sub_menu_items as $k => $val ) {
+ if ( false === GFCommon::current_user_can_any( rgar( $sub_menu_items[ $k ], 'capabilities' ) ) ) {
+ unset( $sub_menu_items[ $k ] );
+ }
+ }
+ $sub_menu_items = array_values( $sub_menu_items ); //reset numeric keys
+ $count_sub_menu_items = count( $sub_menu_items );
+ }
+
+ $menu_class = rgar( $menu_item, 'menu_class' );
+
+ if ( $count_sub_menu_items == 1 ) {
+ $label = $compact ? rgar( $menu_item, 'label' ) : rgar( $sub_menu_items[0], 'label' );
+ $menu_item = $sub_menu_items[0];
+ } else {
+ $label = rgar( $menu_item, 'label' );
+ $sub_menu_str = self::toolbar_sub_menu_items( $sub_menu_items, $compact );
+ }
+ $link_class = rgar( $menu_item, 'link_class' );
+ $icon = rgar( $menu_item, 'icon' );
+ $url = rgar( $menu_item, 'url' );
+ $title = rgar( $menu_item, 'title' );
+ $onclick = rgar( $menu_item, 'onclick' );
+
+ $target = rgar( $menu_item, 'target' );
+ $link = "{$icon} {$label} " . $sub_menu_str;
+ if ( $compact ) {
+ if ( $key == 'delete' ) {
+ $link = apply_filters( 'gform_form_delete_link', $link );
+ }
+ $divider = $key == $last_key ? '' : ' | ';
+ if ( $count_sub_menu_items > 0 ) {
+ $menu_class .= ' gf_form_action_has_submenu';
+ }
+ $output .= '';
+ } else {
+
+ $output .= "";
+ }
+ }
+ } elseif ( $compact ) {
+ //for backwards compatibility <1.7: form actions only
+ $divider = $key == $last_key ? '' : ' | ';
+ $output .= '' . $menu_item . $divider . ' ';
+ }
+ }
+
+ return $output;
+ }
+
+ public static function get_toolbar_menu_items( $form_id, $compact = false ) {
+ $menu_items = array();
+
+ //---- Form Editor ----
+ $edit_capabilities = array( 'gravityforms_edit_forms' );
+
+ $menu_items['edit'] = array(
+ 'label' => $compact ? __( 'Edit', 'gravityforms' ) : __( 'Form Editor', 'gravityforms' ),
+ 'icon' => ' ',
+ 'title' => __( 'Edit this form', 'gravityforms' ),
+ 'url' => '?page=gf_edit_forms&id=' . $form_id,
+ 'menu_class' => 'gf_form_toolbar_editor',
+ 'link_class' => self::toolbar_class( 'editor' ),
+ 'capabilities' => $edit_capabilities,
+ 'priority' => 1000,
+ );
+
+ //---- Form Settings ----
+
+ $sub_menu_items = self::get_form_settings_sub_menu_items( $form_id );
+
+ $menu_items['settings'] = array(
+ 'label' => $compact ? __( 'Settings', 'gravityforms' ) : __( 'Form Settings', 'gravityforms' ),
+ 'icon' => ' ',
+ 'title' => __( 'Edit settings for this form', 'gravityforms' ),
+ 'url' => '?page=gf_edit_forms&view=settings&id=' . $form_id,
+ 'menu_class' => 'gf_form_toolbar_settings',
+ 'link_class' => self::toolbar_class( 'settings' ),
+ 'sub_menu_items' => $sub_menu_items,
+ 'capabilities' => $edit_capabilities,
+ 'priority' => 900,
+ );
+
+
+ //---- Entries ----
+
+ $entries_capabilities = array( 'gravityforms_view_entries', 'gravityforms_edit_entries', 'gravityforms_delete_entries' );
+
+ $menu_items['entries'] = array(
+ 'label' => __( 'Entries', 'gravityforms' ),
+ 'icon' => '',
+ 'title' => __( 'View entries generated by this form', 'gravityforms' ),
+ 'url' => '?page=gf_entries&id=' . $form_id,
+ 'menu_class' => 'gf_form_toolbar_entries',
+ 'link_class' => self::toolbar_class( 'entries' ),
+ 'capabilities' => $entries_capabilities,
+ 'priority' => 800,
+ );
+
+ //---- Preview ----
+
+ $preview_capabilities = array( 'gravityforms_edit_forms', 'gravityforms_create_form', 'gravityforms_preview_forms' );
+
+ $menu_items['preview'] = array(
+ 'label' => __( 'Preview', 'gravityforms' ),
+ 'icon' => ' ',
+ 'title' => __( 'Preview this form', 'gravityforms' ),
+ 'url' => trailingslashit( site_url() ) . '?gf_page=preview&id=' . $form_id,
+ 'menu_class' => 'gf_form_toolbar_preview',
+ 'link_class' => self::toolbar_class( 'preview' ),
+ 'target' => '_blank',
+ 'capabilities' => $preview_capabilities,
+ 'priority' => 700,
+ );
+
+
+ return $menu_items;
+ }
+
+ public static function toolbar_sub_menu_items( $menu_items, $compact = false ) {
+ if ( empty( $menu_items ) ) {
+ return '';
+ }
+
+ $sub_menu_items_string = '';
+ foreach ( $menu_items as $menu_item ) {
+ if ( GFCommon::current_user_can_any( rgar( $menu_item, 'capabilities' ) ) ) {
+ $menu_class = rgar( $menu_item, 'menu_class' );
+ $link_class = rgar( $menu_item, 'link_class' );
+ $url = rgar( $menu_item, 'url' );
+ $label = rgar( $menu_item, 'label' );
+ $target = rgar( $menu_item, 'target' );
+ $sub_menu_items_string .= "";
+ }
+ }
+ if ( $compact ) {
+ $sub_menu_items_string = '';
+ } else {
+ $sub_menu_items_string = '';
+ }
+
+ return $sub_menu_items_string;
+ }
+
+ public static function get_form_settings_sub_menu_items( $form_id ) {
+ require_once( GFCommon::get_base_path() . '/form_settings.php' );
+
+ $sub_menu_items = array();
+ $tabs = GFFormSettings::get_tabs( $form_id );
+
+ foreach ( $tabs as $tab ) {
+
+ if ( $tab['name'] == 'settings' ) {
+ $form_setting_menu_item['label'] = 'Settings';
+ }
+
+ $sub_menu_items[] = array(
+ 'url' => admin_url( "admin.php?page=gf_edit_forms&view=settings&subview={$tab['name']}&id={$form_id}" ),
+ 'label' => $tab['label'],
+ 'capabilities' => array( 'gravityforms_edit_forms' )
+ );
+
+ }
+
+ return $sub_menu_items;
+ }
+
+ private static function toolbar_class( $item ) {
+
+ switch ( $item ) {
+
+ case 'editor':
+ if ( in_array( rgget( 'page' ), array( 'gf_edit_forms', 'gf_new_form' ) ) && rgempty( 'view', $_GET ) ) {
+ return 'gf_toolbar_active';
+ }
+ break;
+
+ case 'settings':
+ if ( rgget( 'view' ) == 'settings' ) {
+ return 'gf_toolbar_active';
+ }
+ break;
+
+ case 'notifications' :
+ if ( rgget( 'page' ) == 'gf_new_form' ) {
+ return 'gf_toolbar_disabled';
+ } else if ( rgget( 'page' ) == 'gf_edit_forms' && rgget( 'view' ) == 'notification' ) {
+ return 'gf_toolbar_active';
+ }
+ break;
+
+ case 'entries' :
+ if ( rgget( 'page' ) == 'gf_new_form' ) {
+ return 'gf_toolbar_disabled';
+ } else if ( rgget( 'page' ) == 'gf_entries' && rgempty( 'view', $_GET ) ) {
+ return 'gf_toolbar_active';
+ }
+
+ break;
+
+ case 'preview' :
+ if ( rgget( 'page' ) == 'gf_new_form' ) {
+ return 'gf_toolbar_disabled';
+ }
+
+ break;
+ }
+
+ return '';
+ }
+
+ public static function admin_bar() {
+ global $wp_admin_bar;
+
+ if ( ! GFCommon::current_user_can_any( 'gravityforms_create_form' ) ) {
+ return;
+ }
+
+ $wp_admin_bar->add_menu(
+ array(
+ 'id' => 'gravityforms-new-form',
+ 'parent' => 'new-content',
+ 'title' => esc_attr__( 'Form', 'gravityforms' ),
+ 'href' => admin_url( 'admin.php?page="gf_new_form' )
+ )
+ );
+ }
+
+ public static function maybe_auto_update( $update, $item ) {
+
+ if ( $item->slug == 'gravityforms' ) {
+
+ GFCommon::log_debug( 'GFForms::maybe_auto_update() - Starting auto-update for gravityforms.' );
+
+ $auto_update_disabled = self::is_auto_update_disabled();
+ GFCommon::log_debug( 'GFForms::maybe_auto_update() - $auto_update_disabled: ' . var_export( $auto_update_disabled, true ) );
+
+ if ( $auto_update_disabled || version_compare( GFForms::$version, $item->new_version, '=>' ) ) {
+ GFCommon::log_debug( 'GFForms::maybe_auto_update() - Aborting update.' );
+ return false;
+ }
+
+ $current_major = implode( '.', array_slice( preg_split( '/[.-]/', GFForms::$version ), 0, 1 ) );
+ $new_major = implode( '.', array_slice( preg_split( '/[.-]/', $item->new_version ), 0, 1 ) );
+
+ $current_branch = implode( '.', array_slice( preg_split( '/[.-]/', GFForms::$version ), 0, 2 ) );
+ $new_branch = implode( '.', array_slice( preg_split( '/[.-]/', $item->new_version ), 0, 2 ) );
+
+ if ( $current_major == $new_major && $current_branch == $new_branch ) {
+ GFCommon::log_debug( 'GFForms::maybe_auto_update() - OK to update.' );
+ return true;
+ }
+ }
+
+ return $update;
+ }
+
+ public static function is_auto_update_disabled(){
+
+ // Background updates are disabled if you don't want file changes.
+ if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ){
+ return true;
+ }
+
+ if ( defined( 'WP_INSTALLING' ) ){
+ return true;
+ }
+
+ $enabled = get_option( 'gform_enable_background_updates' );
+ GFCommon::log_debug( 'GFForms::is_auto_update_disabled() - $enabled: ' . var_export( $enabled, true ) );
+
+ $disabled = apply_filters( 'gform_disable_auto_update', ! $enabled );
+ GFCommon::log_debug( 'GFForms::is_auto_update_disabled() - $disabled: ' . var_export( $disabled, true ) );
+
+ if ( ! $disabled ) {
+ $disabled = defined( 'GFORM_DISABLE_AUTO_UPDATE' ) && GFORM_DISABLE_AUTO_UPDATE;
+ GFCommon::log_debug( 'GFForms::is_auto_update_disabled() - GFORM_DISABLE_AUTO_UPDATE: ' . var_export( $disabled, true ) );
+ }
+
+ return $disabled;
+ }
+
+ public static function deprecate_add_on_methods() {
+ if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) || ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) ) {
+ return;
+ }
+ $deprecated = GFAddOn::get_all_deprecated_protected_methods();
+ if ( ! empty( $deprecated ) ) {
+ foreach ( $deprecated as $method ) {
+ _deprecated_function( $method, '1.9', 'public access level' );
+ }
+ }
+ }
+
+ /**
+ * Shortcode UI
+ */
+
+ /**
+ * Output a shortcode.
+ * ajax callback for displaying the shortcode in the TinyMCE editor.
+ *
+ * @return null
+ */
+ public static function handle_ajax_do_shortcode( ) {
+
+ $shortcode = ! empty( $_POST['shortcode'] ) ? sanitize_text_field( stripslashes( $_POST['shortcode'] ) ) : null;
+ $post_id = ! empty( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : null;
+
+ if ( ! current_user_can( 'edit_post', $post_id ) || ! wp_verify_nonce( $_POST['nonce'], 'gf-shortcode-ui-preview' ) ) {
+ echo esc_html__( 'Error', 'gravityforms' );
+ exit;
+ }
+
+ $form_id = ! empty( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : null;
+
+ global $post;
+ $post = get_post( $post_id );
+ setup_postdata( $post );
+
+ self::enqueue_form_scripts( $form_id, true );
+ wp_print_scripts();
+ wp_print_styles();
+
+ echo do_shortcode( $shortcode );
+
+ // Disable the elements on the form
+ ?>
+
+ id ) ] = esc_html( $form->title );
+ }
+
+ $default_attrs = array(
+ array(
+ 'label' => __( 'Select a form below to add it to your post or page.', 'gravityforms' ),
+ 'tooltip' => __( 'Select a form from the list to add it to your post or page.', 'gravityforms' ),
+ 'attr' => 'id',
+ 'type' => 'select',
+ 'section' => 'required',
+ 'description' => __( "Can't find your form? Make sure it is active.", 'gravityforms' ),
+ 'options' => $forms_options,
+ ),
+ array(
+ 'label' => __( 'Display form title', 'gravityforms' ),
+ 'attr' => 'title',
+ 'default' => 'true',
+ 'section' => 'standard',
+ 'type' => 'checkbox',
+ 'tooltip' => __( 'Whether or not do display the form title.', 'gravityforms' )
+ ),
+ array(
+ 'label' => __( 'Display form description', 'gravityforms' ),
+ 'attr' => 'description',
+ 'default' => 'true',
+ 'section' => 'standard',
+ 'type' => 'checkbox',
+ 'tooltip' => __( 'Whether or not do display the form description.', 'gravityforms' )
+ ),
+ array(
+ 'label' => __( 'Enable AJAX', 'gravityforms' ),
+ 'attr' => 'ajax',
+ 'section' => 'standard',
+ 'type' => 'checkbox',
+ 'tooltip' => __( 'Specify whether or not to use AJAX to submit the form.', 'gravityforms' )
+ ),
+ array(
+ 'label' => 'Tabindex',
+ 'attr' => 'tabindex',
+ 'type' => 'number',
+ 'tooltip' => __( 'Specify the starting tab index for the fields of this form.', 'gravityforms' )
+ ),
+
+ );
+
+ $add_on_actions = apply_filters( 'gform_shortcode_builder_actions', array() );
+
+ if ( ! empty( $add_on_actions ) ) {
+ $action_options = array( '' => __( 'Select an action', 'gravityforms' ) );
+ foreach ( $add_on_actions as $add_on_action ) {
+ foreach ( $add_on_action as $key => $array ) {
+ $action_options[ $key ] = $array['label'];
+ }
+ }
+
+ $default_attrs[] = array(
+ 'label' => 'Action',
+ 'attr' => 'action',
+ 'type' => 'select',
+ 'options' => $action_options,
+ 'tooltip' => __( 'Select an action for this shortcode. Actions are added by some add-ons.', 'gravityforms' )
+ );
+ }
+
+ $shortcode = array(
+ 'shortcode_tag' => 'gravityform',
+ 'action_tag' => '',
+ 'label' => 'Gravity Forms',
+ 'attrs' => $default_attrs,
+ );
+
+ $shortcodes[] = $shortcode;
+
+ if ( ! empty( $add_on_actions ) ) {
+ foreach ( $add_on_actions as $add_on_action ) {
+ foreach ( $add_on_action as $key => $array ) {
+ $attrs = array_merge( $default_attrs, $array['attrs'] );
+ $shortcode = array(
+ 'shortcode_tag' => 'gravityform',
+ 'action_tag' => $key,
+ 'label' => rgar( $array, 'label' ),
+ 'attrs' => $attrs,
+ );
+ }
+ }
+ $shortcodes[] = $shortcode;
+ }
+
+ return $shortcodes;
+ }
+
+ public static function enqueue_form_scripts( $form_id, $is_ajax = false ) {
+ require_once( GFCommon::get_base_path() . '/form_display.php' );
+ $form = RGFormsModel::get_form_meta( $form_id );
+ GFFormDisplay::enqueue_form_scripts( $form, $is_ajax );
+ $addons = GFAddOn::get_registered_addons();
+ foreach ( $addons as $addon ) {
+ $a = call_user_func( array( $addon, 'get_instance' ) );
+ $a->enqueue_scripts( $form, $is_ajax );
+ }
+ }
+
+}
+
+class RGForms extends GFForms {
+}
+
+//Main function call. Should be used to insert a Gravity Form from code.
+function gravity_form( $id, $display_title = true, $display_description = true, $display_inactive = false, $field_values = null, $ajax = false, $tabindex = 1, $echo = true ) {
+ if ( ! $echo ) {
+ return RGForms::get_form( $id, $display_title, $display_description, $display_inactive, $field_values, $ajax, $tabindex );
+ }
+
+ echo RGForms::get_form( $id, $display_title, $display_description, $display_inactive, $field_values, $ajax, $tabindex );
+}
+
+//Enqueues the appropriate scripts for the specified form
+function gravity_form_enqueue_scripts( $form_id, $is_ajax = false ) {
+ GFForms::enqueue_form_scripts( $form_id, $is_ajax );
+}
+
+if ( ! function_exists( 'rgget' ) ) {
+ function rgget( $name, $array = null ) {
+ if ( ! isset( $array ) ) {
+ $array = $_GET;
+ }
+
+ if ( isset( $array[ $name ] ) ) {
+ return $array[ $name ];
+ }
+
+ return '';
+ }
+}
+
+if ( ! function_exists( 'rgpost' ) ) {
+ function rgpost( $name, $do_stripslashes = true ) {
+ if ( isset( $_POST[ $name ] ) ) {
+ return $do_stripslashes ? stripslashes_deep( $_POST[ $name ] ) : $_POST[ $name ];
+ }
+
+ return '';
+ }
+}
+
+if ( ! function_exists( 'rgar' ) ) {
+ function rgar( $array, $name ) {
+ if ( isset( $array[ $name ] ) ) {
+ return $array[ $name ];
+ }
+
+ return '';
+ }
+}
+
+if ( ! function_exists( 'rgars' ) ) {
+ function rgars( $array, $name ) {
+ $names = explode( '/', $name );
+ $val = $array;
+ foreach ( $names as $current_name ) {
+ $val = rgar( $val, $current_name );
+ }
+
+ return $val;
+ }
+}
+
+if ( ! function_exists( 'rgempty' ) ) {
+ function rgempty( $name, $array = null ) {
+
+ if ( is_array( $name ) ) {
+ return empty( $name );
+ }
+
+ if ( ! $array ) {
+ $array = $_POST;
+ }
+
+ $val = rgar( $array, $name );
+
+ return empty( $val );
+ }
+}
+
+if ( ! function_exists( 'rgblank' ) ) {
+ function rgblank( $text ) {
+ return empty( $text ) && strval( $text ) != '0';
+ }
+}
+
+if ( ! function_exists( 'rgobj' ) ) {
+ function rgobj( $obj, $name ) {
+ if ( isset( $obj->$name ) ) {
+ return $obj->$name;
+ }
+
+ return '';
+ }
+}
+if ( ! function_exists( 'rgexplode' ) ) {
+ function rgexplode( $sep, $string, $count ) {
+ $ary = explode( $sep, $string );
+ while ( count( $ary ) < $count ) {
+ $ary[] = '';
+ }
+
+ return $ary;
+ }
+}
\ No newline at end of file
diff --git a/help.php b/help.php
index 8770e71..55e71d4 100644
--- a/help.php
+++ b/help.php
@@ -1,154 +1,153 @@
-
-
-
-
-
-
-
-
-
frequently asked questions (FAQ) first. If you still can't find the answer
open a support ticket and we will be happy to answer your questions and assist you with any problems.
Please note: If you have not
purchased a license from us, you won't have access to these help resources.", "gravityforms"); ?>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Please Note: Only licensed Gravity Forms customers are granted access to the downloads section.", "gravityforms"); ?>
-
-
-
-
-
-
- Please note: The Gravity Forms support team does not provide support for third party scripts, widgets, etc.", "gravityforms"); ?>
-
-
-
-
-
-
-
-
\ No newline at end of file
+ public static function help_page() {
+ if ( ! GFCommon::ensure_wp_version() ) {
+ return;
+ }
+
+ echo GFCommon::get_remote_message();
+
+ ?>
+
+
+
+
+
+
+
+
+
frequently asked questions (FAQ) first. If you still can't find the answer
open a support ticket and we will be happy to answer your questions and assist you with any problems.
Please note: If you have not
purchased a license from us, you won't have access to these help resources.", 'gravityforms' ); ?>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Please Note: Only licensed Gravity Forms customers are granted access to the downloads section.', 'gravityforms' ); ?>
+
+
+
+
+
+
+ Please note: The Gravity Forms support team does not provide support for third party scripts, widgets, etc.', 'gravityforms' ); ?>
+
+
+
+
+
+
+
+ _addon = $addon;
- $capabilities = isset($config["capabilities"]) ? $config["capabilities"] : array();
- $redirect_url = isset($config["redirect_url"]) ? $config["redirect_url"] : "";
- $edit_url = isset($config["edit_url"]) ? $config["edit_url"] : "";
- $object_type = isset($config["object_type"]) ? $config["object_type"] : "";
- $this->_strings = isset($config["strings"]) ? $config["strings"] : array();
- parent::__construct($object_type, $redirect_url, $edit_url, $capabilities);
- }
-
- public function get_strings() {
- return array_merge(parent::get_strings(), $this->_strings);
- }
-
- protected function is_edit_page() {
- return $this->_addon->is_locking_edit_page();
- }
-
- protected function is_list_page() {
- return $this->_addon->is_locking_list_page();
- }
-
- protected function is_view_page() {
- return $this->_addon->is_locking_view_page();
- }
-
- protected function get_object_id() {
- return $this->_addon->get_locking_object_id();
- }
-
- protected function is_object_locked($object_id){
- return $this->is_object_locked($object_id);
- }
+ protected $_strings;
+ /* @var GFAddOn $_addon */
+ protected $_addon;
+
+ /**
+ * e.g.
+ *
+ * array(
+ * "object_type" => 'contact',
+ * "capabilities" => array("gravityforms_contacts_edit_contacts"),
+ * "redirect_url" => admin_url("admin.php?page=gf_contacts"),
+ * "edit_url" => admin_url(sprintf("admin.php?page=gf_contacts&id=%d", $contact_id)),
+ * "strings" => $strings
+ * );
+ *
+ * @param array $config
+ * @param GFAddOn $addon
+ */
+ public function __construct( $config, $addon ) {
+ $this->_addon = $addon;
+ $capabilities = isset( $config['capabilities'] ) ? $config['capabilities'] : array();
+ $redirect_url = isset( $config['redirect_url'] ) ? $config['redirect_url'] : '';
+ $edit_url = isset( $config['edit_url'] ) ? $config['edit_url'] : '';
+ $object_type = isset( $config['object_type'] ) ? $config['object_type'] : '';
+ $this->_strings = isset( $config['strings'] ) ? $config['strings'] : array();
+ parent::__construct( $object_type, $redirect_url, $edit_url, $capabilities );
+ }
+
+ public function get_strings() {
+ return array_merge( parent::get_strings(), $this->_strings );
+ }
+
+ protected function is_edit_page() {
+ return $this->_addon->is_locking_edit_page();
+ }
+
+ protected function is_list_page() {
+ return $this->_addon->is_locking_list_page();
+ }
+
+ protected function is_view_page() {
+ return $this->_addon->is_locking_view_page();
+ }
+
+ protected function get_object_id() {
+ return $this->_addon->get_locking_object_id();
+ }
+
+ protected function is_object_locked( $object_id ) {
+ return $this->is_object_locked( $object_id );
+ }
}
\ No newline at end of file
diff --git a/includes/addon/class-gf-addon.php b/includes/addon/class-gf-addon.php
index d053151..65b74ca 100644
--- a/includes/addon/class-gf-addon.php
+++ b/includes/addon/class-gf-addon.php
@@ -1,124 +1,123 @@
array(), 'inactive' => array() );
- /**
- * Class constructor which hooks the instance into the WordPress init action
- */
- function __construct() {
+ /**
+ * Class constructor which hooks the instance into the WordPress init action
+ */
+ function __construct() {
- add_action('init', array($this, 'init'));
- if ($this->_enable_rg_autoupgrade) {
- require_once("class-gf-auto-upgrade.php");
- $is_gravityforms_supported = $this->is_gravityforms_supported($this->_min_gravityforms_version);
- new GFAutoUpgrade($this->_slug, $this->_version, $this->_min_gravityforms_version, $this->_title, $this->_full_path, $this->_path, $this->_url, $is_gravityforms_supported);
- }
+ add_action( 'init', array( $this, 'init' ) );
+ if ( $this->_enable_rg_autoupgrade ) {
+ require_once( 'class-gf-auto-upgrade.php' );
+ $is_gravityforms_supported = $this->is_gravityforms_supported( $this->_min_gravityforms_version );
+ new GFAutoUpgrade( $this->_slug, $this->_version, $this->_min_gravityforms_version, $this->_title, $this->_full_path, $this->_path, $this->_url, $is_gravityforms_supported );
+ }
- $this->pre_init();
- }
+ $this->pre_init();
+ }
/**
* Registers an addon so that it gets initialized appropriately
@@ -146,16 +145,19 @@ public static function register( $class, $overrides = null ){
self::$_registered_addons['inactive'][] = $overrides;
}
+ }
+ public static function get_registered_addons(){
+ return self::$_registered_addons['active'];
}
/**
- * Target of gform_init Gravity Forms hook. Initializes all addons.
+ * Initializes all addons.
*/
public static function init_addons(){
//Removing duplicate add-ons
- $active_addons = array_unique(self::$_registered_addons['active']);
+ $active_addons = array_unique( self::$_registered_addons['active'] );
foreach ( $active_addons as $addon ){
@@ -165,3694 +167,3861 @@ public static function init_addons(){
}
/**
- * Gets executed before all init functions. Override this function to perform initialization tasks that must be done prior to init
- */
- public function pre_init(){
+ * Gets executed before all init functions. Override this function to perform initialization tasks that must be done prior to init
+ */
+ public function pre_init() {
+
+ if ( $this->is_gravityforms_supported() ) {
+
+ //Entry meta
+ if ( $this->method_is_overridden( 'get_entry_meta' ) ) {
+ add_filter( 'gform_entry_meta', array( $this, 'get_entry_meta' ), 10, 2 );
+ }
+ }
+ }
+
+ /**
+ * Plugin starting point. Handles hooks and loading of language files.
+ */
+ public function init() {
+
+ load_plugin_textdomain( $this->_slug, false, $this->_slug . '/languages' );
+
+ add_filter( 'gform_logging_supported', array( $this, 'set_logging_supported' ) );
- if($this->is_gravityforms_supported()){
+ if ( RG_CURRENT_PAGE == 'admin-ajax.php' ) {
- //Entry meta
- if ($this->method_is_overridden('get_entry_meta'))
- add_filter('gform_entry_meta', array($this, 'get_entry_meta'), 10, 2);
+ //If gravity forms is supported, initialize AJAX
+ if ( $this->is_gravityforms_supported() ) {
+ $this->init_ajax();
+ }
+ } else if ( is_admin() ) {
- }
- }
+ $this->init_admin();
- /**
- * Plugin starting point. Handles hooks and loading of language files.
- */
- public function init() {
+ } else {
- load_plugin_textdomain($this->_slug, FALSE, $this->_slug . '/languages');
+ if ( $this->is_gravityforms_supported() ) {
+ $this->init_frontend();
+ }
+ }
- add_filter("gform_logging_supported", array($this, "set_logging_supported"));
- if(RG_CURRENT_PAGE == 'admin-ajax.php') {
+ }
- //If gravity forms is supported, initialize AJAX
- if($this->is_gravityforms_supported()){
- $this->init_ajax();
- }
+ /**
+ * Override this function to add initialization code (i.e. hooks) for the admin site (WP dashboard)
+ */
+ protected function init_admin() {
- }
- else if (is_admin()) {
+ // enqueues admin scripts
+ add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 10, 0 );
- $this->init_admin();
+ // message enforcing min version of Gravity Forms
+ if ( isset( $this->_min_gravityforms_version ) && RG_CURRENT_PAGE == 'plugins.php' && false === $this->_enable_rg_autoupgrade ) {
+ add_action( 'after_plugin_row_' . $this->_path, array( $this, 'plugin_row' ) );
+ }
- }
- else {
+ // STOP HERE IF GRAVITY FORMS IS NOT SUPPORTED
+ if ( isset( $this->_min_gravityforms_version ) && ! $this->is_gravityforms_supported( $this->_min_gravityforms_version ) ) {
+ return;
+ }
- if($this->is_gravityforms_supported()){
- $this->init_frontend();
- }
- }
+ $this->setup();
+ // Add form settings only when there are form settings fields configured or form_settings() method is implemented
+ if ( self::has_form_settings_page() ) {
+ $this->form_settings_init();
+ }
- }
+ // Add plugin page when there is a plugin page configured or plugin_page() method is implemented
+ if ( self::has_plugin_page() ) {
+ $this->plugin_page_init();
+ }
- /**
- * Override this function to add initialization code (i.e. hooks) for the admin site (WP dashboard)
- */
- protected function init_admin(){
+ // Add addon settings page only when there are addon settings fields configured or settings_page() method is implemented
+ if ( self::has_plugin_settings_page() ) {
+ if ( $this->current_user_can_any( $this->_capabilities_settings_page ) ) {
+ $this->plugin_settings_init();
+ }
+ }
- // enqueues admin scripts
- add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'), 10, 0);
+ // creates the top level app left menu
+ if ( self::has_app_menu() ) {
+ if ( $this->current_user_can_any( $this->_capabilities_app_menu ) ) {
+ add_action( 'admin_menu', array( $this, 'create_app_menu' ) );
+ }
+ }
- // message enforcing min version of Gravity Forms
- if (isset($this->_min_gravityforms_version) && RG_CURRENT_PAGE == "plugins.php" && false === $this->_enable_rg_autoupgrade)
- add_action('after_plugin_row_' . $this->_path, array($this, 'plugin_row'));
- // STOP HERE IF GRAVITY FORMS IS NOT SUPPORTED
- if (isset($this->_min_gravityforms_version) && !$this->is_gravityforms_supported($this->_min_gravityforms_version))
- return;
+ // Members plugin integration
+ if ( self::has_members_plugin() ) {
+ add_filter( 'members_get_capabilities', array( $this, 'members_get_capabilities' ) );
+ }
- $this->setup();
+ // Results page
+ if ( $this->method_is_overridden( 'get_results_page_config' ) ) {
+ $results_page_config = $this->get_results_page_config();
+ $results_capabilities = rgar( $results_page_config, 'capabilities' );
+ if ( $results_page_config && $this->current_user_can_any( $results_capabilities ) ) {
+ $this->results_page_init( $results_page_config );
+ }
+ }
- // Add form settings only when there are form settings fields configured or form_settings() method is implemented
- if (self::has_form_settings_page()) {
- $this->form_settings_init();
- }
+ // Locking
+ if ( $this->method_is_overridden( 'get_locking_config' ) ) {
+ require_once( GFCommon::get_base_path() . '/includes/locking/class-gf-locking.php' );
+ require_once( 'class-gf-addon-locking.php' );
+ $config = $this->get_locking_config();
+ new GFAddonLocking( $config, $this );
+ }
- // Add plugin page when there is a plugin page configured or plugin_page() method is implemented
- if (self::has_plugin_page()) {
- $this->plugin_page_init();
- }
+ // No conflict scripts
+ add_filter( 'gform_noconflict_scripts', array( $this, 'register_noconflict_scripts' ) );
+ add_filter( 'gform_noconflict_styles', array( $this, 'register_noconflict_styles' ) );
- // Add addon settings page only when there are addon settings fields configured or settings_page() method is implemented
- if (self::has_plugin_settings_page()) {
- if ($this->current_user_can_any($this->_capabilities_settings_page)) {
- $this->plugin_settings_init();
- }
- }
+ }
- // creates the top level app left menu
- if (self::has_app_menu()) {
- if ($this->current_user_can_any($this->_capabilities_app_menu)) {
- add_action('admin_menu', array($this, 'create_app_menu'));
- }
- }
+ /**
+ * Override this function to add initialization code (i.e. hooks) for the public (customer facing) site
+ */
+ protected function init_frontend() {
+ $this->setup();
- // Members plugin integration
- if (self::has_members_plugin())
- add_filter('members_get_capabilities', array($this, 'members_get_capabilities'));
+ add_filter( 'gform_preview_styles', array( $this, 'enqueue_preview_styles' ), 10, 2 );
+ add_filter( 'gform_print_styles', array( $this, 'enqueue_print_styles' ), 10, 2 );
+ add_action( 'gform_enqueue_scripts', array( $this, 'enqueue_scripts' ), 10, 2 );
- // Results page
- if ($this->method_is_overridden('get_results_page_config') ) {
- $results_page_config = $this->get_results_page_config();
- $results_capabilities = rgar($results_page_config, "capabilities");
- if($results_page_config && $this->current_user_can_any($results_capabilities))
- $this->results_page_init($results_page_config);
- }
-
- // Locking
- if ($this->method_is_overridden('get_locking_config')) {
- require_once(GFCommon::get_base_path() . "/includes/locking/class-gf-locking.php");
- require_once("class-gf-addon-locking.php");
- $config = $this->get_locking_config();
- new GFAddonLocking($config, $this);
- }
-
- // No conflict scripts
- add_filter('gform_noconflict_scripts', array($this, 'register_noconflict_scripts'));
- add_filter('gform_noconflict_styles', array($this, 'register_noconflict_styles'));
-
- }
-
- /**
- * Override this function to add initialization code (i.e. hooks) for the public (customer facing) site
- */
- protected function init_frontend(){
-
- $this->setup();
-
- add_filter('gform_preview_styles', array($this, "enqueue_preview_styles"), 10, 2);
- add_filter('gform_print_styles', array($this, "enqueue_print_styles"), 10, 2);
- add_action("gform_enqueue_scripts", array($this, "enqueue_scripts"), 10, 2);
-
- }
-
- /**
- * Override this function to add AJAX hooks or to add initialization code when an AJAX request is being performed
- */
- protected function init_ajax(){
- if (rgpost("view") == "gf_results_" . $this->_slug) {
- require_once(GFCommon::get_base_path() . "/tooltips.php");
- require_once("class-gf-results.php");
- $gf_results = new GFResults($this->_slug, $this->get_results_page_config());
- add_action('wp_ajax_gresults_get_results_gf_results_' . $this->_slug, array($gf_results, 'ajax_get_results'));
- add_action('wp_ajax_gresults_get_more_results_gf_results_' . $this->_slug, array($gf_results, 'ajax_get_more_results'));
- } elseif ($this->method_is_overridden('get_locking_config')) {
- require_once(GFCommon::get_base_path() . "/includes/locking/class-gf-locking.php");
- require_once("class-gf-addon-locking.php");
- $config = $this->get_locking_config();
- new GFAddonLocking($config, $this);
- }
- }
-
-
- //-------------- Setup ---------------
-
- /**
- * Performs upgrade tasks when the version of the Add-On changes. To add additional upgrade tasks, override the upgrade() function, which will only get executed when the plugin version has changed.
- */
- protected function setup() {
-
- //Upgrading add-on
- $installed_version = get_option("gravityformsaddon_" . $this->_slug . "_version");
-
- //Making sure version has really changed. Gets around aggressive caching issue on some sites that cause setup to run multiple times.
- if ($installed_version != $this->_version){
- $installed_version = GFForms::get_wp_option("gravityformsaddon_" . $this->_slug . "_version");
- }
-
- //Upgrade if version has changed
- if ($installed_version != $this->_version){
-
- $this->upgrade($installed_version);
- update_option("gravityformsaddon_" . $this->_slug . "_version", $this->_version);
- }
- }
-
- /**
- * Override this function to add to add database update scripts or any other code to be executed when the Add-On version changes
- */
- protected function upgrade($previous_version) {
- return;
- }
-
-
- //-------------- Script enqueuing ---------------
-
- /**
- * Override this function to provide a list of styles to be enqueued.
- * When overriding this function, be sure to call parent::styles() to ensure the base class scripts are enqueued.
- * See scripts() for an example of the format expected to be returned.
- */
- protected function styles(){
- return array(
- array( "handle" => "gaddon_form_settings_css",
- "src" => GFAddOn::get_gfaddon_base_url() . "/css/gaddon_settings.css",
- "version" => GFCommon::$version,
- "enqueue" => array(
- array("admin_page" => array("form_settings", "plugin_settings", "plugin_page") )
- )
- ),
- array( "handle" => "gaddon_results_css",
- "src" => GFAddOn::get_gfaddon_base_url() . "/css/gaddon_results.css",
- "version" => GFCommon::$version,
- "enqueue" => array(
- array("admin_page" => array("results") )
- )
- )
- );
- }
-
-
- /**
- * Override this function to provide a list of scripts to be enqueued.
- * When overriding this function, be sure to call parent::scripts() to ensure the base class scripts are enqueued.
- * Following is an example of the array that is expected to be returned by this function:
- *
- *
- *
- * array(
- * array( "handle" => "maskedinput",
- * "src" => GFCommon::get_base_url() . "/js/jquery.maskedinput-1.3.min.js",
- * "version" => GFCommon::$version,
- * "deps" => array("jquery"),
- * "in_footer" => false,
- *
- * //Determines where the script will be enqueued. The script will be enqueued if any of the conditions match
- * "enqueue" => array(
- * //admin_page - Specified one or more pages (known pages) where the script is supposed to be enqueued. When this setting is specified, scripts will only be enqueued in those pages.
- * //To enqueue scripts in the front end (public website), simply don't define this setting
- * array("admin_page" => array("form_settings", "plugin_settings") ),
- *
- * //tab - Specifies a form settings or plugin settings tab in which the script is supposed to be enqueued. If none is specified, the script will be enqueued in any of the form settings or plugin_settings page
- * array("tab" => "signature"),
- *
- * //query - Specifies a set of query string ($_GET) values. If all specified query string values match the current requested page, the script will be enqueued
- * array("query" => "page=gf_edit_forms&view=settings&id=_notempty_")
- *
- * //post - Specifies a set of post ($_POST) values. If all specified posted values match the current request, the script will be enqueued
- * array("post" => "posted_field=val")
- *
- * )
- * ),
- * array(
- * "handle" => "super_signature_script",
- * "src" => $this->get_base_url() . "/super_signature/ss.js",
- * "version" => $this->_version,
- * "deps" => array("jquery"),
- * "callback" => array($this, "localize_scripts"),
- * "strings" => array(
- * // Accessible in JavaScript using the global variable "[script handle]_strings"
- * "stringKey1" => __("The string", "gravityforms"),
- * "stringKey2" => __("Another string.", "gravityforms")
- * )
- * "enqueue" => array(
- * //field_types - Specifies one or more field types that requires this script. The script will only be enqueued if the current form has a field of any of the specified field types. Only applies when a current form is available.
- * array("field_types" => array("signature"))
- * )
- * )
- * );
- *
- *
- *
- */
- protected function scripts(){
- return array(
- array(
- 'handle' => 'gform_form_admin',
- 'enqueue' => array( array( "admin_page" => array("form_settings") ) )
- ),
- array(
- 'handle' => 'gform_gravityforms',
- 'enqueue' => array( array( "admin_page" => array("form_settings") ) )
- ),
- array("handle" => "google_charts",
- "src" => "https://www.google.com/jsapi",
- "version" => GFCommon::$version,
- "enqueue" => array(
- array("admin_page" => array("results")),
- )
- ),
- array("handle" => "gaddon_results_js",
- "src" => GFAddOn::get_gfaddon_base_url() . "/js/gaddon_results.js",
- "version" => GFCommon::$version,
- "deps" => array('jquery', 'sack', 'jquery-ui-resizable', 'jquery-ui-datepicker' , 'google_charts', 'gform_field_filter'),
- "callback" => array('GFResults', "localize_results_scripts"),
- "enqueue" => array(
- array("admin_page" => array("results")),
- )
- ),
- array(
- 'handle' => 'gaddon_repeater',
- 'src' => GFAddOn::get_gfaddon_base_url() . '/js/repeater.js',
- 'version' => GFCommon::$version,
- 'deps' => array( 'jquery' ),
- 'enqueue' => array(
- array(
- 'admin_page' => array( 'form_settings' )
-// ,
-// 'field_types' => array( 'dynamic_field_map' )
- )
- )
- )
- );
- }
-
-
- /**
- * Target of admin_enqueue_scripts and gform_enqueue_scripts hooks.
- * Not intended to be overridden by child classes.
- * In order to enqueue scripts and styles, override the scripts() and styles() functions
- *
- * @ignore
- */
- public function enqueue_scripts($form="", $is_ajax=false){
-
- if(empty($form))
- $form = $this->get_current_form();
-
- //Enqueueing scripts
- $scripts = $this->scripts();
- foreach($scripts as $script){
- $src = isset($script["src"]) ? $script["src"] : false;
- $deps = isset($script["deps"]) ? $script["deps"] : array();
- $version = isset($script["version"]) ? $script["version"] : false;
- $in_footer = isset($script["in_footer"]) ? $script["in_footer"] : false;
- wp_register_script($script["handle"],$src, $deps, $version, $in_footer);
- if(isset($script["enqueue"]) && $this->_can_enqueue_script($script["enqueue"], $form, $is_ajax)){
- $this->add_no_conflict_scripts(array($script["handle"]));
- wp_enqueue_script($script["handle"]);
- if(isset($script["strings"]))
- wp_localize_script($script["handle"], $script["handle"]."_strings", $script["strings"]);
- if(isset($script["callback"]) && is_callable($script["callback"])){
- $args = compact("form", "is_ajax");
- call_user_func_array($script["callback"], $args);
- }
- }
- }
-
- //Enqueueing styles
- $styles = $this->styles();
- foreach($styles as $style){
- $src = isset($style["src"]) ? $style["src"] : false;
- $deps = isset($style["deps"]) ? $style["deps"] : array();
- $version = isset($style["version"]) ? $style["version"] : false;
- $media = isset($style["media"]) ? $style["media"] : "all";
- wp_register_style($style["handle"], $src, $deps, $version, $media);
- if($this->_can_enqueue_script($style["enqueue"], $form, $is_ajax)){
- $this->add_no_conflict_styles(array($style["handle"]));
- if ($this->is_preview()) {
- $this->_preview_styles[] = $style["handle"];
- } elseif ($this->is_print()) {
- $this->_print_styles[] = $style["handle"];
- } else {
- wp_enqueue_style($style["handle"]);
- }
- }
- }
- }
-
- /**
- * Target of gform_preview_styles. Enqueue styles to the preview page.
- * Not intented to be overriden by child classes
- *
- * @ignore
- */
- public function enqueue_preview_styles($preview_styles, $form) {
- return array_merge($preview_styles, $this->_preview_styles);
- }
-
-
- /**
- * Target of gform_print_styles. Enqueue styles to the print entry page.
- * Not intented to be overriden by child classes
- *
- * @ignore
- */
- public function enqueue_print_styles($print_styles, $form) {
- if(false === $print_styles)
- $print_styles = array();
-
- $styles = $this->styles();
- foreach($styles as $style){
- if($this->_can_enqueue_script($style["enqueue"], $form, false)){
- $this->add_no_conflict_styles(array($style["handle"]));
- $src = isset($style["src"]) ? $style["src"] : false;
- $deps = isset($style["deps"]) ? $style["deps"] : array();
- $version = isset($style["version"]) ? $style["version"] : false;
- $media = isset($style["media"]) ? $style["media"] : "all";
- wp_register_style($style["handle"], $src, $deps, $version, $media);
- $print_styles[] = $style["handle"];
- }
- }
-
- return array_merge($print_styles, $this->_print_styles);
- }
-
-
- /**
- * Adds scripts to the list of white-listed no conflict scripts.
- *
- * @param $scripts
- */
- private function add_no_conflict_scripts($scripts) {
- $this->_no_conflict_scripts = array_merge($scripts, $this->_no_conflict_scripts);
-
- }
-
- /**
- * Adds styles to the list of white-listed no conflict styles.
- *
- * @param $styles
- */
- private function add_no_conflict_styles($styles) {
- $this->_no_conflict_styles = array_merge($styles, $this->_no_conflict_styles);
- }
-
- private function _can_enqueue_script($enqueue_conditions, $form, $is_ajax){
- if(empty($enqueue_conditions))
- return false;
-
- foreach($enqueue_conditions as $condition){
- if(is_callable($condition)){
- return call_user_func($condition, $form, $is_ajax);
- }
- else{
- $query_matches = isset($condition["query"]) ? $this->_request_condition_matches($_GET, $condition["query"]) : true;
- $post_matches = isset($condition["post"]) ? $this->_request_condition_matches($_POST, $condition["query"]) : true;
- $admin_page_matches = isset($condition["admin_page"]) ? $this->_page_condition_matches($condition["admin_page"], rgar($condition,"tab")) : true;
- $field_type_matches = isset($condition["field_types"]) ? $this->_field_condition_matches($condition["field_types"], $form) : true;
-
- //Scripts will only be enqueued in any admin page if "admin_page", "query" or "post" variable is set.
- //Scripts will only be enqueued in the front end if "admin_page" is not set.
- $site_matches = ( isset($condition["admin_page"]) && is_admin() ) || ( !isset($condition["admin_page"]) && !is_admin() ) || ( isset($condition["query"]) || isset($condition["post"]) ) ;
-
- if($query_matches && $post_matches && $admin_page_matches && $field_type_matches && $site_matches){
- return true;
- }
- }
- }
- return false;
- }
-
- private function _request_condition_matches($request, $query){
- parse_str($query, $query_array);
- foreach($query_array as $key => $value){
-
- switch ($value){
- case "_notempty_" :
- if(rgempty($key, $request))
- return false;
- break;
- case "_empty_" :
- if(!rgempty($key, $request))
- return false;
- break;
- default :
- if (rgar($request, $key) != $value)
- return false;
- break;
- }
-
- }
- return true;
- }
-
- private function _page_condition_matches($pages, $tab){
- if(!is_array($pages))
- $pages = array($pages);
-
- foreach($pages as $page){
- switch($page){
- case "form_editor" :
- if($this->is_form_editor())
- return true;
-
- break;
-
- case "form_settings" :
- if($this->is_form_settings($tab))
- return true;
-
- break;
-
- case "plugin_settings" :
- if($this->is_plugin_settings($tab))
- return true;
-
- break;
-
- case "app_settings" :
- if($this->is_app_settings($tab))
- return true;
-
- break;
-
- case "plugin_page" :
- if($this->is_plugin_page())
- return true;
-
- break;
-
- case "entry_view" :
- if($this->is_entry_view())
- return true;
-
- break;
-
- case "entry_edit" :
- if($this->is_entry_edit())
- return true;
-
- break;
-
- case "results" :
- if($this->is_results())
- return true;
-
- break;
-
- }
- }
- return false;
-
- }
-
- private function _field_condition_matches($field_types, $form){
- if(!is_array($field_types))
- $field_types = array($field_types);
-
- $fields = GFCommon::get_fields_by_type($form, $field_types);
- if(count($fields) > 0)
- return true;
-
- return false;
- }
-
- /**
- * Target for the gform_noconflict_scripts filter. Adds scripts to the list of white-listed no conflict scripts.
- *
- * Not intended to be overridden or called directed by Add-Ons.
- *
- * @ignore
- *
- * @param array $scripts Array of scripts to be white-listed
- * @return array
- */
- public function register_noconflict_scripts($scripts) {
- //registering scripts with Gravity Forms so that they get enqueued when running in no-conflict mode
- return array_merge($scripts, $this->_no_conflict_scripts);
- }
-
- /**
- * Target for the gform_noconflict_styles filter. Adds styles to the list of white-listed no conflict scripts.
- *
- * Not intended to be overridden or called directed by Add-Ons.
- *
- * @ignore
- *
- * @param array $styles Array of styles to be white-listed
- * @return array
- */
- public function register_noconflict_styles($styles) {
- //registering styles with Gravity Forms so that they get enqueued when running in no-conflict mode
- return array_merge($styles, $this->_no_conflict_styles);
- }
-
-
-
- //-------------- Entry meta --------------------------------------
-
- /**
- * Override this method to activate and configure entry meta.
- *
- *
- * @param array $entry_meta An array of entry meta already registered with the gform_entry_meta filter.
- * @param int $form_id The form id
- *
- * @return array The filtered entry meta array.
- */
- protected function get_entry_meta($entry_meta, $form_id) {
- return $entry_meta;
- }
-
-
- //-------------- Results page --------------------------------------
- /**
- * Returns the configuration for the results page. By default this is not activated.
- * To activate the results page override this function and return an array with the configuration data.
- *
- * Example:
- * public function get_results_page_config() {
- * return array(
- * "title" => "Quiz Results",
- * "capabilities" => array("gravityforms_quiz_results"),
- * "callbacks" => array(
- * "fields" => array($this, "results_fields"),
- * "calculation" => array($this, "results_calculation"),
- * "markup" => array($this, "results_markup"),
- * )
- * );
- * }
- *
- */
- public function get_results_page_config() {
- return false;
- }
-
- /**
- * Initializes the result page functionality. To activate result page functionality, override the get_results_page_config() function.
- *
- * @param $results_page_config - configuration returned by get_results_page_config()
- */
- protected function results_page_init($results_page_config) {
- require_once("class-gf-results.php");
-
- if(isset($results_page_config["callbacks"]["filters"]))
- add_filter("gform_filters_pre_results", $results_page_config["callbacks"]["filters"], 10, 2);
-
- if(isset($results_page_config["callbacks"]["filter_ui"])){
- add_filter("gform_filter_ui", $results_page_config["callbacks"]["filter_ui"], 10, 5);
- }
-
- $gf_results = new GFResults($this->_slug, $results_page_config);
- $gf_results->init();
- }
-
- //-------------- Logging integration --------------------------------------
-
- function set_logging_supported($plugins){
- $plugins[$this->_slug] = $this->_title;
- return $plugins;
- }
-
-
- //-------------- Members plugin integration --------------------------------------
-
- /**
- * Checks whether the Members plugin is installed and activated.
- *
- * Not intended to be overridden or called directly by Add-Ons.
- *
- * @ignore
- *
- * @return bool
- */
- protected function has_members_plugin() {
- return function_exists('members_get_capabilities');
- }
-
- /**
- * Not intended to be overridden or called directly by Add-Ons.
- *
- * @ignore
- *
- * @param $caps
- * @return array
- */
- public function members_get_capabilities($caps) {
- return array_merge($caps, $this->_capabilities);
- }
-
- //-------------- Permissions: Capabilities and Roles ----------------------------
-
- /**
- * Checks whether the current user is assigned to a capability or role.
- *
- * @param string|array $caps An string or array of capabilities to check
- * @return bool Returns true if the current user is assigned to any of the capabilities.
- */
- protected function current_user_can_any($caps) {
- return GFCommon::current_user_can_any($caps);
- }
-
-
- //------- Settings Helper Methods (Common to all settings pages) -------------------
-
- /***
- * Renders the UI of all settings page based on the specified configuration array $sections
- *
- * @param array $sections - Configuration array containing all fields to be rendered grouped into sections
- */
- protected function render_settings( $sections ) {
-
- if( ! $this->has_setting_field_type( 'save', $sections ) )
- $sections = $this->add_default_save_button( $sections );
-
- ?>
-
-
-
- setting_dependency_met( rgar( $section, 'dependency' ) ) )
- $this->single_section( $section, $is_first);
-
- $is_first = false;
- }
- }
+ }
- /***
- * Displays the UI for a field section
- *
- * @param array $section - The section to be displayed
- * @param bool $is_first - true for the first section in the list, false for all others
- */
- protected function single_section( $section, $is_first = false) {
-
- extract( wp_parse_args( $section, array(
- 'title' => false,
- 'description' => false,
- 'id' => '',
- 'class' => false,
- 'style' => '',
- ''
- ) ) );
-
- $classes = array( 'gaddon-section' );
-
- if( $is_first )
- $classes[] = 'gaddon-first-section';
-
- if( $class )
- $classes[] = $class;
-
- ?>
-
-
-
-
-
- " >
-
- single_setting_label( $field ); ?>
-
-
- single_setting( $field ); ?>
-
-
-
- get_required_indicator( $field );
-
- }
-
- protected function single_setting_row_save( $field ) {
- ?>
-
-
-
- single_setting( $field ); ?>
-
-
-
- _saved_settings = $settings;
- }
-
- /***
- * Sets the previous settings to a class variable so that it can be accessed by lower level functions providing support for
- * verifying whether a value was changed before executing an action
- *
- * @param array $settings: Settings to be stored
- */
- protected function set_previous_settings( $settings ) {
- $this->_previous_settings = $settings;
- }
-
- protected function get_previous_settings() {
- return $this->_previous_settings;
- }
-
-
-
- /***
- * Gets settings from $_POST variable, returning a name/value collection of setting name and setting value
- */
- protected function get_posted_settings(){
- global $_gaddon_posted_settings;
-
- if(isset($_gaddon_posted_settings))
- return $_gaddon_posted_settings;
-
- $_gaddon_posted_settings = array();
- if(count($_POST) > 0){
- foreach($_POST as $key => $value){
- if(preg_match("|_gaddon_setting_(.*)|", $key, $matches)){
- $_gaddon_posted_settings[$matches[1]] = self::maybe_decode_json(stripslashes_deep($value));
- }
- }
- }
-
- return $_gaddon_posted_settings;
- }
-
- protected static function maybe_decode_json($value) {
- if (self::is_json($value))
- return json_decode($value, ARRAY_A);
-
- return $value;
- }
-
- protected static function is_json($value) {
- if (is_string($value) && in_array( substr($value, 0, 1), array( '{', '[' ) ) && is_array(json_decode($value, ARRAY_A)))
- return true;
-
- return false;
- }
-
- /***
- * Gets the "current" settings, which are settings from $_POST variables if this is a postback request, or the current saved settings for a get request.
- */
- protected function get_current_settings(){
- //try getting settings from post
- $settings = $this->get_posted_settings();
-
- //if nothing has been posted, get current saved settings
- if(empty($settings)){
- $settings = $this->_saved_settings;
- }
-
- return $settings;
- }
-
- /***
- * Retrieves the setting for a specific field/input
- *
- * @param string $setting_name The field or input name
- * @param string $default_value Optional. The default value
- * @return string|array
- */
- protected function get_setting( $setting_name, $default_value = "" , $settings = false ) {
-
- if( ! $settings )
- $settings = $this->get_current_settings();
-
- if (false === $settings) {
- return $default_value;
- }
-
- if (strpos($setting_name, "[") !== false) {
- $path_parts = explode("[", $setting_name);
- foreach ($path_parts as $part) {
- $part = trim($part, "]");
- if (false === isset($settings[$part])) {
- return $default_value;
- }
- $settings = rgar($settings, $part);
- }
- $setting = $settings;
- } else {
- if (false === isset($settings[$setting_name])) {
- return $default_value;
- }
- $setting = $settings[$setting_name];
- }
-
- return $setting;
- }
-
- protected function get_mapped_field_value( $setting_name, $form, $entry, $settings = false ){
-
- $field_id = $this->get_setting($setting_name, "", $settings);
-
- $field_type = GFFormsModel::get_input_type(GFFormsModel::get_field($form, $field_id));
- $is_field_id_integer = ctype_digit($field_id);
-
- if($is_field_id_integer && $field_type == "name"){
- //Full Name
- $value = $this->get_full_name($entry, $field_id);
- }
- else if($is_field_id_integer && $field_type == "address"){
- //Full Address
- $value = $this->get_full_address($entry, $field_id);
- }
- else{
- $value = rgar($entry, $field_id);
- }
-
- return $value;
- }
-
- protected function get_full_address($entry, $field_id){
- $street_value = str_replace(" ", " ", trim($entry[$field_id . ".1"]));
- $street2_value = str_replace(" ", " ", trim($entry[$field_id . ".2"]));
- $city_value = str_replace(" ", " ", trim($entry[$field_id . ".3"]));
- $state_value = str_replace(" ", " ", trim($entry[$field_id . ".4"]));
- $zip_value = trim($entry[$field_id . ".5"]);
- $country_value = GFCommon::get_country_code(trim($entry[$field_id . ".6"]));
-
- $address = $street_value;
- $address .= !empty($address) && !empty($street2_value) ? " $street2_value" : $street2_value;
- $address .= !empty($address) && (!empty($city_value) || !empty($state_value)) ? ", $city_value," : $city_value;
- $address .= !empty($address) && !empty($city_value) && !empty($state_value) ? " $state_value" : $state_value;
- $address .= !empty($address) && !empty($zip_value) ? " $zip_value," : $zip_value;
- $address .= !empty($address) && !empty($country_value) ? " $country_value" : $country_value;
-
-
- return $address;
- }
-
- protected function get_full_name($entry, $field_id){
-
- //If field is aweber (one input), simply return full content
- $name = rgar($entry,$field_id);
- if(!empty($name))
- return $name;
-
- //Complex field (multiple inputs). Join all pieces and create name
- $prefix = trim(rgar($entry,$field_id . ".2"));
- $first = trim(rgar($entry,$field_id . ".3"));
- $last = trim(rgar($entry,$field_id . ".6"));
- $suffix = trim(rgar($entry,$field_id . ".8"));
-
- $name = $prefix;
- $name .= !empty($name) && !empty($first) ? " $first" : $first;
- $name .= !empty($name) && !empty($last) ? " $last" : $last;
- $name .= !empty($name) && !empty($suffix) ? " $suffix" : $suffix;
- return $name;
- }
-
-
- /***
- * Determines if a dependent field has been populated.
- *
- * @param string $dependency - Field or input name of the "parent" field.
- * @return bool - true if the "parent" field has been filled out and false if it has not.
- *
- */
- protected function setting_dependency_met( $dependency ) {
-
- // if no dependency, always return true
- if( ! $dependency )
- return true;
-
- //use a callback if one is specified in the configuration
- if(is_callable($dependency)){
- return call_user_func($dependency);
- }
-
- if(is_array($dependency)){
- //supports: "dependency" => array("field" => "myfield", "values" => array("val1", "val2"))
- $dependency_field = $dependency["field"];
- $dependency_value = $dependency["values"];
- }
- else{
- //supports: "dependency" => "myfield"
- $dependency_field = $dependency;
- $dependency_value = "_notempty_";
- }
-
- if(!is_array($dependency_value))
- $dependency_value = array($dependency_value);
-
- $current_value = $this->get_setting($dependency_field);
-
- foreach($dependency_value as $val){
- if($current_value == $val)
- return true;
-
- if($val == "_notempty_" && !rgblank($current_value))
- return true;
- }
-
- return false;
- }
-
- protected function has_setting_field_type( $type, $fields ) {
-
- foreach( $fields as &$section ) {
- foreach( $section['fields'] as $field ) {
- if( rgar( $field, 'type' ) == $type )
- return true;
- }
- }
-
- return false;
- }
-
- protected function add_default_save_button( $sections ) {
- $sections[count($sections) - 1]['fields'][] = array( 'type' => 'save' );
- return $sections;
- }
-
- protected function get_save_success_message( $sections ) {
- $save_button = $this->get_save_button($sections);
- return isset($save_button["messages"]["success"]) ? $save_button["messages"]["success"] : __("Settings updated", "gravityforms") ;
- }
-
- protected function get_save_error_message( $sections ) {
- $save_button = $this->get_save_button($sections);
- return isset($save_button["messages"]["error"]) ? $save_button["messages"]["error"] : __("There was an error while saving your settings", "gravityforms") ;
- }
-
- protected function get_save_button($sections){
- $fields = $sections[count($sections) - 1]['fields'];
+ /**
+ * Override this function to add AJAX hooks or to add initialization code when an AJAX request is being performed
+ */
+ protected function init_ajax() {
+ if ( rgpost( 'view' ) == 'gf_results_' . $this->_slug ) {
+ require_once( GFCommon::get_base_path() . '/tooltips.php' );
+ require_once( 'class-gf-results.php' );
+ $gf_results = new GFResults( $this->_slug, $this->get_results_page_config() );
+ add_action( 'wp_ajax_gresults_get_results_gf_results_' . $this->_slug, array( $gf_results, 'ajax_get_results' ) );
+ add_action( 'wp_ajax_gresults_get_more_results_gf_results_' . $this->_slug, array( $gf_results, 'ajax_get_more_results' ) );
+ } elseif ( $this->method_is_overridden( 'get_locking_config' ) ) {
+ require_once( GFCommon::get_base_path() . '/includes/locking/class-gf-locking.php' );
+ require_once( 'class-gf-addon-locking.php' );
+ $config = $this->get_locking_config();
+ new GFAddonLocking( $config, $this );
+ }
+ }
- foreach($fields as $field){
- if ($field["type"] == "save")
- return $field;
- }
-
- return false;
- }
-
-
-
- //------------- Field Types ------------------------------------------------------
-
- /***
- * Renders and initializes a text field based on the $field array
- *
- * @param array $field - Field array containing the configuration options of this field
- * @param bool $echo = true - true to echo the output to the screen, false to simply return the contents as a string
- * @return string The HTML for the field
- */
- protected function settings_text($field, $echo = true) {
-
- $field["type"] = "text"; //making sure type is set to text
- $attributes = $this->get_field_attributes($field);
- $default_value = rgar( $field, 'value' ) ? rgar( $field, 'value' ) : rgar( $field, 'default_value' );
- $value = $this->get_setting($field["name"], $default_value);
-
-
- $name = esc_attr($field["name"]);
- $tooltip = isset( $choice['tooltip'] ) ? gform_tooltip( $choice['tooltip'], rgar( $choice, 'tooltip_class'), true ) : "";
- $html = "";
- $html .= ' ';
-
- $feedback_callback = rgar($field, 'feedback_callback');
- if(is_callable($feedback_callback)){
- $is_valid = call_user_func_array( $feedback_callback, array( $value, $field ) );
- $icon = "";
- if($is_valid === true)
- $icon = "icon-check fa-check gf_valid"; // check icon
- else if($is_valid === false)
- $icon = "icon-remove fa-times gf_invalid"; // x icon
-
- if(!empty($icon))
- $html .= " ";
- }
-
- if( $this->field_failed_validation( $field ) )
- $html .= $this->get_error_icon( $field );
-
- if ($echo)
- echo $html;
-
- return $html;
- }
-
- /***
- * Renders and initializes a textarea field based on the $field array
- *
- * @param array $field - Field array containing the configuration options of this field
- * @param bool $echo = true - true to echo the output to the screen, false to simply return the contents as a string
- * @return string The HTML for the field
- */
- protected function settings_textarea($field, $echo = true) {
- $field["type"] = "textarea"; //making sure type is set to textarea
- $attributes = $this->get_field_attributes($field);
- $default_value = rgar( $field, 'value' ) ? rgar( $field, 'value' ) : rgar( $field, 'default_value' );
- $value = $this->get_setting($field["name"], $default_value);
-
- $name = '' . esc_attr($field["name"]);
- $tooltip = isset( $choice['tooltip'] ) ? gform_tooltip( $choice['tooltip'], rgar( $choice, 'tooltip_class'), true ) : "";
- $html = "";
-
- $html .= '';
+ //-------------- Setup ---------------
- if( $this->field_failed_validation( $field ) )
- $html .= $this->get_error_icon( $field );
+ /**
+ * Performs upgrade tasks when the version of the Add-On changes. To add additional upgrade tasks, override the upgrade() function, which will only get executed when the plugin version has changed.
+ */
+ protected function setup() {
- if ($echo)
- echo $html;
+ //Upgrading add-on
+ $installed_version = get_option( 'gravityformsaddon_' . $this->_slug . '_version' );
- return $html;
- }
+ //Making sure version has really changed. Gets around aggressive caching issue on some sites that cause setup to run multiple times.
+ if ( $installed_version != $this->_version ) {
+ $installed_version = GFForms::get_wp_option( 'gravityformsaddon_' . $this->_slug . '_version' );
+ }
+ //Upgrade if version has changed
+ if ( $installed_version != $this->_version ) {
- /***
- * Renders and initializes a hidden field based on the $field array
- *
- * @param array $field - Field array containing the configuration options of this field
- * @param bool $echo = true - true to echo the output to the screen, false to simply return the contents as a string
- * @return string The HTML for the field
- */
- protected function settings_hidden($field, $echo = true) {
- $field["type"] = "hidden"; //making sure type is set to hidden
- $attributes = $this->get_field_attributes($field);
+ $this->upgrade( $installed_version );
+ update_option( 'gravityformsaddon_' . $this->_slug . '_version', $this->_version );
+ }
+ }
- $default_value = rgar( $field, 'value' ) ? rgar( $field, 'value' ) : rgar( $field, 'default_value' );
- $value = $this->get_setting($field["name"], $default_value);
+ /**
+ * Override this function to add to add database update scripts or any other code to be executed when the Add-On version changes
+ */
+ protected function upgrade( $previous_version ) {
+ return;
+ }
- if ( is_array( $value ) )
- $value = json_encode($value);
- $html = ' ';
+ //-------------- Script enqueuing ---------------
- if ($echo)
- echo $html;
+ /**
+ * Override this function to provide a list of styles to be enqueued.
+ * When overriding this function, be sure to call parent::styles() to ensure the base class scripts are enqueued.
+ * See scripts() for an example of the format expected to be returned.
+ */
+ protected function styles() {
+ return array(
+ array(
+ 'handle' => 'gaddon_form_settings_css',
+ 'src' => GFAddOn::get_gfaddon_base_url() . '/css/gaddon_settings.css',
+ 'version' => GFCommon::$version,
+ 'enqueue' => array(
+ array( 'admin_page' => array( 'form_settings', 'plugin_settings', 'plugin_page' ) ),
+ )
+ ),
+ array(
+ 'handle' => 'gaddon_results_css',
+ 'src' => GFAddOn::get_gfaddon_base_url() . '/css/gaddon_results.css',
+ 'version' => GFCommon::$version,
+ 'enqueue' => array(
+ array( 'admin_page' => array( 'results' ) ),
+ )
+ ),
+ );
+ }
- return $html;
- }
- /***
- * Renders and initializes a checkbox field or a collection of checkbox fields based on the $field array
+ /**
+ * Override this function to provide a list of scripts to be enqueued.
+ * When overriding this function, be sure to call parent::scripts() to ensure the base class scripts are enqueued.
+ * Following is an example of the array that is expected to be returned by this function:
+ *
+ *
*
- * @param array $field - Field array containing the configuration options of this field
- * @param bool $echo = true - true to echo the output to the screen, false to simply return the contents as a string
+ * array(
+ * array( "handle" => 'maskedinput',
+ * "src" => GFCommon::get_base_url() . '/js/jquery.maskedinput-1.3.min.js',
+ * "version" => GFCommon::$version,
+ * "deps" => array("jquery"),
+ * "in_footer" => false,
*
- * @return string The HTML for the field
+ * //Determines where the script will be enqueued. The script will be enqueued if any of the conditions match
+ * "enqueue" => array(
+ * //admin_page - Specified one or more pages (known pages) where the script is supposed to be enqueued.
+ * //To enqueue scripts in the front end (public website), simply don't define this setting
+ * array("admin_page" => array("form_settings", 'plugin_settings') ),
+ *
+ * //tab - Specifies a form settings or plugin settings tab in which the script is supposed to be enqueued. If none is specified, the script will be enqueued in any of the form settings or plugin_settings page
+ * array("tab" => 'signature'),
+ *
+ * //query - Specifies a set of query string ($_GET) values. If all specified query string values match the current requested page, the script will be enqueued
+ * array("query" => 'page=gf_edit_forms&view=settings&id=_notempty_')
+ *
+ * //post - Specifies a set of post ($_POST) values. If all specified posted values match the current request, the script will be enqueued
+ * array("post" => 'posted_field=val')
+ *
+ * )
+ * ),
+ * array(
+ * "handle" => 'super_signature_script',
+ * "src" => $this->get_base_url() . '/super_signature/ss.js',
+ * "version" => $this->_version,
+ * "deps" => array("jquery"),
+ * "callback" => array($this, 'localize_scripts'),
+ * "strings" => array(
+ * // Accessible in JavaScript using the global variable "[script handle]_strings"
+ * "stringKey1" => __("The string", 'gravityforms'),
+ * "stringKey2" => __("Another string.", 'gravityforms')
+ * )
+ * "enqueue" => array(
+ * //field_types - Specifies one or more field types that requires this script. The script will only be enqueued if the current form has a field of any of the specified field types. Only applies when a current form is available.
+ * array("field_types" => array("signature"))
+ * )
+ * )
+ * );
+ *
+ *
+ *
*/
- protected function settings_checkbox( $field, $echo = true ) {
-
- $field['type'] = 'checkbox'; //making sure type is set to checkbox
+ protected function scripts() {
+ return array(
+ array(
+ 'handle' => 'gform_form_admin',
+ 'enqueue' => array( array( 'admin_page' => array( 'form_settings' ) ) )
+ ),
+ array(
+ 'handle' => 'gform_gravityforms',
+ 'enqueue' => array( array( 'admin_page' => array( 'form_settings' ) ) )
+ ),
+ array(
+ 'handle' => 'google_charts',
+ 'src' => 'https://www.google.com/jsapi',
+ 'version' => GFCommon::$version,
+ 'enqueue' => array(
+ array( 'admin_page' => array( 'results' ) ),
+ )
+ ),
+ array(
+ 'handle' => 'gaddon_results_js',
+ 'src' => GFAddOn::get_gfaddon_base_url() . '/js/gaddon_results.js',
+ 'version' => GFCommon::$version,
+ 'deps' => array( 'jquery', 'sack', 'jquery-ui-resizable', 'jquery-ui-datepicker', 'google_charts', 'gform_field_filter' ),
+ 'callback' => array( 'GFResults', 'localize_results_scripts' ),
+ 'enqueue' => array(
+ array( 'admin_page' => array( 'results' ) ),
+ )
+ ),
+ array(
+ 'handle' => 'gaddon_repeater',
+ 'src' => GFAddOn::get_gfaddon_base_url() . '/js/repeater.js',
+ 'version' => GFCommon::$version,
+ 'deps' => array( 'jquery' ),
+ 'enqueue' => array(
+ array(
+ 'admin_page' => array( 'form_settings' )
+ // ,
+ // 'field_types' => array( 'dynamic_field_map' )
+ ),
+ ),
+ ),
+ );
+ }
- $field_attributes = $this->get_field_attributes( $field, array() );
- $horizontal = rgar( $field, 'horizontal' ) ? ' gaddon-setting-inline' : '';
- $default_choice_attributes = array( 'onclick' => 'jQuery(this).siblings("input[type=hidden]").val(jQuery(this).prop("checked") ? 1 : 0);' );
- $html = '';
- if ( is_array( $field['choices'] ) ) {
- foreach ( $field['choices'] as $choice ) {
- $choice['id'] = sanitize_title($choice['name']);
- $choice_attributes = $this->get_choice_attributes( $choice, $field_attributes, $default_choice_attributes );
- $value = $this->get_setting( $choice['name'], rgar( $choice, 'default_value' ) );
- $tooltip = isset( $choice['tooltip'] ) ? gform_tooltip( $choice['tooltip'], rgar( $choice, 'tooltip_class' ), true ) : '';
+ /**
+ * Target of admin_enqueue_scripts and gform_enqueue_scripts hooks.
+ * Not intended to be overridden by child classes.
+ * In order to enqueue scripts and styles, override the scripts() and styles() functions
+ *
+ * @ignore
+ */
+ public function enqueue_scripts( $form = '', $is_ajax = false ) {
- $html .= $this->checkbox_item( $choice, $horizontal, $choice_attributes, $value, $tooltip );
+ if ( empty( $form ) ) {
+ $form = $this->get_current_form();
+ }
+ //Enqueueing scripts
+ $scripts = $this->scripts();
+ foreach ( $scripts as $script ) {
+ $src = isset( $script['src'] ) ? $script['src'] : false;
+ $deps = isset( $script['deps'] ) ? $script['deps'] : array();
+ $version = isset( $script['version'] ) ? $script['version'] : false;
+ $in_footer = isset( $script['in_footer'] ) ? $script['in_footer'] : false;
+ wp_register_script( $script['handle'], $src, $deps, $version, $in_footer );
+ if ( isset( $script['enqueue'] ) && $this->_can_enqueue_script( $script['enqueue'], $form, $is_ajax ) ) {
+ $this->add_no_conflict_scripts( array( $script['handle'] ) );
+ wp_enqueue_script( $script['handle'] );
+ if ( isset( $script['strings'] ) ) {
+ wp_localize_script( $script['handle'], $script['handle'] . '_strings', $script['strings'] );
+ }
+ if ( isset( $script['callback'] ) && is_callable( $script['callback'] ) ) {
+ $args = compact( 'form', 'is_ajax' );
+ call_user_func_array( $script['callback'], $args );
+ }
}
}
- if ( $this->field_failed_validation( $field ) )
- $html .= $this->get_error_icon( $field );
-
- if ( $echo )
- echo $html;
+ //Enqueueing styles
+ $styles = $this->styles();
+ foreach ( $styles as $style ) {
+ $src = isset( $style['src'] ) ? $style['src'] : false;
+ $deps = isset( $style['deps'] ) ? $style['deps'] : array();
+ $version = isset( $style['version'] ) ? $style['version'] : false;
+ $media = isset( $style['media'] ) ? $style['media'] : 'all';
+ wp_register_style( $style['handle'], $src, $deps, $version, $media );
+ if ( $this->_can_enqueue_script( $style['enqueue'], $form, $is_ajax ) ) {
+ $this->add_no_conflict_styles( array( $style['handle'] ) );
+ if ( $this->is_preview() ) {
+ $this->_preview_styles[] = $style['handle'];
+ } elseif ( $this->is_print() ) {
+ $this->_print_styles[] = $style['handle'];
+ } else {
+ wp_enqueue_style( $style['handle'] );
+ }
+ }
+ }
+ }
- return $html;
+ /**
+ * Target of gform_preview_styles. Enqueue styles to the preview page.
+ * Not intented to be overriden by child classes
+ *
+ * @ignore
+ */
+ public function enqueue_preview_styles( $preview_styles, $form ) {
+ return array_merge( $preview_styles, $this->_preview_styles );
}
/**
- * Returns the markup for an individual checkbox item give the parameters
- *
- * @param $choice - Choice array with all configured properties
- * @param $horizontal_class - CSS class to style checkbox items horizontally
- * @param $attributes - String containing all the attributes for the input tag.
- * @param $value - Currently selection (1 if field has been checked. 0 or null otherwise)
- * @param $tooltip - String containing a tooltip for this checkbox item.
+ * Target of gform_print_styles. Enqueue styles to the print entry page.
+ * Not intented to be overriden by child classes
*
- * @return string - The markup of an individual checkbox item
+ * @ignore
*/
- protected function checkbox_item( $choice, $horizontal_class, $attributes, $value, $tooltip ) {
- $hidden_field_value = $value == '1' ? '1' : '0';
- $checkbox_item = '
-
- ';
-
- if ( is_callable( array( $this, "checkbox_input_{$choice['name']}" ) ) ) {
- $markup = call_user_func( array( $this, "checkbox_input_{$choice['name']}" ), $choice, $attributes, $value, $tooltip );
- } else {
- $markup = $this->checkbox_input( $choice, $attributes, $value, $tooltip );
+ public function enqueue_print_styles( $print_styles, $form ) {
+ if ( false === $print_styles ) {
+ $print_styles = array();
}
- $checkbox_item .= $markup . '
';
+ $styles = $this->styles();
+ foreach ( $styles as $style ) {
+ if ( $this->_can_enqueue_script( $style['enqueue'], $form, false ) ) {
+ $this->add_no_conflict_styles( array( $style['handle'] ) );
+ $src = isset( $style['src'] ) ? $style['src'] : false;
+ $deps = isset( $style['deps'] ) ? $style['deps'] : array();
+ $version = isset( $style['version'] ) ? $style['version'] : false;
+ $media = isset( $style['media'] ) ? $style['media'] : 'all';
+ wp_register_style( $style['handle'], $src, $deps, $version, $media );
+ $print_styles[] = $style['handle'];
+ }
+ }
- return $checkbox_item;
+ return array_merge( $print_styles, $this->_print_styles );
}
+
/**
- * Returns the markup for an individual checkbox input and its associated label
- *
- * @param $choice - Choice array with all configured properties
- * @param $attributes - String containing all the attributes for the input tag.
- * @param $value - Currently selection (1 if field has been checked. 0 or null otherwise)
- * @param $tooltip - String containing a tooltip for this checkbox item.
+ * Adds scripts to the list of white-listed no conflict scripts.
*
- * @return string - The markup of an individual checkbox input and its associated label
+ * @param $scripts
*/
- protected function checkbox_input( $choice, $attributes, $value, $tooltip ) {
- $markup = '
- ' . esc_html( $choice['label'] ) . ' ' . $tooltip . ' ';
+ private function add_no_conflict_scripts( $scripts ) {
+ $this->_no_conflict_scripts = array_merge( $scripts, $this->_no_conflict_scripts );
- return $markup;
}
+ /**
+ * Adds styles to the list of white-listed no conflict styles.
+ *
+ * @param $styles
+ */
+ private function add_no_conflict_styles( $styles ) {
+ $this->_no_conflict_styles = array_merge( $styles, $this->_no_conflict_styles );
+ }
- /***
- * Renders and initializes a radio field or a collection of radio fields based on the $field array
- *
- * @param array $field - Field array containing the configuration options of this field
- * @param bool $echo = true - true to echo the output to the screen, false to simply return the contents as a string
- * @return string Returns the markup for the radio buttons
- *
- */
- protected function settings_radio($field, $echo = true) {
-
- $field["type"] = "radio"; //making sure type is set to radio
-
- $selected_value = $this->get_setting($field['name'], rgar($field, "default_value"));
- $field_attributes = $this->get_field_attributes($field);
- $horizontal = rgar($field, "horizontal") ? " gaddon-setting-inline" : "";
- $html = "";
- if (is_array($field["choices"])) {
- foreach ($field["choices"] as $i => $choice) {
- $choice['id'] = $field['name'] . $i;
- $choice_attributes = $this->get_choice_attributes($choice, $field_attributes);
-
- $tooltip = isset( $choice['tooltip'] ) ? gform_tooltip( $choice['tooltip'], rgar( $choice, 'tooltip_class'), true ) : "";
-
- $radio_value = isset($choice["value"]) ? $choice["value"] : $choice["label"];
- $checked = checked($selected_value, $radio_value, false);
- $html .= '
-
-
- '. esc_html($choice['label']) . ' ' . $tooltip . '
-
-
- ';
- }
- }
+ private function _can_enqueue_script( $enqueue_conditions, $form, $is_ajax ) {
+ if ( empty( $enqueue_conditions ) ) {
+ return false;
+ }
- if( $this->field_failed_validation( $field ) )
- $html .= $this->get_error_icon( $field );
+ foreach ( $enqueue_conditions as $condition ) {
+ if ( is_callable( $condition ) ) {
+ return call_user_func( $condition, $form, $is_ajax );
+ } else {
+ $query_matches = isset( $condition['query'] ) ? $this->_request_condition_matches( $_GET, $condition['query'] ) : true;
+ $post_matches = isset( $condition['post'] ) ? $this->_request_condition_matches( $_POST, $condition['query'] ) : true;
+ $admin_page_matches = isset( $condition['admin_page'] ) ? $this->_page_condition_matches( $condition['admin_page'], rgar( $condition, 'tab' ) ) : true;
+ $field_type_matches = isset( $condition['field_types'] ) ? $this->_field_condition_matches( $condition['field_types'], $form ) : true;
+
+ if ( $query_matches && $post_matches && $admin_page_matches && $field_type_matches) {
+ return true;
+ }
+ }
+ }
- if ($echo)
- echo $html;
+ return false;
+ }
- return $html;
- }
+ private function _request_condition_matches( $request, $query ) {
+ parse_str( $query, $query_array );
+ foreach ( $query_array as $key => $value ) {
- /***
- * Renders and initializes a drop down field based on the $field array
- *
- * @param array $field - Field array containing the configuration options of this field
- * @param bool $echo = true - true to echo the output to the screen, false to simply return the contents as a string
- *
- * @return string The HTML for the field
- */
- protected function settings_select( $field, $echo = true ) {
+ switch ( $value ) {
+ case '_notempty_' :
+ if ( rgempty( $key, $request ) ) {
+ return false;
+ }
+ break;
+ case '_empty_' :
+ if ( ! rgempty( $key, $request ) ) {
+ return false;
+ }
+ break;
+ default :
+ if ( rgar( $request, $key ) != $value ) {
+ return false;
+ }
+ break;
+ }
+ }
- $field['type'] = 'select'; // making sure type is set to select
- $attributes = $this->get_field_attributes($field);
- $value = $this->get_setting( $field['name'], rgar( $field, 'default_value' ) );
- $name = '' . esc_attr( $field['name'] );
+ return true;
+ }
- $html = sprintf(
- '%3$s ',
- '_gaddon_setting_' . $name, implode( ' ', $attributes ), $this->get_select_options( $field['choices'], $value )
- );
+ private function _page_condition_matches( $pages, $tab ) {
+ if ( ! is_array( $pages ) ) {
+ $pages = array( $pages );
+ }
- if( $this->field_failed_validation( $field ) )
- $html .= $this->get_error_icon( $field );
+ foreach ( $pages as $page ) {
+ switch ( $page ) {
+ case 'form_editor' :
+ if ( $this->is_form_editor() ) {
+ return true;
+ }
- if( $echo )
- echo $html;
+ break;
- return $html;
- }
+ case 'form_settings' :
+ if ( $this->is_form_settings( $tab ) ) {
+ return true;
+ }
- public function get_select_options( $choices, $selected_value ) {
+ break;
- $options = '';
+ case 'plugin_settings' :
+ if ( $this->is_plugin_settings( $tab ) ) {
+ return true;
+ }
- foreach( $choices as $choice ) {
+ break;
- if( isset( $choice['choices'] ) ) {
+ case 'app_settings' :
+ if ( $this->is_app_settings( $tab ) ) {
+ return true;
+ }
- $options .= sprintf( '%2$s ', $choice['label'], $this->get_select_options( $choice['choices'], $selected_value ) );
+ break;
- } else {
+ case 'plugin_page' :
+ if ( $this->is_plugin_page() ) {
+ return true;
+ }
- if( ! isset( $choice['value'] ) )
- $choice['value'] = $choice['label'];
+ break;
- $options .= $this->get_select_option( $choice, $selected_value );
+ case 'entry_view' :
+ if ( $this->is_entry_view() ) {
+ return true;
+ }
- }
+ break;
- }
+ case 'entry_edit' :
+ if ( $this->is_entry_edit() ) {
+ return true;
+ }
- return $options;
- }
+ break;
- protected function get_select_option( $choice, $selected_value ) {
- return sprintf( '%3$s ', esc_attr( $choice['value'] ), selected( $selected_value, $choice['value'], false ), $choice['label'] );
- }
+ case 'results' :
+ if ( $this->is_results() ) {
+ return true;
+ }
+ break;
- //------------- Field Map Field Type --------------------------
+ }
+ }
- public function settings_field_map( $field, $echo = true ) {
+ return false;
- $html = '';
- $field_map = rgar( $field, 'field_map' );
+ }
- if( empty( $field_map ) ) {
- return $html;
+ private function _field_condition_matches( $field_types, $form ) {
+ if ( ! is_array( $field_types ) ) {
+ $field_types = array( $field_types );
}
- $form_id = rgget( 'id' );
-
- $html .= '
-
-
-
- ' . $this->field_map_title() . '
- ' . __( "Form Field", "gravityforms" ) . '
-
-
- ';
+ $fields = GFCommon::get_fields_by_type( $form, $field_types );
+ if ( count( $fields ) > 0 ) {
+ return true;
+ }
- foreach( $field['field_map'] as $child_field ) {
+ return false;
+ }
- if( ! $this->setting_dependency_met( rgar( $child_field, 'dependency' ) ) )
- continue;
+ /**
+ * Target for the gform_noconflict_scripts filter. Adds scripts to the list of white-listed no conflict scripts.
+ *
+ * Not intended to be overridden or called directed by Add-Ons.
+ *
+ * @ignore
+ *
+ * @param array $scripts Array of scripts to be white-listed
+ *
+ * @return array
+ */
+ public function register_noconflict_scripts( $scripts ) {
+ //registering scripts with Gravity Forms so that they get enqueued when running in no-conflict mode
+ return array_merge( $scripts, $this->_no_conflict_scripts );
+ }
- $child_field['name'] = $this->get_mapped_field_name( $field, $child_field['name'] );
- $required = rgar( $child_field, 'required' ) ? $this->get_required_indicator( $child_field ) : '';
+ /**
+ * Target for the gform_noconflict_styles filter. Adds styles to the list of white-listed no conflict scripts.
+ *
+ * Not intended to be overridden or called directed by Add-Ons.
+ *
+ * @ignore
+ *
+ * @param array $styles Array of styles to be white-listed
+ *
+ * @return array
+ */
+ public function register_noconflict_styles( $styles ) {
+ //registering styles with Gravity Forms so that they get enqueued when running in no-conflict mode
+ return array_merge( $styles, $this->_no_conflict_styles );
+ }
- $html .= '
-
-
- ' . $child_field['label'] . ' ' . $required . '
-
- ' .
- $this->settings_field_map_select( $child_field, $form_id ) .
- '
- ';
- }
- $html .= '
-
-
';
- if( $echo )
- echo $html;
+ //-------------- Entry meta --------------------------------------
- return $html;
- }
+ /**
+ * Override this method to activate and configure entry meta.
+ *
+ *
+ * @param array $entry_meta An array of entry meta already registered with the gform_entry_meta filter.
+ * @param int $form_id The form id
+ *
+ * @return array The filtered entry meta array.
+ */
+ protected function get_entry_meta( $entry_meta, $form_id ) {
+ return $entry_meta;
+ }
- public function settings_field_map_select( $field, $form_id ) {
- $field['choices'] = $this->get_field_map_choices( $form_id );
- return $this->settings_select( $field, false );
- }
- protected function field_map_title(){
- return __("Field", "gravityforms");
- }
+ //-------------- Results page --------------------------------------
+ /**
+ * Returns the configuration for the results page. By default this is not activated.
+ * To activate the results page override this function and return an array with the configuration data.
+ *
+ * Example:
+ * public function get_results_page_config() {
+ * return array(
+ * "title" => 'Quiz Results',
+ * "capabilities" => array("gravityforms_quiz_results"),
+ * "callbacks" => array(
+ * "fields" => array($this, 'results_fields'),
+ * "calculation" => array($this, 'results_calculation'),
+ * "markup" => array($this, 'results_markup'),
+ * )
+ * );
+ * }
+ *
+ */
+ public function get_results_page_config() {
+ return false;
+ }
- public static function get_field_map_choices( $form_id ) {
+ /**
+ * Initializes the result page functionality. To activate result page functionality, override the get_results_page_config() function.
+ *
+ * @param $results_page_config - configuration returned by get_results_page_config()
+ */
+ protected function results_page_init( $results_page_config ) {
+ require_once( 'class-gf-results.php' );
- $form = RGFormsModel::get_form_meta($form_id);
+ if ( isset( $results_page_config['callbacks']['filters'] ) ) {
+ add_filter( 'gform_filters_pre_results', $results_page_config['callbacks']['filters'], 10, 2 );
+ }
- $fields = array();
+ if ( isset( $results_page_config['callbacks']['filter_ui'] ) ) {
+ add_filter( 'gform_filter_ui', $results_page_config['callbacks']['filter_ui'], 10, 5 );
+ }
- // Adding default fields
- $fields[] = array( "value" => "", "label" => "" );
- $fields[] = array( "value" => "date_created" , "label" => __("Entry Date", "gravityforms") );
- $fields[] = array( "value" => "ip" , "label" => __("User IP", "gravityforms") );
- $fields[] = array( "value" => "source_url" , "label" => __("Source Url", "gravityforms") );
- $fields[] = array( "value" => "form_title" , "label" => __("Form Title", "gravityforms") );
+ $gf_results = new GFResults( $this->_slug, $results_page_config );
+ $gf_results->init();
+ }
- // Populate entry meta
- $entry_meta = GFFormsModel::get_entry_meta( $form["id"] );
- foreach( $entry_meta as $meta_key => $meta ) {
- $fields[] = array( 'value' => $meta_key , 'label' => rgars($entry_meta, "{$meta_key}/label") );
- }
+ //-------------- Logging integration --------------------------------------
- // Populate form fields
- if( is_array( $form["fields"] ) ) {
- foreach( $form["fields"] as $field ) {
- if( is_array( rgar( $field, "inputs") ) ) {
+ public function set_logging_supported( $plugins ) {
+ $plugins[ $this->_slug ] = $this->_title;
- //If this is an address field, add full name to the list
- if(RGFormsModel::get_input_type($field) == "address")
- $fields[] = array( 'value' => $field["id"], 'label' => GFCommon::get_label($field) . " (" . __("Full" , "gravityforms") . ")" );
+ return $plugins;
+ }
- //If this is a name field, add full name to the list
- if(RGFormsModel::get_input_type($field) == "name")
- $fields[] = array( 'value' => $field["id"], 'label' => GFCommon::get_label($field) . " (" . __("Full" , "gravityforms") . ")" );
- //If this is a checkbox field, add to the list
- if(RGFormsModel::get_input_type($field) == "checkbox")
- $fields[] = array( 'value' => $field["id"], 'label' => GFCommon::get_label($field) . " (" . __("Selected" , "gravityforms") . ")" );
+ //-------------- Members plugin integration --------------------------------------
- foreach( $field['inputs'] as $input ) {
- if( RGFormsModel::get_input_type( $field ) == 'creditcard' ) {
- //only include the credit card type (field_id.4) and number (field_id.1)
- if ( $input['id'] == $field['id'] . '.1' || $input['id'] == $field['id'] . '.4' ) {
- $fields[] = array( 'value' => $input["id"], 'label' => GFCommon::get_label( $field, $input['id'] ) );
- }
- }
- else {
- $fields[] = array( 'value' => $input["id"], 'label' => GFCommon::get_label($field, $input["id"]) );
- }
- }
- }
- else if(!rgar($field,"displayOnly")){
- $fields[] = array( 'value' => $field["id"], 'label' => GFCommon::get_label($field));
- }
- }
- }
+ /**
+ * Checks whether the Members plugin is installed and activated.
+ *
+ * Not intended to be overridden or called directly by Add-Ons.
+ *
+ * @ignore
+ *
+ * @return bool
+ */
+ protected function has_members_plugin() {
+ return function_exists( 'members_get_capabilities' );
+ }
- return $fields;
- }
+ /**
+ * Not intended to be overridden or called directly by Add-Ons.
+ *
+ * @ignore
+ *
+ * @param $caps
+ *
+ * @return array
+ */
+ public function members_get_capabilities( $caps ) {
+ return array_merge( $caps, $this->_capabilities );
+ }
- public function get_mapped_field_name( $parent_field, $field_name ) {
- return "{$parent_field['name']}_{$field_name}";
- }
+ //-------------- Permissions: Capabilities and Roles ----------------------------
- public static function get_field_map_fields( $feed, $field_name ) {
+ /**
+ * Checks whether the current user is assigned to a capability or role.
+ *
+ * @param string|array $caps An string or array of capabilities to check
+ *
+ * @return bool Returns true if the current user is assigned to any of the capabilities.
+ */
+ protected function current_user_can_any( $caps ) {
+ return GFCommon::current_user_can_any( $caps );
+ }
- $fields = array();
- $prefix = "{$field_name}_";
- foreach( $feed['meta'] as $name => $value ) {
- if( strpos( $name, $prefix ) === 0 ) {
- $name = str_replace( $prefix, '', $name );
- $fields[$name] = $value;
- }
- }
+ //------- Settings Helper Methods (Common to all settings pages) -------------------
- return $fields;
- }
+ /***
+ * Renders the UI of all settings page based on the specified configuration array $sections
+ *
+ * @param array $sections - Configuration array containing all fields to be rendered grouped into sections
+ */
+ protected function render_settings( $sections ) {
- //----------------------------------------------------------------
+ if ( ! $this->has_setting_field_type( 'save', $sections ) ) {
+ $sections = $this->add_default_save_button( $sections );
+ }
+ ?>
- public function settings_dynamic_field_map( $field, $echo = true ) {
+
- $field['choices'] = $field['key_choices'];
- if( $field['enable_custom_key'] )
- $field['choices']['_gf_add_custom'] = array( 'value' => '_gf_add_custom', 'label' => __( 'Add Custom', 'gravityforms' ) );
+ setting_dependency_met( rgar( $section, 'dependency' ) ) ) {
+ $this->single_section( $section, $is_first );
+ }
- $value_field = $field;
- $value_field['name'] = '_value';
- $value_field['class'] = 'value_{i}';
+ $is_first = false;
+ }
+ }
- $html .= '
-
-
-
- ' . $this->field_map_title() . '
- ' . __( 'Form Field', 'gravityforms' ) . '
-
-
-
-
-
- ' .
- $this->settings_select( $key_field, false ) .
- '
- ' .
- $this->settings_field_map_select( $value_field, $form['id'] ) .
- '
- {buttons}
-
-
-
';
+ /***
+ * Displays the UI for a field section
+ *
+ * @param array $section - The section to be displayed
+ * @param bool $is_first - true for the first section in the list, false for all others
+ */
+ protected function single_section( $section, $is_first = false ) {
+
+ extract(
+ wp_parse_args(
+ $section, array(
+ 'title' => false,
+ 'description' => false,
+ 'id' => '',
+ 'class' => false,
+ 'style' => '',
+ ''
+ )
+ )
+ );
+
+ $classes = array( 'gaddon-section' );
+
+ if ( $is_first ) {
+ $classes[] = 'gaddon-first-section';
+ }
- $html .= $this->settings_hidden( $field, false );
+ if ( $class )
+ $classes[] = $class;
- if( $echo )
- echo $html;
+ ?>
- return $html;
+
- }
+
+
+
- public function settings_field_select( $field, $echo = true ) {
+
+
+
- $args = is_array( rgar( $field, 'args' ) ) ? rgar( $field, 'args' ) : array( rgar( $field, 'args' ) );
+
- if( ! empty( $args['append_choices'] ) )
- $field['choices'] = array_merge( $field['choices'], $args['append_choices'] );
+
- $html = $this->settings_select( $field, false );
+
- $fields = array();
+ >
+
+ single_setting_label( $field ); ?>
+
+
+ single_setting( $field ); ?>
+
+
- if( ! is_array( $form['fields'] ) )
- return $fields;
+ array(),
- 'input_types' => array()
- ) );
+ protected function single_setting_label( $field ) {
- foreach( $form['fields'] as $field ) {
+ echo $field['label'];
- $input_type = GFFormsModel::get_input_type( $field );
+ if ( isset( $field['tooltip'] ) ) {
+ echo ' ' . gform_tooltip( $field['tooltip'], rgar( $field, 'tooltip_class' ), true );
+ }
- if( ! empty( $args['input_types'] ) && ! in_array( $input_type, $args['input_types'] ) )
- continue;
+ if ( rgar( $field, 'required' ) ) {
+ echo ' ' . $this->get_required_indicator( $field );
+ }
- if( is_array( rgar( $field, 'inputs' ) ) ) {
+ }
- // if this is an address field, add full name to the list
- if( $input_type == 'address' )
- $fields[] = array( 'value' => $field['id'], 'label' => GFCommon::get_label( $field ) . ' (' . __( 'Full' , 'gravityforms' ) . ')' );
+ protected function single_setting_row_save( $field ) {
+ ?>
- // if this is a name field, add full name to the list
- if( $input_type == 'name' )
- $fields[] = array( 'value' => $field["id"], 'label' => GFCommon::get_label($field) . ' (' . __( 'Full', 'gravityforms' ) . ')' );
+
+
+ single_setting( $field ); ?>
+
+
- // if this is a checkbox field, add to the list
- if( $input_type == 'checkbox' )
- $fields[] = array( 'value' => $field['id'], 'label' => GFCommon::get_label($field) . ' (' . __( 'Selected', 'gravityforms' ) . ')' );
+ $input['id'], 'label' => GFCommon::get_label( $field, $input['id'] ) );
- }
+ /***
+ * Calls the appropriate field function to handle rendering of each specific field type
+ *
+ * @param array $field - The field to be rendered
+ */
+ protected function single_setting( $field ) {
+ if ( is_callable( rgar( $field, 'callback' ) ) ) {
+ call_user_func( $field['callback'], $field );
+ } else if ( is_callable( array( $this, "settings_{$field['type']}" ) ) ) {
+ call_user_func( array( $this, "settings_{$field['type']}" ), $field );
+ } else {
+ printf( __( "Field type '%s' has not been implemented", 'gravityforms' ), $field['type'] );
+ }
+ }
- }
- else if( ! rgar( $field, 'displayOnly' ) ) {
+ /***
+ * Sets the current saved settings to a class variable so that it can be accessed by lower level functions in order to initialize inputs with the appropriate values
+ *
+ * @param array $settings : Settings to be saved
+ */
+ protected function set_settings( $settings ) {
+ $this->_saved_settings = $settings;
+ }
- $fields[] = array( 'value' => $field['id'], 'label' => GFCommon::get_label( $field ) );
+ /***
+ * Sets the previous settings to a class variable so that it can be accessed by lower level functions providing support for
+ * verifying whether a value was changed before executing an action
+ *
+ * @param array $settings : Settings to be stored
+ */
+ protected function set_previous_settings( $settings ) {
+ $this->_previous_settings = $settings;
+ }
- }
- }
+ protected function get_previous_settings() {
+ return $this->_previous_settings;
+ }
- return $fields;
- }
+ /***
+ * Gets settings from $_POST variable, returning a name/value collection of setting name and setting value
+ */
+ protected function get_posted_settings() {
+ global $_gaddon_posted_settings;
+ if ( isset( $_gaddon_posted_settings ) )
+ return $_gaddon_posted_settings;
+ $_gaddon_posted_settings = array();
+ if ( count( $_POST ) > 0 ) {
+ foreach ( $_POST as $key => $value ) {
+ if ( preg_match( '|_gaddon_setting_(.*)|', $key, $matches ) ) {
+ $_gaddon_posted_settings[ $matches[1] ] = self::maybe_decode_json( stripslashes_deep( $value ) );
+ }
+ }
+ }
+ return $_gaddon_posted_settings;
+ }
+ protected static function maybe_decode_json( $value ) {
+ if ( self::is_json( $value ) )
+ return json_decode( $value, ARRAY_A );
- /***
- * Renders the save button for settings pages
- *
- * @param array $field - Field array containing the configuration options of this field
- * @param bool $echo = true - true to echo the output to the screen, false to simply return the contents as a string
- *
- * @return string The HTML
- */
- protected function settings_save( $field, $echo = true ) {
+ return $value;
+ }
- $field['type'] = 'submit';
- $field['name'] = 'gform-settings-save';
- $field['class'] = 'button-primary gfbutton';
+ protected static function is_json( $value ) {
+ if ( is_string( $value ) && in_array( substr( $value, 0, 1 ), array( '{', '[' ) ) && is_array( json_decode( $value, ARRAY_A ) ) )
+ return true;
- if( ! rgar( $field, 'value' ) )
- $field['value'] = __( 'Update Settings', 'gravityforms' );
+ return false;
+ }
- $attributes = $this->get_field_attributes( $field );
+ /***
+ * Gets the "current" settings, which are settings from $_POST variables if this is a postback request, or the current saved settings for a get request.
+ */
+ protected function get_current_settings() {
+ //try getting settings from post
+ $settings = $this->get_posted_settings();
- $html = ' ';
+ //if nothing has been posted, get current saved settings
+ if ( empty( $settings ) ) {
+ $settings = $this->_saved_settings;
+ }
- if ( $echo )
- echo $html;
+ return $settings;
+ }
- return $html;
- }
+ /***
+ * Retrieves the setting for a specific field/input
+ *
+ * @param string $setting_name The field or input name
+ * @param string $default_value Optional. The default value
+ * @param bool|array $settings Optional. THe settings array
+ *
+ * @return string|array
+ */
+ protected function get_setting( $setting_name, $default_value = '', $settings = false ) {
- /**
- * Helper to create a simple conditional logic set of fields. It creates one row of conditional logic with Field/Operator/Value inputs.
- *
- * @param mixed $setting_name_root - The root name to be used for inputs. It will be used as a prefix to the inputs that make up the conditional logic fields.
- * @return string The HTML
- */
- protected function simple_condition($setting_name_root){
+ if ( ! $settings )
+ $settings = $this->get_current_settings();
- $conditional_fields = $this->get_conditional_logic_fields();
+ if ( false === $settings )
+ return $default_value;
- $value_input = '_gaddon_setting_' . esc_attr( $setting_name_root ) . '_value';
- $object_type = "simple_condition_{$setting_name_root}";
+ if ( strpos( $setting_name, '[' ) !== false ) {
+ $path_parts = explode( '[', $setting_name );
+ foreach ( $path_parts as $part ) {
+ $part = trim( $part, ']' );
+ if ( $part != '0'){
+ if ( empty( $part ) ) {
+ return $settings;
+ }
+ }
+ if ( false === isset( $settings[ $part ] ) ) {
+ return $default_value;
+ }
- $str = $this->settings_select(array(
- 'name' => "{$setting_name_root}_field_id",
- 'type' => 'select',
- 'choices' => $conditional_fields,
- 'class' => 'optin_select',
- 'onchange' => "jQuery('#" . esc_attr( $setting_name_root ) . "_container').html(GetRuleValues('{$object_type}', 0, jQuery(this).val(), '', '{$value_input}'));"
- ), false);
+ $settings = rgar( $settings, $part );
+ }
+ $setting = $settings;
+ } else {
+ if ( false === isset( $settings[ $setting_name ] ) ){
+ return $default_value;
+ }
+ $setting = $settings[ $setting_name ];
+ }
- $str .= $this->settings_select(array(
- 'name' => "{$setting_name_root}_operator",
- 'type' => 'select',
- 'onchange' => "SetRuleProperty('{$object_type}', 0, 'operator', jQuery(this).val()); jQuery('#" . esc_attr( $setting_name_root ) . "_container').html(GetRuleValues('{$object_type}', 0, jQuery('#{$setting_name_root}_field_id').val(), '', '{$value_input}'));",
- "choices" => array(
- array(
- "value" => "is",
- "label" => __("is", "gravityforms")
- ),
- array(
- "value" => "isnot",
- "label" => __("is not", "gravityforms")
- ),
- array(
- "value" => ">",
- "label" => __("greater than", "gravityforms")
- ),
- array(
- "value" => "<",
- "label" => __("less than", "gravityforms")
- ),
- array(
- "value" => "contains",
- "label" => __("contains", "gravityforms")
- ),
- array(
- "value" => "starts_with",
- "label" => __("starts with", "gravityforms")
- ),
- array(
- "value" => "ends_with",
- "label" => __("ends with", "gravityforms")
- )
- ),
- ), false);
+ return $setting;
+ }
- $str .= " ";
+ protected function get_mapped_field_value( $setting_name, $form, $entry, $settings = false ) {
- $field_id = $this->get_setting("{$setting_name_root}_field_id");
+ $field_id = $this->get_setting( $setting_name, '', $settings );
- $value = $this->get_setting("{$setting_name_root}_value");
- $operator = $this->get_setting("{$setting_name_root}_operator");
- if ( empty( $operator ) ){
- $operator = 'is';
+ $field_type = GFFormsModel::get_input_type( GFFormsModel::get_field( $form, $field_id ) );
+ $is_field_id_integer = ctype_digit( $field_id );
+
+ if ( $is_field_id_integer && $field_type == 'name' ) {
+ //Full Name
+ $value = $this->get_full_name( $entry, $field_id );
+ } else if ( $is_field_id_integer && $field_type == 'address' ) {
+ //Full Address
+ $value = $this->get_full_address( $entry, $field_id );
+ } else {
+ $value = rgar( $entry, $field_id );
}
+ return $value;
+ }
- $field_id_attribute = ! empty( $field_id ) ? $field_id : 'jQuery("#' . esc_attr( $setting_name_root ) . '_field_id").val()';
+ protected function get_full_address( $entry, $field_id ) {
+ $street_value = str_replace( ' ', ' ', trim( $entry[ $field_id . '.1' ] ) );
+ $street2_value = str_replace( ' ', ' ', trim( $entry[ $field_id . '.2' ] ) );
+ $city_value = str_replace( ' ', ' ', trim( $entry[ $field_id . '.3' ] ) );
+ $state_value = str_replace( ' ', ' ', trim( $entry[ $field_id . '.4' ] ) );
+ $zip_value = trim( $entry[ $field_id . '.5' ] );
+ $country_value = GFCommon::get_country_code( trim( $entry[ $field_id . '.6' ] ) );
- $str .= "";
+ protected function get_full_name( $entry, $field_id ) {
- return $str;
- }
-
- /**
- * Parses the properties of the $field meta array and returns a set of HTML attributes to be added to the HTML element.
- * @param array $field - current field meta to be parsed.
- * @param array $default - default set of properties. Will be appended to the properties specified in the $field array
- * @return array - resulting HTML attributes ready to be included in the HTML element.
- */
- protected function get_field_attributes($field, $default = array()){
-
- // each nonstandard property will be extracted from the $props array so it is not auto-output in the field HTML
- $no_output_props = apply_filters( 'gaddon_no_output_field_properties',
- array( 'default_value', 'label', 'choices', 'feedback_callback', 'checked', 'checkbox_label', 'value', 'type',
- 'validation_callback', 'required', 'hidden', 'tooltip', 'dependency', 'messages', 'name' ), $field );
-
- $default_props = array(
- 'class' => '', // will default to gaddon-setting
- 'default_value' => '' // default value that should be selected or entered for the field
- );
-
- // Property switch case
- switch( $field['type'] ) {
- case 'select':
- $default_props['choices'] = array();
- break;
- case 'checkbox':
- $default_props['checked'] = false;
- $default_props['checkbox_label'] = '';
- $default_props['choices'] = array();
- break;
- case 'text':
- default:
- break;
- }
-
- $props = wp_parse_args( $field, $default_props );
- $props['id'] = rgempty("id", $props) ? rgar( $props, 'name' ) : rgar( $props, "id");
- $props['class'] = trim("{$props['class']} gaddon-setting gaddon-{$props['type']}");
-
- // extract no-output properties from $props array so they are not auto-output in the field HTML
- foreach( $no_output_props as $prop ) {
- if( isset( $props[$prop] ) ) {
- ${$prop} = $props[$prop];
- unset( $props[$prop] );
- }
- }
-
- //adding default attributes
- foreach($default as $key=>$value){
- if(isset($props[$key]))
- $props[$key] = $value . $props[$key];
- else
- $props[$key] = $value;
- }
-
- // get an array of property strings, example: name='myFieldName'
- $prop_strings = array();
- foreach( $props as $prop => $value ) {
- $prop_strings[$prop] = "{$prop}='" . esc_attr($value) . "'";
- }
-
- return $prop_strings;
- }
-
- /**
- * Parses the properties of the $choice meta array and returns a set of HTML attributes to be added to the HTML element.
- * @param array $choice - current choice meta to be parsed.
- * @param array $field_attributes - current field's attributes.
- * @return array - resulting HTML attributes ready to be included in the HTML element.
- */
- protected function get_choice_attributes($choice, $field_attributes, $default_choice_attributes = array()){
+ //If field is aweber (one input), simply return full content
+ $name = rgar( $entry, $field_id );
+ if ( ! empty( $name ) )
+ return $name;
- $choice_attributes = $field_attributes;
+ //Complex field (multiple inputs). Join all pieces and create name
+ $prefix = trim( rgar( $entry, $field_id . '.2' ) );
+ $first = trim( rgar( $entry, $field_id . '.3' ) );
+ $last = trim( rgar( $entry, $field_id . '.6' ) );
+ $suffix = trim( rgar( $entry, $field_id . '.8' ) );
- foreach( $choice as $prop => $val ) {
- $no_output_choice_attributes = array( 'default_value', 'label', 'checked', 'value', 'type',
- 'validation_callback', 'required', 'tooltip' );
- if(in_array($prop, $no_output_choice_attributes)){
- unset($choice_attributes[$prop]);
- }
- else if ( in_array( $prop, $default_choice_attributes ) ) {
- $choice_attributes[$prop] = "{$prop}='" . esc_attr( $default_choice_attributes[ $prop ] ) . esc_attr($val) . "'";
- }
- else{
- $choice_attributes[$prop] = "{$prop}='" . esc_attr($val) . "'";
- }
- }
+ $name = $prefix;
+ $name .= ! empty( $name ) && ! empty( $first ) ? " $first" : $first;
+ $name .= ! empty( $name ) && ! empty( $last ) ? " $last" : $last;
+ $name .= ! empty( $name ) && ! empty( $suffix ) ? " $suffix" : $suffix;
- //Adding default attributes. Either creating a new attribute or pre-pending to an existing one.
- foreach ( $default_choice_attributes as $default_attr_name => $default_attr_value ){
+ return $name;
+ }
- if ( isset( $choice_attributes[ $default_attr_name ] ) ){
- $choice_attributes[ $default_attr_name ] = $this->prepend_attribute( $default_attr_name, $default_attr_value, $choice_attributes[ $default_attr_name ] );
- }
- else {
- $choice_attributes[ $default_attr_name ] = "{$default_attr_name}='" . esc_attr( $default_attr_value ) . "'";
- }
+
+ /***
+ * Determines if a dependent field has been populated.
+ *
+ * @param string $dependency - Field or input name of the "parent" field.
+ *
+ * @return bool - true if the "parent" field has been filled out and false if it has not.
+ *
+ */
+ protected function setting_dependency_met( $dependency ) {
+
+ // if no dependency, always return true
+ if ( ! $dependency )
+ return true;
+
+ //use a callback if one is specified in the configuration
+ if ( is_callable( $dependency ) ) {
+ return call_user_func( $dependency );
}
- return $choice_attributes;
- }
+ if ( is_array( $dependency ) ) {
+ //supports: 'dependency' => array("field" => 'myfield', 'values' => array("val1", 'val2'))
+ $dependency_field = $dependency['field'];
+ $dependency_value = $dependency['values'];
+ } else {
+ //supports: 'dependency' => 'myfield'
+ $dependency_field = $dependency;
+ $dependency_value = '_notempty_';
+ }
- protected function prepend_attribute( $name, $attribute, $current_attribute ){
- return str_replace( "{$name}='", "{$name}='{$attribute}", $current_attribute);
+ if ( ! is_array( $dependency_value ) )
+ $dependency_value = array( $dependency_value );
+
+ $current_value = $this->get_setting( $dependency_field );
+
+ foreach ( $dependency_value as $val ) {
+ if ( $current_value == $val )
+ return true;
+
+ if ( $val == '_notempty_' && ! rgblank( $current_value ) )
+ return true;
+ }
+
+ return false;
}
- /**
- * Validates settings fields.
- * Validates that all fields are valid. Fields can be invalid when they are blank and marked as required or if it fails a custom validation check.
- * To specify a custom validation, use the 'validation_callback' field meta property and implement the validation function with the custom logic.
- * @param $fields - A list of all fields from the field meta configuration
- * @param $settings - A list of submitted settings values
- * @return bool - Returns true if all fields have passed validation, and false otherwise.
- */
- protected function validate_settings( $fields, $settings ) {
+ protected function has_setting_field_type( $type, $fields ) {
- foreach( $fields as $section ) {
+ foreach ( $fields as &$section ) {
+ foreach ( $section['fields'] as $field ) {
+ if ( rgar( $field, 'type' ) == $type )
+ return true;
+ }
+ }
- if( ! $this->setting_dependency_met( rgar( $section, 'dependency' ) ) )
- continue;
+ return false;
+ }
- foreach( $section['fields'] as $field ) {
+ protected function add_default_save_button( $sections ) {
+ $sections[ count( $sections ) - 1 ]['fields'][] = array( 'type' => 'save' );
- if( ! $this->setting_dependency_met( rgar( $field, 'dependency' ) ) )
- continue;
+ return $sections;
+ }
- if( is_callable( rgar( $field, 'validation_callback' ) ) ) {
- call_user_func( rgar( $field, 'validation_callback' ), $field, $field_setting );
- continue;
- }
+ protected function get_save_success_message( $sections ) {
+ $save_button = $this->get_save_button( $sections );
- switch($field["type"]){
- case "field_map" :
+ return isset( $save_button['messages']['success'] ) ? $save_button['messages']['success'] : __( 'Settings updated', 'gravityforms' );
+ }
- $this->validate_field_map_settings($field, $settings);
+ protected function get_save_error_message( $sections ) {
+ $save_button = $this->get_save_button( $sections );
- break;
+ return isset( $save_button['messages']['error'] ) ? $save_button['messages']['error'] : __( 'There was an error while saving your settings', 'gravityforms' );
+ }
- case "checkbox" :
+ protected function get_save_button( $sections ) {
+ $fields = $sections[ count( $sections ) - 1 ]['fields'];
- $this->validate_checkbox_settings($field, $settings);
+ foreach ( $fields as $field ) {
+ if ( $field['type'] == 'save' )
+ return $field;
+ }
- break;
+ return false;
+ }
- default :
- $field_setting = rgar( $settings, rgar( $field, 'name' ) );
- if( rgar( $field, 'required' ) && rgblank( $field_setting )) {
- $this->set_field_error( $field, rgar( $field, 'error_message' ) );
- }
+ //------------- Field Types ------------------------------------------------------
- break;
- }
- }
- }
+ /***
+ * Renders and initializes a text field based on the $field array
+ *
+ * @param array $field - Field array containing the configuration options of this field
+ * @param bool $echo = true - true to echo the output to the screen, false to simply return the contents as a string
+ *
+ * @return string The HTML for the field
+ */
+ protected function settings_text( $field, $echo = true ) {
- $field_errors = $this->get_field_errors();
- $is_valid = empty( $field_errors );
+ $field['type'] = 'text'; //making sure type is set to text
+ $attributes = $this->get_field_attributes( $field );
+ $default_value = rgar( $field, 'value' ) ? rgar( $field, 'value' ) : rgar( $field, 'default_value' );
+ $value = $this->get_setting( $field['name'], $default_value );
- return $is_valid;
- }
- protected function validate_checkbox_settings( $field, $settings ) {
+ $name = esc_attr( $field['name'] );
+ $tooltip = isset( $choice['tooltip'] ) ? gform_tooltip( $choice['tooltip'], rgar( $choice, 'tooltip_class' ), true ) : '';
+ $html = '';
- if( !rgar( $field, 'required' ) )
- return;
+ $html .= ' ';
+
+ $feedback_callback = rgar( $field, 'feedback_callback' );
+ if ( is_callable( $feedback_callback ) ) {
+ $is_valid = call_user_func_array( $feedback_callback, array( $value, $field ) );
+ $icon = '';
+ if ( $is_valid === true )
+ $icon = 'icon-check fa-check gf_valid'; // check icon
+ else if ( $is_valid === false )
+ $icon = 'icon-remove fa-times gf_invalid'; // x icon
+
+ if ( ! empty( $icon ) )
+ $html .= " ";
+ }
- if(!is_array(rgar($field, "choices")))
- return;
+ if ( $this->field_failed_validation( $field ) )
+ $html .= $this->get_error_icon( $field );
- foreach($field["choices"] as $choice){
- $choice_setting = rgar( $settings, rgar( $choice, 'name' ) );
- if(!empty($choice_setting))
- return;
- }
+ if ( $echo )
+ echo $html;
- $this->set_field_error( $field, rgar( $field, 'error_message' ) );
- }
+ return $html;
+ }
- protected function validate_field_map_settings( $field, $settings ) {
+ /***
+ * Renders and initializes a textarea field based on the $field array
+ *
+ * @param array $field - Field array containing the configuration options of this field
+ * @param bool $echo = true - true to echo the output to the screen, false to simply return the contents as a string
+ *
+ * @return string The HTML for the field
+ */
+ protected function settings_textarea( $field, $echo = true ) {
+ $field['type'] = 'textarea'; //making sure type is set to textarea
+ $attributes = $this->get_field_attributes( $field );
+ $default_value = rgar( $field, 'value' ) ? rgar( $field, 'value' ) : rgar( $field, 'default_value' );
+ $value = $this->get_setting( $field['name'], $default_value );
- $field_map = rgar( $field, 'field_map' );
+ $name = '' . esc_attr( $field['name'] );
+ $tooltip = isset( $choice['tooltip'] ) ? gform_tooltip( $choice['tooltip'], rgar( $choice, 'tooltip_class' ), true ) : '';
+ $html = '';
- if( empty( $field_map ) )
- return;
+ $html .= '';
- foreach( $field_map as $child_field ) {
+ if ( $this->field_failed_validation( $field ) )
+ $html .= $this->get_error_icon( $field );
- if( ! $this->setting_dependency_met( rgar( $child_field, 'dependency' ) ) )
- continue;
+ if ( $echo )
+ echo $html;
- $child_field['name'] = $this->get_mapped_field_name( $field, $child_field['name'] );
- $setting_value = rgar( $settings, $child_field['name'] );
+ return $html;
+ }
- if( rgar( $child_field, 'required' ) && rgblank( $setting_value ) ) {
- $this->set_field_error( $child_field );
- } else if( rgar( $child_field, 'validation_callback' ) && is_callable( rgar( $child_field, 'validation_callback' ) ) ) {
- call_user_func( rgar( $child_field, 'validation_callback' ), $child_field, $field );
- }
- }
+ /***
+ * Renders and initializes a hidden field based on the $field array
+ *
+ * @param array $field - Field array containing the configuration options of this field
+ * @param bool $echo = true - true to echo the output to the screen, false to simply return the contents as a string
+ *
+ * @return string The HTML for the field
+ */
+ protected function settings_hidden( $field, $echo = true ) {
+ $field['type'] = 'hidden'; //making sure type is set to hidden
+ $attributes = $this->get_field_attributes( $field );
- }
+ $default_value = rgar( $field, 'value' ) ? rgar( $field, 'value' ) : rgar( $field, 'default_value' );
+ $value = $this->get_setting( $field['name'], $default_value );
- /**
- * Sets the validation error message
- * Sets the error message to be displayed when a field fails validation.
- * When implementing a custom validation callback function, use this function to specify the error message to be displayed.
- *
- * @param array $field - The current field meta
- * @param string $error_message - The error message to be displayed
- */
- protected function set_field_error( $field, $error_message = '' ) {
+ if ( is_array( $value ) )
+ $value = json_encode( $value );
- // set default error message if none passed
- if( !$error_message )
- $error_message = __( 'This field is required.', 'gravityforms' );
+ $html = ' ';
- $this->_setting_field_errors[$field['name']] = $error_message;
- }
+ if ( $echo )
+ echo $html;
- /**
- * Gets the validation errors for a field.
- * Returns validation errors associated with the specified field or a list of all validation messages (if a field isn't specified)
- * @param array|boolean $field - Optional. The field meta. When specified, errors for this field will be returned
- * @return mixed - If a field is specified, a string containing the error message will be returned. Otherwise, an array of all errors will be returned
- */
- protected function get_field_errors( $field = false ) {
+ return $html;
+ }
- if( !$field )
- return $this->_setting_field_errors;
+ /***
+ * Renders and initializes a checkbox field or a collection of checkbox fields based on the $field array
+ *
+ * @param array $field - Field array containing the configuration options of this field
+ * @param bool $echo = true - true to echo the output to the screen, false to simply return the contents as a string
+ *
+ * @return string The HTML for the field
+ */
+ protected function settings_checkbox( $field, $echo = true ) {
- return isset( $this->_setting_field_errors[$field['name']] ) ? $this->_setting_field_errors[$field['name']] : array();
- }
+ $field['type'] = 'checkbox'; //making sure type is set to checkbox
- /**
- * Gets the invalid field icon
- * Returns the markup for an alert icon to indicate and highlight invalid fields.
- * @param array $field - The field meta.
- * @return string - The full markup for the icon
- */
- protected function get_error_icon( $field ) {
+ $field_attributes = $this->get_field_attributes( $field, array() );
+ $horizontal = rgar( $field, 'horizontal' ) ? ' gaddon-setting-inline' : '';
- $error = $this->get_field_errors( $field );
+ $html = '';
+ $default_choice_attributes = array( 'onclick' => 'jQuery(this).siblings("input[type=hidden]").val(jQuery(this).prop("checked") ? 1 : 0);' );
+ if ( is_array( $field['choices'] ) ) {
+ foreach ( $field['choices'] as $choice ) {
+ $choice['id'] = sanitize_title( $choice['name'] );
+ $choice_attributes = $this->get_choice_attributes( $choice, $field_attributes, $default_choice_attributes );
+ $value = $this->get_setting( $choice['name'], rgar( $choice, 'default_value' ) );
+ $tooltip = isset( $choice['tooltip'] ) ? gform_tooltip( $choice['tooltip'], rgar( $choice, 'tooltip_class' ), true ) : '';
- return 'checkbox_item( $choice, $horizontal, $choice_attributes, $value, $tooltip );
+
+ }
+ }
+
+ if ( $this->field_failed_validation( $field ) )
+ $html .= $this->get_error_icon( $field );
+
+ if ( $echo )
+ echo $html;
+
+ return $html;
+ }
+
+
+ /**
+ * Returns the markup for an individual checkbox item give the parameters
+ *
+ * @param $choice - Choice array with all configured properties
+ * @param $horizontal_class - CSS class to style checkbox items horizontally
+ * @param $attributes - String containing all the attributes for the input tag.
+ * @param $value - Currently selection (1 if field has been checked. 0 or null otherwise)
+ * @param $tooltip - String containing a tooltip for this checkbox item.
+ *
+ * @return string - The markup of an individual checkbox item
+ */
+ protected function checkbox_item( $choice, $horizontal_class, $attributes, $value, $tooltip ) {
+ $hidden_field_value = $value == '1' ? '1' : '0';
+ $checkbox_item = '
+
+ ';
+
+ if ( is_callable( array( $this, "checkbox_input_{$choice['name']}" ) ) ) {
+ $markup = call_user_func( array( $this, "checkbox_input_{$choice['name']}" ), $choice, $attributes, $value, $tooltip );
+ } else {
+ $markup = $this->checkbox_input( $choice, $attributes, $value, $tooltip );
+ }
+
+ $checkbox_item .= $markup . '
';
+
+ return $checkbox_item;
+ }
+
+ /**
+ * Returns the markup for an individual checkbox input and its associated label
+ *
+ * @param $choice - Choice array with all configured properties
+ * @param $attributes - String containing all the attributes for the input tag.
+ * @param $value - Currently selection (1 if field has been checked. 0 or null otherwise)
+ * @param $tooltip - String containing a tooltip for this checkbox item.
+ *
+ * @return string - The markup of an individual checkbox input and its associated label
+ */
+ protected function checkbox_input( $choice, $attributes, $value, $tooltip ) {
+ $markup = '
+ ' . esc_html( $choice['label'] ) . ' ' . $tooltip . ' ';
+
+ return $markup;
+ }
+
+
+ /***
+ * Renders and initializes a radio field or a collection of radio fields based on the $field array
+ *
+ * @param array $field - Field array containing the configuration options of this field
+ * @param bool $echo = true - true to echo the output to the screen, false to simply return the contents as a string
+ *
+ * @return string Returns the markup for the radio buttons
+ *
+ */
+ protected function settings_radio( $field, $echo = true ) {
+
+ $field['type'] = 'radio'; //making sure type is set to radio
+
+ $selected_value = $this->get_setting( $field['name'], rgar( $field, 'default_value' ) );
+ $field_attributes = $this->get_field_attributes( $field );
+ $horizontal = rgar( $field, 'horizontal' ) ? ' gaddon-setting-inline' : '';
+ $html = '';
+ if ( is_array( $field['choices'] ) ) {
+ foreach ( $field['choices'] as $i => $choice ) {
+ $choice['id'] = $field['name'] . $i;
+ $choice_attributes = $this->get_choice_attributes( $choice, $field_attributes );
+
+ $tooltip = isset( $choice['tooltip'] ) ? gform_tooltip( $choice['tooltip'], rgar( $choice, 'tooltip_class' ), true ) : '';
+
+ $radio_value = isset( $choice['value'] ) ? $choice['value'] : $choice['label'];
+ $checked = checked( $selected_value, $radio_value, false );
+ $html .= '
+
+
+ ' . esc_html( $choice['label'] ) . ' ' . $tooltip . '
+
+
+ ';
+ }
+ }
+
+ if ( $this->field_failed_validation( $field ) )
+ $html .= $this->get_error_icon( $field );
+
+ if ( $echo )
+ echo $html;
+
+ return $html;
+ }
+
+ /***
+ * Renders and initializes a drop down field based on the $field array
+ *
+ * @param array $field - Field array containing the configuration options of this field
+ * @param bool $echo = true - true to echo the output to the screen, false to simply return the contents as a string
+ *
+ * @return string The HTML for the field
+ */
+ protected function settings_select( $field, $echo = true ) {
+
+ $field['type'] = 'select'; // making sure type is set to select
+ $attributes = $this->get_field_attributes( $field );
+ $value = $this->get_setting( $field['name'], rgar( $field, 'default_value' ) );
+ $name = '' . esc_attr( $field['name'] );
+
+ $html = sprintf(
+ '%3$s ',
+ '_gaddon_setting_' . $name, implode( ' ', $attributes ), $this->get_select_options( $field['choices'], $value )
+ );
+
+ if ( $this->field_failed_validation( $field ) )
+ $html .= $this->get_error_icon( $field );
+
+ if ( $echo )
+ echo $html;
+
+ return $html;
+ }
+
+ public function get_select_options( $choices, $selected_value ) {
+
+ $options = '';
+
+ foreach ( $choices as $choice ) {
+
+ if ( isset( $choice['choices'] ) ) {
+
+ $options .= sprintf( '%2$s ', $choice['label'], $this->get_select_options( $choice['choices'], $selected_value ) );
+
+ } else {
+
+ if ( ! isset( $choice['value'] ) )
+ $choice['value'] = $choice['label'];
+
+ $options .= $this->get_select_option( $choice, $selected_value );
+
+ }
+ }
+
+ return $options;
+ }
+
+ protected function get_select_option( $choice, $selected_value ) {
+ if ( is_array( $selected_value ) ) {
+ $selected = in_array( $choice['value'], $selected_value ) ? "selected='selected'" : '';
+ } else {
+ $selected = selected( $selected_value, $choice['value'], false );
+ }
+
+ return sprintf( '%3$s ', esc_attr( $choice['value'] ), $selected, $choice['label'] );
+ }
+
+
+ //------------- Field Map Field Type --------------------------
+
+ public function settings_field_map( $field, $echo = true ) {
+
+ $html = '';
+ $field_map = rgar( $field, 'field_map' );
+
+ if ( empty( $field_map ) )
+ return $html;
+
+ $form_id = rgget( 'id' );
+
+ $html .= '
+
+
+
+ ' . $this->field_map_title() . '
+ ' . __( 'Form Field', 'gravityforms' ) . '
+
+
+ ';
+
+ foreach ( $field['field_map'] as $child_field ) {
+
+ if ( ! $this->setting_dependency_met( rgar( $child_field, 'dependency' ) ) )
+ continue;
+
+ $child_field['name'] = $this->get_mapped_field_name( $field, $child_field['name'] );
+ $required = rgar( $child_field, 'required' ) ? $this->get_required_indicator( $child_field ) : '';
+
+ $html .= '
+
+
+ ' . $child_field['label'] . ' ' . $required . '
+
+ ' .
+ $this->settings_field_map_select( $child_field, $form_id ) .
+ '
+ ';
+ }
+
+ $html .= '
+
+
';
+
+ if ( $echo )
+ echo $html;
+
+ return $html;
+ }
+
+ public function settings_field_map_select( $field, $form_id ) {
+ $field['choices'] = $this->get_field_map_choices( $form_id );
+
+ return $this->settings_select( $field, false );
+ }
+
+ protected function field_map_title() {
+ return __( 'Field', 'gravityforms' );
+ }
+
+ public static function get_field_map_choices( $form_id ) {
+
+ $form = RGFormsModel::get_form_meta( $form_id );
+
+ $fields = array();
+
+ // Adding default fields
+ $fields[] = array( 'value' => '', 'label' => '' );
+ $fields[] = array( 'value' => 'date_created', 'label' => __( 'Entry Date', 'gravityforms' ) );
+ $fields[] = array( 'value' => 'ip', 'label' => __( 'User IP', 'gravityforms' ) );
+ $fields[] = array( 'value' => 'source_url', 'label' => __( 'Source Url', 'gravityforms' ) );
+ $fields[] = array( 'value' => 'form_title', 'label' => __( 'Form Title', 'gravityforms' ) );
+
+ // Populate entry meta
+ $entry_meta = GFFormsModel::get_entry_meta( $form['id'] );
+ foreach ( $entry_meta as $meta_key => $meta ) {
+ $fields[] = array( 'value' => $meta_key, 'label' => rgars( $entry_meta, "{$meta_key}/label" ) );
+ }
+
+ // Populate form fields
+ if ( is_array( $form['fields'] ) ) {
+ foreach ( $form['fields'] as $field ) {
+ if ( is_array( rgar( $field, 'inputs' ) ) ) {
+
+ //If this is an address field, add full name to the list
+ if ( RGFormsModel::get_input_type( $field ) == 'address' ) {
+ $fields[] = array( 'value' => $field['id'], 'label' => GFCommon::get_label( $field ) . ' (' . __( 'Full', 'gravityforms' ) . ')' );
+ }
+ //If this is a name field, add full name to the list
+ if ( RGFormsModel::get_input_type( $field ) == 'name' ){
+ $fields[] = array( 'value' => $field['id'], 'label' => GFCommon::get_label( $field ) . ' (' . __( 'Full', 'gravityforms' ) . ')' );
+ }
+ //If this is a checkbox field, add to the list
+ if ( RGFormsModel::get_input_type( $field ) == 'checkbox' ) {
+ $fields[] = array( 'value' => $field['id'], 'label' => GFCommon::get_label( $field ) . ' (' . __( 'Selected', 'gravityforms' ) . ')' );
+ }
+ foreach ( $field['inputs'] as $input ) {
+ if( RGFormsModel::get_input_type( $field ) == 'creditcard' ) {
+ //only include the credit card type (field_id.4) and number (field_id.1)
+ if ( $input['id'] == $field['id'] . '.1' || $input['id'] == $field['id'] . '.4' ) {
+ $fields[] = array( 'value' => $input['id'], 'label' => GFCommon::get_label( $field, $input['id'] ) );
+ }
+ }
+ else {
+ $fields[] = array( 'value' => $input['id'], 'label' => GFCommon::get_label( $field, $input['id'] ) );
+ }
+ }
+ }
+ else if ( ! rgar( $field, 'displayOnly' ) ) {
+ $fields[] = array( 'value' => $field['id'], 'label' => GFCommon::get_label( $field ) );
+ }
+ }
+ }
+
+ return $fields;
+ }
+
+ public function get_mapped_field_name( $parent_field, $field_name ) {
+ return "{$parent_field['name']}_{$field_name}";
+ }
+
+ public static function get_field_map_fields( $feed, $field_name ) {
+
+ $fields = array();
+ $prefix = "{$field_name}_";
+
+ foreach ( $feed['meta'] as $name => $value ) {
+ if ( strpos( $name, $prefix ) === 0 ) {
+ $name = str_replace( $prefix, '', $name );
+ $fields[ $name ] = $value;
+ }
+ }
+
+ return $fields;
+ }
+
+ //----------------------------------------------------------------
+
+
+ public function settings_dynamic_field_map( $field, $echo = true ) {
+
+ $html = '';
+ $form = $this->get_current_form();
+
+ $field = wp_parse_args(
+ $field, array(
+ 'label' => false,
+ 'name' => false,
+ 'key_choices' => array(),
+ 'enable_custom_key' => true,
+ )
+ );
+
+ $field['choices'] = $field['key_choices'];
+ if ( $field['enable_custom_key'] )
+ $field['choices']['_gf_add_custom'] = array( 'value' => '_gf_add_custom', 'label' => __( 'Add Custom', 'gravityforms' ) );
+
+ $key_field = $field;
+ $key_field['name'] .= '_key';
+ $key_field['class'] = 'key_{i}';
+
+ $value_field = $field;
+ $value_field['name'] = '_value';
+ $value_field['class'] = 'value_{i}';
+
+ $html .= '
+
+
+
+ ' . $this->field_map_title() . '
+ ' . __( 'Form Field', 'gravityforms' ) . '
+
+
+
+
+
+ ' .
+ $this->settings_select( $key_field, false ) .
+ '
+ ' .
+ $this->settings_field_map_select( $value_field, $form['id'] ) .
+ '
+ {buttons}
+
+
+
';
+
+ $html .= $this->settings_hidden( $field, false );
+
+ if ( $echo )
+ echo $html;
+
+ return $html;
+
+ }
+
+ public function settings_field_select( $field, $echo = true ) {
+
+ $args = is_array( rgar( $field, 'args' ) ) ? rgar( $field, 'args' ) : array( rgar( $field, 'args' ) );
+
+ $args = wp_parse_args(
+ $args, array(
+ 'append_choices' => array(),
+ 'disable_first_choice' => false,
+ )
+ );
+
+ $field['choices'] = array();
+
+ if ( ! $args['disable_first_choice'] )
+ $field['choices'][] = array( 'value' => '', 'label' => '' );
+
+ $field['choices'] = array_merge( $field['choices'], $this->get_form_fields_as_choices( $this->get_current_form(), $args ) );
+
+ if ( ! empty( $args['append_choices'] ) )
+ $field['choices'] = array_merge( $field['choices'], $args['append_choices'] );
+
+ $html = $this->settings_select( $field, false );
+
+ if ( $echo )
+ echo $html;
+
+ return $html;
+ }
+
+ public function get_form_fields_as_choices( $form, $args = array() ) {
+
+ $fields = array();
+
+ if ( ! is_array( $form['fields'] ) )
+ return $fields;
+
+ $args = wp_parse_args(
+ $args, array(
+ 'field_types' => array(),
+ 'input_types' => array()
+ )
+ );
+
+ foreach ( $form['fields'] as $field ) {
+
+ $input_type = GFFormsModel::get_input_type( $field );
+
+ if ( ! empty( $args['input_types'] ) && ! in_array( $input_type, $args['input_types'] ) )
+ continue;
+
+ if ( is_array( rgar( $field, 'inputs' ) ) ) {
+
+ // if this is an address field, add full name to the list
+ if ( $input_type == 'address' )
+ $fields[] = array( 'value' => $field['id'], 'label' => GFCommon::get_label( $field ) . ' (' . __( 'Full', 'gravityforms' ) . ')' );
+
+ // if this is a name field, add full name to the list
+ if ( $input_type == 'name' )
+ $fields[] = array( 'value' => $field['id'], 'label' => GFCommon::get_label( $field ) . ' (' . __( 'Full', 'gravityforms' ) . ')' );
+
+ // if this is a checkbox field, add to the list
+ if ( $input_type == 'checkbox' )
+ $fields[] = array( 'value' => $field['id'], 'label' => GFCommon::get_label( $field ) . ' (' . __( 'Selected', 'gravityforms' ) . ')' );
+
+ foreach ( $field['inputs'] as $input ) {
+ $fields[] = array( 'value' => $input['id'], 'label' => GFCommon::get_label( $field, $input['id'] ) );
+ }
+ } else if ( ! rgar( $field, 'displayOnly' ) ) {
+
+ $fields[] = array( 'value' => $field['id'], 'label' => GFCommon::get_label( $field ) );
+
+ }
+ }
+
+ return $fields;
+ }
+
+
+ /***
+ * Renders the save button for settings pages
+ *
+ * @param array $field - Field array containing the configuration options of this field
+ * @param bool $echo = true - true to echo the output to the screen, false to simply return the contents as a string
+ *
+ * @return string The HTML
+ */
+ protected function settings_save( $field, $echo = true ) {
+
+ $field['type'] = 'submit';
+ $field['name'] = 'gform-settings-save';
+ $field['class'] = 'button-primary gfbutton';
+
+ if ( ! rgar( $field, 'value' ) )
+ $field['value'] = __( 'Update Settings', 'gravityforms' );
+
+ $attributes = $this->get_field_attributes( $field );
+
+ $html = ' ';
+
+ if ( $echo )
+ echo $html;
+
+ return $html;
+ }
+
+ /**
+ * Helper to create a simple conditional logic set of fields. It creates one row of conditional logic with Field/Operator/Value inputs.
+ *
+ * @param mixed $setting_name_root - The root name to be used for inputs. It will be used as a prefix to the inputs that make up the conditional logic fields.
+ *
+ * @return string The HTML
+ */
+ protected function simple_condition( $setting_name_root ){
+
+ $conditional_fields = $this->get_conditional_logic_fields();
+
+ $value_input = '_gaddon_setting_' . esc_attr( $setting_name_root ) . '_value';
+ $object_type = "simple_condition_{$setting_name_root}";
+
+ $str = $this->settings_select(array(
+ 'name' => "{$setting_name_root}_field_id",
+ 'type' => 'select',
+ 'choices' => $conditional_fields,
+ 'class' => 'optin_select',
+ 'onchange' => "jQuery('#" . esc_attr( $setting_name_root ) . "_container').html(GetRuleValues('{$object_type}', 0, jQuery(this).val(), '', '{$value_input}'));"
+ ), false);
+
+ $str .= $this->settings_select(array(
+ 'name' => "{$setting_name_root}_operator",
+ 'type' => 'select',
+ 'onchange' => "SetRuleProperty('{$object_type}', 0, 'operator', jQuery(this).val()); jQuery('#" . esc_attr( $setting_name_root ) . "_container').html(GetRuleValues('{$object_type}', 0, jQuery('#{$setting_name_root}_field_id').val(), '', '{$value_input}'));",
+ 'choices' => array(
+ array(
+ 'value' => 'is',
+ 'label' => __( 'is', 'gravityforms' ),
+ ),
+ array(
+ 'value' => 'isnot',
+ 'label' => __( 'is not', 'gravityforms' ),
+ ),
+ array(
+ 'value' => '>',
+ 'label' => __( 'greater than', 'gravityforms' ),
+ ),
+ array(
+ 'value' => '<',
+ 'label' => __( 'less than', 'gravityforms' ),
+ ),
+ array(
+ 'value' => 'contains',
+ 'label' => __( 'contains', 'gravityforms' ),
+ ),
+ array(
+ 'value' => 'starts_with',
+ 'label' => __( 'starts with', 'gravityforms' ),
+ ),
+ array(
+ 'value' => 'ends_with',
+ 'label' => __( 'ends with', 'gravityforms' ),
+ ),
+ ),
+
+ ), false);
+
+ $str .= " ";
+
+ $field_id = $this->get_setting( "{$setting_name_root}_field_id" );
+
+ $value = $this->get_setting( "{$setting_name_root}_value" );
+ $operator = $this->get_setting( "{$setting_name_root}_operator" );
+ if ( empty( $operator ) ){
+ $operator = 'is';
+ }
+
+ $field_id_attribute = ! empty( $field_id ) ? $field_id : 'jQuery("#' . esc_attr( $setting_name_root ) . '_field_id").val()';
+
+ $str .= "";
+
+ return $str;
+ }
+
+ /**
+ * Parses the properties of the $field meta array and returns a set of HTML attributes to be added to the HTML element.
+ *
+ * @param array $field - current field meta to be parsed.
+ * @param array $default - default set of properties. Will be appended to the properties specified in the $field array
+ *
+ * @return array - resulting HTML attributes ready to be included in the HTML element.
+ */
+ protected function get_field_attributes( $field, $default = array() ) {
+
+ // each nonstandard property will be extracted from the $props array so it is not auto-output in the field HTML
+ $no_output_props = apply_filters(
+ 'gaddon_no_output_field_properties',
+ array(
+ 'default_value', 'label', 'choices', 'feedback_callback', 'checked', 'checkbox_label', 'value', 'type',
+ 'validation_callback', 'required', 'hidden', 'tooltip', 'dependency', 'messages', 'name',
+ ), $field
+ );
+
+ $default_props = array(
+ 'class' => '', // will default to gaddon-setting
+ 'default_value' => '', // default value that should be selected or entered for the field
+ );
+
+ // Property switch case
+ switch ( $field['type'] ) {
+ case 'select':
+ $default_props['choices'] = array();
+ break;
+ case 'checkbox':
+ $default_props['checked'] = false;
+ $default_props['checkbox_label'] = '';
+ $default_props['choices'] = array();
+ break;
+ case 'text':
+ default:
+ break;
+ }
+
+ $props = wp_parse_args( $field, $default_props );
+ $props['id'] = rgempty( 'id', $props ) ? rgar( $props, 'name' ) : rgar( $props, 'id' );
+ $props['class'] = trim( "{$props['class']} gaddon-setting gaddon-{$props['type']}" );
+
+ // extract no-output properties from $props array so they are not auto-output in the field HTML
+ foreach ( $no_output_props as $prop ) {
+ if ( isset( $props[ $prop ] ) ) {
+ ${$prop} = $props[ $prop ];
+ unset( $props[ $prop ] );
+ }
+ }
+
+ //adding default attributes
+ foreach ( $default as $key => $value ) {
+ if ( isset( $props[ $key ] ) )
+ $props[ $key ] = $value . $props[ $key ];
+ else
+ $props[ $key ] = $value;
+ }
+
+ // get an array of property strings, example: name='myFieldName'
+ $prop_strings = array();
+ foreach ( $props as $prop => $value ) {
+ $prop_strings[ $prop ] = "{$prop}='" . esc_attr( $value ) . "'";
+ }
+
+ return $prop_strings;
+ }
+
+ /**
+ * Parses the properties of the $choice meta array and returns a set of HTML attributes to be added to the HTML element.
+ *
+ * @param array $choice - current choice meta to be parsed.
+ * @param array $field_attributes - current field's attributes.
+ *
+ * @return array - resulting HTML attributes ready to be included in the HTML element.
+ */
+ protected function get_choice_attributes( $choice, $field_attributes, $default_choice_attributes = array() ){
+ $choice_attributes = $field_attributes;
+ foreach ( $choice as $prop => $val ) {
+ $no_output_choice_attributes = array(
+ 'default_value', 'label', 'checked', 'value', 'type',
+ 'validation_callback', 'required', 'tooltip',
+ );
+ if ( in_array( $prop, $no_output_choice_attributes ) || is_array( $val ) )
+ unset( $choice_attributes[ $prop ] );
+ else
+ $choice_attributes[ $prop ] = "{$prop}='" . esc_attr( $val ) . "'";
+ }
+
+ //Adding default attributes. Either creating a new attribute or pre-pending to an existing one.
+ foreach ( $default_choice_attributes as $default_attr_name => $default_attr_value ){
+
+ if ( isset( $choice_attributes[ $default_attr_name ] ) ){
+ $choice_attributes[ $default_attr_name ] = $this->prepend_attribute( $default_attr_name, $default_attr_value, $choice_attributes[ $default_attr_name ] );
+ }
+ else {
+ $choice_attributes[ $default_attr_name ] = "{$default_attr_name}='" . esc_attr( $default_attr_value ) . "'";
+ }
+ }
+
+ return $choice_attributes;
+ }
+
+ /***
+ * @param $name - The name of the attribute to be added
+ * @param $attribute - The attribute value to be added
+ * @param $current_attribute - The full string containing the current attribute value
+ * @return mixed - The new attribute string with the new value added to the beginning of the list
+ */
+ protected function prepend_attribute( $name, $attribute, $current_attribute ){
+ return str_replace( "{$name}='", "{$name}='{$attribute}", $current_attribute );
+ }
+
+ /**
+ * Validates settings fields.
+ * Validates that all fields are valid. Fields can be invalid when they are blank and marked as required or if it fails a custom validation check.
+ * To specify a custom validation, use the 'validation_callback' field meta property and implement the validation function with the custom logic.
+ *
+ * @param $fields - A list of all fields from the field meta configuration
+ * @param $settings - A list of submitted settings values
+ *
+ * @return bool - Returns true if all fields have passed validation, and false otherwise.
+ */
+ protected function validate_settings( $fields, $settings ) {
+
+ foreach ( $fields as $section ) {
+
+ if ( ! $this->setting_dependency_met( rgar( $section, 'dependency' ) ) )
+ continue;
+
+ foreach ( $section['fields'] as $field ) {
+
+ if ( ! $this->setting_dependency_met( rgar( $field, 'dependency' ) ) )
+ continue;
+
+ $field_setting = rgar( $settings, rgar( $field, 'name' ) );
+
+ if ( is_callable( rgar( $field, 'validation_callback' ) ) ) {
+ call_user_func( rgar( $field, 'validation_callback' ), $field, $field_setting );
+ continue;
+ }
+
+ switch ( $field['type'] ) {
+ case 'field_map' :
+
+ $this->validate_field_map_settings( $field, $settings );
+
+ break;
+
+ case 'checkbox' :
+
+ $this->validate_checkbox_settings( $field, $settings );
+
+ break;
+
+ default :
+
+ if ( rgar( $field, 'required' ) && rgblank( $field_setting ) ) {
+ $this->set_field_error( $field, rgar( $field, 'error_message' ) );
+ }
+
+ break;
+ }
+ }
+ }
+
+ $field_errors = $this->get_field_errors();
+ $is_valid = empty( $field_errors );
+
+ return $is_valid;
+ }
+
+ protected function validate_checkbox_settings( $field, $settings ) {
+
+ if ( ! rgar( $field, 'required' ) )
+ return;
+
+ if ( ! is_array( rgar( $field, 'choices' ) ) )
+ return;
+
+ foreach ( $field['choices'] as $choice ) {
+ $choice_setting = rgar( $settings, rgar( $choice, 'name' ) );
+ if ( ! empty( $choice_setting ) )
+ return;
+ }
+
+ $this->set_field_error( $field, rgar( $field, 'error_message' ) );
+ }
+
+ protected function validate_field_map_settings( $field, $settings ) {
+
+ $field_map = rgar( $field, 'field_map' );
+
+ if ( empty( $field_map ) )
+ return;
+
+ foreach ( $field_map as $child_field ) {
+
+ if ( ! $this->setting_dependency_met( rgar( $child_field, 'dependency' ) ) )
+ continue;
+
+ $child_field['name'] = $this->get_mapped_field_name( $field, $child_field['name'] );
+ $setting_value = rgar( $settings, $child_field['name'] );
+
+ if ( rgar( $child_field, 'required' ) && rgblank( $setting_value ) ) {
+ $this->set_field_error( $child_field );
+ } else if ( rgar( $child_field, 'validation_callback' ) && is_callable( rgar( $child_field, 'validation_callback' ) ) ) {
+ call_user_func( rgar( $child_field, 'validation_callback' ), $child_field, $field );
+ }
+ }
+
+ }
+
+ /**
+ * Sets the validation error message
+ * Sets the error message to be displayed when a field fails validation.
+ * When implementing a custom validation callback function, use this function to specify the error message to be displayed.
+ *
+ * @param array $field - The current field meta
+ * @param string $error_message - The error message to be displayed
+ */
+ protected function set_field_error( $field, $error_message = '' ) {
+
+ // set default error message if none passed
+ if ( ! $error_message )
+ $error_message = __( 'This field is required.', 'gravityforms' );
+
+ $this->_setting_field_errors[ $field['name'] ] = $error_message;
+ }
+
+ /**
+ * Gets the validation errors for a field.
+ * Returns validation errors associated with the specified field or a list of all validation messages (if a field isn't specified)
+ *
+ * @param array|boolean $field - Optional. The field meta. When specified, errors for this field will be returned
+ *
+ * @return mixed - If a field is specified, a string containing the error message will be returned. Otherwise, an array of all errors will be returned
+ */
+ protected function get_field_errors( $field = false ) {
+
+ if ( ! $field )
+ return $this->_setting_field_errors;
+
+ return isset( $this->_setting_field_errors[ $field['name'] ] ) ? $this->_setting_field_errors[ $field['name'] ] : array();
+ }
+
+ /**
+ * Gets the invalid field icon
+ * Returns the markup for an alert icon to indicate and highlight invalid fields.
+ *
+ * @param array $field - The field meta.
+ *
+ * @return string - The full markup for the icon
+ */
+ protected function get_error_icon( $field ) {
+
+ $error = $this->get_field_errors( $field );
+
+ return '
';
- }
-
- /**
- * Gets the required indicator
- * Gets the markup of the required indicator symbol to highlight fields that are required
- * @param $field - The field meta.
- * @return string - Returns markup of the required indicator symbol
- */
- protected function get_required_indicator( $field ) {
- return '* ';
- }
-
- /**
- * Checks if the specified field failed validation
- *
- * @param $field - The field meta to be checked
- * @return bool|mixed - Returns a validation error string if the field has failed validation. Otherwise returns false
- */
- protected function field_failed_validation( $field ) {
- $field_error = $this->get_field_errors( $field );
- return !empty( $field_error ) ? $field_error : false;
- }
-
- protected function add_field_before( $name, $fields, $settings ) {
- return $this->add_field( $name, $fields, $settings, 'before' );
- }
-
- protected function add_field_after( $name, $fields, $settings ) {
- return $this->add_field( $name, $fields, $settings, 'after' );
- }
-
- protected function add_field( $name, $fields, $settings, $pos ) {
-
- if( rgar( $fields, 'name' ) )
- $fields = array( $fields );
-
- $pos_mod = $pos == 'before' ? 0 : 1;
-
- foreach( $settings as &$section ) {
- for( $i = 0; $i < count( $section['fields'] ); $i++ ) {
- if( $section['fields'][$i]['name'] == $name ) {
- array_splice( $section['fields'], $i + $pos_mod, 0, $fields );
- break 2;
- }
- }
- }
-
- return $settings;
- }
-
- protected function remove_field( $name, $settings ) {
-
- foreach( $settings as &$section ) {
- for( $i = 0; $i < count( $section['fields'] ); $i++ ) {
- if( $section['fields'][$i]['name'] == $name ) {
- array_splice( $section['fields'], $i, 1 );
- break 2;
- }
- }
- }
-
- return $settings;
- }
-
- protected function replace_field( $name, $fields, $settings ) {
-
- if( rgar( $fields, 'name' ) )
- $fields = array( $fields );
-
- foreach( $settings as &$section ) {
- for( $i = 0; $i < count( $section['fields'] ); $i++ ) {
- if( $section['fields'][$i]['name'] == $name ) {
- array_splice( $section['fields'], $i, 1, $fields );
- break 2;
- }
- }
- }
-
- return $settings;
-
- }
-
- protected function get_field( $name, $settings ) {
- foreach( $settings as $section ) {
- for( $i = 0; $i < count( $section['fields'] ); $i++ ) {
- if( $section['fields'][$i]['name'] == $name )
- return $section['fields'][$i];
- }
- }
- return false;
- }
-
- public function build_choices( $key_value_pairs ) {
-
- $choices = array();
-
- if( ! is_array( $key_value_pairs ) )
- return $choices;
-
- $first_key = key( $key_value_pairs );
- $is_numeric = is_int( $first_key ) && $first_key === 0;
-
- foreach( $key_value_pairs as $value => $label ) {
- if( $is_numeric )
- $value = $label;
- $choices[] = array( 'value' => $value, 'label' => $label );
- }
-
- return $choices;
- }
-
- //-------------- Form settings ---------------------------------------------------
-
- /**
- * Initializes form settings page
- * Hooks up the required scripts and actions for the Form Settings page
- */
- protected function form_settings_init() {
- $view = rgget("view");
- $subview = rgget("subview");
- if($this->current_user_can_any($this->_capabilities_form_settings)){
- add_action('gform_form_settings_menu', array($this, 'add_form_settings_menu'), 10, 2);
- }
-
- if (rgget("page") == "gf_edit_forms" && $view == "settings" && $subview == $this->_slug && $this->current_user_can_any($this->_capabilities_form_settings)) {
- require_once(GFCommon::get_base_path() . "/tooltips.php");
- add_action("gform_form_settings_page_" . $this->_slug, array($this, 'form_settings_page'));
- }
- }
-
- /**
- * Initializes plugin settings page
- * Hooks up the required scripts and actions for the Plugin Settings page
- */
- protected function plugin_page_init(){
-
- if($this->current_user_can_any($this->_capabilities_plugin_page)){
- //creates the subnav left menu
- add_filter("gform_addon_navigation", array($this, 'create_plugin_page_menu'));
- }
-
- }
-
- /**
- * Creates plugin page menu item
- * Target of gform_addon_navigation filter. Creates a menu item in the left nav, linking to the plugin page
- *
- * @param $menus - Current list of menu items
- * @return array - Returns a new list of menu items
- */
- public function create_plugin_page_menu($menus){
-
- $menus[] = array("name" => $this->_slug, "label" => $this->get_short_title(), "callback" => array($this, "plugin_page_container"), "permission" => $this->_capabilities_plugin_page);
-
- return $menus;
- }
-
- /**
- * Renders the form settings page.
- *
- * Not intended to be overridden or called directly by Add-Ons.
- * Sets up the form settings page.
- *
- * @ignore
- */
- public function form_settings_page() {
-
- GFFormSettings::page_header($this->_title);
- ?>
-
-
-
-
-
- is_save_postback() ){
-
- // store a copy of the previous settings for cases where action whould only happen if value has changed
- $this->set_previous_settings( $this->get_form_settings( $form ) );
-
- $settings = $this->get_posted_settings();
- $sections = $this->form_settings_fields($form);
-
- $is_valid = $this->validate_settings( $sections, $settings );
- $result = false;
-
- if( $is_valid )
- $result = $this->save_form_settings($form, $settings);
-
- if( $result ) {
- GFCommon::add_message( $this->get_save_success_message($sections) );
- } else {
- GFCommon::add_error_message( $this->get_save_error_message($sections) );
- }
-
- return $result;
- }
-
- }
-
- /***
- * Saves form settings to form object
- *
- * @param array $form
- * @param array $settings
- * @return true|false True on success or false on error
- */
- public function save_form_settings($form, $settings){
- $form[$this->_slug] = $settings;
- $result = GFFormsModel::update_form_meta($form["id"], $form);
- return ! (false === $result);
- }
-
- /**
- * Checks whether the current Add-On has a form settings page.
- *
- * @return bool
- */
- private function has_form_settings_page() {
- return $this->method_is_overridden('form_settings_fields') || $this->method_is_overridden('form_settings');
- }
-
- /**
- * Custom form settings page
- * Override this function to implement a complete custom form settings page.
- * Before overriding this function, consider using the form_settings_fields() and specifying your field meta.
- */
- protected function form_settings($form){}
-
- /**
- * Custom form settings title
- * Override this function to display a custom title on the Form Settings Page.
- * By default, the first section in the configuration done in form_settings_fields() will be used as the page title.
- * Use this function to override that behavior and add a custom page title.
- */
- protected function form_settings_page_title(){
- return "";
- }
-
- /**
- * Override this function to customize the form settings icon
- */
- protected function form_settings_icon(){
- return "";
- }
-
- /**
- * Checks whether the current Add-On has a plugin page.
- *
- * @return bool
- */
- private function has_plugin_page() {
- return $this->method_is_overridden('plugin_page');
- }
-
- /**
- * Override this function to create a custom plugin page
- */
- protected function plugin_page(){
- }
-
- /**
- * Override this function to customize the plugin page icon
- */
- protected function plugin_page_icon(){
- return "";
- }
-
- /**
- * Override this function to customize the plugin page title
- */
- protected function plugin_page_title(){
- return $this->_title;
- }
-
- /**
- * Plugin page container
- * Target of the plugin menu left nav icon. Displays the outer plugin page markup and calls plugin_page() to render the actual page.
- * Override plugin_page() in order to provide a custom plugin page
- */
- public function plugin_page_container(){
- ?>
-
- plugin_page_icon();
- if(!empty($icon)){
- ?>
-
-
-
-
plugin_page_title() ?>
- plugin_page();
- ?>
-
- has_app_settings() || $this->method_is_overridden('get_app_menu_items');
- }
-
- /**
- * Creates a top level app menu. Adds the app settings page automatically if it's configured.
- * Target of the WordPress admin_menu action.
- * Not intended to be overridden or called directly by add-ons.
- */
- public function create_app_menu() {
-
- $has_full_access = current_user_can("gform_full_access");
- $min_cap = GFCommon::current_user_can_which($this->_capabilities_app_menu);
- if (empty($min_cap))
- $min_cap = "gform_full_access";
-
- $menu_items = $this->get_app_menu_items();
-
- $addon_menus = array();
-
- $addon_menus = apply_filters('gform_addon_app_navigation_' . $this->_slug, $addon_menus);
-
- $parent_menu = self::get_parent_menu($menu_items, $addon_menus);
-
- if(empty($parent_menu)){
- return;
- }
-
- // Add a top-level left nav
- $callback = isset($parent_menu["callback"]) ? $parent_menu["callback"] : array($this, "app_tab_page");
- $this->app_hook_suffix = add_menu_page($this->get_short_title(), $this->get_short_title(), $has_full_access ? "gform_full_access" : $min_cap, $parent_menu["name"], $callback, $this->get_app_menu_icon(), apply_filters('gform_app_menu_position_', "16.10"));
-
- add_action("load-$this->app_hook_suffix", array($this, "load_screen_options"));
-
- // Adding submenu pages
- foreach($menu_items as $menu_item){
- $callback = isset($menu_item["callback"]) ? $menu_item["callback"] : array($this, "app_tab_page");
- add_submenu_page($parent_menu["name"], $menu_item["label"], $menu_item["label"], $has_full_access || empty($menu_item["permission"]) ? "gform_full_access" : $menu_item["permission"], $menu_item["name"], $callback);
- }
-
- if (is_array($addon_menus)) {
- foreach ($addon_menus as $addon_menu){
- add_submenu_page($parent_menu["name"], $addon_menu["label"], $addon_menu["label"], $has_full_access ? "gform_full_access" : $addon_menu["permission"], $addon_menu["name"], $addon_menu["callback"]);
- }
- }
-
- if($this->has_app_settings()){
- add_submenu_page($parent_menu["name"], __("Settings", "gravityforms"), __("Settings", "gravityforms"), $has_full_access ? "gform_full_access" : $this->_capabilities_app_settings, $this->_slug . "_settings", array($this, 'app_tab_page'));
- }
-
- }
-
- /**
- * Returns the parent menu item
- *
- * @param $menu_items
- * @param $addon_menus
- *
- * @return array|bool The parent menu araray or false if none
- */
- private function get_parent_menu($menu_items, $addon_menus) {
- $parent = false;
- if (GFCommon::current_user_can_any($this->_capabilities_app_menu)){
- foreach ($menu_items as $menu_item){
- if ($this->current_user_can_any($menu_item["permission"])) {
- $parent = $menu_item;
- break;
- }
- }
- } elseif (is_array($addon_menus) && sizeof($addon_menus) > 0) {
- foreach ($addon_menus as $addon_menu){
- if ($this->current_user_can_any($addon_menu["permission"])) {
- $parent = array("name" => $addon_menu["name"], "callback" => $addon_menu["callback"]);
- break;
- }
- }
- } elseif ($this->has_app_settings() && $this->current_user_can_any($this->_capabilities_app_settings)){
- $parent = array("name" => $this->_slug . "_settings", "callback" => array($this, "app_settings"));
- }
-
- return $parent;
- }
-
- /**
- * Override this function to create a top level app menu.
- *
- * e.g.
- * $menu_item["name"] = "gravitycontacts";
- * $menu_item["label"] = __("Contacts", "gravitycontacts");
- * $menu_item["permission"] = "gravitycontacts_view_contacts";
- * $menu_item["callback"] = array($this, "app_menu");
- *
- * @return array The array of menu items
- */
- protected function get_app_menu_items(){
- return array();
- }
-
-
- /**
- * Override this function to specify a custom icon for the top level app menu.
- * Accepts a dashicon class or a URL.
- *
- * @return string
- */
- protected function get_app_menu_icon(){ return ""; }
-
- /**
- * Override this function to load custom screen options.
- *
- * e.g.
- * $screen = get_current_screen();
- * if(!is_object($screen) || $screen->id != $this->app_hook_suffix)
- * return;
- *
- * if($this->is_contact_list_page()){
- * $args = array(
- * 'label' => __('Contacts per page', 'gravitycontacts'),
- * 'default' => 20,
- * 'option' => 'gcontacts_per_page'
- * );
- * add_screen_option( 'per_page', $args );
- */
- protected function load_screen_options(){ }
-
-
- /**
- * Handles the rendering of app menu items that implement the tabs UI.
- *
- * Not intended to be overridden or called directly by add-ons.
- */
- public function app_tab_page(){
- $page = rgget("page");
- $current_tab = rgget("view");
-
- if($page == $this->_slug . "_settings"){
-
- $tabs = $this->get_app_settings_tabs();
-
- } else {
-
- $menu_items = $this->get_app_menu_items();
-
- $current_menu_item = false;
- foreach($menu_items as $menu_item){
- if($menu_item["name"] == $page){
- $current_menu_item = $menu_item;
- break;
- }
- }
-
- if(empty($current_menu_item)){
- return;
- }
-
- if(empty($current_menu_item["tabs"])){
- return;
- }
-
- $tabs = $current_menu_item["tabs"];
- }
-
- if(empty($current_tab)){
- foreach($tabs as $tab){
- if(!isset($tab["permission"]) || $this->current_user_can_any($tab["permission"])){
- $current_tab = $tab["name"];
- break;
- }
- }
- }
-
- if(empty($current_tab)){
- wp_die(__("You don't have adequate permissions to view this page", "gravityforms"));
- }
-
- foreach($tabs as $tab){
- if($tab["name"] == $current_tab && isset($tab["callback"]) && is_callable($tab["callback"])){
- if(isset($tab["permission"]) && !$this->current_user_can_any($tab["permission"])){
- wp_die(__("You don't have adequate permissions to view this page", "gravityforms"));
- }
- $title = isset($tab["label"]) ? $tab['label'] : $tab['name'];
- $this->app_tab_page_header($tabs, $current_tab, $title, "");
- call_user_func($tab["callback"]);
- $this->app_tab_page_footer();
-
- return;
- }
- }
-
- $this->app_tab_page_header($tabs, $current_tab, $current_tab, "");
- $action_hook = "gform_addon_app_" . $page . "_" . str_replace(" ", "_", $current_tab);
- do_action($action_hook);
- $this->app_tab_page_footer();
-
- }
-
-
- /**
- * Returns the form settings for the Add-On
- *
- * @param $form
- * @return string
- */
- protected function get_form_settings($form) {
- return rgar( $form, $this->_slug );
- }
-
- /**
- * Add the form settings tab.
- *
- * Override this function to add the tab conditionally.
- *
- *
- * @param $tabs
- * @param $form_id
- * @return array
- */
- public function add_form_settings_menu($tabs, $form_id) {
-
- $tabs[] = array("name" => $this->_slug, "label" => $this->get_short_title(), "query" => array("fid" => null));
-
- return $tabs;
- }
-
- /**
- * Override this function to specify the settings fields to be rendered on the form settings page
- */
- protected function form_settings_fields($form){
- // should return an array of sections, each section contains a title, description and an array of fields
- return array();
- }
-
- //-------------- Plugin Settings ---------------------------------------------------
-
- protected function plugin_settings_init() {
- $subview = rgget("subview");
- RGForms::add_settings_page( array(
- 'name' => $this->_slug,
- 'tab_label' => $this->get_short_title(),
- 'handler' => array($this, 'plugin_settings_page')
- ) );
- if (rgget("page") == "gf_settings" && $subview == $this->_slug && $this->current_user_can_any($this->_capabilities_settings_page)) {
- require_once(GFCommon::get_base_path() . "/tooltips.php");
- }
-
- add_filter('plugin_action_links', array($this, 'plugin_settings_link'),10,2);
- }
-
- public function plugin_settings_link( $links, $file ) {
- if ( $file != $this->_path ){
- return $links;
- }
-
- array_unshift($links, '_slug . '">' . __( 'Settings', 'gravityforms' ) . ' ');
-
- return $links;
- }
-
- /**
- * Plugin settings page
- */
- public function plugin_settings_page() {
- $icon = $this->plugin_settings_icon();
- if (empty($icon)){
+ }
+
+ /**
+ * Gets the required indicator
+ * Gets the markup of the required indicator symbol to highlight fields that are required
+ *
+ * @param $field - The field meta.
+ *
+ * @return string - Returns markup of the required indicator symbol
+ */
+ protected function get_required_indicator( $field ) {
+ return '* ';
+ }
+
+ /**
+ * Checks if the specified field failed validation
+ *
+ * @param $field - The field meta to be checked
+ *
+ * @return bool|mixed - Returns a validation error string if the field has failed validation. Otherwise returns false
+ */
+ protected function field_failed_validation( $field ) {
+ $field_error = $this->get_field_errors( $field );
+
+ return ! empty( $field_error ) ? $field_error : false;
+ }
+
+ protected function add_field_before( $name, $fields, $settings ) {
+ return $this->add_field( $name, $fields, $settings, 'before' );
+ }
+
+ protected function add_field_after( $name, $fields, $settings ) {
+ return $this->add_field( $name, $fields, $settings, 'after' );
+ }
+
+ protected function add_field( $name, $fields, $settings, $pos ) {
+
+ if ( rgar( $fields, 'name' ) )
+ $fields = array( $fields );
+
+ $pos_mod = $pos == 'before' ? 0 : 1;
+
+ foreach ( $settings as &$section ) {
+ for ( $i = 0; $i < count( $section['fields'] ); $i ++ ) {
+ if ( $section['fields'][ $i ]['name'] == $name ) {
+ array_splice( $section['fields'], $i + $pos_mod, 0, $fields );
+ break 2;
+ }
+ }
+ }
+
+ return $settings;
+ }
+
+ protected function remove_field( $name, $settings ) {
+
+ foreach ( $settings as &$section ) {
+ for ( $i = 0; $i < count( $section['fields'] ); $i ++ ) {
+ if ( $section['fields'][ $i ]['name'] == $name ) {
+ array_splice( $section['fields'], $i, 1 );
+ break 2;
+ }
+ }
+ }
+
+ return $settings;
+ }
+
+ protected function replace_field( $name, $fields, $settings ) {
+
+ if ( rgar( $fields, 'name' ) )
+ $fields = array( $fields );
+
+ foreach ( $settings as &$section ) {
+ for ( $i = 0; $i < count( $section['fields'] ); $i ++ ) {
+ if ( $section['fields'][ $i ]['name'] == $name ) {
+ array_splice( $section['fields'], $i, 1, $fields );
+ break 2;
+ }
+ }
+ }
+
+ return $settings;
+
+ }
+
+ protected function get_field( $name, $settings ) {
+ foreach ( $settings as $section ) {
+ for ( $i = 0; $i < count( $section['fields'] ); $i ++ ) {
+ if ( $section['fields'][ $i ]['name'] == $name )
+ return $section['fields'][ $i ];
+ }
+ }
+
+ return false;
+ }
+
+ public function build_choices( $key_value_pairs ) {
+
+ $choices = array();
+
+ if ( ! is_array( $key_value_pairs ) )
+ return $choices;
+
+ $first_key = key( $key_value_pairs );
+ $is_numeric = is_int( $first_key ) && $first_key === 0;
+
+ foreach ( $key_value_pairs as $value => $label ) {
+ if ( $is_numeric )
+ $value = $label;
+ $choices[] = array( 'value' => $value, 'label' => $label );
+ }
+
+ return $choices;
+ }
+
+ //-------------- Form settings ---------------------------------------------------
+
+ /**
+ * Initializes form settings page
+ * Hooks up the required scripts and actions for the Form Settings page
+ */
+ protected function form_settings_init() {
+ $view = rgget( 'view' );
+ $subview = rgget( 'subview' );
+ if ( $this->current_user_can_any( $this->_capabilities_form_settings ) ) {
+ add_action( 'gform_form_settings_menu', array( $this, 'add_form_settings_menu' ), 10, 2 );
+ }
+
+ if ( rgget( 'page' ) == 'gf_edit_forms' && $view == 'settings' && $subview == $this->_slug && $this->current_user_can_any( $this->_capabilities_form_settings ) ) {
+ require_once( GFCommon::get_base_path() . '/tooltips.php' );
+ add_action( 'gform_form_settings_page_' . $this->_slug, array( $this, 'form_settings_page' ) );
+ }
+ }
+
+ /**
+ * Initializes plugin settings page
+ * Hooks up the required scripts and actions for the Plugin Settings page
+ */
+ protected function plugin_page_init() {
+
+ if ( $this->current_user_can_any( $this->_capabilities_plugin_page ) ) {
+ //creates the subnav left menu
+ add_filter( 'gform_addon_navigation', array( $this, 'create_plugin_page_menu' ) );
+ }
+
+ }
+
+ /**
+ * Creates plugin page menu item
+ * Target of gform_addon_navigation filter. Creates a menu item in the left nav, linking to the plugin page
+ *
+ * @param $menus - Current list of menu items
+ *
+ * @return array - Returns a new list of menu items
+ */
+ public function create_plugin_page_menu( $menus ) {
+
+ $menus[] = array( 'name' => $this->_slug, 'label' => $this->get_short_title(), 'callback' => array( $this, 'plugin_page_container' ), 'permission' => $this->_capabilities_plugin_page );
+
+ return $menus;
+ }
+
+ /**
+ * Renders the form settings page.
+ *
+ * Not intended to be overridden or called directly by Add-Ons.
+ * Sets up the form settings page.
+ *
+ * @ignore
+ */
+ public function form_settings_page() {
+
+ GFFormSettings::page_header( $this->_title );
+ ?>
+
+
+ get_current_form();
+
+ $form_id = $form['id'];
+ $form = apply_filters( "gform_admin_pre_render_{$form_id}", apply_filters( 'gform_admin_pre_render', $form ) );
+
+ if ( $this->method_is_overridden( 'form_settings' ) ) {
+
+ //enables plugins to override settings page by implementing a form_settings() function
+ $this->form_settings( $form );
+ } else {
+
+ //saves form settings if save button was pressed
+ $this->maybe_save_form_settings( $form );
+
+ //reads current form settings
+ $settings = $this->get_form_settings( $form );
+ $this->set_settings( $settings );
+
+ //reading addon fields
+ $sections = $this->form_settings_fields( $form );
+
+ GFCommon::display_admin_message();
+
+ $page_title = $this->form_settings_page_title();
+ if ( empty( $page_title ) ) {
+ $page_title = rgar( $sections[0], 'title' );
+
+ //using first section title as page title, so disable section title
+ $sections[0]['title'] = false;
+ }
+ $icon = $this->form_settings_icon();
+ if ( empty( $icon ) ) {
+ $icon = ' ';
+ }
+
+ ?>
+
+ render_settings( $sections );
+ }
+ ?>
+
+
+
+ is_save_postback() ) {
+
+ // store a copy of the previous settings for cases where action whould only happen if value has changed
+ $this->set_previous_settings( $this->get_form_settings( $form ) );
+
+ $settings = $this->get_posted_settings();
+ $sections = $this->form_settings_fields( $form );
+
+ $is_valid = $this->validate_settings( $sections, $settings );
+ $result = false;
+
+ if ( $is_valid )
+ $result = $this->save_form_settings( $form, $settings );
+
+ if ( $result ) {
+ GFCommon::add_message( $this->get_save_success_message( $sections ) );
+ } else {
+ GFCommon::add_error_message( $this->get_save_error_message( $sections ) );
+ }
+
+ return $result;
+ }
+
+ }
+
+ /***
+ * Saves form settings to form object
+ *
+ * @param array $form
+ * @param array $settings
+ *
+ * @return true|false True on success or false on error
+ */
+ public function save_form_settings( $form, $settings ) {
+ $form[ $this->_slug ] = $settings;
+ $result = GFFormsModel::update_form_meta( $form['id'], $form );
+
+ return ! ( false === $result );
+ }
+
+ /**
+ * Checks whether the current Add-On has a form settings page.
+ *
+ * @return bool
+ */
+ private function has_form_settings_page() {
+ return $this->method_is_overridden( 'form_settings_fields' ) || $this->method_is_overridden( 'form_settings' );
+ }
+
+ /**
+ * Custom form settings page
+ * Override this function to implement a complete custom form settings page.
+ * Before overriding this function, consider using the form_settings_fields() and specifying your field meta.
+ */
+ protected function form_settings( $form ) {
+ }
+
+ /**
+ * Custom form settings title
+ * Override this function to display a custom title on the Form Settings Page.
+ * By default, the first section in the configuration done in form_settings_fields() will be used as the page title.
+ * Use this function to override that behavior and add a custom page title.
+ */
+ protected function form_settings_page_title() {
+ return '';
+ }
+
+ /**
+ * Override this function to customize the form settings icon
+ */
+ protected function form_settings_icon() {
+ return '';
+ }
+
+ /**
+ * Checks whether the current Add-On has a plugin page.
+ *
+ * @return bool
+ */
+ private function has_plugin_page() {
+ return $this->method_is_overridden( 'plugin_page' );
+ }
+
+ /**
+ * Override this function to create a custom plugin page
+ */
+ protected function plugin_page() {
+ }
+
+ /**
+ * Override this function to customize the plugin page icon
+ */
+ protected function plugin_page_icon() {
+ return '';
+ }
+
+ /**
+ * Override this function to customize the plugin page title
+ */
+ protected function plugin_page_title() {
+ return $this->_title;
+ }
+
+ /**
+ * Plugin page container
+ * Target of the plugin menu left nav icon. Displays the outer plugin page markup and calls plugin_page() to render the actual page.
+ * Override plugin_page() in order to provide a custom plugin page
+ */
+ public function plugin_page_container() {
+ ?>
+
+ plugin_page_icon();
+ if ( ! empty( $icon ) ) {
+ ?>
+
+
+
+
plugin_page_title() ?>
+ plugin_page();
+ ?>
+
+ has_app_settings() || $this->method_is_overridden( 'get_app_menu_items' );
+ }
+
+ /**
+ * Creates a top level app menu. Adds the app settings page automatically if it's configured.
+ * Target of the WordPress admin_menu action.
+ * Not intended to be overridden or called directly by add-ons.
+ */
+ public function create_app_menu() {
+
+ $has_full_access = current_user_can( 'gform_full_access' );
+ $min_cap = GFCommon::current_user_can_which( $this->_capabilities_app_menu );
+ if ( empty( $min_cap ) )
+ $min_cap = 'gform_full_access';
+
+ $menu_items = $this->get_app_menu_items();
+
+ $addon_menus = array();
+
+ $addon_menus = apply_filters( 'gform_addon_app_navigation_' . $this->_slug, $addon_menus );
+
+ $parent_menu = self::get_parent_menu( $menu_items, $addon_menus );
+
+ if ( empty( $parent_menu ) ) {
+ return;
+ }
+
+ // Add a top-level left nav
+ $callback = isset( $parent_menu['callback'] ) ? $parent_menu['callback'] : array( $this, 'app_tab_page' );
+ $this->app_hook_suffix = add_menu_page( $this->get_short_title(), $this->get_short_title(), $has_full_access ? 'gform_full_access' : $min_cap, $parent_menu['name'], $callback, $this->get_app_menu_icon(), apply_filters( 'gform_app_menu_position_', '16.10' ) );
+
+ add_action( "load-$this->app_hook_suffix", array( $this, 'load_screen_options' ) );
+
+ // Adding submenu pages
+ foreach ( $menu_items as $menu_item ) {
+ $callback = isset( $menu_item['callback'] ) ? $menu_item['callback'] : array( $this, 'app_tab_page' );
+ add_submenu_page( $parent_menu['name'], $menu_item['label'], $menu_item['label'], $has_full_access || empty( $menu_item['permission'] ) ? 'gform_full_access' : $menu_item['permission'], $menu_item['name'], $callback );
+ }
+
+ if ( is_array( $addon_menus ) ) {
+ foreach ( $addon_menus as $addon_menu ) {
+ add_submenu_page( $parent_menu['name'], $addon_menu['label'], $addon_menu['label'], $has_full_access ? 'gform_full_access' : $addon_menu['permission'], $addon_menu['name'], $addon_menu['callback'] );
+ }
+ }
+
+ if ( $this->has_app_settings() ) {
+ add_submenu_page( $parent_menu['name'], __( 'Settings', 'gravityforms' ), __( 'Settings', 'gravityforms' ), $has_full_access ? 'gform_full_access' : $this->_capabilities_app_settings, $this->_slug . '_settings', array( $this, 'app_tab_page' ) );
+ }
+
+ }
+
+ /**
+ * Returns the parent menu item
+ *
+ * @param $menu_items
+ * @param $addon_menus
+ *
+ * @return array|bool The parent menu araray or false if none
+ */
+ private function get_parent_menu( $menu_items, $addon_menus ) {
+ $parent = false;
+ if ( GFCommon::current_user_can_any( $this->_capabilities_app_menu ) ) {
+ foreach ( $menu_items as $menu_item ) {
+ if ( $this->current_user_can_any( $menu_item['permission'] ) ) {
+ $parent = $menu_item;
+ break;
+ }
+ }
+ } elseif ( is_array( $addon_menus ) && sizeof( $addon_menus ) > 0 ) {
+ foreach ( $addon_menus as $addon_menu ) {
+ if ( $this->current_user_can_any( $addon_menu['permission'] ) ) {
+ $parent = array( 'name' => $addon_menu['name'], 'callback' => $addon_menu['callback'] );
+ break;
+ }
+ }
+ } elseif ( $this->has_app_settings() && $this->current_user_can_any( $this->_capabilities_app_settings ) ) {
+ $parent = array( 'name' => $this->_slug . '_settings', 'callback' => array( $this, 'app_settings' ) );
+ }
+
+ return $parent;
+ }
+
+ /**
+ * Override this function to create a top level app menu.
+ *
+ * e.g.
+ * $menu_item['name'] = 'gravitycontacts';
+ * $menu_item['label'] = __("Contacts", 'gravitycontacts');
+ * $menu_item['permission'] = 'gravitycontacts_view_contacts';
+ * $menu_item['callback'] = array($this, 'app_menu');
+ *
+ * @return array The array of menu items
+ */
+ protected function get_app_menu_items() {
+ return array();
+ }
+
+
+ /**
+ * Override this function to specify a custom icon for the top level app menu.
+ * Accepts a dashicon class or a URL.
+ *
+ * @return string
+ */
+ protected function get_app_menu_icon() {
+ return '';
+ }
+
+ /**
+ * Override this function to load custom screen options.
+ *
+ * e.g.
+ * $screen = get_current_screen();
+ * if(!is_object($screen) || $screen->id != $this->app_hook_suffix)
+ * return;
+ *
+ * if($this->is_contact_list_page()){
+ * $args = array(
+ * 'label' => __('Contacts per page', 'gravitycontacts'),
+ * 'default' => 20,
+ * 'option' => 'gcontacts_per_page'
+ * );
+ * add_screen_option( 'per_page', $args );
+ */
+ protected function load_screen_options() {
+ }
+
+
+ /**
+ * Handles the rendering of app menu items that implement the tabs UI.
+ *
+ * Not intended to be overridden or called directly by add-ons.
+ */
+ public function app_tab_page() {
+ $page = rgget( 'page' );
+ $current_tab = rgget( 'view' );
+
+ if ( $page == $this->_slug . '_settings' ) {
+
+ $tabs = $this->get_app_settings_tabs();
+
+ } else {
+
+ $menu_items = $this->get_app_menu_items();
+
+ $current_menu_item = false;
+ foreach ( $menu_items as $menu_item ) {
+ if ( $menu_item['name'] == $page ) {
+ $current_menu_item = $menu_item;
+ break;
+ }
+ }
+
+ if ( empty( $current_menu_item ) ) {
+ return;
+ }
+
+ if ( empty( $current_menu_item['tabs'] ) ) {
+ return;
+ }
+
+ $tabs = $current_menu_item['tabs'];
+ }
+
+ if ( empty( $current_tab ) ) {
+ foreach ( $tabs as $tab ) {
+ if ( ! isset( $tab['permission'] ) || $this->current_user_can_any( $tab['permission'] ) ) {
+ $current_tab = $tab['name'];
+ break;
+ }
+ }
+ }
+
+ if ( empty( $current_tab ) ) {
+ wp_die( __( "You don't have adequate permission to view this page", 'gravityforms' ) );
+ }
+
+ foreach ( $tabs as $tab ) {
+ if ( $tab['name'] == $current_tab && isset( $tab['callback'] ) && is_callable( $tab['callback'] ) ) {
+ if ( isset( $tab['permission'] ) && ! $this->current_user_can_any( $tab['permission'] ) ) {
+ wp_die( __( "You don't have adequate permission to view this page", 'gravityforms' ) );
+ }
+ $title = isset( $tab['label'] ) ? $tab['label'] : $tab['name'];
+ $this->app_tab_page_header( $tabs, $current_tab, $title, '' );
+ call_user_func( $tab['callback'] );
+ $this->app_tab_page_footer();
+
+ return;
+ }
+ }
+
+ $this->app_tab_page_header( $tabs, $current_tab, $current_tab, '' );
+ $action_hook = 'gform_addon_app_' . $page . '_' . str_replace( ' ', '_', $current_tab );
+ do_action( $action_hook );
+ $this->app_tab_page_footer();
+
+ }
+
+
+ /**
+ * Returns the form settings for the Add-On
+ *
+ * @param $form
+ *
+ * @return string
+ */
+ protected function get_form_settings( $form ) {
+ return rgar( $form, $this->_slug );
+ }
+
+ /**
+ * Add the form settings tab.
+ *
+ * Override this function to add the tab conditionally.
+ *
+ *
+ * @param $tabs
+ * @param $form_id
+ *
+ * @return array
+ */
+ public function add_form_settings_menu( $tabs, $form_id ) {
+
+ $tabs[] = array( 'name' => $this->_slug, 'label' => $this->get_short_title(), 'query' => array( 'fid' => null ) );
+
+ return $tabs;
+ }
+
+ /**
+ * Override this function to specify the settings fields to be rendered on the form settings page
+ */
+ protected function form_settings_fields( $form ) {
+ // should return an array of sections, each section contains a title, description and an array of fields
+ return array();
+ }
+
+ //-------------- Plugin Settings ---------------------------------------------------
+
+ protected function plugin_settings_init() {
+ $subview = rgget( 'subview' );
+ RGForms::add_settings_page(
+ array(
+ 'name' => $this->_slug,
+ 'tab_label' => $this->get_short_title(),
+ 'title' => $this->plugin_settings_title(),
+ 'handler' => array( $this, 'plugin_settings_page' ),
+ )
+ );
+ if ( rgget( 'page' ) == 'gf_settings' && $subview == $this->_slug && $this->current_user_can_any( $this->_capabilities_settings_page ) ) {
+ require_once( GFCommon::get_base_path() . '/tooltips.php' );
+ }
+
+ add_filter( 'plugin_action_links', array( $this, 'plugin_settings_link' ), 10, 2 );
+
+ }
+
+ public function plugin_settings_link( $links, $file ) {
+ if ( $file != $this->_path ) {
+ return $links;
+ }
+
+ array_unshift( $links, '' . __( 'Settings', 'gravityforms' ) . ' ' );
+
+ return $links;
+ }
+
+ /**
+ * Plugin settings page
+ */
+ public function plugin_settings_page() {
+ $icon = $this->plugin_settings_icon();
+ if ( empty( $icon ) ) {
+ $icon = ' ';
+ }
+ ?>
+
+ plugin_settings_title() ?>
+
+ has_deprecated_elements() ) : ?>
+
+
+
+
+
+ method_is_overridden( 'plugin_settings' ) ) {
+ //enables plugins to override settings page by implementing a plugin_settings() function
+ $this->plugin_settings();
+ } else if ( $this->maybe_uninstall() ) {
+ ?>
+
+ _title, "
", ' ' ), 'gravityforms' ) ?>
+
+ maybe_save_plugin_settings();
+
+ //reads main addon settings
+ $settings = $this->get_plugin_settings();
+ $this->set_settings( $settings );
+
+ //reading addon fields
+ $sections = $this->plugin_settings_fields();
+
+ GFCommon::display_admin_message();
+
+ //rendering settings based on fields and current settings
+ $this->render_settings( $sections, $settings );
+
+ //renders uninstall section
+ $this->render_uninstall();
+
+ }
+
+ }
+
+ public function plugin_settings_title() {
+ return sprintf( __( "%s Settings", "gravityforms" ), $this->get_short_title() );
+ }
+
+ protected function plugin_settings_icon() {
+ return '';
+ }
+
+ /**
+ * Override this function to add a custom settings page.
+ */
+ protected function plugin_settings() {
+ }
+
+ /**
+ * Checks whether the current Add-On has a settings page.
+ *
+ * @return bool
+ */
+ public function has_plugin_settings_page() {
+ return $this->method_is_overridden( 'plugin_settings_fields' ) || $this->method_is_overridden( 'plugin_settings' );
+ }
+
+ /**
+ * Returns the currently saved plugin settings
+ * @return mixed
+ */
+ protected function get_plugin_settings() {
+ return get_option( 'gravityformsaddon_' . $this->_slug . '_settings' );
+ }
+
+ /**
+ * Get plugin setting
+ * Returns the plugin setting specified by the $setting_name parameter
+ *
+ * @param string $setting_name - Plugin setting to be returned
+ *
+ * @return mixed - Returns the specified plugin setting or null if the setting doesn't exist
+ */
+ protected function get_plugin_setting( $setting_name ) {
+ $settings = $this->get_plugin_settings();
+
+ return isset( $settings[ $setting_name ] ) ? $settings[ $setting_name ] : null;
+ }
+
+ /**
+ * Updates plugin settings with the provided settings
+ *
+ * @param array $settings - Plugin settings to be saved
+ */
+ protected function update_plugin_settings( $settings ) {
+ update_option( 'gravityformsaddon_' . $this->_slug . '_settings', $settings );
+ }
+
+ /**
+ * Saves the plugin settings if the submit button was pressed
+ *
+ */
+ protected function maybe_save_plugin_settings() {
+
+ if ( $this->is_save_postback() ) {
+
+ // store a copy of the previous settings for cases where action whould only happen if value has changed
+ $this->set_previous_settings( $this->get_plugin_settings() );
+
+ $settings = $this->get_posted_settings();
+ $sections = $this->plugin_settings_fields();
+ $is_valid = $this->validate_settings( $sections, $settings );
+
+ if ( $is_valid ) {
+ $this->update_plugin_settings( $settings );
+ GFCommon::add_message( $this->get_save_success_message( $sections ) );
+ } else {
+ GFCommon::add_error_message( $this->get_save_error_message( $sections ) );
+ }
+ }
+
+ }
+
+ /**
+ * Override this function to specify the settings fields to be rendered on the plugin settings page
+ * @return array
+ */
+ public function plugin_settings_fields() {
+ // should return an array of sections, each section contains a title, description and an array of fields
+ return array();
+ }
+
+ //-------------- App Settings ---------------------------------------------------
+
+ /**
+ * Returns the tabs for the settings app menu item
+ *
+ * Not intended to be overridden or called directly by add-ons.
+ *
+ * @return array|mixed|void
+ */
+ public function get_app_settings_tabs() {
+
+ //build left side options, always have app Settings first and Uninstall last, put add-ons in the middle
+
+ $setting_tabs = array( array( 'name' => 'settings', 'label' => __( 'Settings', 'gravityforms' ), 'callback' => array( $this, 'app_settings_tab' ) ) );
+
+ $setting_tabs = apply_filters( 'gform_addon_app_settings_menu_' . $this->_slug, $setting_tabs );
+
+ if ( $this->current_user_can_any( $this->_capabilities_uninstall ) ) {
+ $setting_tabs[] = array( 'name' => 'uninstall', 'label' => __( 'Uninstall', 'gravityforms' ), 'callback' => array( $this, 'app_settings_uninstall_tab' ) );
+ }
+
+ ksort( $setting_tabs, SORT_NUMERIC );
+
+ return $setting_tabs;
+ }
+
+ /**
+ * Renders the app settings uninstall tab.
+ *
+ * Not intended to be overridden or called directly by add-ons.
+ */
+ protected function app_settings_uninstall_tab() {
+
+ if ( $this->maybe_uninstall() ) {
+ ?>
+
+ _title, "
", ' ' ), 'gravityforms' ) ?>
+
+
+