PageRenderTime 45ms CodeModel.GetById 13ms 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

Large files files are truncated, but you can click here to view the full file

  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_basenam

Large files files are truncated, but you can click here to view the full file