PageRenderTime 75ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/plugins/gravityforms/gravityforms.php

https://github.com/ttimsmith/Anythin-Goes
PHP | 2254 lines | 1621 code | 450 blank | 183 comment | 333 complexity | 77872b40552880aeab1b273d1878e4f7 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. Plugin Name: Gravity Forms
  4. Plugin URI: http://www.gravityforms.com
  5. Description: Easily create web forms and manage form entries within the WordPress admin.
  6. Version: 1.7.2
  7. Author: rocketgenius
  8. Author URI: http://www.rocketgenius.com
  9. ------------------------------------------------------------------------
  10. Copyright 2009-2013 Rocketgenius Inc.
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. //------------------------------------------------------------------------------------------------------------------
  24. //---------- Gravity Forms License Key -----------------------------------------------------------------------------
  25. //------------------------------------------------------------------------------------------------------------------
  26. //If you hardcode a Gravity Forms License Key here, it will automatically populate on activation.
  27. $gf_license_key = "";
  28. //-- OR ---//
  29. //You can also add the Gravity Forms license key to your wp-config.php file to automatically populate on activation
  30. //Add the code in the comment below to your wp-config.php to do so:
  31. //define('GF_LICENSE_KEY','YOUR_KEY_GOES_HERE');
  32. //------------------------------------------------------------------------------------------------------------------
  33. //------------------------------------------------------------------------------------------------------------------
  34. //---------- reCAPTCHA Keys -----------------------------------------------------------------------------
  35. //------------------------------------------------------------------------------------------------------------------
  36. //If you hardcode your reCAPTCHA Keys here, it will automatically populate on activation.
  37. $gf_recaptcha_private_key = "";
  38. $gf_recaptcha_public_key = "";
  39. //-- OR ---//
  40. //You can also add the reCAPTCHA keys to your wp-config.php file to automatically populate on activation
  41. //Add the two lines of code in the comment below to your wp-config.php to do so:
  42. //define('GF_RECAPTCHA_PRIVATE_KEY','YOUR_PRIVATE_KEY_GOES_HERE');
  43. //define('GF_RECAPTCHA_PUBLIC_KEY','YOUR_PUBLIC_KEY_GOES_HERE');
  44. //------------------------------------------------------------------------------------------------------------------
  45. if(!defined("RG_CURRENT_PAGE"))
  46. define("RG_CURRENT_PAGE", basename($_SERVER['PHP_SELF']));
  47. if(!defined("IS_ADMIN")){
  48. define("IS_ADMIN", is_admin());
  49. }
  50. define("RG_CURRENT_VIEW", RGForms::get("view"));
  51. define("GF_MIN_WP_VERSION", '3.2');
  52. define("GF_SUPPORTED_WP_VERSION", version_compare(get_bloginfo("version"), GF_MIN_WP_VERSION, '>='));
  53. if(!defined("GRAVITY_MANAGER_URL"))
  54. define("GRAVITY_MANAGER_URL", "http://www.gravityhelp.com/wp-content/plugins/gravitymanager");
  55. //initializing translations
  56. load_plugin_textdomain( 'gravityforms', false, '/gravityforms/languages' );
  57. require_once(WP_PLUGIN_DIR . "/" . basename(dirname(__FILE__)) . "/common.php");
  58. require_once(WP_PLUGIN_DIR . "/" . basename(dirname(__FILE__)) . "/forms_model.php");
  59. require_once(WP_PLUGIN_DIR . "/" . basename(dirname(__FILE__)) . "/widget.php");
  60. add_action('init', array('RGForms', 'init'));
  61. add_action('wp', array('RGForms', 'maybe_process_form'), 9);
  62. add_action('wp', array('RGForms', 'process_exterior_pages'));
  63. add_filter('user_has_cap', array("RGForms", "user_has_cap"), 10, 3);
  64. //Hooks for no-conflict functionality
  65. if(is_admin() && (RGForms::is_gravity_page() || RGForms::is_gravity_ajax_action())){
  66. add_action("wp_print_scripts", array("RGForms", "no_conflict_mode_script"), 1000);
  67. add_action("admin_print_footer_scripts", array("RGForms", "no_conflict_mode_script"), 9);
  68. add_action("wp_print_styles", array("RGForms", "no_conflict_mode_style"), 1000);
  69. add_action("admin_print_styles", array("RGForms", "no_conflict_mode_style"), 1);
  70. add_action("admin_print_footer_scripts", array("RGForms", "no_conflict_mode_style"), 1);
  71. add_action("admin_footer", array("RGForms", "no_conflict_mode_style"), 1);
  72. }
  73. class GFForms {
  74. public static function has_members_plugin(){
  75. return function_exists( 'members_get_capabilities' );
  76. }
  77. //Plugin starting point. Will load appropriate files
  78. public static function init(){
  79. add_filter("gform_logging_supported", array("RGForms", "set_logging_supported"));
  80. self::register_scripts();
  81. if(IS_ADMIN){
  82. global $current_user;
  83. //Members plugin integration. Adding Gravity Forms roles to the checkbox list
  84. if (self::has_members_plugin())
  85. add_filter('members_get_capabilities', array("RGForms", "members_get_capabilities"));
  86. if(is_multisite()) {
  87. add_filter('wpmu_drop_tables', array('GFFormsModel', 'mu_drop_tables'));
  88. }
  89. add_action('admin_enqueue_scripts', array('GFForms', 'enqueue_admin_scripts'));
  90. //Loading Gravity Forms if user has access to any functionality
  91. if(GFCommon::current_user_can_any(GFCommon::all_caps()))
  92. {
  93. require_once(GFCommon::get_base_path() . "/export.php");
  94. GFExport::maybe_export();
  95. //runs the setup when version changes
  96. self::setup();
  97. //creates the "Forms" left menu
  98. add_action('admin_menu', array('RGForms', 'create_menu'));
  99. if(GF_SUPPORTED_WP_VERSION){
  100. add_action('admin_footer', array('RGForms', 'check_upload_folder'));
  101. add_action('wp_dashboard_setup', array('RGForms', 'dashboard_setup'));
  102. //Adding "embed form" button
  103. add_action('media_buttons', array('RGForms', 'add_form_button'), 20);
  104. // Add "Form" to the "New" menu in WP admin bar
  105. add_action( 'wp_before_admin_bar_render', array('GFForms', 'admin_bar') );
  106. //Plugin update actions
  107. add_filter("transient_update_plugins", array('RGForms', 'check_update'));
  108. add_filter("site_transient_update_plugins", array('RGForms', 'check_update'));
  109. if(in_array(RG_CURRENT_PAGE, array('post.php', 'page.php', 'page-new.php', 'post-new.php'))){
  110. add_action('admin_footer', array('RGForms', 'add_mce_popup'));
  111. }
  112. else if(self::is_gravity_page()){
  113. require_once(GFCommon::get_base_path() . "/tooltips.php");
  114. add_action("admin_print_scripts", array('RGForms', 'print_scripts'));
  115. }
  116. else if(RG_CURRENT_PAGE == 'media-upload.php'){
  117. require_once(GFCommon::get_base_path() . "/entry_list.php");
  118. }
  119. else if(in_array(RG_CURRENT_PAGE, array("admin.php", "admin-ajax.php"))){
  120. add_action('wp_ajax_rg_save_form', array('RGForms', 'save_form'));
  121. add_action('wp_ajax_rg_change_input_type', array('RGForms', 'change_input_type'));
  122. add_action('wp_ajax_rg_add_field', array('RGForms', 'add_field'));
  123. add_action('wp_ajax_rg_duplicate_field', array('RGForms', 'duplicate_field'));
  124. add_action('wp_ajax_rg_delete_field', array('RGForms', 'delete_field'));
  125. add_action('wp_ajax_rg_delete_file', array('RGForms', 'delete_file'));
  126. add_action('wp_ajax_rg_select_export_form', array('RGForms', 'select_export_form'));
  127. add_action('wp_ajax_rg_start_export', array('RGForms', 'start_export'));
  128. add_action('wp_ajax_gf_upgrade_license', array('RGForms', 'upgrade_license'));
  129. add_action('wp_ajax_gf_delete_custom_choice', array('RGForms', 'delete_custom_choice'));
  130. add_action('wp_ajax_gf_save_custom_choice', array('RGForms', 'save_custom_choice'));
  131. add_action('wp_ajax_gf_get_post_categories', array('RGForms', 'get_post_category_values'));
  132. add_action('wp_ajax_gf_get_notification_post_categories', array('RGForms', 'get_notification_post_category_values'));
  133. add_action('wp_ajax_gf_save_confirmation', array('RGForms', 'save_confirmation'));
  134. add_action('wp_ajax_gf_delete_confirmation', array('RGForms', 'delete_confirmation'));
  135. add_action('wp_ajax_gf_save_new_form', array('RGForms', 'save_new_form'));
  136. //entry list ajax operations
  137. add_action('wp_ajax_rg_update_lead_property', array('RGForms', 'update_lead_property'));
  138. add_action('wp_ajax_delete-gf_entry', array('RGForms', 'update_lead_status'));
  139. //form list ajax operations
  140. add_action('wp_ajax_rg_update_form_active', array('RGForms', 'update_form_active'));
  141. //dynamic captcha image
  142. add_action('wp_ajax_rg_captcha_image', array('RGForms', 'captcha_image'));
  143. //dashboard message "dismiss upgrade" link
  144. add_action("wp_ajax_rg_dismiss_upgrade", array('RGForms', 'dashboard_dismiss_upgrade'));
  145. // entry detail: resend notifications
  146. add_action("wp_ajax_gf_resend_notifications", array('RGForms', 'resend_notifications'));
  147. }
  148. add_filter("plugins_api", array("RGForms", "get_addon_info"), 10, 3);
  149. add_action('after_plugin_row_gravityforms/gravityforms.php', array('RGForms', 'plugin_row') );
  150. add_action('install_plugins_pre_plugin-information', array('RGForms', 'display_changelog'));
  151. add_filter('plugin_action_links', array('RGForms', 'plugin_settings_link'),10,2);
  152. }
  153. }
  154. }
  155. else{
  156. add_action('wp_enqueue_scripts', array('RGForms', 'enqueue_scripts'));
  157. add_action('wp', array('RGForms', 'ajax_parse_request'), 10);
  158. // ManageWP premium update filters
  159. add_filter( 'mwp_premium_update_notification', array('RGForms', 'premium_update_push') );
  160. add_filter( 'mwp_premium_perform_update', array('RGForms', 'premium_update') );
  161. }
  162. add_shortcode('gravityform', array('RGForms', 'parse_shortcode'));
  163. add_shortcode('gravityforms', array('RGForms', 'parse_shortcode'));
  164. }
  165. public static function set_logging_supported($plugins)
  166. {
  167. $plugins["gravityforms"] = "Gravity Forms Core";
  168. return $plugins;
  169. }
  170. public static function maybe_process_form(){
  171. $form_id = isset($_POST["gform_submit"]) ? $_POST["gform_submit"] : 0;
  172. if($form_id){
  173. $form_info = RGFormsModel::get_form($form_id);
  174. $is_valid_form = $form_info && $form_info->is_active;
  175. if($is_valid_form){
  176. require_once(GFCommon::get_base_path() . "/form_display.php");
  177. GFFormDisplay::process_form($form_id);
  178. }
  179. }
  180. }
  181. public static function process_exterior_pages(){
  182. if(rgempty("gf_page", $_GET))
  183. return;
  184. //ensure users are logged in
  185. if(!is_user_logged_in())
  186. auth_redirect();
  187. switch(rgget("gf_page")){
  188. case "preview":
  189. require_once(GFCommon::get_base_path() . "/preview.php");
  190. break;
  191. case "print-entry" :
  192. require_once(GFCommon::get_base_path() . "/print-entry.php");
  193. break;
  194. case "select_columns" :
  195. require_once(GFCommon::get_base_path() . "/select_columns.php");
  196. break;
  197. }
  198. exit();
  199. }
  200. public static function check_update($update_plugins_option){
  201. if(!class_exists("GFCommon"))
  202. require_once("common.php");
  203. return GFCommon::check_update($update_plugins_option, true);
  204. }
  205. //Creates or updates database tables. Will only run when version changes
  206. public static function setup($force_setup = false){
  207. global $wpdb;
  208. $version = GFCommon::$version;
  209. if(get_option("rg_form_version") != $version || $force_setup){
  210. $error = "";
  211. if(!self::has_database_permission($error)){
  212. ?>
  213. <div class='error' style="padding:15px;"><?php echo $error?></div>
  214. <?php
  215. }
  216. require_once(ABSPATH . '/wp-admin/includes/upgrade.php');
  217. if ( ! empty($wpdb->charset) )
  218. $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
  219. if ( ! empty($wpdb->collate) )
  220. $charset_collate .= " COLLATE $wpdb->collate";
  221. //Fixes issue with dbDelta lower-casing table names, which cause problems on case sensitive DB servers.
  222. add_filter( 'dbdelta_create_queries', array("RGForms", "dbdelta_fix_case"));
  223. //------ FORM -----------------------------------------------
  224. $form_table_name = RGFormsModel::get_form_table_name();
  225. $sql = "CREATE TABLE " . $form_table_name . " (
  226. id mediumint(8) unsigned not null auto_increment,
  227. title varchar(150) not null,
  228. date_created datetime not null,
  229. is_active tinyint(1) not null default 1,
  230. PRIMARY KEY (id)
  231. ) $charset_collate;";
  232. dbDelta($sql);
  233. //droping table that was created by mistake in version 1.6.3.2
  234. $wpdb->query("DROP TABLE IF EXISTS A" . $form_table_name);
  235. //------ META -----------------------------------------------
  236. $meta_table_name = RGFormsModel::get_meta_table_name();
  237. $sql = "CREATE TABLE " . $meta_table_name . " (
  238. form_id mediumint(8) unsigned not null,
  239. display_meta longtext,
  240. entries_grid_meta longtext,
  241. confirmations longtext,
  242. notifications longtext,
  243. PRIMARY KEY (form_id)
  244. ) $charset_collate;";
  245. dbDelta($sql);
  246. //droping outdated form_id index (if one exists)
  247. self::drop_index($meta_table_name, 'form_id');
  248. //------ FORM VIEW -----------------------------------------------
  249. $form_view_table_name = RGFormsModel::get_form_view_table_name();
  250. $sql = "CREATE TABLE " . $form_view_table_name . " (
  251. id bigint(20) unsigned not null auto_increment,
  252. form_id mediumint(8) unsigned not null,
  253. date_created datetime not null,
  254. ip char(15),
  255. count mediumint(8) unsigned not null default 1,
  256. PRIMARY KEY (id),
  257. KEY form_id (form_id)
  258. ) $charset_collate;";
  259. dbDelta($sql);
  260. //------ LEAD -----------------------------------------------
  261. $lead_table_name = RGFormsModel::get_lead_table_name();
  262. $sql = "CREATE TABLE " . $lead_table_name . " (
  263. id int(10) unsigned not null auto_increment,
  264. form_id mediumint(8) unsigned not null,
  265. post_id bigint(20) unsigned,
  266. date_created datetime not null,
  267. is_starred tinyint(1) not null default 0,
  268. is_read tinyint(1) not null default 0,
  269. ip varchar(39) not null,
  270. source_url varchar(200) not null default '',
  271. user_agent varchar(250) not null default '',
  272. currency varchar(5),
  273. payment_status varchar(15),
  274. payment_date datetime,
  275. payment_amount decimal(19,2),
  276. transaction_id varchar(50),
  277. is_fulfilled tinyint(1),
  278. created_by bigint(20) unsigned,
  279. transaction_type tinyint(1),
  280. status varchar(20) not null default 'active',
  281. PRIMARY KEY (id),
  282. KEY form_id (form_id),
  283. KEY status (status)
  284. ) $charset_collate;";
  285. dbDelta($sql);
  286. //------ LEAD NOTES ------------------------------------------
  287. $lead_notes_table_name = RGFormsModel::get_lead_notes_table_name();
  288. $sql = "CREATE TABLE " . $lead_notes_table_name . " (
  289. id int(10) unsigned not null auto_increment,
  290. lead_id int(10) unsigned not null,
  291. user_name varchar(250),
  292. user_id bigint(20),
  293. date_created datetime not null,
  294. value longtext,
  295. PRIMARY KEY (id),
  296. KEY lead_id (lead_id),
  297. KEY lead_user_key (lead_id,user_id)
  298. ) $charset_collate;";
  299. dbDelta($sql);
  300. //------ LEAD DETAIL -----------------------------------------
  301. $lead_detail_table_name = RGFormsModel::get_lead_details_table_name();
  302. $sql = "CREATE TABLE " . $lead_detail_table_name . " (
  303. id bigint(20) unsigned not null auto_increment,
  304. lead_id int(10) unsigned not null,
  305. form_id mediumint(8) unsigned not null,
  306. field_number float not null,
  307. value varchar(". GFORMS_MAX_FIELD_LENGTH ."),
  308. PRIMARY KEY (id),
  309. KEY form_id (form_id),
  310. KEY lead_id (lead_id),
  311. KEY lead_field_number (lead_id,field_number)
  312. ) $charset_collate;";
  313. dbDelta($sql);
  314. //------ LEAD DETAIL LONG -----------------------------------
  315. $lead_detail_long_table_name = RGFormsModel::get_lead_details_long_table_name();
  316. $sql = "CREATE TABLE " . $lead_detail_long_table_name . " (
  317. lead_detail_id bigint(20) unsigned not null,
  318. value longtext,
  319. PRIMARY KEY (lead_detail_id)
  320. ) $charset_collate;";
  321. dbDelta($sql);
  322. //droping outdated form_id index (if one exists)
  323. self::drop_index($lead_detail_long_table_name, 'lead_detail_key');
  324. //------ LEAD META -----------------------------------
  325. $lead_meta_table_name = RGFormsModel::get_lead_meta_table_name();
  326. $sql = "CREATE TABLE " . $lead_meta_table_name . " (
  327. id bigint(20) unsigned not null auto_increment,
  328. form_id mediumint(8) unsigned not null default 0,
  329. lead_id bigint(20) unsigned not null,
  330. meta_key varchar(255),
  331. meta_value longtext,
  332. PRIMARY KEY (id),
  333. KEY meta_key (meta_key),
  334. KEY lead_id (lead_id),
  335. KEY form_id_meta_key (form_id,meta_key)
  336. ) $charset_collate;";
  337. dbDelta($sql);
  338. remove_filter('dbdelta_create_queries', array("RGForms", "dbdelta_fix_case"));
  339. //fix form_id value needed to update from version 1.6.11
  340. self::fix_lead_meta_form_id_values();
  341. //fix checkbox value. needed for version 1.0 and below but won't hurt for higher versions
  342. self::fix_checkbox_value();
  343. //auto-setting license key based on value configured via the GF_LICENSE_KEY constant or the gf_license_key variable
  344. global $gf_license_key;
  345. $license_key = defined("GF_LICENSE_KEY") && empty($gf_license_key) ? GF_LICENSE_KEY : $gf_license_key;
  346. if(!empty($license_key))
  347. update_option("rg_gforms_key", md5($license_key));
  348. //auto-setting recaptcha keys based on value configured via the constant or global variable
  349. global $gf_recaptcha_public_key, $gf_recaptcha_private_key;
  350. $private_key = defined("GF_RECAPTCHA_PRIVATE_KEY") && empty($gf_recaptcha_private_key) ? GF_RECAPTCHA_PRIVATE_KEY : $gf_recaptcha_private_key;
  351. if(!empty($private_key))
  352. update_option("rg_gforms_captcha_private_key", $private_key);
  353. $public_key = defined("GF_RECAPTCHA_PUBLIC_KEY") && empty($gf_recaptcha_public_key) ? GF_RECAPTCHA_PUBLIC_KEY : $gf_recaptcha_public_key;
  354. if(!empty($public_key))
  355. update_option("rg_gforms_captcha_public_key", $public_key);
  356. //Auto-importing forms based on GF_IMPORT_FILE AND GF_THEME_IMPORT_FILE
  357. if(defined("GF_IMPORT_FILE") && !get_option("gf_imported_file")){
  358. GFExport::import_file(GF_IMPORT_FILE);
  359. update_option("gf_imported_file", true);
  360. }
  361. //adds empty index.php files to upload folders. only for v1.5.2 and below
  362. if(version_compare(get_option("rg_form_version"), "1.6", "<")){
  363. self::add_empty_index_files();
  364. }
  365. update_option("rg_form_version", $version);
  366. }
  367. //Import theme specific forms if configured. Will only import forms once per theme.
  368. if(defined("GF_THEME_IMPORT_FILE")){
  369. $themes = get_option("gf_imported_theme_file");
  370. if(!is_array($themes))
  371. $themes = array();
  372. //if current theme has already imported it's forms, don't import again
  373. $theme = get_template();
  374. if(!isset($themes[$theme])){
  375. //importing forms
  376. GFExport::import_file(get_stylesheet_directory() . "/" . GF_THEME_IMPORT_FILE);
  377. //adding current theme to the list of imported themes. So that forms are not imported again for it.
  378. $themes[$theme] = true;
  379. update_option("gf_imported_theme_file", $themes);
  380. }
  381. }
  382. }
  383. //Changes form_id values from default value "0" to the correct value. Neededed when upgrading users from 1.6.11
  384. private static function fix_lead_meta_form_id_values(){
  385. global $wpdb;
  386. $lead_meta_table_name = RGFormsModel::get_lead_meta_table_name();
  387. $lead_table_name = RGFormsModel::get_lead_table_name();
  388. $sql = "UPDATE $lead_meta_table_name lm,$lead_table_name l SET lm.form_id = l.form_id
  389. WHERE lm.form_id=0 AND lm.lead_id = l.id;
  390. ";
  391. $wpdb->get_results($sql);
  392. }
  393. public static function dbdelta_fix_case($cqueries){
  394. foreach ($cqueries as $table => $qry) {
  395. $table_name = $table;
  396. if(preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)){
  397. $query_table_name = trim($matches[1], '`' );
  398. //fix table names that are different just by their casing
  399. if(strtolower($query_table_name) == $table){
  400. $table_name = $query_table_name;
  401. }
  402. }
  403. $queries[$table_name] = $qry;
  404. }
  405. return $queries;
  406. }
  407. public static function no_conflict_mode_style(){
  408. if(!get_option("gform_enable_noconflict"))
  409. return;
  410. global $wp_styles;
  411. $wp_required_styles = array("admin-bar", "colors", "ie", "wp-admin", "editor-style");
  412. $gf_required_styles = array(
  413. "common" => array(),
  414. "gf_edit_forms" => array("thickbox", "editor-buttons", "wp-jquery-ui-dialog", "media-views", "buttons" ),
  415. "gf_edit_forms_notification" => array("thickbox", "editor-buttons", "wp-jquery-ui-dialog", "media-views", "buttons"),
  416. "gf_new_form" => array("thickbox"),
  417. "gf_entries" => array("thickbox"),
  418. "gf_settings" => array(),
  419. "gf_export" => array(),
  420. "gf_help" => array()
  421. );
  422. self::no_conflict_mode($wp_styles, $wp_required_styles, $gf_required_styles, "styles");
  423. }
  424. public static function no_conflict_mode_script(){
  425. if(!get_option("gform_enable_noconflict"))
  426. return;
  427. global $wp_scripts;
  428. $wp_required_scripts = array("admin-bar", "common", "jquery-color", "utils");
  429. $gf_required_scripts = array(
  430. "common" => array("qtip-init", "sack"),
  431. "gf_edit_forms" => array("backbone", "editor", "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-tabs", "json2", "media-editor", "media-models", "media-upload", "media-views", "plupload", "plupload-flash", "plupload-html4", "plupload-html5", "plupload-silverlight", "quicktags", "rg_currency", "thickbox", "word-count", "wp-plupload", "wpdialogs-popup", "wplink"),
  432. "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"),
  433. "gf_new_form" => array("thickbox", "jquery-ui-core", "jquery-ui-sortable", "jquery-ui-tabs", "rg_currency", "gforms_gravityforms" ),
  434. "gf_entries" => array("thickbox", "gforms_gravityforms", "wp-lists", "gform_json"),
  435. "gf_settings" => array(),
  436. "gf_export" => array("gform_form_admin","jquery-ui-datepicker"),
  437. "gf_help" => array(),
  438. );
  439. self::no_conflict_mode($wp_scripts, $wp_required_scripts, $gf_required_scripts, "scripts");
  440. }
  441. private static function no_conflict_mode(&$wp_objects, $wp_required_objects, $gf_required_objects, $type="scripts"){
  442. $current_page = trim(strtolower(rgget("page")));
  443. if(empty($current_page))
  444. $current_page = trim(strtolower(rgget("gf_page")));
  445. if(empty($current_page))
  446. $current_page = RG_CURRENT_PAGE;
  447. $view = rgempty("view", $_GET) ? "default" : rgget("view");
  448. $page_objects = isset($gf_required_objects[$current_page . "_" . $view]) ? $gf_required_objects[$current_page . "_" . $view] : rgar($gf_required_objects, $current_page);
  449. //disable no-conflict if $page_objects is false
  450. if($page_objects === false)
  451. return;
  452. if(!is_array($page_objects))
  453. $page_objects = array();
  454. //merging wp scripts with gravity forms scripts
  455. $required_objects = array_merge($wp_required_objects, $gf_required_objects["common"], $page_objects);
  456. //allowing addons or other products to change the list of no conflict scripts
  457. $required_objects = apply_filters("gform_noconflict_{$type}", $required_objects);
  458. $queue = array();
  459. foreach($wp_objects->queue as $object){
  460. if(in_array($object, $required_objects))
  461. $queue[] = $object;
  462. }
  463. $wp_objects->queue = $queue;
  464. $required_objects = self::add_script_dependencies($wp_objects->registered, $required_objects);
  465. //unregistering scripts
  466. $registered = array();
  467. foreach($wp_objects->registered as $script_name => $script_registration){
  468. if(in_array($script_name, $required_objects)){
  469. $registered[$script_name] = $script_registration;
  470. }
  471. }
  472. $wp_objects->registered = $registered;
  473. }
  474. private static function add_script_dependencies($registered, $scripts){
  475. //gets all dependent scripts linked to the $scripts array passed
  476. do{
  477. $dependents = array();
  478. foreach($scripts as $script){
  479. $deps = isset($registered[$script]) && is_array($registered[$script]->deps) ? $registered[$script]->deps : array();
  480. foreach($deps as $dep){
  481. if(!in_array($dep, $scripts) && !in_array($dep, $dependents)){
  482. $dependents[] = $dep;
  483. }
  484. }
  485. }
  486. $scripts = array_merge($scripts, $dependents);
  487. }while(!empty($dependents));
  488. return $scripts;
  489. }
  490. //Integration with ManageWP
  491. public static function premium_update_push( $premium_update ){
  492. if( !function_exists( 'get_plugin_data' ) )
  493. include_once( ABSPATH.'wp-admin/includes/plugin.php');
  494. $update = GFCommon::get_version_info();
  495. if( $update["is_valid_key"] == true && version_compare(GFCommon::$version, $update["version"], '<') ){
  496. $gforms = get_plugin_data( __FILE__ );
  497. $gforms['type'] = 'plugin';
  498. $gforms['slug'] = 'gravityforms/gravityforms.php';
  499. $gforms['new_version'] = isset($update['version']) ? $update['version'] : false ;
  500. $premium_update[] = $gforms;
  501. }
  502. return $premium_update;
  503. }
  504. //Integration with ManageWP
  505. public static function premium_update( $premium_update ){
  506. if( !function_exists( 'get_plugin_data' ) )
  507. include_once( ABSPATH.'wp-admin/includes/plugin.php');
  508. $update = GFCommon::get_version_info();
  509. if( $update["is_valid_key"] == true && version_compare(GFCommon::$version, $update["version"], '<') ){
  510. $gforms = get_plugin_data( __FILE__ );
  511. $gforms['slug'] = 'gravityforms/gravityforms.php'; // If not set by default, always pass theme template
  512. $gforms['type'] = 'plugin';
  513. $gforms['url'] = isset($update["url"]) ? $update["url"] : false; // OR provide your own callback function for managing the update
  514. array_push($premium_update, $gforms);
  515. }
  516. return $premium_update;
  517. }
  518. private static function drop_index($table, $index){
  519. global $wpdb;
  520. $has_index = $wpdb->get_var("SHOW INDEX FROM {$table} WHERE Key_name='{$index}'");
  521. if($has_index){
  522. $wpdb->query("DROP INDEX {$index} ON {$table}");
  523. }
  524. }
  525. private static function add_empty_index_files(){
  526. $upload_root = RGFormsModel::get_upload_root();
  527. GFCommon::recursive_add_index_file($upload_root);
  528. }
  529. private static function has_database_permission(&$error){
  530. global $wpdb;
  531. $wpdb->hide_errors();
  532. $has_permission = true;
  533. $sql = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}rg_test ( col1 int )";
  534. $wpdb->query($sql);
  535. $error = "Current database user does not have necessary permissions to create tables.";
  536. if(!empty($wpdb->last_error))
  537. $has_permission = false;
  538. if($has_permission){
  539. $sql = "ALTER TABLE {$wpdb->prefix}rg_test ADD COLUMN a" . uniqid() ." int";
  540. $wpdb->query($sql);
  541. $error = "Current database user does not have necessary permissions to modify (ALTER) tables.";
  542. if(!empty($wpdb->last_error))
  543. $has_permission = false;
  544. $sql = "DROP TABLE {$wpdb->prefix}rg_test";
  545. $wpdb->query($sql);
  546. }
  547. $wpdb->show_errors();
  548. return $has_permission;
  549. }
  550. //Changes checkbox entry values from "!" to the current choice text. Neededed when upgrading users from 1.0
  551. private static function fix_checkbox_value(){
  552. global $wpdb;
  553. $table_name = RGFormsModel::get_lead_details_table_name();
  554. $sql = "select * from $table_name where value= '!'";
  555. $results = $wpdb->get_results($sql);
  556. foreach($results as $result){
  557. $form = RGFormsModel::get_form_meta($result->form_id);
  558. $field = RGFormsModel::get_field($form, $result->field_number);
  559. if($field["type"] == "checkbox"){
  560. $input = GFCommon::get_input($field, $result->field_number);
  561. $wpdb->update($table_name, array("value" => $input["label"]), array("id" => $result->id));
  562. }
  563. }
  564. }
  565. public static function user_has_cap($all_caps, $cap, $args){
  566. $gf_caps = GFCommon::all_caps();
  567. $capability = rgar($cap, 0);
  568. if($capability != "gform_full_access"){
  569. return $all_caps;
  570. }
  571. if(!self::has_members_plugin()){
  572. //give full access to administrators if the members plugin is not installed
  573. if(current_user_can("administrator") || is_super_admin()){
  574. $all_caps["gform_full_access"] = true;
  575. }
  576. }
  577. else if(current_user_can("administrator")|| is_super_admin()){
  578. //checking if user has any GF permission.
  579. $has_gf_cap = false;
  580. foreach($gf_caps as $gf_cap){
  581. if(rgar($all_caps, $gf_cap))
  582. $has_gf_cap = true;
  583. }
  584. if(!$has_gf_cap){
  585. //give full access to administrators if none of the GF permissions are active by the Members plugin
  586. $all_caps["gform_full_access"] = true;
  587. }
  588. }
  589. return $all_caps;
  590. }
  591. //Target of Member plugin filter. Provides the plugin with Gravity Forms lists of capabilities
  592. public static function members_get_capabilities( $caps ) {
  593. return array_merge($caps, GFCommon::all_caps());
  594. }
  595. //Tests if the upload folder is writable and displays an error message if not
  596. public static function check_upload_folder(){
  597. //check if upload folder is writable
  598. $folder = RGFormsModel::get_upload_root();
  599. if(empty($folder))
  600. echo "<div class='error'>Upload folder is not writable. Export and file upload features will not be functional.</div>";
  601. }
  602. //Prints common admin scripts
  603. public static function print_scripts(){
  604. wp_enqueue_script("sack");
  605. wp_print_scripts();
  606. }
  607. public static function is_gravity_ajax_action(){
  608. //Gravity Forms AJAX requests
  609. $current_action = self::post("action");
  610. $gf_ajax_actions = array('rg_save_form', 'rg_change_input_type', 'rg_add_field', 'rg_duplicate_field',
  611. 'rg_delete_field', 'rg_select_export_form', 'rg_start_export', 'gf_upgrade_license',
  612. 'gf_delete_custom_choice', 'gf_save_custom_choice', 'gf_get_notification_post_categories',
  613. 'rg_update_lead_property', 'delete-gf_entry', 'rg_update_form_active',
  614. 'gf_resend_notifications', 'rg_dismiss_upgrade', 'gf_save_confirmation');
  615. if(defined("DOING_AJAX") && DOING_AJAX && in_array($current_action, $gf_ajax_actions))
  616. return true;
  617. //not a gravity forms ajax request.
  618. return false;
  619. }
  620. //Returns true if the current page is one of Gravity Forms pages. Returns false if not
  621. public static function is_gravity_page(){
  622. //Gravity Forms pages
  623. $current_page = trim(strtolower(self::get("page")));
  624. $gf_pages = array("gf_edit_forms", "gf_new_form", "gf_entries", "gf_settings", "gf_export", "gf_help");
  625. return in_array($current_page, $gf_pages);
  626. }
  627. //Creates "Forms" left nav
  628. public static function create_menu(){
  629. $has_full_access = current_user_can("gform_full_access");
  630. $min_cap = GFCommon::current_user_can_which(GFCommon::all_caps());
  631. if(empty($min_cap))
  632. $min_cap = "gform_full_access";
  633. $addon_menus = array();
  634. $addon_menus = apply_filters("gform_addon_navigation", $addon_menus);
  635. $parent_menu = self::get_parent_menu($addon_menus);
  636. // Add a top-level left nav
  637. $update_icon = GFCommon::has_update() ? "<span title='" . esc_attr(__("Update Available", "gravityforms")) . "' class='update-plugins count-1'><span class='update-count'>1</span></span>" : "";
  638. add_menu_page(__('Forms', "gravityforms"), __("Forms", "gravityforms") . $update_icon , $has_full_access ? "gform_full_access" : $min_cap, $parent_menu["name"] , $parent_menu["callback"], GFCommon::get_base_url() . '/images/gravity-admin-icon.png', apply_filters("gform_menu_position", "16.9"));
  639. // Adding submenu pages
  640. 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"));
  641. 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"));
  642. 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"));
  643. if(is_array($addon_menus)){
  644. foreach($addon_menus as $addon_menu)
  645. 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"]);
  646. }
  647. 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"));
  648. 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"));
  649. if(current_user_can("install_plugins")){
  650. 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"));
  651. 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"));
  652. }
  653. add_submenu_page($parent_menu["name"], __("Help", "gravityforms"), __("Help", "gravityforms"), $has_full_access ? "gform_full_access" : $min_cap, "gf_help", array("RGForms", "help_page"));
  654. }
  655. //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)
  656. public static function get_parent_menu($addon_menus){
  657. if(GFCommon::current_user_can_any("gravityforms_edit_forms"))
  658. $parent = array("name" => "gf_edit_forms", "callback" => array("RGForms", "forms"));
  659. else if(GFCommon::current_user_can_any("gravityforms_create_form"))
  660. $parent = array("name" => "gf_new_form", "callback" => array("RGForms", "new_form"));
  661. else if(GFCommon::current_user_can_any("gravityforms_view_entries"))
  662. $parent = array("name" => "gf_entries", "callback" => array("RGForms", "all_leads_page"));
  663. else if(is_array($addon_menus) && sizeof($addon_menus) > 0){
  664. foreach($addon_menus as $addon_menu)
  665. if(GFCommon::current_user_can_any($addon_menu["permission"]))
  666. {
  667. $parent = array("name" => $addon_menu["name"], "callback" => $addon_menu["callback"]);
  668. break;
  669. }
  670. }
  671. else if(GFCommon::current_user_can_any("gravityforms_view_settings"))
  672. $parent = array("name" => "gf_settings", "callback" => array("RGForms", "settings_page"));
  673. else if(GFCommon::current_user_can_any("gravityforms_export_entries"))
  674. $parent = array("name" => "gf_export", "callback" => array("RGForms", "export_page"));
  675. else if(GFCommon::current_user_can_any("gravityforms_view_updates"))
  676. $parent = array("name" => "gf_update", "callback" => array("RGForms", "update_page"));
  677. else if(GFCommon::current_user_can_any("gravityforms_view_addons"))
  678. $parent = array("name" => "gf_addons", "callback" => array("RGForms", "addons_page"));
  679. else if(GFCommon::current_user_can_any(GFCommon::all_caps()))
  680. $parent = array("name" => "gf_help", "callback" => array("RGForms", "help_page"));
  681. return $parent;
  682. }
  683. //Parses the [gravityform shortcode and returns the front end form UI
  684. public static function parse_shortcode( $attributes, $content = null ) {
  685. extract( shortcode_atts( array(
  686. 'title' => true,
  687. 'description' => true,
  688. 'id' => 0,
  689. 'name' => '',
  690. 'field_values' => "",
  691. 'ajax' => false,
  692. 'tabindex' => 1,
  693. 'action' => 'form'
  694. ), $attributes ) );
  695. $shortcode_string = "";
  696. switch($action) {
  697. case 'conditional':
  698. $shortcode_string = GFCommon::conditional_shortcode($attributes, $content);
  699. break;
  700. case 'form' :
  701. //displaying form
  702. $title = strtolower($title) == "false" ? false : true;
  703. $description = strtolower($description) == "false" ? false : true;
  704. $field_values = htmlspecialchars_decode($field_values);
  705. $field_values = str_replace("&#038;", "&", $field_values);
  706. $ajax = strtolower($ajax) == "true" ? true : false;
  707. //using name to lookup form if id is not specified
  708. if(empty($id))
  709. $id = $name;
  710. parse_str($field_values, $field_value_array); //parsing query string like string for field values and placing them into an associative array
  711. $field_value_array = stripslashes_deep($field_value_array);
  712. $shortcode_string = self::get_form($id, $title, $description, false, $field_value_array, $ajax, $tabindex);
  713. break;
  714. }
  715. $shortcode_string = apply_filters("gform_shortcode_{$action}", $shortcode_string, $attributes, $content);
  716. return $shortcode_string;
  717. }
  718. //-------------------------------------------------
  719. //----------- AJAX --------------------------------
  720. public static function ajax_parse_request($wp) {
  721. if (isset($_POST["gform_ajax"])) {
  722. parse_str($_POST["gform_ajax"]);
  723. require_once(GFCommon::get_base_path() . "/form_display.php");
  724. $result = GFFormDisplay::get_form($form_id, $title, $description, false, $_POST["gform_field_values"], true);
  725. die($result);
  726. }
  727. }
  728. //------------------------------------------------------
  729. //------------- PAGE/POST EDIT PAGE ---------------------
  730. //Action target that adds the "Insert Form" button to the post/page edit screen
  731. public static function add_form_button(){
  732. $is_post_edit_page = in_array(RG_CURRENT_PAGE, array('post.php', 'page.php', 'page-new.php', 'post-new.php'));
  733. if(!$is_post_edit_page)
  734. return;
  735. // do a version check for the new 3.5 UI
  736. $version = get_bloginfo('version');
  737. if ($version < 3.5) {
  738. // show button for v 3.4 and below
  739. $image_btn = GFCommon::get_base_url() . "/images/form-button.png";
  740. echo '<a href="#TB_inline?width=480&inlineId=select_gravity_form" class="thickbox" id="add_gform" title="' . __("Add Gravity Form", 'gravityforms') . '"><img src="'.$image_btn.'" alt="' . __("Add Gravity Form", 'gravityforms') . '" /></a>';
  741. } else {
  742. // display button matching new UI
  743. echo '<style>.gform_media_icon{
  744. background:url(' . GFCommon::get_base_url() . '/images/gravity-admin-icon.png) no-repeat top left;
  745. display: inline-block;
  746. height: 16px;
  747. margin: 0 2px 0 0;
  748. vertical-align: text-top;
  749. width: 16px;
  750. }
  751. .wp-core-ui a.gform_media_link{
  752. padding-left: 0.4em;
  753. }
  754. </style>
  755. <a href="#TB_inline?width=480&inlineId=select_gravity_form" class="thickbox button gform_media_link" id="add_gform" title="' . __("Add Gravity Form", 'gravityforms') . '"><span class="gform_media_icon "></span> ' . __("Add Form", "gravityforms") . '</a>';
  756. }
  757. }
  758. //Action target that displays the popup to insert a form to a post/page
  759. public static function add_mce_popup(){
  760. ?>
  761. <script>
  762. function InsertForm(){
  763. var form_id = jQuery("#add_form_id").val();
  764. if(form_id == ""){
  765. alert("<?php _e("Please select a form", "gravityforms") ?>");
  766. return;
  767. }
  768. var form_name = jQuery("#add_form_id option[value='" + form_id + "']").text().replace(/[\[\]]/g, '');
  769. var display_title = jQuery("#display_title").is(":checked");
  770. var display_description = jQuery("#display_description").is(":checked");
  771. var ajax = jQuery("#gform_ajax").is(":checked");
  772. var title_qs = !display_title ? " title=\"false\"" : "";
  773. var description_qs = !display_description ? " description=\"false\"" : "";
  774. var ajax_qs = ajax ? " ajax=\"true\"" : "";
  775. window.send_to_editor("[gravityform id=\"" + form_id + "\" name=\"" + form_name + "\"" + title_qs + description_qs + ajax_qs + "]");
  776. }
  777. </script>
  778. <div id="select_gravity_form" style="display:none;">
  779. <div class="wrap">
  780. <div>
  781. <div style="padding:15px 15px 0 15px;">
  782. <h3 style="color:#5A5A5A!important; font-family:Georgia,Times New Roman,Times,serif!important; font-size:1.8em!important; font-weight:normal!important;"><?php _e("Insert A Form", "gravityforms"); ?></h3>
  783. <span>
  784. <?php _e("Select a form below to add it to your post or page.", "gravityforms"); ?>
  785. </span>
  786. </div>
  787. <div style="padding:15px 15px 0 15px;">
  788. <select id="add_form_id">
  789. <option value=""> <?php _e("Select a Form", "gravityforms"); ?> </option>
  790. <?php
  791. $forms = RGFormsModel::get_forms(1, "title");
  792. foreach($forms as $form){
  793. ?>
  794. <option value="<?php echo absint($form->id) ?>"><?php echo esc_html($form->title) ?></option>
  795. <?php
  796. }
  797. ?>
  798. </select> <br/>
  799. <div style="padding:8px 0 0 0; font-size:11px; font-style:italic; color:#5A5A5A"><?php _e("Can't find your form? Make sure it is active.", "gravityforms"); ?></div>
  800. </div>
  801. <div style="padding:15px 15px 0 15px;">
  802. <input type="checkbox" id="display_title" checked='checked' /> <label for="display_title"><?php _e("Display form title", "gravityforms"); ?></label> &nbsp;&nbsp;&nbsp;
  803. <input type="checkbox" id="display_description" checked='checked' /> <label for="display_description"><?php _e("Display form description", "gravityforms"); ?></label>&nbsp;&nbsp;&nbsp;
  804. <input type="checkbox" id="gform_ajax" /> <label for="gform_ajax"><?php _e("Enable AJAX", "gravityforms"); ?></label>
  805. </div>
  806. <div style="padding:15px;">
  807. <input type="button" class="button-primary" value="Insert Form" onclick="InsertForm();"/>&nbsp;&nbsp;&nbsp;
  808. <a class="button" style="color:#bbb;" href="#" onclick="tb_remove(); return false;"><?php _e("Cancel", "gravityforms"); ?></a>
  809. </div>
  810. </div>
  811. </div>
  812. </div>
  813. <?php
  814. }
  815. //------------------------------------------------------
  816. //------------- PLUGINS PAGE ---------------------------
  817. //------------------------------------------------------
  818. public static function plugin_settings_link( $links, $file ) {
  819. if ( $file != plugin_basename( __FILE__ ))
  820. return $links;
  821. array_unshift($links, '<a href="' . admin_url("admin.php") . '?page=gf_settings">' . __( 'Settings', 'gravityforms' ) . '</a>');
  822. return $links;
  823. }
  824. //Displays message on Plugin's page
  825. public static function plugin_row($plugin_name){
  826. $key = GFCommon::get_key();
  827. $version_info = GFCommon::get_version_info();
  828. if(!$version_info["is_valid_key"]){
  829. $plugin_name = "gravityforms/gravityforms.php";
  830. $new_version = version_compare(GFCommon::$version, $version_info["version"], '<') ? __('There is a new version of Gravity Forms available.', 'gravityforms') .' <a class="thickbox" title="Gravity Forms" href="plugin-install.php?tab=plugin-information&plugin=gravityforms&TB_iframe=true&width=640&height=808">'. sprintf(__('View version %s Details', 'gravityforms'), $version_info["version"]) . '</a>. ' : '';
  831. echo '</tr><tr class="plugin-update-tr"><td colspan="3" class="plugin-update"><div class="update-message">' . $new_version . __('<a href="' . admin_url() . 'admin.php?page=gf_settings">Register</a> your copy of Gravity Forms to receive access to automatic upgrades and support. Need a license key? <a href="http://www.gravityforms.com">Purchase one now</a>.', 'gravityforms') . '</div></td>';
  832. }
  833. }
  834. //Displays current version details on Plugin's page
  835. public static function display_changelog(){
  836. if($_REQUEST["plugin"] != "gravityforms")
  837. return;
  838. $page_text = self::get_changelog();
  839. echo $page_text;
  840. exit;
  841. }
  842. public static function get_changelog(){
  843. $key = GFCommon::get_key();
  844. $body = "key=$key";
  845. $options = array('method' => 'POST', 'timeout' => 3, 'body' => $body);
  846. $options['headers'] = array(
  847. 'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset'),
  848. 'Content-Length' => strlen($body),
  849. 'User-Agent' => 'WordPress/' . get_bloginfo("version"),
  850. 'Referer' => get_bloginfo("url")
  851. );
  852. $raw_response = wp_remote_request(GRAVITY_MANAGER_URL . "/changelog.php?" . GFCommon::get_remote_request_params(), $options);
  853. if ( is_wp_error( $raw_response ) || 200 != $raw_response['response']['code']){
  854. $page_text = __("Oops!! Something went wrong.<br/>Please try again or <a href='http://www.gravityforms.com'>contact us</a>.", 'gravityforms');
  855. }
  856. else{
  857. $page_text = $raw_response['body'];
  858. if(substr($page_text, 0, 10) != "<!--GFM-->")
  859. $page_text = "";
  860. }
  861. return stripslashes($page_text);
  862. }
  863. //------------------------------------------------------
  864. //-------------- DASHBOARD PAGE -------------------------
  865. //Registers the dashboard widget
  866. public static function dashboard_setup(){
  867. $dashboard_title = apply_filters("gform_dashboard_title", __("Forms", "gravityforms"));
  868. wp_add_dashboard_widget('rg_forms_dashboard', $dashboard_title, array('RGForms', 'dashboard'));
  869. }
  870. //Displays the dashboard UI
  871. public static function dashboard(){
  872. $forms = RGFormsModel::get_form_summary();
  873. if(sizeof($forms) > 0){
  874. ?>
  875. <table class="widefat gf_dashboard_view" cellspacing="0" style="border:0px;">
  876. <thead>
  877. <tr>
  878. <td class="gf_dashboard_form_title_header" style="text-align:left; padding:8px 18px!important; font-weight:bold;"><i><?php _e("Title", "gravityforms") ?></i></td>
  879. <td class="gf_dashboard_entries_unread_header" style="text-align:center; padding:8px 18px!important; font-weight:bold;"><i><?php _e("Unread", "gravityforms") ?></i></td>
  880. <td class="gf_dashboard_entries_total_header" style="text-align:center; padding:8px 18px!important; font-weight:bold;"><i><?php _e("Total", "gravityforms") ?></i></td>
  881. </tr>
  882. </thead>
  883. <tbody class="list:user user-list">
  884. <?php
  885. foreach($forms as $form){
  886. $date_display = GFCommon::format_date($form["last_lead_date"]);
  887. if(!empty($form["total_leads"])){
  888. ?>
  889. <tr class='author-self status-inherit' valign="top">
  890. <td class="gf_dashboard_form_title column-title" style="padding:8px 18px;">
  891. <a <?php echo $form["unread_count"] > 0 ? "class='form_title_unread' style='font-weight:bold;'" : "" ?> href="admin.php?page=gf_entries&view=entries&id=<?php echo absint($form["id"]) ?>" title="<?php echo esc_html($form["title"]) ?> : <?php _e("View All Entries", "gravityforms") ?>"><?php echo esc_html($form["title"]) ?></a>
  892. </td>
  893. <td class="gf_dashboard_entries_unread column-date" style="padding:8px 18px; text-align:center;"><a <?php echo $form["unread_count"] > 0 ? "class='form_entries_unread' style='font-weight:bold;'" : "" ?> href="admin.php?page=gf_entries&view=entries&filter=unread&id=<?php echo absint($form["id"]) ?>" title="<?php printf(__("Last Entry: %s", "gravityforms"), $date_display); ?>"><?php echo absint($form["unread_count"]) ?></a></td>
  894. <td class="gf_dashboard_entries_total column-date" style="padding:8px 18px; text-align:center;"><a href="admin.php?page=gf_entries&view=entries&id=<?php echo absint($form["id"]) ?>" title="<?php _e("View All Entries", "gravityforms") ?>"><?php echo absint($form["total_leads"]) ?></a></td>
  895. </tr>
  896. <?php
  897. }
  898. }
  899. ?>
  900. </tbody>
  901. </table>
  902. <p class="textright">
  903. <a class="gf_dashboard_button button" href="admin.php?page=gf_edit_forms"><?php _e("View All Forms", "gravityforms") ?></a>
  904. </p>
  905. <?php
  906. }
  907. else{
  908. ?>
  909. <div class="gf_dashboard_noforms_notice">
  910. <?php echo sprintf(__("You don't have any forms. Let's go %s create one %s!", 'gravityforms'), '<a href="admin.php?page=gf_new_form">', '</a>'); ?>
  911. </div>
  912. <?php
  913. }
  914. if(GFCommon::current_user_can_any("gravityforms_view_updates") && (!function_exists("is_multisite") || !is_multisite() || is_super_admin())){
  915. //displaying update message if there is an update and user has permission
  916. self::dashboard_update_message();
  917. }
  918. }
  919. public static function dashboard_update_message(){
  920. $version_info = GFCommon::get_version_info();
  921. //don't display a message if use has dismissed the message for this version
  922. $ary_dismissed = get_option("gf_dismissed_upgrades");
  923. $is_dismissed = !empty($ary_dismissed) && in_array($version_info["version"], $ary_dismissed);
  924. if($is_dismissed)
  925. return;
  926. if(version_compare(GFCommon::$version, $version_info["version"], '<')) {
  927. $auto_upgrade = "";
  928. /*if($version_info["is_valid_key"]){
  929. $plugin_file = "gravityforms/gravityforms.php";
  930. $upgrade_url = wp_nonce_url('update.php?action=upgrade-plugin&amp;plugin=' . urlencode($plugin_file), 'upgrade-plugin_' . $plugin_file);
  931. $auto_upgrade = sprintf(__(" or %sUpgrade Automatically%s", "gravityforms"), "<a href='{$upgrade_url}'>", "</a>");
  932. }*/
  933. $message = sprintf(__("There is an update available for Gravity Forms. %sView Details%s %s", "gravityforms"), "<a href='admin.php?page=gf_update'>", "</a>", $auto_upgrade);
  934. ?>
  935. <div class='updated' style='padding:15px; position:relative;' id='gf_dashboard_message'><?php echo $message ?>
  936. <a href="javascript:void(0);" onclick="GFDismissUpgrade();" style='float:right;'><?php _e("Dismiss", "gravityforms") ?></a>
  937. </div>
  938. <script type="text/javascript">
  939. function GFDismissUpgrade(){
  940. jQuery("#gf_dashboard_message").slideUp();
  941. jQuery.post(ajaxurl, {action:"rg_dismiss_upgrade", version:"<?php echo $version_info["version"] ?>"});
  942. }
  943. </script>
  944. <?php
  945. }
  946. }
  947. public static function dashboard_dismiss_upgrade(){
  948. $ary = get_option("gf_dismissed_upgrades");
  949. if(!is_array($ary))
  950. $ary = array();
  951. $ary[] = $_POST["version"];
  952. update_option("gf_dismissed_upgrades", $ary);
  953. }
  954. //------------------------------------------------------
  955. //--------------- ALL OTHER PAGES ----------------------
  956. public static function register_scripts() {
  957. wp_register_script('gform_chosen', plugins_url('/js/chosen.jquery.min.js', __FILE__), array('jquery'), GFCommon::$version );
  958. wp_register_script('gform_conditional_logic', plugins_url('/js/conditional_logic.js', __FILE__), array('jquery'), GFCommon::$version );
  959. wp_register_script('gform_datepicker_init', plugins_url('/js/datepicker.js', __FILE__), array('jquery', 'jquery-ui-datepicker'), GFCommon::$version );
  960. wp_register_script('gform_dimensions', plugins_url('/js/jquery.dimensions.js', __FILE__), array('jquery'), GFCommon::$version );
  961. wp_register_script('gform_floatmenu', plugins_url('/js/floatmenu_init.js', __FILE__), array('jquery'), GFCommon::$version );
  962. wp_register_script('gform_form_admin', plugins_url('/js/form_admin.js', __FILE__), array('jquery', 'jquery-ui-autocomplete'), GFCommon::$version );
  963. wp_register_script('gform_form_editor', plugins_url('/js/form_editor.js', __FILE__), array('jquery', 'gform_json'), GFCommon::$version );
  964. wp_register_script('gform_forms', plugins_url('/js/forms.js', __FILE__), array('jquery'), GFCommon::$version );
  965. wp_register_script('gform_gravityforms', plugins_url('/js/gravityforms.js', __FILE__), array('jquery'), GFCommon::$version );
  966. wp_register_script('gform_json', plugins_url('/js/jquery.json-1.3.js', __FILE__), array('jquery'), GFCommon::$version );
  967. wp_register_script('gform_masked_input', plugins_url('/js/jquery.maskedinput-1.3.min.js', __FILE__), array('jquery'), GFCommon::$version );
  968. wp_register_script('gform_menu', plugins_url('/js/menu.js', __FILE__), array('jquery'), GFCommon::$version );
  969. wp_register_script('gform_placeholder', plugins_url('/js/jquery.placeholder.1.2.min.js', __FILE__), array('jquery'), GFCommon::$version );
  970. wp_register_script('gform_qtip', plugins_url('/js/jquery.qtip-1.0.0-rc2.min.js', __FILE__), array('jquery'), GFCommon::$version );
  971. wp_register_script('gform_qtip_init', plugins_url('/js/qtip_init.js', __FILE__), array('jquery'), GFCommon::$version );
  972. wp_register_script('gform_textarea_counter', plugins_url('/js/jquery.textareaCounter.plugin.js', __FILE__), array('jquery'), GFCommon::$version );
  973. // only required for WP versions prior to 3.3
  974. wp_register_script("gf_thickbox", GFCommon::get_base_url() . "/js/thickbox.js", null, GFCommon::$version);
  975. wp_register_style("gf_thickbox", GFCommon::get_base_url() . "/js/thickbox.css", null, GFCommon::$version);
  976. wp_localize_script('gf_thickbox', 'thickboxL10n', array(
  977. 'next' => __('Next &gt;'),
  978. 'prev' => __('&lt; Prev'),
  979. 'image' => __('Image'),
  980. 'of' => __('of'),
  981. 'close' => __('Close'),
  982. 'noiframes' => __('This feature requires inline frames. You have iframes disabled or your browser does not support them.'),
  983. 'loadingAnimation' => includes_url('js/thickbox/loadingAnimation.gif'),
  984. 'closeImage' => includes_url('js/thickbox/tb-close.png'))
  985. );
  986. }
  987. public static function enqueue_admin_scripts() {
  988. $scripts = array();
  989. $page = self::get_page();
  990. switch($page) {
  991. case 'new_form' :
  992. case 'form_list':
  993. $scripts = array(
  994. 'gform_json',
  995. 'gform_form_admin',
  996. 'thickbox'
  997. );
  998. break;
  999. case 'form_settings':
  1000. $scripts = array(
  1001. 'gform_gravityforms',
  1002. 'gform_forms',
  1003. 'gform_json',
  1004. 'gform_form_admin',
  1005. 'gform_placeholder',
  1006. 'jquery-ui-datepicker',
  1007. 'gform_masked_input',
  1008. 'jquery-ui-sortable'
  1009. );
  1010. break;
  1011. case 'form_editor':
  1012. $thickbox = !GFCommon::is_wp_version("3.3") ? 'gf_thickbox' : 'thickbox';
  1013. $scripts = array(
  1014. $thickbox,
  1015. 'jquery-ui-core',
  1016. 'jquery-ui-sortable',
  1017. 'jquery-ui-tabs',
  1018. 'sack',
  1019. 'gform_gravityforms',
  1020. 'gform_forms',
  1021. 'gform_json',
  1022. 'gform_form_admin',
  1023. 'gform_floatmenu',
  1024. 'gform_menu',
  1025. 'gform_placeholder',
  1026. 'jquery-ui-autocomplete'
  1027. );
  1028. break;
  1029. case 'entry_detail':
  1030. $scripts = array('gform_json');
  1031. break;
  1032. case 'entry_detail_edit':
  1033. $scripts = array('gform_gravityforms');
  1034. break;
  1035. case 'entry_list':
  1036. $scripts = array(
  1037. 'wp-lists',
  1038. 'wp-ajax-response',
  1039. 'thickbox',
  1040. 'gform_json',
  1041. 'thickbox'
  1042. );
  1043. break;
  1044. case 'notification_list':
  1045. $scripts = array(
  1046. 'gform_forms',
  1047. 'gform_json',
  1048. 'gform_form_admin'
  1049. );
  1050. break;
  1051. case 'notification_new':
  1052. case 'notification_edit':
  1053. $scripts = array(
  1054. 'jquery-ui-autocomplete',
  1055. 'gform_gravityforms',
  1056. 'gform_placeholder',
  1057. 'gform_form_admin',
  1058. 'gform_forms',
  1059. 'gform_json'
  1060. );
  1061. break;
  1062. case 'confirmation':
  1063. $scripts = array(
  1064. 'gform_form_admin',
  1065. 'gform_forms',
  1066. 'gform_gravityforms',
  1067. 'gform_placeholder',
  1068. 'gform_json'
  1069. );
  1070. break;
  1071. case 'addons':
  1072. $scripts = array('thickbox');
  1073. break;
  1074. case 'export_entry':
  1075. $scripts = array(
  1076. 'jquery-ui-datepicker',
  1077. 'gform_form_admin'
  1078. );
  1079. break;
  1080. }
  1081. if(empty($scripts))
  1082. return;
  1083. foreach($scripts as $script){
  1084. wp_enqueue_script($script);
  1085. }
  1086. }
  1087. public static function get_page() {
  1088. /**
  1089. * Page names:
  1090. *
  1091. * new_form
  1092. * form_list
  1093. * form_editor
  1094. * form_settings
  1095. * confirmation
  1096. * notification_list
  1097. * notification_new
  1098. * notification_edit
  1099. * entry_list
  1100. * entry_detail
  1101. * entry_detail_edit
  1102. * settings
  1103. * addons
  1104. * export_entry
  1105. * export_form
  1106. * import_form
  1107. */
  1108. if( rgget('page') == 'gf_new_form' )
  1109. return 'new_form';
  1110. if( rgget('page') == 'gf_edit_forms' && !rgget('id') )
  1111. return 'form_list';
  1112. if( rgget('page') == 'gf_edit_forms' && !rgget('view') )
  1113. return 'form_editor';
  1114. if( rgget('page') == 'gf_edit_forms' && rgget('view') == 'settings' && ( !rgget('subview') || rgget('subview') == 'settings') )
  1115. return 'form_settings';
  1116. if( rgget('page') == 'gf_edit_forms' && rgget('view') == 'settings' && rgget('subview') == 'confirmation' )
  1117. return 'confirmation';
  1118. if( rgget('page') == 'gf_edit_forms' && rgget('view') == 'settings' && rgget('subview') == 'notification' && rgget('nid') )
  1119. return 'notification_edit';
  1120. if( rgget('page') == 'gf_edit_forms' && rgget('view') == 'settings' && rgget('subview') == 'notification' && isset($_GET['nid']) )
  1121. return 'notification_edit';
  1122. if( rgget('page') == 'gf_edit_forms' && rgget('view') == 'settings' && rgget('subview') == 'notification' )
  1123. return 'notification_list';
  1124. if( rgget('page') == 'gf_entries' && (!rgget('view') || rgget('view') == 'entries' ) )
  1125. return 'entry_list';
  1126. if( rgget('page') == 'gf_entries' && rgget('view') == 'entry' && isset($_POST['screen_mode']) && rgpost('screen_mode') != 'view')
  1127. return 'entry_detail_edit';
  1128. if( rgget('page') == 'gf_entries' && rgget('view') == 'entry')
  1129. return 'entry_detail';
  1130. if( rgget('page') == 'gf_settings' )
  1131. return 'settings';
  1132. if(rgget('page') == 'gf_addons')
  1133. return 'addons';
  1134. if(rgget('page') == 'gf_export' && ( rgget('view') == 'export_entry' || !isset($_GET['view']) ) )
  1135. return 'export_entry';
  1136. if(rgget('page') == 'gf_export' && rgget('view') == 'export_form')
  1137. return 'export_form';
  1138. if(rgget('page') == 'gf_export' && rgget('view') == 'import_form')
  1139. return 'import_form';
  1140. return false;
  1141. }
  1142. public static function get_form($form_id, $display_title=true, $display_description=true, $force_display=false, $field_values=null, $ajax=false, $tabindex = 1){
  1143. require_once(GFCommon::get_base_path() . "/form_display.php");
  1144. return GFFormDisplay::get_form($form_id, $display_title, $display_description, $force_display, $field_values, $ajax, $tabindex);
  1145. }
  1146. public static function new_form(){
  1147. self::form_list_page();
  1148. }
  1149. public static function enqueue_scripts(){
  1150. require_once(GFCommon::get_base_path() . "/form_display.php");
  1151. GFFormDisplay::enqueue_scripts();
  1152. }
  1153. public static function print_form_scripts($form, $ajax){
  1154. require_once(GFCommon::get_base_path() . "/form_display.php");
  1155. GFFormDisplay::print_form_scripts($form, $ajax);
  1156. }
  1157. public static function forms_page($form_id){
  1158. require_once(GFCommon::get_base_path() . "/form_detail.php");
  1159. GFFormDetail::forms_page($form_id);
  1160. }
  1161. public static function settings_page(){
  1162. require_once(GFCommon::get_base_path() . "/settings.php");
  1163. GFSettings::settings_page();
  1164. }
  1165. public static function add_settings_page($name, $handle, $icon_path=""){
  1166. require_once(GFCommon::get_base_path() . "/settings.php");
  1167. GFSettings::add_settings_page($name, $handle, $icon_path);
  1168. }
  1169. public static function help_page(){
  1170. require_once(GFCommon::get_base_path() . "/help.php");
  1171. GFHelp::help_page();
  1172. }
  1173. public static function export_page(){
  1174. require_once(GFCommon::get_base_path() . "/export.php");
  1175. GFExport::export_page();
  1176. }
  1177. public static function update_page(){
  1178. require_once(GFCommon::get_base_path() . "/update.php");
  1179. GFUpdate::update_page();
  1180. }
  1181. public static function addons_page(){
  1182. wp_print_styles(array("thickbox"));
  1183. $plugins = get_plugins();
  1184. $installed_plugins = array();
  1185. foreach($plugins as $key => $plugin){
  1186. $is_active = is_plugin_active($key);
  1187. $installed_plugin = array("plugin" => $key, "name" => $plugin["Name"], "is_active"=>$is_active);
  1188. $installed_plugin["activation_url"] = $is_active ? "" : wp_nonce_url("plugins.php?action=activate&plugin={$key}", "activate-plugin_{$key}");
  1189. $installed_plugin["deactivation_url"] = !$is_active ? "" : wp_nonce_url("plugins.php?action=deactivate&plugin={$key}", "deactivate-plugin_{$key}");
  1190. $installed_plugins[] = $installed_plugin;
  1191. }
  1192. $nonces = self::get_addon_nonces();
  1193. $body = array("plugins" => urlencode(serialize($installed_plugins)), "nonces" => urlencode(serialize($nonces)), "key" => GFCommon::get_key());
  1194. $options = array('body' => $body, 'headers' => array('Referer' => get_bloginfo("url")));
  1195. $request_url = GRAVITY_MANAGER_URL . "/api.php?op=plugin_browser&{$_SERVER["QUERY_STRING"]}";
  1196. $raw_response = wp_remote_post($request_url, $options);
  1197. if ( is_wp_error( $raw_response ) || $raw_response['response']['code'] != 200){
  1198. echo "<div class='error' style='margin-top:50px; padding:20px;'>" . __("Add-On browser is currently unavailable. Please try again later.", "gravityforms") . "</div>";
  1199. }
  1200. else{
  1201. echo GFCommon::get_remote_message();
  1202. echo $raw_response["body"];
  1203. }
  1204. }
  1205. public static function get_addon_info($api, $action, $args){
  1206. if($action == "plugin_information" && empty($api) && !rgempty("rg", $_GET)){
  1207. $request_url = GRAVITY_MANAGER_URL . "/api.php?op=get_plugin&slug={$args->slug}";
  1208. $raw_response = wp_remote_post($request_url);
  1209. if ( is_wp_error( $raw_response ) || $raw_response['response']['code'] != 200)
  1210. return false;
  1211. $plugin = unserialize($raw_response["body"]);
  1212. $api = new stdClass();
  1213. $api->name = $plugin["title"];
  1214. $api->version = $plugin["version"];
  1215. $api->download_link = $plugin["download_url"];
  1216. }
  1217. return $api;
  1218. }
  1219. public static function get_addon_nonces(){
  1220. $request_url = GRAVITY_MANAGER_URL . "/api.php?op=get_plugins";
  1221. $raw_response = wp_remote_get($request_url);
  1222. if ( is_wp_error( $raw_response ) || $raw_response['response']['code'] != 200)
  1223. return false;
  1224. $addons = unserialize($raw_response["body"]);
  1225. $nonces = array();
  1226. foreach($addons as $addon){
  1227. $nonces[$addon["key"]] = wp_create_nonce("install-plugin_{$addon["key"]}");
  1228. }
  1229. return $nonces;
  1230. }
  1231. public static function start_export(){
  1232. require_once(GFCommon::get_base_path() . "/export.php");
  1233. GFExport::start_export();
  1234. }
  1235. public static function get_post_category_values(){
  1236. require_once(GFCommon::get_base_path() . "/form_detail.php");
  1237. GFFormDetail::get_post_category_values();
  1238. }
  1239. public static function get_notification_post_category_values(){
  1240. require_once(GFCommon::get_base_path() . "/notification.php");
  1241. GFNotification::get_post_category_values();
  1242. }
  1243. public static function all_leads_page(){
  1244. //displaying lead detail page if lead id is in the query string
  1245. if(rgget('lid') || !rgblank(rgget('pos')))
  1246. {
  1247. require_once(GFCommon::get_base_path() . "/entry_detail.php");
  1248. GFEntryDetail::lead_detail_page();
  1249. }
  1250. else{
  1251. require_once(GFCommon::get_base_path() . "/entry_list.php");
  1252. GFEntryList::all_leads_page();
  1253. }
  1254. }
  1255. public static function form_list_page(){
  1256. require_once(GFCommon::get_base_path() . "/form_list.php");
  1257. GFFormList::form_list_page();
  1258. }
  1259. public static function forms(){
  1260. if(!GFCommon::ensure_wp_version())
  1261. return;
  1262. $id = RGForms::get("id");
  1263. $view = RGForms::get("view");
  1264. if($view == "entries"){
  1265. require_once(GFCommon::get_base_path() . "/entry_list.php");
  1266. GFEntryList::leads_page($id);
  1267. } else if($view == "entry"){
  1268. require_once(GFCommon::get_base_path() . "/entry_detail.php");
  1269. GFEntryDetail::lead_detail_page();
  1270. } else if($view == "notification"){
  1271. require_once(GFCommon::get_base_path() . "/notification.php");
  1272. //GFNotification::notification_page($id);
  1273. } else if($view == 'settings') {
  1274. require_once(GFCommon::get_base_path() . "/form_settings.php");
  1275. GFFormSettings::form_settings_page($id);
  1276. } else if(empty($view)){
  1277. if(is_numeric($id)){
  1278. self::forms_page($id);
  1279. } else{
  1280. self::form_list_page();
  1281. }
  1282. }
  1283. do_action("gform_view", $view, $id);
  1284. }
  1285. public static function get($name, $array=null){
  1286. if(!$array)
  1287. $array = $_GET;
  1288. if(isset($array[$name]))
  1289. return $array[$name];
  1290. return "";
  1291. }
  1292. public static function post($name){
  1293. if(isset($_POST[$name]))
  1294. return $_POST[$name];
  1295. return "";
  1296. }
  1297. // AJAX Function
  1298. public static function resend_notifications(){
  1299. check_admin_referer('gf_resend_notifications', 'gf_resend_notifications');
  1300. $leads = rgpost('leadIds'); // may be a single ID or an array of IDs
  1301. $leads = !is_array($leads) ? array($leads) : $leads;
  1302. $form_id = rgpost('formId');
  1303. $form = apply_filters("gform_before_resend_notifications_{$form_id}", apply_filters('gform_before_resend_notifications', RGFormsModel::get_form_meta($form_id), $leads), $leads);
  1304. if(empty($leads) || empty($form)) {
  1305. _e("There was an error while resending the notifications.", "gravityforms");
  1306. die();
  1307. };
  1308. $notifications = json_decode(rgpost('notifications'));
  1309. if(!is_array($notifications))
  1310. die(__("No notifications have been selected. Please select a notification to be sent.", "gravityforms"));
  1311. if(rgpost('sendTo') && GFCommon::is_invalid_or_empty_email(rgpost('sendTo')))
  1312. die(__("The <strong>Send To</strong> email address provided is not valid.", "gravityforms"));
  1313. foreach($leads as $lead_id){
  1314. $lead = RGFormsModel::get_lead($lead_id);
  1315. foreach($notifications as $notification_id){
  1316. $notification = $form["notifications"][$notification_id];
  1317. if(!$notification)
  1318. continue;
  1319. //overriding To email if one was specified
  1320. if(rgpost('sendTo')){
  1321. $notification["to"] = rgpost('sendTo');
  1322. $notification["toType"] = "email";
  1323. }
  1324. GFCommon::send_notification($notification, $form, $lead);
  1325. }
  1326. }
  1327. die();
  1328. }
  1329. //-------------------------------------------------
  1330. //----------- AJAX CALLS --------------------------
  1331. //captcha image
  1332. public static function captcha_image(){
  1333. $field = array("simpleCaptchaSize" => $_GET["size"], "simpleCaptchaFontColor"=> $_GET["fg"], "simpleCaptchaBackgroundColor"=>$_GET["bg"]);
  1334. if($_GET["type"] == "math")
  1335. $captcha = GFCommon::get_math_captcha($field, $_GET["pos"]);
  1336. else
  1337. $captcha = GFCommon::get_captcha($field);
  1338. @ini_set('memory_limit', '256M');
  1339. $image = imagecreatefrompng($captcha["path"]);
  1340. include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
  1341. wp_stream_image($image, "image/png", 0);
  1342. imagedestroy($image);
  1343. die();
  1344. }
  1345. //entry list
  1346. public static function update_form_active(){
  1347. check_ajax_referer('rg_update_form_active','rg_update_form_active');
  1348. RGFormsModel::update_form_active($_POST["form_id"], $_POST["is_active"]);
  1349. }
  1350. public static function update_lead_property(){
  1351. check_ajax_referer('rg_update_lead_property','rg_update_lead_property');
  1352. RGFormsModel::update_lead_property($_POST["lead_id"], $_POST["name"], $_POST["value"]);
  1353. }
  1354. public static function update_lead_status(){
  1355. check_ajax_referer('gf_delete_entry');
  1356. $status = rgpost("status");
  1357. $lead_id = rgpost("entry");
  1358. switch($status){
  1359. case "unspam" :
  1360. //TODO: call akismet and set entry as not spam.
  1361. RGFormsModel::update_lead_property($lead_id, "status", "active");
  1362. break;
  1363. case "delete" :
  1364. RGFormsModel::delete_lead($lead_id);
  1365. break;
  1366. default :
  1367. RGFormsModel::update_lead_property($lead_id, "status", $status);
  1368. break;
  1369. }
  1370. header("Content-Type: text/xml");
  1371. echo "<?xml version='1.0' standalone='yes'?><wp_ajax></wp_ajax>";
  1372. exit();
  1373. }
  1374. //settings
  1375. public static function upgrade_license(){
  1376. require_once(GFCommon::get_base_path() . "/settings.php");
  1377. GFSettings::upgrade_license();
  1378. }
  1379. //form detail
  1380. public static function save_form(){
  1381. require_once(GFCommon::get_base_path() . "/form_detail.php");
  1382. GFFormDetail::save_form();
  1383. }
  1384. public static function add_field(){
  1385. require_once(GFCommon::get_base_path() . "/form_detail.php");
  1386. GFFormDetail::add_field();
  1387. }
  1388. public static function duplicate_field(){
  1389. require_once(GFCommon::get_base_path() . "/form_detail.php");
  1390. GFFormDetail::duplicate_field();
  1391. }
  1392. public static function delete_field(){
  1393. require_once(GFCommon::get_base_path() . "/form_detail.php");
  1394. GFFormDetail::delete_field();
  1395. }
  1396. public static function change_input_type(){
  1397. require_once(GFCommon::get_base_path() . "/form_detail.php");
  1398. GFFormDetail::change_input_type();
  1399. }
  1400. public static function delete_custom_choice(){
  1401. require_once(GFCommon::get_base_path() . "/form_detail.php");
  1402. GFFormDetail::delete_custom_choice();
  1403. }
  1404. public static function save_custom_choice(){
  1405. require_once(GFCommon::get_base_path() . "/form_detail.php");
  1406. GFFormDetail::save_custom_choice();
  1407. }
  1408. //entry detail
  1409. public static function delete_file(){
  1410. check_ajax_referer("rg_delete_file", "rg_delete_file");
  1411. $lead_id = intval($_POST["lead_id"]);
  1412. $field_id = intval($_POST["field_id"]);
  1413. RGFormsModel::delete_file($lead_id, $field_id);
  1414. die("EndDeleteFile($field_id);");
  1415. }
  1416. //export
  1417. public static function select_export_form(){
  1418. check_ajax_referer("rg_select_export_form", "rg_select_export_form");
  1419. $form_id = intval($_POST["form_id"]);
  1420. $form = RGFormsModel::get_form_meta($form_id);
  1421. $fields = array();
  1422. $form = GFExport::add_default_export_fields($form);
  1423. if(is_array($form["fields"])){
  1424. foreach($form["fields"] as $field){
  1425. if(is_array(rgar($field,"inputs"))){
  1426. foreach($field["inputs"] as $input)
  1427. $fields[] = array($input["id"], GFCommon::get_label($field, $input["id"]));
  1428. }
  1429. else if(!rgar($field,"displayOnly")){
  1430. $fields[] = array($field["id"], GFCommon::get_label($field));
  1431. }
  1432. }
  1433. }
  1434. $field_json = GFCommon::json_encode($fields);
  1435. die("EndSelectExportForm($field_json);");
  1436. }
  1437. // form settings
  1438. public static function save_confirmation() {
  1439. require_once(GFCommon::get_base_path() . '/form_settings.php');
  1440. GFFormSettings::save_confirmation();
  1441. }
  1442. public static function delete_confirmation() {
  1443. require_once(GFCommon::get_base_path() . '/form_settings.php');
  1444. GFFormSettings::delete_confirmation();
  1445. }
  1446. // form list
  1447. public static function save_new_form() {
  1448. require_once(GFCommon::get_base_path() . '/form_list.php');
  1449. GFFormList::save_new_form();
  1450. }
  1451. public static function top_toolbar(){
  1452. $forms = RGFormsModel::get_forms(null, "title");
  1453. $id = rgempty("id", $_GET) ? count($forms) > 0 ? $forms[0]->id : "0" : rgget("id");
  1454. ?>
  1455. <script type="text/javascript">
  1456. function GF_ReplaceQuery(key, newValue){
  1457. var new_query = "";
  1458. var query = document.location.search.substring(1);
  1459. var ary = query.split("&");
  1460. var has_key=false;
  1461. for (i=0; i < ary.length; i++) {
  1462. var key_value = ary[i].split("=");
  1463. if (key_value[0] == key){
  1464. new_query += key + "=" + newValue + "&";
  1465. has_key = true;
  1466. }
  1467. else if(key_value[0] != "display_settings"){
  1468. new_query += key_value[0] + "=" + key_value[1] + "&";
  1469. }
  1470. }
  1471. if(new_query.length > 0)
  1472. new_query = new_query.substring(0, new_query.length-1);
  1473. if(!has_key)
  1474. new_query += new_query.length > 0 ? "&" + key + "=" + newValue : "?" + key + "=" + newValue;
  1475. return new_query;
  1476. }
  1477. function GF_RemoveQuery(key, query){
  1478. var new_query = "";
  1479. if (query == "")
  1480. {
  1481. query = document.location.search.substring(1);
  1482. }
  1483. var ary = query.split("&");
  1484. for (i=0; i < ary.length; i++) {
  1485. var key_value = ary[i].split("=");
  1486. if (key_value[0] != key){
  1487. new_query += key_value[0] + "=" + key_value[1] + "&";
  1488. }
  1489. }
  1490. if(new_query.length > 0)
  1491. new_query = new_query.substring(0, new_query.length-1);
  1492. return new_query;
  1493. }
  1494. function GF_SwitchForm(id){
  1495. if(id.length > 0){
  1496. query = GF_ReplaceQuery("id", id);
  1497. //remove paging from querystring when changing forms
  1498. new_query = GF_RemoveQuery("paged", query);
  1499. new_query = new_query.replace("gf_new_form", "gf_edit_forms");
  1500. document.location = "?" + new_query;
  1501. }
  1502. }
  1503. function ToggleFormSettings(){
  1504. FieldClick(jQuery('#gform_heading')[0]);
  1505. }
  1506. jQuery(document).ready(function(){
  1507. if(document.location.search.indexOf("display_settings") > 0)
  1508. ToggleFormSettings()
  1509. jQuery('a.gf_toolbar_disabled').click(function(event){
  1510. event.preventDefault();
  1511. });
  1512. });
  1513. </script>
  1514. <div id="gf_form_toolbar">
  1515. <ul id="gf_form_toolbar_links">
  1516. <?php
  1517. $menu_items = apply_filters("gform_toolbar_menu", self::get_toolbar_menu_items($id), $id);
  1518. echo self::format_toolbar_menu_items($menu_items);
  1519. ?>
  1520. <li class="gf_form_switcher">
  1521. <label for="export_form"><?php _e("Select A Form", "gravityforms") ?></label>
  1522. <?php
  1523. if(RG_CURRENT_VIEW != 'entry'){ ?>
  1524. <select name="form_switcher" id="form_switcher" onchange="GF_SwitchForm(jQuery(this).val());">
  1525. <option value=""><?php _e("Switch Form", "gravityforms") ?></option>
  1526. <?php
  1527. foreach($forms as $form_info){
  1528. ?>
  1529. <option value="<?php echo $form_info->id ?>"><?php echo $form_info->title ?></option>
  1530. <?php
  1531. }
  1532. ?>
  1533. </select>
  1534. <?php
  1535. } // end view check ?>
  1536. </li>
  1537. </ul>
  1538. </div>
  1539. <?php
  1540. }
  1541. public static function format_toolbar_menu_items($menu_items, $compact = false){
  1542. if (empty($menu_items))
  1543. return "";
  1544. $output = "";
  1545. $priorities = array();
  1546. foreach($menu_items as $k => $menu_item){
  1547. $priorities[$k] = rgar($menu_item,"priority");
  1548. }
  1549. array_multisort($priorities, SORT_DESC, $menu_items);
  1550. $last_key = array_pop(array_keys($menu_items));
  1551. foreach($menu_items as $key => $menu_item){
  1552. if(is_array($menu_item)){
  1553. if(GFCommon::current_user_can_any(rgar($menu_item, "capabilities"))){
  1554. $sub_menu_str = "";
  1555. $count_sub_menu_items = 0;
  1556. $sub_menu_items = rgar($menu_item, "sub_menu_items");
  1557. if (is_array($sub_menu_items)){
  1558. foreach($sub_menu_items as $k => $val){
  1559. if(false === GFCommon::current_user_can_any(rgar($sub_menu_items[$k], "capabilities")))
  1560. unset($sub_menu_items[$k]);
  1561. }
  1562. $count_sub_menu_items = count($sub_menu_items);
  1563. }
  1564. $menu_class = rgar($menu_item, "menu_class");
  1565. if ($count_sub_menu_items == 1){
  1566. $label = $compact ? rgar($menu_item, "label") : rgar($sub_menu_items[0], "label");
  1567. $menu_item = $sub_menu_items[0];
  1568. } else {
  1569. $label = rgar($menu_item, "label");
  1570. $sub_menu_str = self::toolbar_sub_menu_items($sub_menu_items, $compact);
  1571. }
  1572. $link_class = rgar($menu_item, "link_class");
  1573. $url = rgar($menu_item, "url");
  1574. $title = rgar($menu_item, "title");
  1575. $onclick = rgar($menu_item, "onclick");
  1576. $target = rgar($menu_item, "target");
  1577. $link = "<a class='{$link_class}' onclick='{$onclick}' title='{$title}' href='{$url}' target='{$target}'>{$label}</a>" . $sub_menu_str;
  1578. if($compact){
  1579. if ($key == "delete")
  1580. $link = apply_filters("gform_form_delete_link", $link);
  1581. $divider = $key == $last_key ? '' : " | ";
  1582. if($count_sub_menu_items > 0)
  1583. $menu_class .= " gf_form_action_has_submenu";
  1584. $output .= '<span class="' . $menu_class . '">'. $link . $divider . '</span>';
  1585. } else {
  1586. $output .= "<li class='{$menu_class}'>{$link}</li>";
  1587. }
  1588. }
  1589. } elseif($compact) {
  1590. //for backwards compatibility <1.7: form actions only
  1591. $divider = $key == $last_key ? '' : " | ";
  1592. $output .= '<span class="edit">'. $menu_item . $divider . '</span>';
  1593. }
  1594. }
  1595. return $output;
  1596. }
  1597. public static function get_toolbar_menu_items($form_id, $compact = false){
  1598. $menu_items = array();
  1599. //---- Form Editor ----
  1600. $edit_capabilities = array("gravityforms_edit_forms");
  1601. $menu_items['edit'] = array(
  1602. 'label' => $compact ? __("Edit", "gravityforms") : __("Form Editor", "gravityforms"),
  1603. 'title' => __('Edit this form', 'gravityforms'),
  1604. 'url' => '?page=gf_edit_forms&id=' . $form_id,
  1605. 'menu_class' => 'gf_form_toolbar_editor',
  1606. 'link_class' => self::toolbar_class("editor"),
  1607. 'capabilities' => $edit_capabilities,
  1608. 'priority' => 1000
  1609. );
  1610. //---- Form Settings ----
  1611. $sub_menu_items = self::get_form_settings_sub_menu_items($form_id);
  1612. $menu_items['settings'] = array(
  1613. 'label' => $compact ? __("Settings", "gravityforms") : __("Form Settings", "gravityforms"),
  1614. 'title' => __('Edit settings for this form', 'gravityforms'),
  1615. 'url' => '?page=gf_edit_forms&view=settings&id=' . $form_id,
  1616. 'menu_class' => 'gf_form_toolbar_settings',
  1617. 'link_class' => self::toolbar_class("settings"),
  1618. 'sub_menu_items' => $sub_menu_items,
  1619. 'capabilities' => $edit_capabilities,
  1620. 'priority' => 900
  1621. );
  1622. //---- Entries ----
  1623. $entries_capabilities = array('gravityforms_view_entries','gravityforms_edit_entries','gravityforms_delete_entries');
  1624. $menu_items['entries'] = array(
  1625. 'label' => __("Entries", "gravityforms"),
  1626. 'title' => __('View entries generated by this form', 'gravityforms'),
  1627. 'url' => '?page=gf_entries&id=' . $form_id,
  1628. 'menu_class' => 'gf_form_toolbar_entries',
  1629. 'link_class' => self::toolbar_class("entries"),
  1630. 'capabilities' => $entries_capabilities,
  1631. 'priority' => 800
  1632. );
  1633. //---- Preview ----
  1634. $preview_capabilities = array("gravityforms_edit_forms", "gravityforms_create_form", "gravityforms_preview_forms");
  1635. $menu_items['preview'] = array(
  1636. 'label' => __("Preview", "gravityforms"),
  1637. 'title' => __('Preview this form', 'gravityforms'),
  1638. 'url' => trailingslashit(site_url()) . '?gf_page=preview&id=' . $form_id,
  1639. 'menu_class' => 'gf_form_toolbar_preview',
  1640. 'link_class' => self::toolbar_class("preview"),
  1641. 'target' => '_blank',
  1642. 'capabilities' => $preview_capabilities,
  1643. 'priority' => 700
  1644. );
  1645. return $menu_items;
  1646. }
  1647. public static function toolbar_sub_menu_items($menu_items, $compact = false){
  1648. if (empty($menu_items))
  1649. return "";
  1650. $sub_menu_items_string = "";
  1651. foreach ($menu_items as $menu_item){
  1652. if(GFCommon::current_user_can_any(rgar($menu_item, "capabilities"))){
  1653. $menu_class = rgar($menu_item, "menu_class");
  1654. $link_class = rgar($menu_item, "link_class");
  1655. $url = rgar($menu_item, "url");
  1656. $label = rgar($menu_item, "label");
  1657. $target = rgar($menu_item, "target");
  1658. $sub_menu_items_string .= "<li class='{$menu_class}'><a href='{$url}' class='{$link_class}' target='{$target}'>{$label}</a></li>";
  1659. }
  1660. }
  1661. if($compact){
  1662. $sub_menu_items_string = '<div class="gf_submenu"><ul>' . $sub_menu_items_string . '</ul></div>';
  1663. }else{
  1664. $sub_menu_items_string = '<div class="gf_submenu"><ul>' . $sub_menu_items_string . '</ul></div>';
  1665. }
  1666. return $sub_menu_items_string;
  1667. }
  1668. public static function get_form_settings_sub_menu_items($form_id) {
  1669. require_once(GFCommon::get_base_path() . '/form_settings.php');
  1670. $sub_menu_items = array();
  1671. $tabs = GFFormSettings::get_tabs($form_id);
  1672. foreach($tabs as $tab) {
  1673. if($tab['name'] == 'settings')
  1674. $form_setting_menu_item['label'] = 'Settings';
  1675. $sub_menu_items[] = array(
  1676. 'url' => admin_url("admin.php?page=gf_edit_forms&view=settings&subview={$tab['name']}&id={$form_id}"),
  1677. 'label' => $tab['label'],
  1678. 'capabilities' => array("gravityforms_edit_forms")
  1679. );
  1680. }
  1681. return $sub_menu_items;
  1682. }
  1683. private static function toolbar_class($item){
  1684. switch($item){
  1685. case "editor":
  1686. if(in_array(rgget("page"), array("gf_edit_forms", "gf_new_form")) && rgempty("view", $_GET))
  1687. return "gf_toolbar_active";
  1688. break;
  1689. case "settings":
  1690. if(rgget('view') == 'settings')
  1691. return "gf_toolbar_active";
  1692. break;
  1693. case "notifications" :
  1694. if(rgget("page") == "gf_new_form")
  1695. return "gf_toolbar_disabled";
  1696. else if(rgget("page") == "gf_edit_forms" && rgget("view") == "notification")
  1697. return "gf_toolbar_active";
  1698. break;
  1699. case "entries" :
  1700. if(rgget("page") == "gf_new_form")
  1701. return "gf_toolbar_disabled";
  1702. else if(rgget("page") == "gf_entries")
  1703. return "gf_toolbar_active";
  1704. break;
  1705. case "preview" :
  1706. if(rgget("page") == "gf_new_form")
  1707. return "gf_toolbar_disabled";
  1708. break;
  1709. }
  1710. return "";
  1711. }
  1712. public static function admin_bar() {
  1713. global $wp_admin_bar;
  1714. if(!GFCommon::current_user_can_any("gravityforms_create_form"))
  1715. return;
  1716. $wp_admin_bar->add_menu(
  1717. array(
  1718. 'id' => 'gravityforms-new-form',
  1719. 'parent' => 'new-content',
  1720. 'title' => esc_attr__( 'Form', 'gravityforms' ),
  1721. 'href' => admin_url( 'admin.php?page="gf_new_form' )
  1722. )
  1723. );
  1724. }
  1725. }
  1726. class RGForms extends GFForms { }
  1727. //Main function call. Should be used to insert a Gravity Form from code.
  1728. function gravity_form($id, $display_title=true, $display_description=true, $display_inactive=false, $field_values=null, $ajax=false, $tabindex = 1){
  1729. echo RGForms::get_form($id, $display_title, $display_description, $display_inactive, $field_values, $ajax, $tabindex);
  1730. }
  1731. //Enqueues the appropriate scripts for the specified form
  1732. function gravity_form_enqueue_scripts($form_id, $is_ajax=false){
  1733. if(!is_admin()){
  1734. require_once(GFCommon::get_base_path() . "/form_display.php");
  1735. $form = RGFormsModel::get_form_meta($form_id);
  1736. GFFormDisplay::enqueue_form_scripts($form, $is_ajax);
  1737. }
  1738. }
  1739. if(!function_exists("rgget")){
  1740. function rgget($name, $array=null){
  1741. if(!isset($array))
  1742. $array = $_GET;
  1743. if(isset($array[$name]))
  1744. return $array[$name];
  1745. return "";
  1746. }
  1747. }
  1748. if(!function_exists("rgpost")){
  1749. function rgpost($name, $do_stripslashes=true){
  1750. if(isset($_POST[$name]))
  1751. return $do_stripslashes ? stripslashes_deep($_POST[$name]) : $_POST[$name];
  1752. return "";
  1753. }
  1754. }
  1755. if(!function_exists("rgar")){
  1756. function rgar($array, $name){
  1757. if(isset($array[$name]))
  1758. return $array[$name];
  1759. return '';
  1760. }
  1761. }
  1762. if(!function_exists("rgars")){
  1763. function rgars($array, $name){
  1764. $names = explode("/", $name);
  1765. $val = $array;
  1766. foreach($names as $current_name){
  1767. $val = rgar($val, $current_name);
  1768. }
  1769. return $val;
  1770. }
  1771. }
  1772. if(!function_exists("rgempty")){
  1773. function rgempty($name, $array = null){
  1774. if(!$array)
  1775. $array = $_POST;
  1776. $val = rgget($name, $array);
  1777. return empty($val);
  1778. }
  1779. }
  1780. if(!function_exists("rgblank")){
  1781. function rgblank($text){
  1782. return empty($text) && strval($text) != "0";
  1783. }
  1784. }
  1785. if(!function_exists("rgobj")){
  1786. function rgobj($obj, $name){
  1787. if(isset($obj->$name))
  1788. return $obj->$name;
  1789. return '';
  1790. }
  1791. }
  1792. if(!function_exists("rgexplode")){
  1793. function rgexplode($sep, $string, $count){
  1794. $ary = explode($sep, $string);
  1795. while(count($ary) < $count)
  1796. $ary[] = "";
  1797. return $ary;
  1798. }
  1799. }
  1800. ?>