PageRenderTime 58ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/gravityforms/gravityforms.php

https://bitbucket.org/nathancorbier/wastark.com
PHP | 1971 lines | 1419 code | 396 blank | 156 comment | 254 complexity | a0c0176dd3514a0e7453898d8fcad44a MD5 | raw file
Possible License(s): BSD-3-Clause

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.6.12
  7. Author: rocketgenius
  8. Author URI: http://www.rocketgenius.com
  9. ------------------------------------------------------------------------
  10. Copyright 2009-2011 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. define("RG_CURRENT_VIEW", RGForms::get("view"));
  50. define("GF_MIN_WP_VERSION", '3.2');
  51. define("GF_SUPPORTED_WP_VERSION", version_compare(get_bloginfo("version"), GF_MIN_WP_VERSION, '>='));
  52. if(!defined("GRAVITY_MANAGER_URL"))
  53. define("GRAVITY_MANAGER_URL", "http://www.gravityhelp.com/wp-content/plugins/gravitymanager");
  54. //initializing translations
  55. load_plugin_textdomain( 'gravityforms', false, '/gravityforms/languages' );
  56. require_once(WP_PLUGIN_DIR . "/" . basename(dirname(__FILE__)) . "/common.php");
  57. require_once(WP_PLUGIN_DIR . "/" . basename(dirname(__FILE__)) . "/forms_model.php");
  58. require_once(WP_PLUGIN_DIR . "/" . basename(dirname(__FILE__)) . "/widget.php");
  59. add_action('init', array('RGForms', 'init'));
  60. add_action('wp', array('RGForms', 'maybe_process_form'), 9);
  61. add_action('wp', array('RGForms', 'process_exterior_pages'));
  62. add_filter('user_has_cap', array("RGForms", "user_has_cap"), 10, 3);
  63. //Hooks for no-conflict functionality
  64. if(is_admin() && (RGForms::is_gravity_page() || RGForms::is_gravity_ajax_action())){
  65. add_action("wp_print_scripts", array("RGForms", "no_conflict_mode_script"), 1000);
  66. add_action("admin_print_footer_scripts", array("RGForms", "no_conflict_mode_script"), 9);
  67. add_action("wp_print_styles", array("RGForms", "no_conflict_mode_style"), 1000);
  68. add_action("admin_print_styles", array("RGForms", "no_conflict_mode_style"), 1);
  69. add_action("admin_print_footer_scripts", array("RGForms", "no_conflict_mode_style"), 1);
  70. add_action("admin_footer", array("RGForms", "no_conflict_mode_style"), 1);
  71. }
  72. class GFForms{
  73. public static function has_members_plugin(){
  74. return function_exists( 'members_get_capabilities' );
  75. }
  76. //Plugin starting point. Will load appropriate files
  77. public static function init(){
  78. add_filter("gform_logging_supported", array("RGForms", "set_logging_supported"));
  79. if(IS_ADMIN){
  80. global $current_user;
  81. //Members plugin integration. Adding Gravity Forms roles to the checkbox list
  82. if (self::has_members_plugin())
  83. add_filter('members_get_capabilities', array("RGForms", "members_get_capabilities"));
  84. //Loading Gravity Forms if user has access to any functionality
  85. if(GFCommon::current_user_can_any(GFCommon::all_caps()))
  86. {
  87. require_once(GFCommon::get_base_path() . "/export.php");
  88. GFExport::maybe_export();
  89. //runs the setup when version changes
  90. self::setup();
  91. //creates the "Forms" left menu
  92. add_action('admin_menu', array('RGForms', 'create_menu'));
  93. if(GF_SUPPORTED_WP_VERSION){
  94. add_action('admin_footer', array('RGForms', 'check_upload_folder'));
  95. add_action('wp_dashboard_setup', array('RGForms', 'dashboard_setup'));
  96. //Adding "embed form" button
  97. add_action('media_buttons', array('RGForms', 'add_form_button'), 20);
  98. //Plugin update actions
  99. add_filter("transient_update_plugins", array('RGForms', 'check_update'));
  100. add_filter("site_transient_update_plugins", array('RGForms', 'check_update'));
  101. if(in_array(RG_CURRENT_PAGE, array('post.php', 'page.php', 'page-new.php', 'post-new.php'))){
  102. add_action('admin_footer', array('RGForms', 'add_mce_popup'));
  103. }
  104. else if(self::is_gravity_page()){
  105. require_once(GFCommon::get_base_path() . "/tooltips.php");
  106. add_action("admin_print_scripts", array('RGForms', 'print_scripts'));
  107. }
  108. else if(RG_CURRENT_PAGE == 'media-upload.php'){
  109. require_once(GFCommon::get_base_path() . "/entry_list.php");
  110. }
  111. else if(in_array(RG_CURRENT_PAGE, array("admin.php", "admin-ajax.php"))){
  112. add_action('wp_ajax_rg_save_form', array('RGForms', 'save_form'));
  113. add_action('wp_ajax_rg_change_input_type', array('RGForms', 'change_input_type'));
  114. add_action('wp_ajax_rg_add_field', array('RGForms', 'add_field'));
  115. add_action('wp_ajax_rg_duplicate_field', array('RGForms', 'duplicate_field'));
  116. add_action('wp_ajax_rg_delete_field', array('RGForms', 'delete_field'));
  117. add_action('wp_ajax_rg_delete_file', array('RGForms', 'delete_file'));
  118. add_action('wp_ajax_rg_select_export_form', array('RGForms', 'select_export_form'));
  119. add_action('wp_ajax_rg_start_export', array('RGForms', 'start_export'));
  120. add_action('wp_ajax_gf_upgrade_license', array('RGForms', 'upgrade_license'));
  121. add_action('wp_ajax_gf_delete_custom_choice', array('RGForms', 'delete_custom_choice'));
  122. add_action('wp_ajax_gf_save_custom_choice', array('RGForms', 'save_custom_choice'));
  123. add_action('wp_ajax_gf_get_post_categories', array('RGForms', 'get_post_category_values'));
  124. add_action('wp_ajax_gf_get_notification_post_categories', array('RGForms', 'get_notification_post_category_values'));
  125. //entry list ajax operations
  126. add_action('wp_ajax_rg_update_lead_property', array('RGForms', 'update_lead_property'));
  127. add_action('wp_ajax_delete-gf_entry', array('RGForms', 'update_lead_status'));
  128. //form list ajax operations
  129. add_action('wp_ajax_rg_update_form_active', array('RGForms', 'update_form_active'));
  130. //dynamic captcha image
  131. add_action('wp_ajax_rg_captcha_image', array('RGForms', 'captcha_image'));
  132. //dashboard message "dismiss upgrade" link
  133. add_action("wp_ajax_rg_dismiss_upgrade", array('RGForms', 'dashboard_dismiss_upgrade'));
  134. // entry detial: resend notifications
  135. add_action("wp_ajax_gf_resend_notifications", array('RGForms', 'resend_notifications'));
  136. }
  137. add_filter("plugins_api", array("RGForms", "get_addon_info"), 10, 3);
  138. add_action('after_plugin_row_gravityforms/gravityforms.php', array('RGForms', 'plugin_row') );
  139. add_action('install_plugins_pre_plugin-information', array('RGForms', 'display_changelog'));
  140. add_filter('plugin_action_links', array('RGForms', 'plugin_settings_link'),10,2);
  141. }
  142. }
  143. }
  144. else{
  145. add_action('wp_enqueue_scripts', array('RGForms', 'enqueue_scripts'));
  146. add_action('wp', array('RGForms', 'ajax_parse_request'), 10);
  147. // ManageWP premium update filters
  148. add_filter( 'mwp_premium_update_notification', array('RGForms', 'premium_update_push') );
  149. add_filter( 'mwp_premium_perform_update', array('RGForms', 'premium_update') );
  150. }
  151. add_shortcode('gravityform', array('RGForms', 'parse_shortcode'));
  152. add_shortcode('gravityforms', array('RGForms', 'parse_shortcode'));
  153. }
  154. public static function set_logging_supported($plugins)
  155. {
  156. $plugins["gravityforms"] = "Gravity Forms Core";
  157. return $plugins;
  158. }
  159. public static function maybe_process_form(){
  160. $form_id = isset($_POST["gform_submit"]) ? $_POST["gform_submit"] : 0;
  161. if($form_id){
  162. $form_info = RGFormsModel::get_form($form_id);
  163. $is_valid_form = $form_info && $form_info->is_active;
  164. if($is_valid_form){
  165. require_once(GFCommon::get_base_path() . "/form_display.php");
  166. GFFormDisplay::process_form($form_id);
  167. }
  168. }
  169. }
  170. public static function process_exterior_pages(){
  171. if(rgempty("gf_page", $_GET))
  172. return;
  173. //ensure users are logged in
  174. if(!is_user_logged_in())
  175. auth_redirect();
  176. switch(rgget("gf_page")){
  177. case "preview":
  178. require_once(GFCommon::get_base_path() . "/preview.php");
  179. break;
  180. case "print-entry" :
  181. require_once(GFCommon::get_base_path() . "/print-entry.php");
  182. break;
  183. case "select_columns" :
  184. require_once(GFCommon::get_base_path() . "/select_columns.php");
  185. break;
  186. }
  187. exit();
  188. }
  189. public static function check_update($update_plugins_option){
  190. if(!class_exists("GFCommon"))
  191. require_once("common.php");
  192. return GFCommon::check_update($update_plugins_option, true);
  193. }
  194. //Creates or updates database tables. Will only run when version changes
  195. public static function setup($force_setup = false){
  196. global $wpdb;
  197. $version = GFCommon::$version;
  198. if(get_option("rg_form_version") != $version || $force_setup){
  199. $error = "";
  200. if(!self::has_database_permission($error)){
  201. ?>
  202. <div class='error' style="padding:15px;"><?php echo $error?></div>
  203. <?php
  204. }
  205. require_once(ABSPATH . '/wp-admin/includes/upgrade.php');
  206. if ( ! empty($wpdb->charset) )
  207. $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
  208. if ( ! empty($wpdb->collate) )
  209. $charset_collate .= " COLLATE $wpdb->collate";
  210. //Fixes issue with dbDelta lower-casing table names, which cause problems on case sensitive DB servers.
  211. add_filter( 'dbdelta_create_queries', array("RGForms", "dbdelta_fix_case"));
  212. //------ FORM -----------------------------------------------
  213. $form_table_name = RGFormsModel::get_form_table_name();
  214. $sql = "CREATE TABLE " . $form_table_name . " (
  215. id mediumint(8) unsigned not null auto_increment,
  216. title varchar(150) not null,
  217. date_created datetime not null,
  218. is_active tinyint(1) not null default 1,
  219. PRIMARY KEY (id)
  220. ) $charset_collate;";
  221. dbDelta($sql);
  222. //droping table that was created by mistake in version 1.6.3.2
  223. $wpdb->query("DROP TABLE IF EXISTS A" . $form_table_name);
  224. //------ META -----------------------------------------------
  225. $meta_table_name = RGFormsModel::get_meta_table_name();
  226. $sql = "CREATE TABLE " . $meta_table_name . " (
  227. form_id mediumint(8) unsigned not null,
  228. display_meta longtext,
  229. entries_grid_meta longtext,
  230. PRIMARY KEY (form_id)
  231. ) $charset_collate;";
  232. dbDelta($sql);
  233. //droping outdated form_id index (if one exists)
  234. self::drop_index($meta_table_name, 'form_id');
  235. //------ FORM VIEW -----------------------------------------------
  236. $form_view_table_name = RGFormsModel::get_form_view_table_name();
  237. $sql = "CREATE TABLE " . $form_view_table_name . " (
  238. id bigint(20) unsigned not null auto_increment,
  239. form_id mediumint(8) unsigned not null,
  240. date_created datetime not null,
  241. ip char(15),
  242. count mediumint(8) unsigned not null default 1,
  243. PRIMARY KEY (id),
  244. KEY form_id (form_id)
  245. ) $charset_collate;";
  246. dbDelta($sql);
  247. //------ LEAD -----------------------------------------------
  248. $lead_table_name = RGFormsModel::get_lead_table_name();
  249. $sql = "CREATE TABLE " . $lead_table_name . " (
  250. id int(10) unsigned not null auto_increment,
  251. form_id mediumint(8) unsigned not null,
  252. post_id bigint(20) unsigned,
  253. date_created datetime not null,
  254. is_starred tinyint(1) not null default 0,
  255. is_read tinyint(1) not null default 0,
  256. ip varchar(39) not null,
  257. source_url varchar(200) not null default '',
  258. user_agent varchar(250) not null default '',
  259. currency varchar(5),
  260. payment_status varchar(15),
  261. payment_date datetime,
  262. payment_amount decimal(19,2),
  263. transaction_id varchar(50),
  264. is_fulfilled tinyint(1),
  265. created_by bigint(20) unsigned,
  266. transaction_type tinyint(1),
  267. status varchar(20) not null default 'active',
  268. PRIMARY KEY (id),
  269. KEY form_id (form_id),
  270. KEY status (status)
  271. ) $charset_collate;";
  272. dbDelta($sql);
  273. //------ LEAD NOTES ------------------------------------------
  274. $lead_notes_table_name = RGFormsModel::get_lead_notes_table_name();
  275. $sql = "CREATE TABLE " . $lead_notes_table_name . " (
  276. id int(10) unsigned not null auto_increment,
  277. lead_id int(10) unsigned not null,
  278. user_name varchar(250),
  279. user_id bigint(20),
  280. date_created datetime not null,
  281. value longtext,
  282. PRIMARY KEY (id),
  283. KEY lead_id (lead_id),
  284. KEY lead_user_key (lead_id,user_id)
  285. ) $charset_collate;";
  286. dbDelta($sql);
  287. //------ LEAD DETAIL -----------------------------------------
  288. $lead_detail_table_name = RGFormsModel::get_lead_details_table_name();
  289. $sql = "CREATE TABLE " . $lead_detail_table_name . " (
  290. id bigint(20) unsigned not null auto_increment,
  291. lead_id int(10) unsigned not null,
  292. form_id mediumint(8) unsigned not null,
  293. field_number float not null,
  294. value varchar(". GFORMS_MAX_FIELD_LENGTH ."),
  295. PRIMARY KEY (id),
  296. KEY form_id (form_id),
  297. KEY lead_id (lead_id)
  298. ) $charset_collate;";
  299. dbDelta($sql);
  300. //------ LEAD DETAIL LONG -----------------------------------
  301. $lead_detail_long_table_name = RGFormsModel::get_lead_details_long_table_name();
  302. $sql = "CREATE TABLE " . $lead_detail_long_table_name . " (
  303. lead_detail_id bigint(20) unsigned not null,
  304. value longtext,
  305. PRIMARY KEY (lead_detail_id)
  306. ) $charset_collate;";
  307. dbDelta($sql);
  308. //droping outdated form_id index (if one exists)
  309. self::drop_index($lead_detail_long_table_name, 'lead_detail_key');
  310. //------ LEAD META -----------------------------------
  311. $lead_meta_table_name = RGFormsModel::get_lead_meta_table_name();
  312. $sql = "CREATE TABLE " . $lead_meta_table_name . " (
  313. id bigint(20) unsigned not null auto_increment,
  314. lead_id bigint(20) unsigned not null,
  315. meta_key varchar(255),
  316. meta_value longtext,
  317. PRIMARY KEY (id),
  318. KEY meta_key (meta_key),
  319. KEY lead_id (lead_id)
  320. ) $charset_collate;";
  321. dbDelta($sql);
  322. remove_filter('dbdelta_create_queries', array("RGForms", "dbdelta_fix_case"));
  323. //fix checkbox value. needed for version 1.0 and below but won't hurt for higher versions
  324. self::fix_checkbox_value();
  325. //auto-setting license key based on value configured via the GF_LICENSE_KEY constant or the gf_license_key variable
  326. global $gf_license_key;
  327. $license_key = defined("GF_LICENSE_KEY") && empty($gf_license_key) ? GF_LICENSE_KEY : $gf_license_key;
  328. if(!empty($license_key))
  329. update_option("rg_gforms_key", md5($license_key));
  330. //auto-setting recaptcha keys based on value configured via the constant or global variable
  331. global $gf_recaptcha_public_key, $gf_recaptcha_private_key;
  332. $private_key = defined("GF_RECAPTCHA_PRIVATE_KEY") && empty($gf_recaptcha_private_key) ? GF_RECAPTCHA_PRIVATE_KEY : $gf_recaptcha_private_key;
  333. if(!empty($private_key))
  334. update_option("rg_gforms_captcha_private_key", $private_key);
  335. $public_key = defined("GF_RECAPTCHA_PUBLIC_KEY") && empty($gf_recaptcha_public_key) ? GF_RECAPTCHA_PUBLIC_KEY : $gf_recaptcha_public_key;
  336. if(!empty($public_key))
  337. update_option("rg_gforms_captcha_public_key", $public_key);
  338. //Auto-importing forms based on GF_IMPORT_FILE AND GF_THEME_IMPORT_FILE
  339. if(defined("GF_IMPORT_FILE") && !get_option("gf_imported_file")){
  340. GFExport::import_file(GF_IMPORT_FILE);
  341. update_option("gf_imported_file", true);
  342. }
  343. //adds empty index.php files to upload folders. only for v1.5.2 and below
  344. if(version_compare(get_option("rg_form_version"), "1.6", "<")){
  345. self::add_empty_index_files();
  346. }
  347. update_option("rg_form_version", $version);
  348. }
  349. //Import theme specific forms if configured. Will only import forms once per theme.
  350. if(defined("GF_THEME_IMPORT_FILE")){
  351. $themes = get_option("gf_imported_theme_file");
  352. if(!is_array($themes))
  353. $themes = array();
  354. //if current theme has already imported it's forms, don't import again
  355. $theme = get_template();
  356. if(!isset($themes[$theme])){
  357. //importing forms
  358. GFExport::import_file(get_stylesheet_directory() . "/" . GF_THEME_IMPORT_FILE);
  359. //adding current theme to the list of imported themes. So that forms are not imported again for it.
  360. $themes[$theme] = true;
  361. update_option("gf_imported_theme_file", $themes);
  362. }
  363. }
  364. }
  365. public static function dbdelta_fix_case($cqueries){
  366. foreach ($cqueries as $table => $qry) {
  367. $table_name = $table;
  368. if(preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)){
  369. $query_table_name = trim($matches[1], '`' );
  370. //fix table names that are different just by their casing
  371. if(strtolower($query_table_name) == $table){
  372. $table_name = $query_table_name;
  373. }
  374. }
  375. $queries[$table_name] = $qry;
  376. }
  377. return $queries;
  378. }
  379. public static function no_conflict_mode_style(){
  380. if(!get_option("gform_enable_noconflict"))
  381. return;
  382. global $wp_styles;
  383. $wp_required_styles = array("admin-bar", "colors", "ie", "wp-admin");
  384. $gf_required_styles = array(
  385. "common" => array(),
  386. "gf_edit_forms" => array("thickbox"),
  387. "gf_edit_forms_notification" => array("thickbox", "editor-buttons", "wp-jquery-ui-dialog"),
  388. "gf_new_form" => array("thickbox"),
  389. "gf_entries" => array("thickbox"),
  390. "gf_settings" => array(),
  391. "gf_export" => array(),
  392. "gf_help" => array()
  393. );
  394. self::no_conflict_mode($wp_styles, $wp_required_styles, $gf_required_styles, "styles");
  395. }
  396. public static function no_conflict_mode_script(){
  397. if(!get_option("gform_enable_noconflict"))
  398. return;
  399. global $wp_scripts;
  400. $wp_required_scripts = array("admin-bar", "common", "jquery-color", "utils");
  401. $gf_required_scripts = array(
  402. "common" => array("qtip-init", "sack"),
  403. "gf_edit_forms" => array("thickbox", "jquery-ui-core", "jquery-ui-sortable", "jquery-ui-tabs", "rg_currency", "gforms_gravityforms" ),
  404. "gf_edit_forms_notification" => array("editor", "word-count", "quicktags", "wpdialogs-popup", "media-upload", "wplink"),
  405. "gf_new_form" => array("thickbox", "jquery-ui-core", "jquery-ui-sortable", "jquery-ui-tabs", "rg_currency", "gforms_gravityforms" ),
  406. "gf_entries" => array("thickbox", "gforms_gravityforms"),
  407. "gf_settings" => array(),
  408. "gf_export" => array(),
  409. "gf_help" => array(),
  410. );
  411. self::no_conflict_mode($wp_scripts, $wp_required_scripts, $gf_required_scripts, "scripts");
  412. }
  413. private static function no_conflict_mode(&$wp_objects, $wp_required_objects, $gf_required_objects, $type="scripts"){
  414. $current_page = trim(strtolower(rgget("page")));
  415. if(empty($current_page))
  416. $current_page = trim(strtolower(rgget("gf_page")));
  417. if(empty($current_page))
  418. $current_page = RG_CURRENT_PAGE;
  419. $view = rgempty("view", $_GET) ? "default" : rgget("view");
  420. $page_objects = isset($gf_required_objects[$current_page . "_" . $view]) ? $gf_required_objects[$current_page . "_" . $view] : rgar($gf_required_objects, $current_page);
  421. //disable no-conflict if $page_objects is false
  422. if($page_objects === false)
  423. return;
  424. if(!is_array($page_objects))
  425. $page_objects = array();
  426. //merging wp scripts with gravity forms scripts
  427. $required_objects = array_merge($wp_required_objects, $gf_required_objects["common"], $page_objects);
  428. //allowing addons or other products to change the list of no conflict scripts
  429. $required_objects = apply_filters("gform_noconflict_{$type}", $required_objects);
  430. $queue = array();
  431. foreach($wp_objects->queue as $object){
  432. if(in_array($object, $required_objects))
  433. $queue[] = $object;
  434. }
  435. $wp_objects->queue = $queue;
  436. $required_objects = self::add_script_dependencies($wp_objects->registered, $required_objects);
  437. //unregistering scripts
  438. $registered = array();
  439. foreach($wp_objects->registered as $script_name => $script_registration){
  440. if(in_array($script_name, $required_objects)){
  441. $registered[$script_name] = $script_registration;
  442. }
  443. }
  444. $wp_objects->registered = $registered;
  445. }
  446. private static function add_script_dependencies($registered, $scripts){
  447. //gets all dependent scripts linked to the $scripts array passed
  448. do{
  449. $dependents = array();
  450. foreach($scripts as $script){
  451. $deps = isset($registered[$script]) && is_array($registered[$script]->deps) ? $registered[$script]->deps : array();
  452. foreach($deps as $dep){
  453. if(!in_array($dep, $scripts) && !in_array($dep, $dependents)){
  454. $dependents[] = $dep;
  455. }
  456. }
  457. }
  458. $scripts = array_merge($scripts, $dependents);
  459. }while(!empty($dependents));
  460. return $scripts;
  461. }
  462. //Integration with ManageWP
  463. public static function premium_update_push( $premium_update ){
  464. if( !function_exists( 'get_plugin_data' ) )
  465. include_once( ABSPATH.'wp-admin/includes/plugin.php');
  466. $update = GFCommon::get_version_info();
  467. if( $update["is_valid_key"] == true && version_compare(GFCommon::$version, $update["version"], '<') ){
  468. $gforms = get_plugin_data( __FILE__ );
  469. $gforms['type'] = 'plugin';
  470. $gforms['slug'] = 'gravityforms/gravityforms.php';
  471. $gforms['new_version'] = isset($update['version']) ? $update['version'] : false ;
  472. $premium_update[] = $gforms;
  473. }
  474. return $premium_update;
  475. }
  476. //Integration with ManageWP
  477. public static function premium_update( $premium_update ){
  478. if( !function_exists( 'get_plugin_data' ) )
  479. include_once( ABSPATH.'wp-admin/includes/plugin.php');
  480. $update = GFCommon::get_version_info();
  481. if( $update["is_valid_key"] == true && version_compare(GFCommon::$version, $update["version"], '<') ){
  482. $gforms = get_plugin_data( __FILE__ );
  483. $gforms['slug'] = 'gravityforms/gravityforms.php'; // If not set by default, always pass theme template
  484. $gforms['type'] = 'plugin';
  485. $gforms['url'] = isset($update["url"]) ? $update["url"] : false; // OR provide your own callback function for managing the update
  486. array_push($premium_update, $gforms);
  487. }
  488. return $premium_update;
  489. }
  490. private static function drop_index($table, $index){
  491. global $wpdb;
  492. $has_index = $wpdb->get_var("SHOW INDEX FROM {$table} WHERE Key_name='{$index}'");
  493. if($has_index){
  494. $wpdb->query("DROP INDEX {$index} ON {$table}");
  495. }
  496. }
  497. private static function add_empty_index_files(){
  498. $upload_root = RGFormsModel::get_upload_root();
  499. GFCommon::recursive_add_index_file($upload_root);
  500. }
  501. private static function has_database_permission(&$error){
  502. global $wpdb;
  503. $wpdb->hide_errors();
  504. $has_permission = true;
  505. $sql = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}rg_test ( col1 int )";
  506. $wpdb->query($sql);
  507. $error = "Current database user does not have necessary permissions to create tables.";
  508. if(!empty($wpdb->last_error))
  509. $has_permission = false;
  510. $sql = "ALTER TABLE {$wpdb->prefix}rg_test ADD COLUMN a" . uniqid() ." int";
  511. $wpdb->query($sql);
  512. $error = "Current database user does not have necessary permissions to modify (ALTER) tables.";
  513. if(!empty($wpdb->last_error))
  514. $has_permission = false;
  515. $sql = "DROP TABLE {$wpdb->prefix}rg_test";
  516. $wpdb->query($sql);
  517. $wpdb->show_errors();
  518. return $has_permission;
  519. }
  520. //Changes checkbox entry values from "!" to the current choice text. Neededed when upgrading users from 1.0
  521. private static function fix_checkbox_value(){
  522. global $wpdb;
  523. $table_name = RGFormsModel::get_lead_details_table_name();
  524. $sql = "select * from $table_name where value= '!'";
  525. $results = $wpdb->get_results($sql);
  526. foreach($results as $result){
  527. $form = RGFormsModel::get_form_meta($result->form_id);
  528. $field = RGFormsModel::get_field($form, $result->field_number);
  529. if($field["type"] == "checkbox"){
  530. $input = GFCommon::get_input($field, $result->field_number);
  531. $wpdb->update($table_name, array("value" => $input["label"]), array("id" => $result->id));
  532. }
  533. }
  534. }
  535. public static function user_has_cap($all_caps, $cap, $args){
  536. $gf_caps = GFCommon::all_caps();
  537. $capability = rgar($cap, 0);
  538. if($capability != "gform_full_access"){
  539. return $all_caps;
  540. }
  541. if(!self::has_members_plugin()){
  542. //give full access to administrators if the members plugin is not installed
  543. if(current_user_can("administrator") || is_super_admin()){
  544. $all_caps["gform_full_access"] = true;
  545. }
  546. }
  547. else if(current_user_can("administrator")|| is_super_admin()){
  548. //checking if user has any GF permission.
  549. $has_gf_cap = false;
  550. foreach($gf_caps as $gf_cap){
  551. if(rgar($all_caps, $gf_cap))
  552. $has_gf_cap = true;
  553. }
  554. if(!$has_gf_cap){
  555. //give full access to administrators if none of the GF permissions are active by the Members plugin
  556. $all_caps["gform_full_access"] = true;
  557. }
  558. }
  559. return $all_caps;
  560. }
  561. //Target of Member plugin filter. Provides the plugin with Gravity Forms lists of capabilities
  562. public static function members_get_capabilities( $caps ) {
  563. return array_merge($caps, GFCommon::all_caps());
  564. }
  565. //Tests if the upload folder is writable and displays an error message if not
  566. public static function check_upload_folder(){
  567. //check if upload folder is writable
  568. $folder = RGFormsModel::get_upload_root();
  569. if(empty($folder))
  570. echo "<div class='error'>Upload folder is not writable. Export and file upload features will not be functional.</div>";
  571. }
  572. //Prints common admin scripts
  573. public static function print_scripts(){
  574. wp_enqueue_script("sack");
  575. wp_print_scripts();
  576. }
  577. public static function is_gravity_ajax_action(){
  578. //Gravity Forms AJAX requests
  579. $current_action = self::post("action");
  580. $gf_ajax_actions = array('rg_save_form', 'rg_change_input_type', 'rg_add_field', 'rg_duplicate_field',
  581. 'rg_delete_field', 'rg_select_export_form', 'rg_start_export', 'gf_upgrade_license',
  582. 'gf_delete_custom_choice', 'gf_save_custom_choice', 'gf_get_notification_post_categories',
  583. 'rg_update_lead_property', 'delete-gf_entry', 'rg_update_form_active',
  584. 'gf_resend_notifications', 'rg_dismiss_upgrade');
  585. if(defined("DOING_AJAX") && DOING_AJAX && in_array($current_action, $gf_ajax_actions))
  586. return true;
  587. //not a gravity forms ajax request.
  588. return false;
  589. }
  590. //Returns true if the current page is one of Gravity Forms pages. Returns false if not
  591. public static function is_gravity_page(){
  592. //Gravity Forms pages
  593. $current_page = trim(strtolower(self::get("page")));
  594. $gf_pages = array("gf_edit_forms","gf_new_form","gf_entries","gf_settings","gf_export","gf_help");
  595. return in_array($current_page, $gf_pages);
  596. }
  597. public static function do_menu_page(){
  598. $args = array( 'show_ui' => true, '_builtin' => false, 'show_in_menu' => true );
  599. $count = (int) count(get_post_types( $args ));
  600. return $count > 0;
  601. }
  602. //Creates "Forms" left nav
  603. public static function create_menu(){
  604. $has_full_access = current_user_can("gform_full_access");
  605. $min_cap = GFCommon::current_user_can_which(GFCommon::all_caps());
  606. if(empty($min_cap))
  607. $min_cap = "gform_full_access";
  608. $addon_menus = array();
  609. $addon_menus = apply_filters("gform_addon_navigation", $addon_menus);
  610. $parent_menu = self::get_parent_menu($addon_menus);
  611. // Add a top-level left nav
  612. $update_icon = GFCommon::has_update() ? "<span title='" . esc_attr(__("Update Available", "alien")) . "' class='update-plugins count-1'><span class='update-count'>1</span></span>" : "";
  613. //Getting around a Wordpress bug that prevents menus from displayeing when site has multiple custom post types
  614. if( self::do_menu_page() )
  615. 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', 16.9);
  616. else
  617. add_object_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');
  618. // Adding submenu pages
  619. 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"));
  620. 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"));
  621. 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"));
  622. if(is_array($addon_menus)){
  623. foreach($addon_menus as $addon_menu)
  624. 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"]);
  625. }
  626. 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"));
  627. 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"));
  628. if(current_user_can("install_plugins")){
  629. 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"));
  630. 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"));
  631. }
  632. add_submenu_page($parent_menu["name"], __("Help", "gravityforms"), __("Help", "gravityforms"), $has_full_access ? "gform_full_access" : $min_cap, "gf_help", array("RGForms", "help_page"));
  633. }
  634. //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)
  635. public static function get_parent_menu($addon_menus){
  636. if(GFCommon::current_user_can_any("gravityforms_edit_forms"))
  637. $parent = array("name" => "gf_edit_forms", "callback" => array("RGForms", "forms"));
  638. else if(GFCommon::current_user_can_any("gravityforms_create_form"))
  639. $parent = array("name" => "gf_new_form", "callback" => array("RGForms", "new_form"));
  640. else if(GFCommon::current_user_can_any("gravityforms_view_entries"))
  641. $parent = array("name" => "gf_entries", "callback" => array("RGForms", "all_leads_page"));
  642. else if(is_array($addon_menus) && sizeof($addon_menus) > 0){
  643. foreach($addon_menus as $addon_menu)
  644. if(GFCommon::current_user_can_any($addon_menu["permission"]))
  645. {
  646. $parent = array("name" => $addon_menu["name"], "callback" => $addon_menu["callback"]);
  647. break;
  648. }
  649. }
  650. else if(GFCommon::current_user_can_any("gravityforms_view_settings"))
  651. $parent = array("name" => "gf_settings", "callback" => array("RGForms", "settings_page"));
  652. else if(GFCommon::current_user_can_any("gravityforms_export_entries"))
  653. $parent = array("name" => "gf_export", "callback" => array("RGForms", "export_page"));
  654. else if(GFCommon::current_user_can_any("gravityforms_view_updates"))
  655. $parent = array("name" => "gf_update", "callback" => array("RGForms", "update_page"));
  656. else if(GFCommon::current_user_can_any("gravityforms_view_addons"))
  657. $parent = array("name" => "gf_addons", "callback" => array("RGForms", "addons_page"));
  658. else if(GFCommon::current_user_can_any(GFCommon::all_caps()))
  659. $parent = array("name" => "gf_help", "callback" => array("RGForms", "help_page"));
  660. return $parent;
  661. }
  662. //Parses the [gravityform shortcode and returns the front end form UI
  663. public static function parse_shortcode($attributes, $content = null){
  664. extract(shortcode_atts(array(
  665. 'title' => true,
  666. 'description' => true,
  667. 'id' => 0,
  668. 'name' => '',
  669. 'field_values' => "",
  670. 'ajax' => false,
  671. 'tabindex' => 1,
  672. 'action' => 'form'
  673. ), $attributes));
  674. $shortcode_string = "";
  675. switch($action) {
  676. case 'conditional':
  677. $shortcode_string = GFCommon::conditional_shortcode($attributes, $content);
  678. break;
  679. case 'form' :
  680. //displaying form
  681. $title = strtolower($title) == "false" ? false : true;
  682. $description = strtolower($description) == "false" ? false : true;
  683. $field_values = htmlspecialchars_decode($field_values);
  684. $field_values = str_replace("&#038;", "&", $field_values);
  685. $ajax = strtolower($ajax) == "true" ? true : false;
  686. //using name to lookup form if id is not specified
  687. if(empty($id))
  688. $id = $name;
  689. parse_str($field_values, $field_value_array); //parsing query string like string for field values and placing them into an associative array
  690. $field_value_array = stripslashes_deep($field_value_array);
  691. $shortcode_string = self::get_form($id, $title, $description, false, $field_value_array, $ajax, $tabindex);
  692. break;
  693. }
  694. $shortcode_string = apply_filters("gform_shortcode_{$action}", $shortcode_string, $attributes, $content);
  695. return $shortcode_string;
  696. }
  697. //-------------------------------------------------
  698. //----------- AJAX --------------------------------
  699. public static function ajax_parse_request($wp) {
  700. if (isset($_POST["gform_ajax"])) {
  701. parse_str($_POST["gform_ajax"]);
  702. require_once(GFCommon::get_base_path() . "/form_display.php");
  703. $result = GFFormDisplay::get_form($form_id, $title, $description, false, $_POST["gform_field_values"], true);
  704. die($result);
  705. }
  706. }
  707. //------------------------------------------------------
  708. //------------- PAGE/POST EDIT PAGE ---------------------
  709. //Action target that adds the "Insert Form" button to the post/page edit screen
  710. public static function add_form_button(){
  711. $is_post_edit_page = in_array(RG_CURRENT_PAGE, array('post.php', 'page.php', 'page-new.php', 'post-new.php'));
  712. if(!$is_post_edit_page)
  713. return;
  714. // do a version check for the new 3.5 UI
  715. $version = get_bloginfo('version');
  716. if ($version < 3.5) {
  717. // show button for v 3.4 and below
  718. $image_btn = GFCommon::get_base_url() . "/images/form-button.png";
  719. 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", 'gravityform') . '" /></a>';
  720. } else {
  721. // display button matching new UI
  722. echo '<style>.gform_media_icon{
  723. background:url(' . GFCommon::get_base_url() . '/images/gravity-admin-icon.png) no-repeat top left;
  724. display: inline-block;
  725. height: 16px;
  726. margin: 0 2px 0 0;
  727. vertical-align: text-top;
  728. width: 16px;
  729. }
  730. .wp-core-ui a.gform_media_link{
  731. padding-left: 0.4em;
  732. }
  733. </style>
  734. <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>';
  735. }
  736. }
  737. //Action target that displays the popup to insert a form to a post/page
  738. public static function add_mce_popup(){
  739. ?>
  740. <script>
  741. function InsertForm(){
  742. var form_id = jQuery("#add_form_id").val();
  743. if(form_id == ""){
  744. alert("<?php _e("Please select a form", "gravityforms") ?>");
  745. return;
  746. }
  747. var form_name = jQuery("#add_form_id option[value='" + form_id + "']").text().replace(/[\[\]]/g, '');
  748. var display_title = jQuery("#display_title").is(":checked");
  749. var display_description = jQuery("#display_description").is(":checked");
  750. var ajax = jQuery("#gform_ajax").is(":checked");
  751. var title_qs = !display_title ? " title=\"false\"" : "";
  752. var description_qs = !display_description ? " description=\"false\"" : "";
  753. var ajax_qs = ajax ? " ajax=\"true\"" : "";
  754. window.send_to_editor("[gravityform id=\"" + form_id + "\" name=\"" + form_name + "\"" + title_qs + description_qs + ajax_qs + "]");
  755. }
  756. </script>
  757. <div id="select_gravity_form" style="display:none;">
  758. <div class="wrap">
  759. <div>
  760. <div style="padding:15px 15px 0 15px;">
  761. <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>
  762. <span>
  763. <?php _e("Select a form below to add it to your post or page.", "gravityforms"); ?>
  764. </span>
  765. </div>
  766. <div style="padding:15px 15px 0 15px;">
  767. <select id="add_form_id">
  768. <option value=""> <?php _e("Select a Form", "gravityforms"); ?> </option>
  769. <?php
  770. $forms = RGFormsModel::get_forms(1, "title");
  771. foreach($forms as $form){
  772. ?>
  773. <option value="<?php echo absint($form->id) ?>"><?php echo esc_html($form->title) ?></option>
  774. <?php
  775. }
  776. ?>
  777. </select> <br/>
  778. <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>
  779. </div>
  780. <div style="padding:15px 15px 0 15px;">
  781. <input type="checkbox" id="display_title" checked='checked' /> <label for="display_title"><?php _e("Display form title", "gravityforms"); ?></label> &nbsp;&nbsp;&nbsp;
  782. <input type="checkbox" id="display_description" checked='checked' /> <label for="display_description"><?php _e("Display form description", "gravityforms"); ?></label>&nbsp;&nbsp;&nbsp;
  783. <input type="checkbox" id="gform_ajax" /> <label for="gform_ajax"><?php _e("Enable AJAX", "gravityforms"); ?></label>
  784. </div>
  785. <div style="padding:15px;">
  786. <input type="button" class="button-primary" value="Insert Form" onclick="InsertForm();"/>&nbsp;&nbsp;&nbsp;
  787. <a class="button" style="color:#bbb;" href="#" onclick="tb_remove(); return false;"><?php _e("Cancel", "gravityforms"); ?></a>
  788. </div>
  789. </div>
  790. </div>
  791. </div>
  792. <?php
  793. }
  794. //------------------------------------------------------
  795. //------------- PLUGINS PAGE ---------------------------
  796. //------------------------------------------------------
  797. public static function plugin_settings_link( $links, $file ) {
  798. if ( $file != plugin_basename( __FILE__ ))
  799. return $links;
  800. array_unshift($links, '<a href="' . admin_url("admin.php") . '?page=gf_settings">' . __( 'Settings', 'gravityforms' ) . '</a>');
  801. return $links;
  802. }
  803. //Displays message on Plugin's page
  804. public static function plugin_row($plugin_name){
  805. $key = GFCommon::get_key();
  806. $version_info = GFCommon::get_version_info();
  807. if(!$version_info["is_valid_key"]){
  808. $plugin_name = "gravityforms/gravityforms.php";
  809. $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>. ' : '';
  810. 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>';
  811. }
  812. }
  813. //Displays current version details on Plugin's page
  814. public static function display_changelog(){
  815. if($_REQUEST["plugin"] != "gravityforms")
  816. return;
  817. $page_text = self::get_changelog();
  818. echo $page_text;
  819. exit;
  820. }
  821. public static function get_changelog(){
  822. $key = GFCommon::get_key();
  823. $body = "key=$key";
  824. $options = array('method' => 'POST', 'timeout' => 3, 'body' => $body);
  825. $options['headers'] = array(
  826. 'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset'),

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