PageRenderTime 67ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/gravityforms/gravityforms.php

https://github.com/petergibbons/OpenCounterWP
PHP | 1754 lines | 1258 code | 352 blank | 144 comment | 231 complexity | c09c5ecd3c3bb16d1ce766e755762717 MD5 | raw file

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

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