/gravity-forms-addons.php

  1. <?php 
  2. /** 
  3. Plugin Name: Gravity Forms Directory & Addons 
  4. Plugin URI: http://katz.co/gravity-forms-addons/ 
  5. Description: Turn <a href="http://katz.si/gravityforms">Gravity Forms</a> into a great WordPress directory...and more! 
  6. Author: Katz Web Services, Inc. 
  7. Version: 3.7.2 
  8. Author URI: http://www.katzwebservices.com 
  9. Text Domain: gravity-forms-addons 
  10. License: GPLv2 or later 
  11. License URI: http://www.gnu.org/licenses/gpl-2.0.html 
  12. Domain Path: /languages 
  13.   
  14. Copyright 2015 Katz Web Services, Inc. (email: info@katzwebservices.com) 
  15.   
  16. This program is free software; you can redistribute it and/or modify 
  17. it under the terms of the GNU General Public License as published by 
  18. the Free Software Foundation; either version 3 of the License, or 
  19. (at your option) any later version. 
  20.   
  21. This program is distributed in the hope that it will be useful,  
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of 
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  24. GNU General Public License for more details. 
  25.   
  26. You should have received a copy of the GNU General Public License 
  27. along with this program. If not, see <http://www.gnu.org/licenses/>. 
  28.   
  29. */ 
  30.  
  31. register_activation_hook( __FILE__, array('GFDirectory', 'activation') ); 
  32. add_action('plugins_loaded', array('GFDirectory', 'plugins_loaded')); 
  33. add_action('plugins_loaded', 'kws_gf_load_functions'); 
  34.  
  35. class GFDirectory { 
  36.  
  37. private static $path = "gravity-forms-addons/gravity-forms-addons.php"; 
  38. private static $slug = "gravity-forms-addons"; 
  39. private static $version = "3.7.2"; 
  40. private static $min_gravityforms_version = "1.5"; 
  41.  
  42. public static function directory_defaults($args = array()) { 
  43. $defaults = array( 
  44. 'form' => 1, // Gravity Forms form ID 
  45. 'approved' => false, // Show only entries that have been Approved (have a field in the form that is an Admin-only checkbox with a value of 'Approved' 
  46. 'smartapproval' => true, // Auto-convert form into Approved-only when an Approved field is detected. 
  47. 'directoryview' => 'table', // Table, list or DL 
  48. 'entryview' => 'table', // Table, list or DL 
  49. 'hovertitle' => true, // Show column name as user hovers over cell 
  50. 'tableclass' => 'gf_directory widefat', // Class for the <table> 
  51. 'tablestyle' => '', // inline CSS for the <table> 
  52. 'rowclass' => '', // Class for the <table> 
  53. 'rowstyle' => '', // inline CSS for all <tbody><tr>'s 
  54. 'valign' => '',  
  55. 'sort' => 'date_created', // Use the input ID ( example: 1.3 or 7 or ip ) 
  56. 'dir' => 'DESC',  
  57.  
  58. 'useredit' => false,  
  59. 'limituser' => false,  
  60. 'adminedit' => false,  
  61.  
  62. 'status' => 'active', // Added in 2.0 
  63. 'start_date' => '', // Added in 2.0 
  64. 'end_date' => '', // Added in 2.0 
  65.  
  66. //'wpautop' => true, // Convert bulk paragraph text to...paragraphs. Deprecated 3.6.3 
  67. 'page_size' => 20, // Number of entries to show at once 
  68. 'startpage' => 1, // If you want to show page 8 instead of 1 
  69.  
  70. 'lightboxstyle' => 3,  
  71. 'lightboxsettings' => array('images' => true, 'entry' => null, 'websites' => null),  
  72.  
  73. 'showcount' => true, // Do you want to show "Displaying 1-19 of 19"? 
  74. 'pagelinksshowall' => true, // Whether to show each page number, or just 7 
  75. 'next_text' => '»',  
  76. 'prev_text' => '«',  
  77. 'pagelinkstype' => 'plain', // 'plain' is just a string with the links separated by a newline character. The other possible values are either 'array' or 'list'. 
  78. //'fulltext' => true, // If there's a textarea or post content field, show the full content or a summary? Deprecated 3.6.3 
  79. 'linkemail' => true, // Convert email fields to email mailto: links 
  80. 'linkwebsite' => true, // Convert URLs to links 
  81. 'linknewwindow' => false, // Open links in new window? (uses target="_blank") 
  82. 'nofollowlinks' => false, // Add nofollow to all links, including emails 
  83. 'titleshow' => true, // Show a form title? By default, the title will be the form title. 
  84. 'titleprefix' => 'Entries for ', // Default GF behavior is 'Entries : ' 
  85. 'tablewidth' => '100%', // 'width' attribute for the table 
  86. 'searchtabindex' => false, // adds tabindex="" to the search field 
  87. 'search' => true, // show the search field 
  88. 'tfoot' => true, // show the <tfoot> 
  89. 'thead' => true, // show the <thead> 
  90. 'showadminonly' => false, // Admin only columns aren't shown by default, but can be (added 2.0.1) 
  91. 'datecreatedformat' => get_option('date_format').' \a\t '.get_option('time_format'), // Use standard PHP date formats (http://php.net/manual/en/function.date.php) 
  92. 'credit' => true, // Credit link 
  93. 'dateformat' => false, // Override the options from Gravity Forms, and use standard PHP date formats (http://php.net/manual/en/function.date.php) 
  94. 'postimage' => 'icon', // Whether to show icon, thumbnail, or large image 
  95. 'getimagesize' => false,  
  96. 'entry' => true, // If there's an Entry ID column, link to the full entry 
  97. 'entrylink' => 'View entry details',  
  98. 'entryth' => 'More Info',  
  99. 'entryback' => '← Back to directory',  
  100. 'entryonly' => true,  
  101. 'entrytitle' => 'Entry Detail',  
  102. 'entrydetailtitle' => '%%formtitle%% : Entry # %%leadid%%',  
  103. 'entryanchor' => true,  
  104. 'truncatelink' => false,  
  105. 'appendaddress' => false,  
  106. 'hideaddresspieces' => false,  
  107. 'jssearch' => true,  
  108. 'jstable' => false,  
  109. 'lightbox' => null, // depreciated - Combining with lightboxsettings 
  110. 'entrylightbox' => null, // depreciated - Combining with lightboxsettings 
  111. ); 
  112.  
  113. $settings = get_option("gf_addons_settings"); 
  114. if(isset($settings['directory_defaults'])) { 
  115. $defaults = wp_parse_args($settings['directory_defaults'], $defaults); 
  116.  
  117. $options = wp_parse_args($args, $defaults); 
  118.  
  119. // Backward Compatibility 
  120. if(!empty($args['lightbox'])) { $options['lightboxsettings']['images'] = 1; } 
  121. if(!empty($args['entrylightbox'])) { $options['lightboxsettings']['entry'] = 1; } 
  122. unset($options['lightbox'], $options['entrylightbox']); // Depreciated for lightboxsettings 
  123.  
  124. return apply_filters('kws_gf_directory_defaults', $options); 
  125.  
  126. public static function plugins_loaded() { 
  127.  
  128. if(!self::is_gravityforms_installed()) { return false; } 
  129.  
  130. include_once(plugin_dir_path( __FILE__ ) .'/edit-form.php'); 
  131. include_once(plugin_dir_path( __FILE__ ) .'/admin.php'); 
  132. include_once(plugin_dir_path( __FILE__ ) .'/gravity-forms-lead-creator.php'); 
  133.  
  134. if(in_array(RG_CURRENT_PAGE, array("gf_entries", "admin.php", "admin-ajax.php"))) { 
  135. self::globals_get_approved_column(); 
  136. if(self::is_gravity_page()) { 
  137. self::load_functionality(); 
  138.  
  139. add_action('init', array('GFDirectory', 'init')); 
  140. add_shortcode('directory', array('GFDirectory', 'make_directory')); 
  141.  
  142.  
  143. //Plugin starting point. Will load appropriate files 
  144. public static function init() { 
  145. global $current_user; 
  146.  
  147. load_plugin_textdomain( 'gravity-forms-addons', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); 
  148.  
  149. self::add_rewrite(); 
  150.  
  151. if(!self::is_gravityforms_supported()) { 
  152. return; 
  153.  
  154. if(!is_admin()) { 
  155.  
  156. add_action('template_redirect', array('GFDirectory', 'enqueue_files')); 
  157. if(apply_filters('kws_gf_directory_canonical_add', true)) { 
  158. add_filter('post_link', array('GFDirectory', 'directory_canonical'), 1, 3); 
  159. add_filter('page_link', array('GFDirectory', 'directory_canonical'), 1, 3); 
  160. if(apply_filters('kws_gf_directory_shortlink', true)) { 
  161. add_filter('get_shortlink', array('GFDirectory', 'shortlink')); 
  162. add_filter('kws_gf_directory_lead_filter', array('GFDirectory', 'show_only_user_entries'), 10, 2); 
  163. add_filter('kws_gf_directory_anchor_text', array('GFDirectory', 'directory_anchor_text')); 
  164.  
  165. //integrating with Members plugin 
  166. if(function_exists('members_get_capabilities')) { 
  167. add_filter('members_get_capabilities', array("GFDirectory", "members_get_capabilities")); 
  168.  
  169. add_filter('kws_gf_directory_td_address', array('GFDirectory', 'format_address'), 1, 2); // Add this filter so it can be removed or overridden by users 
  170.  
  171. if(self::is_directory_page()) { 
  172.  
  173. //enqueueing sack for AJAX requests 
  174. wp_enqueue_script(array("sack", 'datepicker')); 
  175. wp_enqueue_style('gravityforms-admin', GFCommon::get_base_url().'/css/admin.css'); 
  176.  
  177. else if(self::is_gravity_page('gf_entries')) { 
  178. wp_enqueue_script( 'thickbox', array('jquery')); 
  179. add_filter("gform_get_field_value", array('GFDirectory', 'add_lead_approved_hidden_input'), 1, 3); 
  180. else if(in_array(RG_CURRENT_PAGE, array("admin-ajax.php"))) { 
  181. add_action('wp_ajax_rg_update_feed_active', array('GFDirectory', 'update_feed_active')); 
  182. add_action('wp_ajax_gf_select_directory_form', array('GFDirectory', 'select_directory_form')); 
  183. add_action('wp_ajax_rg_update_approved', array('GFDirectory', 'directory_update_approved_hook')); 
  184. add_action('wp_ajax_change_directory_columns', array('GFDirectory', 'change_directory_columns')); 
  185. } else if(in_array(RG_CURRENT_PAGE, array("plugins.php"))) { 
  186.  
  187. add_filter('plugin_action_links', array('GFDirectory', 'settings_link'), 10, 2 ); 
  188.  
  189.  
  190.  
  191. //Target of Member plugin filter. Provides the plugin with Gravity Forms lists of capabilities 
  192. public static function members_get_capabilities( $caps ) { 
  193. return array_merge($caps, array("gravityforms_directory", "gravityforms_directory_uninstall")); 
  194.  
  195. static public function activation() { 
  196. self::add_permissions(); 
  197. self::flush_rules(); 
  198.  
  199. public static function is_gravityforms_installed() { 
  200. return class_exists("RGForms"); 
  201.  
  202. public static function add_permissions() { 
  203. global $wp_roles; 
  204. $wp_roles->add_cap("administrator", "gravityforms_directory"); 
  205. $wp_roles->add_cap("administrator", "gravityforms_directory_uninstall"); 
  206.  
  207. static public function flush_rules() { 
  208. global $wp_rewrite; 
  209. self::add_rewrite(); 
  210. $wp_rewrite->flush_rules(); 
  211. return; 
  212.  
  213.  
  214.  
  215. static private function load_functionality() { 
  216.  
  217. register_deactivation_hook( __FILE__, array('GFDirectory', 'uninstall') ); 
  218.  
  219. $settings = self::get_settings(); 
  220. extract($settings); 
  221.  
  222. if($referrer) { 
  223. // Load Joost's referrer tracker 
  224. @include_once('gravity-forms-referrer.php'); 
  225.  
  226.  
  227. static public function shortlink($link = '') { 
  228. global $post; 
  229. if(empty($post)) { return; } 
  230. if(empty($link) && isset($post->guid)) { 
  231. $link = $post->guid; 
  232. return $link; 
  233.  
  234. $url = add_query_arg(array()); 
  235. if(preg_match('/'.sanitize_title(apply_filters('kws_gf_directory_endpoint', 'entry')).'\/([0-9]+)(?:\/|-)([0-9]+)\/?/ism', $url, $matches)) { 
  236. $link = add_query_arg(array('form'=>(int)$matches[1], 'leadid'=>(int)$matches[2]), $link); 
  237. } elseif(isset($_REQUEST['leadid']) && isset($_REQUEST['form'])) { 
  238. $link = wp_nonce_url(add_query_arg(array('leadid'=>(int)$_REQUEST['leadid'], 'form'=>(int)$_REQUEST['form']), $link), sprintf('view-%d-%d', $_REQUEST['leadid'], $_REQUEST['form']), 'view'); 
  239. return esc_url_raw( $link ); 
  240.  
  241. static public function directory_canonical($permalink, $sentPost = '', $leavename = '') { 
  242.  
  243. // This was messing up the wp menu links 
  244. if(did_action('wp_head')) { return $permalink; } 
  245.  
  246. global $post; 
  247.  
  248. if( is_object( $post ) ) { 
  249. $post->permalink = $permalink; 
  250.  
  251. $url = add_query_arg(array()); 
  252.  
  253. $sentPostID = is_object($sentPost) ? $sentPost->ID : $sentPost; 
  254. // $post->ID === $sentPostID is so that add_query_arg match doesn't apply to prev/next posts; just current 
  255. preg_match('/('.sanitize_title(apply_filters('kws_gf_directory_endpoint', 'entry')).'\/([0-9]+)(?:\/|-)([0-9]+)\/?)/ism', $url, $matches); 
  256. if(isset($post->ID) && $post->ID === $sentPostID && !empty($matches)) { 
  257. return trailingslashit($permalink).$matches[0]; 
  258. } elseif(isset($post->ID) && $post->ID === $sentPostID && (isset($_REQUEST['leadid']) && isset($_REQUEST['form'])) || !empty($matches)) { 
  259. if($matches) { $leadid = $matches[2]; $form = $matches[1]; } 
  260. else { $leadid = $_REQUEST['leadid']; $form = $_REQUEST['form']; } 
  261.  
  262. return esc_url_raw( wp_nonce_url(add_query_arg(array('leadid' =>$leadid, 'form'=>$form), trailingslashit($permalink)), sprintf('view-%d-%d', $leadid, $form), 'view') ); 
  263. return $permalink; 
  264.  
  265. static public function enqueue_files() { 
  266. global $post, $kws_gf_styles, $kws_gf_scripts, $kws_gf_directory_options; 
  267.  
  268. $kws_gf_styles = isset($kws_gf_styles) ? $kws_gf_styles : array(); 
  269. $kws_gf_scripts = isset($kws_gf_scripts) ? $kws_gf_scripts : array(); 
  270.  
  271. if( !empty($post) && 
  272. !empty($post->post_content) && 
  273. preg_match('/(.?)\[(directory)\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?(.?)/', $post->post_content, $matches) 
  274. ) { 
  275.  
  276. $options = self::directory_defaults(shortcode_parse_atts($matches[3])); 
  277. if(!is_array($options['lightboxsettings'])) { $options['lightboxsettings'] = explode(', ', $options['lightboxsettings']); } 
  278.  
  279. $kws_gf_directory_options = $options; 
  280. do_action('kws_gf_directory_enqueue', $options, $post); 
  281.  
  282. extract($options); 
  283.  
  284. if($jstable) { 
  285. $theme = apply_filters('kws_gf_tablesorter_theme', 'blue', $form); 
  286. wp_enqueue_style('tablesorter-'.$theme, plugins_url( "/bower_components/jquery.tablesorter/css/theme.{$theme}.css", __FILE__)); 
  287. wp_enqueue_script('tablesorter-min', plugins_url( "/bower_components/jquery.tablesorter/js/jquery.tablesorter.min.js", __FILE__), array('jquery')); 
  288. $kws_gf_styles[] = 'tablesorter-'.$theme; 
  289. $kws_gf_scripts[] = 'tablesorter-min'; 
  290.  
  291. if(!empty($lightboxsettings)) { 
  292. wp_enqueue_script('colorbox', plugins_url( "/bower_components/colorbox/jquery.colorbox-min.js", __FILE__), array('jquery')); 
  293. wp_enqueue_style('colorbox', plugins_url( "/bower_components/colorbox/example{$lightboxstyle}/colorbox.css", __FILE__), array()); 
  294. $kws_gf_scripts[] = $kws_gf_styles[] = 'colorbox'; 
  295. add_action(apply_filters('kws_gf_directory_colorbox_action', 'wp_footer'), array('GFDirectory', 'load_colorbox'), 1000); 
  296.  
  297. list($urlformid, $urlleadid) = self::get_form_and_lead_ids(); 
  298. if(isset($_GET['edit']) && !empty($urlformid) && isset($urlleadid)) { 
  299.  
  300. $edit_scripts = array( 'jquery', 'gform_json', 'gform_placeholder', 'sack', 'plupload-all' ); 
  301. wp_enqueue_script('gform_gravityforms', $edit_scripts ); 
  302.  
  303. $kws_gf_scripts[] = array_merge( $kws_gf_scripts, $edit_scripts ); 
  304.  
  305. static function format_colorbox_settings($colorboxSettings = array()) { 
  306. $settings = array(); 
  307. if(!empty($colorboxSettings) && is_array($colorboxSettings)) { 
  308. foreach($colorboxSettings as $key => $value) { 
  309. if($value === null) { continue; } 
  310. if($value === true) { 
  311. $value = 'true'; 
  312. } elseif(empty($value) && $value !== 0) { 
  313. $value = 'false'; 
  314. } else { 
  315. $value = '"'.$value.'"'; 
  316. $settings["{$key}"] = $key.':'.$value.''; 
  317. return $settings; 
  318.  
  319. static public function load_colorbox() { 
  320. global $kws_gf_directory_options; 
  321. extract($kws_gf_directory_options); 
  322.  
  323. $lightboxsettings = apply_filters('kws_gf_directory_lightbox_settings', $lightboxsettings); 
  324. $colorboxSettings = apply_filters('kws_gf_directory_colorbox_settings', array( 
  325. 'width' => apply_filters('kws_gf_directory_lightbox_width', '70%'),  
  326. 'height' => apply_filters('kws_gf_directory_lightbox_height', '70%'),  
  327. 'iframe' => true,  
  328. 'maxWidth' => '95%',  
  329. 'maxHeight' => '95%',  
  330. 'current' => '{current} of {total}',  
  331. 'rel' => apply_filters('kws_gf_directory_lightbox_settings_rel', null) 
  332. )); 
  333.  
  334. ?> 
  335. <script> 
  336. jQuery(document).ready(function($) { 
  337. <?php 
  338. $output = ''; 
  339. foreach($lightboxsettings as $key => $value) { 
  340. $settings = $colorboxSettings; 
  341. if(is_numeric($key)) { $key = $value; } 
  342. switch($key) { 
  343. case "images": 
  344. $settings['width'] = $settings['height'] = $settings['iframe'] = null; 
  345. break; 
  346. case "urls": 
  347. $settings['height'] = '80%'; 
  348. break; 
  349. $output .= "\t\t".'$(".colorbox[rel~=\'directory_'.$key.'\']").colorbox('; 
  350. if(!empty($settings)) { 
  351. $output .= "{\n\t\t\t".implode(", \n\t\t\t", self::format_colorbox_settings(apply_filters("kws_gf_directory_lightbox_{$key}_settings", $settings)))."\n\t\t}"; 
  352. $output .= ");\n\n"; 
  353. echo $output; 
  354. do_action('kws_gf_directory_jquery', $kws_gf_directory_options); 
  355. ?> 
  356. }); 
  357. </script> 
  358. <?php 
  359.  
  360. static public function add_rewrite() { 
  361. global $wp_rewrite, $wp; 
  362.  
  363. if(!$wp_rewrite->using_permalinks()) { return; } 
  364. $endpoint = sanitize_title(apply_filters('kws_gf_directory_endpoint', 'entry')); 
  365.  
  366. # @TODO: Make sure this works in MU 
  367. $wp_rewrite->add_permastruct("{$endpoint}", $endpoint.'/%'.$endpoint.'%/?', true); 
  368. $wp_rewrite->add_endpoint("{$endpoint}", EP_ALL); 
  369.  
  370. //Returns true if the current page is one of Gravity Forms pages. Returns false if not 
  371. public static function is_gravity_page($page = array()) { 
  372. $current_page = trim(strtolower(RGForms::get("page"))); 
  373. if(empty($page)) { 
  374. $gf_pages = array("gf_edit_forms", "gf_new_form", "gf_entries", "gf_settings", "gf_export", "gf_help"); 
  375. } else { 
  376. $gf_pages = is_array($page) ? $page : array($page); 
  377. return in_array($current_page, $gf_pages); 
  378.  
  379. static function directory_update_approved($lead_id = 0, $approved = 0, $form_id = 0, $approvedcolumn = 0) { 
  380. global $wpdb, $_gform_directory_approvedcolumn, $current_user; 
  381. $current_user = wp_get_current_user(); 
  382. $user_data = get_userdata($current_user->ID); 
  383.  
  384. if(!empty($approvedcolumn)) { $_gform_directory_approvedcolumn = $approvedcolumn; } 
  385.  
  386. if(empty($_gform_directory_approvedcolumn)) { return false; } 
  387.  
  388. $lead_detail_table = RGFormsModel::get_lead_details_table_name(); 
  389.  
  390. // This will be faster in the 1.6+ future. 
  391. if(function_exists('gform_update_meta')) { gform_update_meta($lead_id, 'is_approved', $approved); } 
  392.  
  393. if(empty($approved)) { 
  394. //Deleting details for this field 
  395. $sql = $wpdb->prepare("DELETE FROM $lead_detail_table WHERE lead_id=%d AND field_number BETWEEN %f AND %f ", $lead_id, $_gform_directory_approvedcolumn - 0.001, $_gform_directory_approvedcolumn + 0.001); 
  396. $wpdb->query($sql); 
  397.  
  398. RGFormsModel::add_note($lead_id, $current_user->ID, $user_data->display_name, stripslashes(__('Disapproved the lead', 'gravity-forms-addons'))); 
  399.  
  400. } else { 
  401.  
  402. // Get the fields for the lead 
  403. $current_fields = $wpdb->get_results($wpdb->prepare("SELECT id, field_number FROM $lead_detail_table WHERE lead_id=%d", $lead_id)); 
  404.  
  405. $lead_detail_id = RGFormsModel::get_lead_detail_id($current_fields, $_gform_directory_approvedcolumn); 
  406.  
  407. // If there's already a field for the approved column, then we update it. 
  408. if($lead_detail_id > 0) { 
  409. $update = $wpdb->update($lead_detail_table, array("value" => $approved), array("lead_id" => $lead_id, 'form_id' => $form_id, 'field_number' => $_gform_directory_approvedcolumn), array("%s"), array("%d", "%d", "%f")); 
  410. // Otherwise, we create it. 
  411. else { 
  412. $update = $wpdb->insert($lead_detail_table, array("lead_id" => $lead_id, "form_id" => $form_id, "field_number" => $_gform_directory_approvedcolumn, "value" => $approved), array("%d", "%d", "%f", "%s")); 
  413.  
  414. RGFormsModel::add_note($lead_id, $current_user->ID, $user_data->display_name, stripslashes(__('Approved the lead', 'gravity-forms-addons'))); 
  415.  
  416. static public function edit_lead_detail($Form, $lead, $options) { 
  417. global $current_user, $_gform_directory_approvedcolumn; 
  418. require_once(GFCommon::get_base_path() . "/form_display.php"); 
  419. if(empty($_gform_directory_approvedcolumn)) { $_gform_directory_approvedcolumn = self::get_approved_column($Form); } 
  420.  
  421. // We fetch this again, since it may have had some admin-only columns taken out. 
  422. #$lead = RGFormsModel::get_lead($lead["id"]); 
  423.  
  424. // If you want to allow users to edit their own approval (?) add a filter and return true. 
  425. if(apply_filters('kws_gf_directory_allow_user_edit_approved', false) === false) { 
  426. $Form['fields'] = self::remove_approved_column('form', $Form['fields'], $_gform_directory_approvedcolumn); 
  427.  
  428. // If this is not the form that should be edited 
  429. list($urlformid, $urlleadid) = self::get_form_and_lead_ids(); 
  430. if(intval($Form['id']) !== intval($urlformid) || intval($lead['id']) !== intval($urlleadid)) { return; } 
  431.  
  432. // If either of these two things are false (creator of lead, or admin) 
  433. if(!( 
  434.  
  435. // Users can edit their own listings, they are logged in, the current user is the creator of the lead 
  436. (!empty($options['useredit']) && is_user_logged_in() && intval($current_user->ID) === intval($lead['created_by'])) === true || // OR 
  437.  
  438. // Administrators can edit every listing, and this person has administrator access 
  439. (!empty($options['adminedit']) && self::has_access("gravityforms_directory")) === true) 
  440. ) { 
  441. // Kick them out. 
  442. printf( esc_html_e( '%sYou do not have permission to edit this form.%s', 'gravity-forms-addons') , '<div class="error">', '</div>'); 
  443. return; 
  444.  
  445. $validation_message = ''; 
  446.  
  447. // If the form is submitted 
  448. if(RGForms::post("action") === "update") { 
  449. check_admin_referer('gforms_save_entry', 'gforms_save_entry'); 
  450.  
  451. $lead = apply_filters('kws_gf_directory_lead_being_updated', $lead, $Form); 
  452.  
  453. // We don't DO passwords. 
  454. foreach($Form['fields'] as $key => $field) { 
  455. if($field['type'] === 'password') { unset($Form['fields'][$key]); } 
  456.  
  457. $is_valid = GFFormDisplay::validate($Form, $lead); 
  458.  
  459. $validation_message = ''; 
  460. foreach($Form['fields'] as $field) { 
  461. if(!GFCommon::is_product_field($field["type"])) { 
  462. $validation_message .= (rgget("failed_validation", $field) && !empty($field["validation_message"])) ? sprintf("<li class='gfield_description validation_message'><strong>%s</strong>: %s</li>", $field["label"], $field["validation_message"]) : "";; 
  463. if(!empty($validation_message)) { 
  464. $validation_message = '<ul>'.$validation_message.'</ul>'; 
  465. echo esc_html( apply_filters('kws_gf_directory_lead_error_message', sprintf( __("%sThere were errors with the edit you made.%s%s", 'gravity-forms-addons'), "<div class='error' id='message' style='padding:.5em .75em; background-color:#ffffcc; border:1px solid #ccc;'><p>", "</p>", $validation_message.'</div>'), $lead, $Form) ); 
  466.  
  467. // So the form submission always throws an error even though there's no problem. 
  468. // Product fields can't be edited, so that doesn't really matter. 
  469. if(!empty($is_valid) || (empty($is_valid) && empty($validation_message))) { 
  470. do_action('kws_gf_directory_pre_update_lead', $lead, $Form); 
  471. // since @3.6.1 to enable conditional fields' updates. 
  472. self::save_lead($Form, $lead); 
  473. $lead = RGFormsModel::get_lead($lead["id"]); 
  474.  
  475. do_action('kws_gf_directory_post_update_lead', $lead, $Form); 
  476. echo apply_filters('kws_gf_directory_lead_updated_message', sprintf( esc_html__("%sThe entry was successfully updated.%s", 'gravity-forms-addons'), "<p class='updated' id='message' style='padding:.5em .75em; background-color:#ffffcc; border:1px solid #ccc;'>", "</p>"), $lead, $Form); 
  477. return $lead; 
  478.  
  479. if((isset($_GET['edit']) && wp_verify_nonce($_GET['edit'], 'edit'.$lead['id'].$Form["id"])) || !empty($validation_message)) { 
  480.  
  481. // The ID of the form needs to be `gform_{form_id}` for the pluploader 
  482. ?> 
  483. <form method="post" id="gform_<?php echo esc_attr( $Form['id'] ); ?>" enctype="multipart/form-data" action="<?php echo esc_url( remove_query_arg(array('gf_search', 'sort', 'dir', 'pagenum', 'edit'), add_query_arg(array())) );?>"> 
  484. <?php 
  485. wp_nonce_field('gforms_save_entry', 'gforms_save_entry'); 
  486. ?> 
  487. <input type="hidden" name="action" id="action" value="update"/> 
  488. <input type="hidden" name="screen_mode" id="screen_mode" value="edit" /> 
  489. <?php 
  490.  
  491. $form_without_products = $Form; 
  492. $post_message_shown = false; 
  493. $product_fields = array(); 
  494. foreach($Form['fields'] as $key => $field) { 
  495. if( 
  496. GFCommon::is_product_field($field["type"]) || 
  497. is_numeric($lead["post_id"]) && GFCommon::is_post_field($field) 
  498. ) { 
  499. if(is_numeric($lead["post_id"]) && GFCommon::is_post_field($field) && !$message_shown ) { 
  500. echo apply_filters('kws_gf_directory_edit_post_details_text', sprintf( esc_html__('You can edit post details from the %1$spost page%2$s.', 'gravity-forms-addons'), '<a href="'.admin_url('post.php?action=edit&post='.$lead["post_id"]).'">', '</a>'), $field, $lead, $lead['post_id']); 
  501. $message_shown = true; 
  502.  
  503. unset($form_without_products['fields'][$key]); 
  504. $product_fields[] = $field['id']; 
  505. if(!empty($field['inputs'])) { 
  506. foreach($field['inputs'] as $input) { 
  507. $product_fields[] = $input['id']; 
  508.  
  509. $lead_without_products = &$lead; 
  510. foreach($product_fields as $product_field) { 
  511. $value = RGFormsModel::get_lead_field_value($lead, $field); 
  512. unset($lead_without_products[$product_field]); 
  513.  
  514. require_once(GFCommon::get_base_path() . "/entry_detail.php"); 
  515. GFEntryDetail::lead_detail_edit(apply_filters( 'kws_gf_directory_form_being_edited', $form_without_products, $lead), apply_filters( 'kws_gf_directory_lead_being_edited', $lead_without_products, $form_without_products)); 
  516. echo '<input class="button-primary" type="submit" tabindex="4" value="'.esc_attr( apply_filters('kws_gf_directory_update_lead_button_text', __('Update Entry', 'gravity-forms-addons') ) ).'" name="save" />'; 
  517. ?> 
  518. </form> 
  519. <?php 
  520. do_action('kws_gf_directory_post_after_edit_lead_form', $lead, $Form); 
  521. return false; 
  522. } elseif((isset($_GET['edit']) && !wp_verify_nonce($_GET['edit'], 'edit'))) { 
  523. echo apply_filters('kws_gf_directory_edit_access_error_message', sprintf( esc_html__("%sThe link to edit this entry is not valid; it may have expired.%s", 'gravity-forms-addons'), "<p class='error' id='message' style='padding:.5em .75em; background-color:#ffffcc; border:1px solid #ccc;'>", "</p>"), $lead, $Form); 
  524.  
  525. return $lead; 
  526.  
  527.  
  528. static public function lead_detail($Form, $lead, $allow_display_empty_fields=false, $inline = true, $options = array()) { 
  529.  
  530. if( !class_exists('GFEntryList')) { require_once(GFCommon::get_base_path() . "/entry_list.php"); } 
  531.  
  532. global $current_user, $_gform_directory_approvedcolumn; 
  533. get_currentuserinfo(); 
  534.  
  535. $display_empty_fields = ''; $allow_display_empty_fields = true; 
  536. if($allow_display_empty_fields) { 
  537. $display_empty_fields = @rgget("gf_display_empty_fields", $_COOKIE); 
  538. if(empty($options)) { 
  539. $options = self::directory_defaults(); 
  540.  
  541. // There is no edit link 
  542. if(isset($_GET['edit']) || RGForms::post("action") === "update") { 
  543. // Process editing leads 
  544. $lead = self::edit_lead_detail($Form, $lead, $options); 
  545. if(RGForms::post("action") !== "update") { return; } 
  546.  
  547. extract($options); 
  548.  
  549. ?> 
  550. <table cellspacing="0" class="widefat fixed entry-detail-view"> 
  551. <?php 
  552. $title = str_replace('%%formtitle%%', $Form["title"], str_replace('%%leadid%%', $lead['id'], $entrydetailtitle)); 
  553. if(!empty($title) && $inline) { ?> 
  554. <thead> 
  555. <tr> 
  556. <th id="details" colspan="2" scope="col"> 
  557. <?php 
  558. $title = apply_filters('kws_gf_directory_detail_title', apply_filters('kws_gf_directory_detail_title_'.(int)$lead['id'], array($title, $lead), true), true); 
  559. if(is_array($title)) { 
  560. echo $title[0]; 
  561. } else { 
  562. echo $title; 
  563. ?> 
  564. </th> 
  565. </tr> 
  566. </thead> 
  567. <?php 
  568. ?> 
  569. <tbody> 
  570. <?php 
  571. $count = 0; 
  572. $has_product_fields = false; 
  573. $field_count = sizeof($Form["fields"]); 
  574. $display_value = ''; 
  575. foreach($Form["fields"] as $field) { 
  576.  
  577. // Don't show fields defined as hide in single. 
  578. if(!empty($field['hideInSingle'])) { 
  579. if(self::has_access("gravityforms_directory")) { 
  580. echo "\n\t\t\t\t\t\t\t\t\t".'<!-- '.sprintf(esc_html__('(Admin-only notice) Field #%d not shown: "Hide This Field in Single Entry View" was selected.', 'gravity-forms-addons'), $field['id']).' -->'."\n\n"; 
  581. continue; 
  582.  
  583. $count++; 
  584. $is_last = $count >= $field_count ? true : false; 
  585.  
  586. switch(RGFormsModel::get_input_type($field)) { 
  587. case "section" : 
  588. if(!GFCommon::is_section_empty($field, $Form, $lead) || $display_empty_fields) { 
  589. $count++; 
  590. $is_last = $count >= $field_count ? true : false; 
  591. ?> 
  592. <tr> 
  593. <td colspan="2" class="entry-view-section-break<?php echo $is_last ? " lastrow" : ""?>"><?php echo esc_html(GFCommon::get_label($field))?></td> 
  594. </tr> 
  595. <?php 
  596. break; 
  597.  
  598. case "captcha": 
  599. case "html": 
  600. case "password": 
  601. case "page": 
  602. //ignore captcha, html, password, page field 
  603. break; 
  604.  
  605. case "post_image" : 
  606. $value = RGFormsModel::get_lead_field_value($lead, $field); 
  607. $valueArray = explode("|:|", $value); 
  608.  
  609. @list($url, $title, $caption, $description) = $valueArray; 
  610.  
  611. if(!empty($url)) { 
  612. $value = $display_value = self::render_image_link( $url, $lead, $options, $title, $caption, $description ); 
  613. break; 
  614.  
  615. default : 
  616. //ignore product fields as they will be grouped together at the end of the grid 
  617. if(GFCommon::is_product_field($field["type"])) { 
  618. $has_product_fields = true; 
  619. continue; 
  620.  
  621. $value = RGFormsModel::get_lead_field_value($lead, $field); 
  622. $display_value = GFCommon::get_lead_field_display($field, $value, $lead["currency"]); 
  623. break; 
  624.  
  625. } // end switch 
  626.  
  627. $display_value = apply_filters("gform_entry_field_value", $display_value, $field, $lead, $Form); 
  628. if($display_empty_fields || !empty($display_value) || $display_value === "0") { 
  629. $count++; 
  630. $is_last = $count >= $field_count && !$has_product_fields ? true : false; 
  631. $last_row = $is_last ? " lastrow" : ""; 
  632.  
  633. $display_value = empty($display_value) && $display_value !== "0" ? " " : $display_value; 
  634.  
  635. $content = ' 
  636. <tr> 
  637. <th colspan="2" class="entry-view-field-name">' . esc_html(GFCommon::get_label($field)) . '</th> 
  638. </tr> 
  639. <tr> 
  640. <td colspan="2" class="entry-view-field-value' . $last_row . '">' . $display_value . '</td> 
  641. </tr>'; 
  642.  
  643. $content = apply_filters("gform_field_content", $content, $field, $value, $lead["id"], $Form["id"]); 
  644.  
  645. echo $content; 
  646.  
  647.  
  648. } // End foreach 
  649.  
  650. $products = array(); 
  651. if($has_product_fields) { 
  652. $products = GFCommon::get_product_fields($Form, $lead); 
  653. if(!empty($products["products"])) { 
  654. ?> 
  655. <tr> 
  656. <td colspan="2" class="entry-view-field-name"><?php echo apply_filters("gform_order_label_{$Form["id"]}", apply_filters("gform_order_label", __("Order", "gravityforms"), $Form["id"]), $Form["id"]) ?></td> 
  657. </tr> 
  658. <tr> 
  659. <td colspan="2" class="entry-view-field-value lastrow"> 
  660. <table class="entry-products" cellspacing="0" width="97%"> 
  661. <colgroup> 
  662. <col class="entry-products-col1"> 
  663. <col class="entry-products-col2"> 
  664. <col class="entry-products-col3"> 
  665. <col class="entry-products-col4"> 
  666. </colgroup> 
  667. <thead> 
  668. <th scope="col"><?php echo apply_filters("gform_product_{$Form['id']}", apply_filters("gform_product", __("Product", "gravityforms"), $Form['id']), $Form['id']) ?></th> 
  669. <th scope="col" class="textcenter"><?php echo apply_filters("gform_product_qty_{$Form['id']}", apply_filters("gform_product_qty", __("Qty", "gravityforms"), $Form['id']), $Form['id']) ?></th> 
  670. <th scope="col"><?php echo apply_filters("gform_product_unitprice_{$Form['id']}", apply_filters("gform_product_unitprice", __("Unit Price", "gravityforms"), $Form['id']), $Form['id']) ?></th> 
  671. <th scope="col"><?php echo apply_filters("gform_product_price_{$Form['id']}", apply_filters("gform_product_price", __("Price", "gravityforms"), $Form['id']), $Form['id']) ?></th> 
  672. </thead> 
  673. <tbody> 
  674. <?php 
  675.  
  676. $total = 0; 
  677. foreach($products["products"] as $product) { 
  678. ?> 
  679. <tr> 
  680. <td> 
  681. <div class="product_name"><?php echo esc_html($product["name"])?></div> 
  682. <ul class="product_options"> 
  683. <?php 
  684. $price = GFCommon::to_number($product["price"]); 
  685. if(is_array(rgar($product, "options"))) { 
  686. $count = sizeof($product["options"]); 
  687. $index = 1; 
  688. foreach($product["options"] as $option) { 
  689. $price += GFCommon::to_number($option["price"]); 
  690. $class = $index == $count ? " class='lastitem'" : ""; 
  691. $index++; 
  692. ?> 
  693. <li<?php echo $class?>><?php echo $option["option_label"]?></li> 
  694. <?php 
  695. $subtotal = floatval($product["quantity"]) * $price; 
  696. $total += $subtotal; 
  697. ?> 
  698. </ul> 
  699. </td> 
  700. <td class="textcenter"><?php echo $product["quantity"] ?></td> 
  701. <td><?php echo GFCommon::to_money($price, $lead["currency"]) ?></td> 
  702. <td><?php echo GFCommon::to_money($subtotal, $lead["currency"]) ?></td> 
  703. </tr> 
  704. <?php 
  705. $total += floatval($products["shipping"]["price"]); 
  706. ?> 
  707. </tbody> 
  708. <tfoot> 
  709. <?php 
  710. if(!empty($products["shipping"]["name"])) { 
  711. ?> 
  712. <tr> 
  713. <td colspan="2" rowspan="2" class="emptycell"> </td> 
  714. <td class="textright shipping"><?php echo $products["shipping"]["name"] ?></td> 
  715. <td class="shipping_amount"><?php echo GFCommon::to_money($products["shipping"]["price"], $lead["currency"])?> </td> 
  716. </tr> 
  717. <?php 
  718. ?> 
  719. <tr> 
  720. <?php 
  721. if(empty($products["shipping"]["name"])) { 
  722. ?> 
  723. <td colspan="2" class="emptycell"> </td> 
  724. <?php 
  725. ?> 
  726. <td class="textright grandtotal"><?php esc_html_e("Total", "gravityforms") ?></td> 
  727. <td class="grandtotal_amount"><?php echo GFCommon::to_money($total, $lead["currency"])?></td> 
  728. </tr> 
  729. </tfoot> 
  730. </table> 
  731. </td> 
  732. </tr> 
  733.  
  734. <?php 
  735.  
  736. // Edit link 
  737. if( 
  738. !empty($options['useredit']) && is_user_logged_in() && intval( $current_user->ID ) === intval( $lead['created_by'] ) || // Is user who created the entry 
  739. !empty($options['adminedit']) && self::has_access("gravityforms_directory") // Or is an administrator 
  740. ) { 
  741.  
  742. if(!empty($options['adminedit']) && self::has_access("gravityforms_directory")) { 
  743. $editbuttontext = apply_filters('kws_gf_directory_edit_entry_text_admin', __("Edit Entry", 'gravity-forms-addons')); 
  744. } else { 
  745. $editbuttontext = apply_filters('kws_gf_directory_edit_entry_text_user', __("Edit Your Entry", 'gravity-forms-addons')); 
  746.  
  747. ?> 
  748. <tr> 
  749. <th scope="row" class="entry-view-field-name"><?php echo esc_html( apply_filters('kws_gf_directory_edit_entry_th', __( "Edit", "gravity-forms-addons" ) ) ); ?></th> 
  750. <td class="entry-view-field-value useredit"><a href="<?php echo esc_url( add_query_arg(array('edit' => wp_create_nonce('edit'.$lead['id'].$Form["id"])))); ?>"><?php echo $editbuttontext; ?></a></td> 
  751. </tr> 
  752. <?php 
  753.  
  754. ?> 
  755. </tbody> 
  756. </table> 
  757. <?php 
  758.  
  759. static public function get_admin_only($form, $adminOnly = array()) { 
  760. if(!is_array($form)) { return false; } 
  761.  
  762. foreach($form['fields'] as $key=>$col) { 
  763. // Only the Go to Entry button adds disableMargins. 
  764.  
  765. if($col['type'] === 'hidden' && !empty($col['useAsEntryLink']) && !empty($col['disableMargins'])) { 
  766. continue; 
  767. if(!empty($col['adminOnly'])) { 
  768. $adminOnly[] = $col['id']; 
  769. if(isset($col['inputs']) && is_array($col['inputs'])) { 
  770. foreach($col['inputs'] as $key2=>$input) { 
  771. if(!empty($col['adminOnly'])) { 
  772. $adminOnly[] = $input['id']; 
  773. return $adminOnly; 
  774.  
  775. /** 
  776. * Get the form and lead IDs from the URL or from $_REQUEST 
  777. * @return array|null $formid, $leadid if found. Null if not. 
  778. */ 
  779. static private function get_form_and_lead_ids() { 
  780. global $wp, $wp_rewrite; 
  781.  
  782. $formid = $leadid = null; 
  783.  
  784. $url = isset($wp->request) ? $wp->request : add_query_arg(array(), home_url() ); 
  785.  
  786. if( 
  787. // If permalinks is turned on 
  788. $wp_rewrite->using_permalinks() && 
  789. // And 
  790. preg_match('/\/?'.sanitize_title(apply_filters('kws_gf_directory_endpoint', 'entry')).'\/([0-9]+)(?:\/|-)([0-9]+)/ism', $url, $matches)) { 
  791. $formid = $matches[1]; 
  792. $leadid = $matches[2]; 
  793. } else { 
  794. $formid = isset($_REQUEST['form']) ? (int)$_REQUEST['form'] : $formid; 
  795. $leadid = isset($_REQUEST['leadid']) ? (int)$_REQUEST['leadid'] : $leadid; 
  796.  
  797. return array($formid, $leadid); 
  798.  
  799.  
  800.  
  801. /** 
  802. * get_back_link function. 
  803. * 
  804. * @access public 
  805. * @param string $entryback (default: '') The text of the back-link anchor 
  806. * @return string The HTML link for the backlink 
  807. */ 
  808. static public function get_back_link($passed_entryback = '') { 
  809. global $pagenow, $wp_rewrite; 
  810.  
  811. $options = self::directory_defaults(); 
  812.  
  813. if(isset($_GET['edit'])) { 
  814. return '<p class="entryback"><a href="'.esc_url( add_query_arg(array(), remove_query_arg(array('edit'))) ).'">'.esc_html(__(apply_filters('kws_gf_directory_edit_entry_cancel', "← Cancel Editing"), "gravity-forms-addons")).'</a></p>'; 
  815.  
  816. list($formid, $leadid) = self::get_form_and_lead_ids(); 
  817. extract($options); 
  818.  
  819. // Use passed value, if available. Otherwise, use default 
  820. $entryback = !empty($passed_entryback) ? $passed_entryback : $entryback; 
  821.  
  822. if($pagenow === 'entry-details.php') { 
  823.  
  824. // If possible, link back to the original post. 
  825. if(isset($_GET['post'])) { 
  826. $href = get_permalink((int)$_GET['post']); 
  827. } else { 
  828. // Otherwise we rely on Javascript below. 
  829. $href = '#'; 
  830.  
  831. $onclick = ' onclick="parent.jQuery.fn.colorbox.close();"'; 
  832. } else { 
  833. $onclick = ''; 
  834. $href = remove_query_arg(array('row', 'leadid', 'form', 'edit')); 
  835. if($wp_rewrite->using_permalinks()) { 
  836. $href = preg_replace('/('.sanitize_title(apply_filters('kws_gf_directory_endpoint', 'entry')).'\/(?:[0-9]+)(?:\/|-)(?:[0-9]+)\/?)/ism', '', $href); 
  837.  
  838. $href = esc_url_raw( $href ); 
  839.  
  840. $url = parse_url(add_query_arg(array(), $href)); 
  841. if(!empty($url['query']) && !empty($permalink)) { $href .= '?'.$url['query']; } 
  842. if(!empty($options['entryanchor'])) { $href .= '#lead_row_'.$leadid; } 
  843.  
  844. // If there's a back link, format it 
  845. if(!empty($entryback) && !empty($entryonly)) { 
  846. $link = apply_filters('kws_gf_directory_backlink', '<p class="entryback"><a href="'.$href.'"'.$onclick.'>'.esc_html($entryback).'</a></p>', $href, $entryback); 
  847. } else { 
  848. $link = ''; 
  849.  
  850. return $link; 
  851.  
  852. static public function process_lead_detail($inline = true, $entryback = '', $showadminonly = false, $adminonlycolumns = array(), $approvedcolumn = null, $options = array(), $entryonly = true) { 
  853. global $wp, $post, $wp_rewrite, $wpdb; 
  854. $formid = $leadid = false; 
  855.  
  856. list($formid, $leadid) = self::get_form_and_lead_ids(); 
  857.  
  858. if(!is_null($leadid) && !is_null($formid)) { 
  859.  
  860. $form = apply_filters('kws_gf_directory_lead_detail_form', RGFormsModel::get_form_meta((int)$formid)); 
  861. $lead = apply_filters('kws_gf_directory_lead_detail', RGFormsModel::get_lead((int)$leadid)); 
  862.  
  863. if(empty($approvedcolumn)) { $approvedcolumn = self::get_approved_column($form); } 
  864. if(empty($adminonlycolumns) && !$showadminonly) { $adminonlycolumns = self::get_admin_only($form); } 
  865.  
  866. //since 3.5 
  867. $lead = self::remove_hidden_fields( array( $lead ), $adminonlycolumns, $approvedcolumn, true, true, $showadminonly , $form ); 
  868. $lead = $lead[0]; 
  869.  
  870. ob_start(); // Using ob_start() allows us to filter output 
  871. self::lead_detail($form, $lead, false, $inline, $options); 
  872. $content = ob_get_contents(); // Get the output 
  873. ob_end_clean(); // Clear the buffer 
  874.  
  875. // Get the back link if this is a single entry. 
  876. $link = !empty($entryonly) ? self::get_back_link($entryback) : ''; 
  877.  
  878. $content = $link . $content; 
  879. $content = apply_filters('kws_gf_directory_detail', apply_filters('kws_gf_directory_detail_'.(int)$leadid, $content, (int)$leadid), (int)$leadid); 
  880.  
  881.  
  882. if(isset($options['entryview'])) { 
  883. $content = self::html_display_type_filter($content, $options['entryview'], true); 
  884.  
  885. return $content; 
  886. } else { 
  887. return false; 
  888.  
  889. static public function change_directory_columns() { 
  890. check_ajax_referer('gforms_directory_columns', 'gforms_directory_columns'); 
  891. $columns = GFCommon::json_decode(stripslashes($_POST["directory_columns"]), true); 
  892. self::update_grid_column_meta((int)$_POST['form_id'], $columns); 
  893.  
  894. public static function update_grid_column_meta($form_id, $columns) { 
  895. global $wpdb; 
  896.  
  897. $meta = maybe_serialize(stripslashes_deep($columns) ); 
  898.  
  899. update_option('gf_directory_form_'.$form_id.'_grid', $meta); 
  900.  
  901. public static function get_grid_column_meta($form_id) { 
  902. global $wpdb; 
  903.  
  904. $grid = get_option('gf_directory_form_'.$form_id.'_grid'); 
  905. if(!$grid) { 
  906. $grid = GFFormsModel::get_grid_column_meta($form_id); 
  907. self::update_grid_column_meta($form_id, $grid); 
  908. return maybe_unserialize($grid); 
  909.  
  910. public static function get_grid_columns($form_id, $input_label_only=false) { 
  911. $form = GFFormsModel::get_form_meta($form_id); 
  912. $field_ids = self::get_grid_column_meta($form_id); 
  913.  
  914. if(!is_array($field_ids)) { 
  915. $field_ids = array(); 
  916. for($i=0, $count=sizeof($form["fields"]); $i<$count && $i<5; $i++) { 
  917. $field = $form["fields"][$i]; 
  918.  
  919. if(RGForms::get("displayOnly", $field)) 
  920. continue; 
  921.  
  922.  
  923. if(isset($field["inputs"]) && is_array($field["inputs"])) { 
  924. $field_ids[] = $field["id"]; 
  925. if($field["type"] == "name") { 
  926. $field_ids[] = $field["id"] . '.3'; //adding first name 
  927. $field_ids[] = $field["id"] . '.6'; //adding last name 
  928. else if(isset($field["inputs"][0])) { 
  929. $field_ids[] = $field["inputs"][0]["id"]; //getting first input 
  930. else{ 
  931. $field_ids[] = $field["id"]; 
  932. //adding default entry meta columns 
  933. $entry_metas = GFFormsModel::get_entry_meta($form_id); 
  934. foreach ($entry_metas as $key => $entry_meta) { 
  935. if (rgar($entry_meta, "is_default_column")) 
  936. $field_ids[] = $key; 
  937.  
  938. $columns = array(); 
  939. $entry_meta = GFFormsModel::get_entry_meta($form_id); 
  940. foreach($field_ids as $field_id) { 
  941.  
  942. switch($field_id) { 
  943. case "id" : 
  944. $columns[$field_id] = array("label" => "Entry Id", "type" => "id"); 
  945. break; 
  946. case "ip" : 
  947. $columns[$field_id] = array("label" => "User IP", "type" => "ip"); 
  948. break; 
  949. case "date_created" : 
  950. $columns[$field_id] = array("label" => "Entry Date", "type" => "date_created"); 
  951. break; 
  952. case "source_url" : 
  953. $columns[$field_id] = array("label" => "Source Url", "type" => "source_url"); 
  954. break; 
  955. case "payment_status" : 
  956. $columns[$field_id] = array("label" => "Payment Status", "type" => "payment_status"); 
  957. break; 
  958. case "transaction_id" : 
  959. $columns[$field_id] = array("label" => "Transaction Id", "type" => "transaction_id"); 
  960. break; 
  961. case "payment_date" : 
  962. $columns[$field_id] = array("label" => "Payment Date", "type" => "payment_date"); 
  963. break; 
  964. case "payment_amount" : 
  965. $columns[$field_id] = array("label" => "Payment Amount", "type" => "payment_amount"); 
  966. break; 
  967. case "created_by" : 
  968. $columns[$field_id] = array("label" => "User", "type" => "created_by"); 
  969. break; 
  970. case ((is_string($field_id) || is_int($field_id)) && array_key_exists($field_id, $entry_meta)) : 
  971. $columns[$field_id] = array("label" => $entry_meta[$field_id]["label"], "type" => $field_id); 
  972. break; 
  973. default : 
  974. $field = GFFormsModel::get_field($form, $field_id); 
  975. if($field) { 
  976. $columns[strval($field_id)] = array("label" => self::get_label($field, $field_id, $input_label_only), "type" => rgget("type", $field), "inputType" => rgget("inputType", $field)); 
  977. return $columns; 
  978.  
  979. /** 
  980. * Get the label for the input field. This is necessary to prevent Admin Labels from being used instead of normal labels. 
  981. */ 
  982. public static function get_label($field, $input_id = 0, $input_only = false) { 
  983. $field_label = rgar($field, "label"); 
  984. $input = GFFormsModel::get_input($field, $input_id); 
  985. if(rgget("type", $field) == "checkbox" && $input != null) 
  986. return $input["label"]; 
  987. else if($input != null) 
  988. return $input_only ? $input["label"] : $field_label . ' (' . $input["label"] . ')'; 
  989. else 
  990. return $field_label; 
  991.  
  992. public static function make_directory($atts) { 
  993. global $wpdb, $wp_rewrite, $post, $wpdb, $directory_shown, $kws_gf_scripts, $kws_gf_styles; 
  994.  
  995. if(!class_exists('GFEntryDetail')) { @require_once(GFCommon::get_base_path() . "/entry_detail.php"); } 
  996. if(!class_exists('GFCommon')) { @require_once(GFCommon::get_base_path() . "/common.php"); } 
  997. if(!class_exists('RGFormsModel')) { @require_once(GFCommon::get_base_path() . "/forms_model.php"); } 
  998. if(!class_exists('GFEntryList')) { require_once(GFCommon::get_base_path() . "/entry_list.php"); } 
  999.  
  1000. //quit if version of wp is not supported 
  1001. if(!class_exists('GFCommon') || !GFCommon::ensure_wp_version()) 
  1002. return; 
  1003.  
  1004. // Already showed edit directory form and there are more than one forms on the page. 
  1005. if(did_action('kws_gf_directory_post_after_edit_lead_form')) { return; } 
  1006.  
  1007. ob_start(); // Using ob_start() allows us to use echo instead of $output .= 
  1008.  
  1009. foreach($atts as $key => $att) { 
  1010. if(strtolower($att) == 'false') { $atts[$key] = false; } 
  1011. if(strtolower($att) == 'true') { $atts[$key] = true; } 
  1012.  
  1013. $atts['approved'] = isset($atts['approved']) ? $atts['approved'] : -1; 
  1014.  
  1015. if(!empty($atts['lightboxsettings']) && is_string($atts['lightboxsettings'])) { 
  1016. $atts['lightboxsettings'] = explode(', ', $atts['lightboxsettings']); 
  1017.  
  1018. $options = self::directory_defaults($atts); 
  1019.  
  1020. // Make sure everything is on the same page. 
  1021. if(is_array($options['lightboxsettings'])) { 
  1022. foreach($options['lightboxsettings'] as $key => $value) { 
  1023. if(is_numeric($key)) { 
  1024. $options['lightboxsettings']["{$value}"] = $value; 
  1025. unset($options['lightboxsettings']["{$key}"]); 
  1026.  
  1027.  
  1028. extract( $options ); 
  1029.  
  1030. $form_id = $form; 
  1031.  
  1032. $form = RGFormsModel::get_form_meta($form_id); 
  1033.  
  1034. if(empty($form)) { return;} 
  1035.  
  1036. $sort_field = empty($_GET["sort"]) ? $sort : $_GET["sort"]; 
  1037. $sort_direction = empty($_GET["dir"]) ? $dir : $_GET["dir"]; 
  1038. $search_query = !empty($_GET["gf_search"]) ? $_GET["gf_search"] : null; 
  1039.  
  1040.  
  1041. $start_date = !empty($_GET["start_date"]) ? $_GET["start_date"] : $start_date; 
  1042. $end_date = !empty($_GET["end_date"]) ? $_GET["end_date"] : $end_date; 
  1043.  
  1044. $page_index = empty($_GET["pagenum"]) ? $startpage -1 : intval($_GET["pagenum"]) - 1; 
  1045. $star = (isset($_GET["star"]) && is_numeric($_GET["star"])) ? intval($_GET["star"]) : null; 
  1046. $read = (isset($_GET["read"]) && is_numeric($_GET["read"])) ? intval($_GET["read"]) : null; 
  1047. $first_item_index = $page_index * $page_size; 
  1048. $link_params = array(); 
  1049. if(!empty($page_index)) { $link_params['pagenum'] = $page_index; } 
  1050. $formaction = esc_url_raw( remove_query_arg(array('gf_search', 'sort', 'dir', 'pagenum', 'edit'), add_query_arg($link_params)) ); 
  1051. $tableclass .= !empty($jstable) ? sprintf(' tablesorter tablesorter-%s', apply_filters('kws_gf_tablesorter_theme', 'blue', $form)) : ''; 
  1052. $title = $form["title"]; 
  1053. $sort_field_meta = RGFormsModel::get_field($form, $sort_field); 
  1054. $is_numeric = $sort_field_meta["type"] == "number"; 
  1055.  
  1056. $columns = self::get_grid_columns($form_id, true); 
  1057.  
  1058. $approvedcolumn = null; 
  1059.  
  1060. if((!$approved && $approved !== -1) || (!empty($smartapproval) && $approved === -1)) { 
  1061. $approvedcolumn = self::get_approved_column($form); 
  1062.  
  1063. if(!empty($smartapproval) && $approved === -1 && !empty($approvedcolumn)) { 
  1064. $approved = true; // If there is an approved column, turn on approval 
  1065. } else { 
  1066. $approved = false; // Otherwise, show entries as normal. 
  1067.  
  1068. $entrylinkcolumns = self::get_entrylink_column($form, $entry); 
  1069. $adminonlycolumns = self::get_admin_only($form); 
  1070.  
  1071. // 
  1072. // Show only a single entry 
  1073. // 
  1074. $detail = self::process_lead_detail(true, $entryback, $showadminonly, $adminonlycolumns, $approvedcolumn, $options, $entryonly); 
  1075.  
  1076. if(!empty($entry) && !empty($detail)) { 
  1077.  
  1078. // Once again, checking to make sure this hasn't been shown already with multiple shortcodes on one page. 
  1079. if(!did_action('kws_gf_after_directory')) { 
  1080. echo $detail; 
  1081.  
  1082. if(!empty($entryonly)) { 
  1083. do_action('kws_gf_after_directory', do_action('kws_gf_after_directory_form_'.$form_id, $form, compact("approved", "sort_field", "sort_direction", "search_query", "first_item_index", "page_size", "star", "read", "is_numeric", "start_date", "end_date"))); 
  1084.  
  1085. $content = ob_get_clean(); // Get the output and clear the buffer 
  1086.  
  1087. // If the form is form #2, two filters are applied: `kws_gf_directory_output_2` and `kws_gf_directory_output` 
  1088. $content = apply_filters('kws_gf_directory_output', apply_filters('kws_gf_directory_output_'.$form_id, self::html_display_type_filter($content, $directoryview))); 
  1089. return $content; 
  1090.  
  1091.  
  1092. // since 3.5 - remove columns of the fields not allowed to be shown 
  1093. $columns = self::remove_hidden_fields( $columns, $adminonlycolumns, $approvedcolumn, false, false, $showadminonly , $form ); 
  1094.  
  1095. // hook for external selection of columns 
  1096. $columns = apply_filters( 'kws_gf_directory_filter_columns', $columns ); 
  1097.  
  1098.  
  1099. //since 3.5 search criteria 
  1100. $show_search_filters = self::get_search_filters( $form ); 
  1101. $show_search_filters = apply_filters( 'kws_gf_directory_search_filters', $show_search_filters, $form ); 
  1102. $search_criteria = array(); 
  1103. foreach( $show_search_filters as $key ) { 
  1104. if( !empty( $_GET['filter_'. $key ] ) ) { 
  1105. $search_criteria[ $key ] = $_GET['filter_'. $key ]; 
  1106.  
  1107.  
  1108. // 
  1109. // Or start to generate the directory 
  1110. // 
  1111. $leads = GFDirectory::get_leads( $form_id, $sort_field, $sort_direction, $search_query, $first_item_index, $page_size, $star, $read, $is_numeric, $start_date, $end_date, 'active', $approvedcolumn, $limituser, $search_criteria ); 
  1112.  
  1113.  
  1114. // Allow lightbox to determine whether showadminonly is valid without passing a query string in URL 
  1115. if($entry === true && !empty($lightboxsettings['entry'])) { 
  1116. if(get_site_transient('gf_form_'.$form_id.'_post_'.$post->ID.'_showadminonly') != $showadminonly) { 
  1117. set_site_transient('gf_form_'.$form_id.'_post_'.$post->ID.'_showadminonly', $showadminonly, HOUR_IN_SECONDS); 
  1118. } else { 
  1119. delete_site_transient('gf_form_'.$form_id.'_post_'.$post->ID.'_showadminonly'); 
  1120.  
  1121.  
  1122. // Get a list of query args for the pagination links 
  1123. if(!empty($search_query)) { $args["gf_search"] = urlencode($search_query); } 
  1124. if(!empty($sort_field)) { $args["sort"] = $sort_field; } 
  1125. if(!empty($sort_direction)) { $args["dir"] = $sort_direction; } 
  1126. if(!empty($star)) { $args["star"] = $star; } 
  1127.  
  1128. if($page_size > 0) { 
  1129.  
  1130. // $leads contains all the entries according to request, since 3.5, to allow multisort. 
  1131. if( apply_filters( 'kws_gf_directory_want_multisort', false ) ) { 
  1132. $lead_count = count( $leads ); 
  1133. $leads = array_slice( $leads, $first_item_index, $page_size ); 
  1134. } else { 
  1135. $lead_count = self::get_lead_count($form_id, $search_query, $star, $read, $approvedcolumn, $approved, $leads, $start_date, $end_date, $limituser, $search_criteria); 
  1136.  
  1137.  
  1138.  
  1139. $page_links = array( 
  1140. 'base' => esc_url_raw( @add_query_arg('pagenum', '%#%') ), // get_permalink().'%_%',  
  1141. 'format' => '&pagenum=%#%',  
  1142. 'add_args' => $args,  
  1143. 'prev_text' => $prev_text,  
  1144. 'next_text' => $next_text,  
  1145. 'total' => ceil($lead_count / $page_size),  
  1146. 'current' => $page_index + 1,  
  1147. 'show_all' => $pagelinksshowall,  
  1148. ); 
  1149.  
  1150. $page_links = apply_filters('kws_gf_results_pagination', $page_links); 
  1151.  
  1152. $page_links = paginate_links($page_links); 
  1153. } else { 
  1154. // Showing all results 
  1155. $page_links = false; 
  1156. $lead_count = sizeof($leads); 
  1157.  
  1158.  
  1159. if(!isset($directory_shown)) { 
  1160. $directory_shown = true; 
  1161.  
  1162.  
  1163. ?> 
  1164.  
  1165. <script> 
  1166. <?php if(!empty($lightboxsettings['images']) || !empty($lightboxsettings['entry'])) { ?> 
  1167.  
  1168. var tb_pathToImage = "<?php echo site_url('/wp-includes/js/thickbox/loadingAnimation.gif'); ?>"; 
  1169. var tb_closeImage = "<?php echo site_url('/wp-includes/js/thickbox/tb-close.png'); ?>"; 
  1170. var tb_height = 600; 
  1171. <?php } ?> 
  1172. function not_empty(variable) { 
  1173. if(variable == '' || variable == null || variable == 'undefined' || typeof(variable) == 'undefined') { 
  1174. return false; 
  1175. } else { 
  1176. return true; 
  1177.  
  1178. <?php if(!empty($jstable)) { ?> 
  1179. jQuery(document).ready(function($) { 
  1180. $('.tablesorter').each(function() { 
  1181. $(this).tablesorter(<?php echo apply_filters('kws_gf_directory_tablesorter_options', '') ?>); 
  1182. }); 
  1183. }); 
  1184. <?php } else if(isset($jssearch) && $jssearch) { ?> 
  1185. function Search(search, sort_field_id, sort_direction, search_criteria ) { 
  1186. if(not_empty(search)) { var search = "&gf_search=" + encodeURIComponent(search); } else { var search = ''; } 
  1187.  
  1188. var search_filters = ''; 
  1189. if( not_empty( search_criteria ) ) { 
  1190. $.each( search_criteria, function( index, value ) { 
  1191. search_filters += "&filter_" + index + "=" + encodeURIComponent(value); 
  1192. } ); 
  1193.  
  1194. if(not_empty(sort_field_id)) { var sort = "&sort=" + sort_field_id; } else { var sort = ''; } 
  1195. if(not_empty(sort_direction)) { var dir = "&dir=" + sort_direction; } else { var dir = ''; } 
  1196. var page = '<?php if($wp_rewrite->using_permalinks()) { echo '?'; } else { echo '&'; } ?>page='+<?php echo isset($_GET['pagenum']) ? intval($_GET['pagenum']) : '"1"'; ?>; 
  1197. var location = "<?php echo get_permalink($post->ID); ?>"+page+search+sort+dir+search_filters; 
  1198. document.location = location; 
  1199. <?php } ?> 
  1200. </script> 
  1201. <?php } ?> 
  1202.  
  1203. <div class="wrap"> 
  1204. <?php if( $titleshow ) : ?> 
  1205. <h2><?php echo $titleprefix.$title; ?></h2> 
  1206. <?php endif; ?> 
  1207.  
  1208. <?php // --- Render Search Box --- 
  1209.  
  1210. if( $search || !empty( $show_search_filters ) ) : ?> 
  1211.  
  1212. <form id="lead_form" method="get" action="<?php echo $formaction; ?>"> 
  1213. <?php 
  1214. //New logic for search criterias (since 3.5) 
  1215.  
  1216. if( !empty( $show_search_filters ) ) { 
  1217.  
  1218. foreach( $show_search_filters as $key ) { 
  1219. $properties = self::get_field_properties( $form, $key ); 
  1220. if( in_array( $properties['type'] , array( 'select', 'checkbox', 'radio', 'post_category' ) ) ) { 
  1221. echo self::render_search_dropdown( $properties['label'], 'filter_'.$properties['id'], $properties['choices'] ); //Label, name attr, choices 
  1222. } else { 
  1223. echo self::render_search_input( $properties['label'], 'filter_'.$properties['id'] ); //label, attr name 
  1224.  
  1225.  
  1226.  
  1227. ?> 
  1228. <p class="search-box"> 
  1229. <?php if( $search ) : ?> 
  1230. <label class="hidden" for="lead_search"><?php esc_html_e("Search Entries:", "gravity-forms-addons"); ?></label> 
  1231. <input type="text" name="gf_search" id="lead_search" value="<?php echo $search_query ?>"<?php if( $searchtabindex ) { echo ' tabindex="'.intval( $searchtabindex ).'"'; } ?> /> 
  1232. <?php endif; ?> 
  1233. <?php 
  1234. // If not using permalinks, let's make the form work! 
  1235. echo !empty($_GET['p']) ? '<input name="p" type="hidden" value="'.esc_html( $_GET['p'] ).'" />' : ''; 
  1236. echo !empty($_GET['page_id']) ? '<input name="page_id" type="hidden" value="'.esc_html($_GET['page_id']).'" />' : ''; 
  1237. ?> 
  1238. <input type="submit" class="button" id="lead_search_button" value="<?php esc_attr_e("Search", "gravity-forms-addons") ?>"<?php if($searchtabindex) { echo ' tabindex="'.intval($searchtabindex++).'"'; } ?> /> 
  1239. </p> 
  1240. </form> 
  1241.  
  1242. <?php endif; 
  1243.  
  1244.  
  1245. //Displaying paging links if appropriate 
  1246.  
  1247. if($lead_count > 0 && $showcount || $page_links) { 
  1248. if($lead_count == 0) { $first_item_index--; } 
  1249. ?> 
  1250. <div class="tablenav"> 
  1251. <div class="tablenav-pages"> 
  1252. <?php if($showcount) { 
  1253. if(($first_item_index + $page_size) > $lead_count || $page_size <= 0) { 
  1254. $second_part = $lead_count; 
  1255. } else { 
  1256. $second_part = $first_item_index + $page_size; 
  1257. ?> 
  1258. <span class="displaying-num"><?php printf(__("Displaying %d - %d of %d", "gravity-forms-addons"), $first_item_index + 1, $second_part, $lead_count) ?></span> 
  1259. <?php } if($page_links) { echo $page_links; } ?> 
  1260. </div> 
  1261. <div class="clear"></div> 
  1262. </div> 
  1263. <?php 
  1264.  
  1265. do_action('kws_gf_before_directory_after_nav', do_action('kws_gf_before_directory_after_nav_form_'.$form_id, $form, $leads, compact("approved", "sort_field", "sort_direction", "search_query", "first_item_index", "page_size", "star", "read", "is_numeric", "start_date", "end_date"))); 
  1266. ?> 
  1267.  
  1268. <table class="<?php echo $tableclass; ?>" cellspacing="0"<?php if(!empty($tablewidth)) { echo ' width="'.$tablewidth.'"'; } echo $tablestyle ? ' style="'.$tablestyle.'"' : ''; ?>> 
  1269. <?php if($thead) {?> 
  1270. <thead> 
  1271. <tr> 
  1272. <?php 
  1273.  
  1274. $addressesExist = false; 
  1275. foreach($columns as $field_id => $field_info) { 
  1276. $dir = $field_id == 0 ? "DESC" : "ASC"; //default every field so ascending sorting except date_created (id=0) 
  1277. if($field_id == $sort_field) { //reverting direction if clicking on the currently sorted field 
  1278. $dir = $sort_direction == "ASC" ? "DESC" : "ASC"; 
  1279. if(is_array($adminonlycolumns) && !in_array($field_id, $adminonlycolumns) || (is_array($adminonlycolumns) && in_array($field_id, $adminonlycolumns) && $showadminonly) || !$showadminonly) { 
  1280. if($field_info['type'] == 'address' && $appendaddress && $hideaddresspieces) { $addressesExist = true; continue; } 
  1281. ?> 
  1282. <?php 
  1283. $_showlink = false; 
  1284. if(isset($jssearch) && $jssearch && !isset($jstable)) { ?> 
  1285. <th scope="col" id="gf-col-<?php echo $form_id.'-'.$field_id ?>" class="manage-column" onclick="Search('<?php echo $search_query ?>', '<?php echo $field_id ?>', '<?php echo $dir ?>', '' );" style="cursor:pointer;"><?php 
  1286. } elseif(isset($jstable) && $jstable || $field_info['type'] === 'id') {?> 
  1287. <th scope="col" id="gf-col-<?php echo $form_id.'-'.$field_id ?>" class="manage-column"> 
  1288. <?php } else { 
  1289. $_showlink = true; 
  1290. ?> 
  1291. <th scope="col" id="gf-col-<?php echo $form_id.'-'.$field_id ?>" class="manage-column"> 
  1292. <a href="<?php 
  1293. $searchpage = isset($_GET['pagenum']) ? intval($_GET['pagenum']) : 1; 
  1294. $new_query_args = array('gf_search' => $search_query, 'sort' => $field_id, 'dir' => $dir, 'pagenum' => $searchpage ); 
  1295. foreach( $search_criteria as $key => $value ) { 
  1296. $new_query_args[ 'filter_'.$key ] = $value; 
  1297. echo add_query_arg( $new_query_args, get_permalink($post->ID)); 
  1298. ?>"><?php 
  1299. if($field_info['type'] == 'id' && $entry) { $label = $entryth; } 
  1300. else { $label = $field_info["label"]; } 
  1301.  
  1302. $label = apply_filters('kws_gf_directory_th', apply_filters('kws_gf_directory_th_'.$field_id, apply_filters('kws_gf_directory_th_'.sanitize_title($label), $label))); 
  1303. echo esc_html($label); 
  1304.  
  1305. if($_showlink) { ?></a><?php } ?> 
  1306. </th> 
  1307. <?php 
  1308.  
  1309. if($appendaddress && $addressesExist) { 
  1310. ?> 
  1311. <th scope="col" id="gf-col-<?php echo $form_id.'-'.$field_id ?>" class="manage-column" onclick="Search('<?php echo $search_query ?>', '<?php echo $field_id ?>', '<?php echo $dir ?>');" style="cursor:pointer;"><?php 
  1312. $label = apply_filters('kws_gf_directory_th', apply_filters('kws_gf_directory_th_address', 'Address')); 
  1313. echo esc_html($label) 
  1314.  
  1315. ?></th> 
  1316. <?php 
  1317. ?> 
  1318. </tr> 
  1319. </thead> 
  1320. <?php } ?> 
  1321. <tbody class="list:user user-list"> 
  1322. <?php 
  1323. include(plugin_dir_path( __FILE__ ) . "/template-row.php"); 
  1324. ?> 
  1325. </tbody> 
  1326. <?php if($tfoot) { 
  1327. if(isset($jssearch) && $jssearch && !isset($jstable)) { 
  1328. $th = '<th scope="col" id="gf-col-'.$form_id.'-'.$field_id.'" class="manage-column" onclick="Search(\''.$search_query.'\', \''.$field_id.'\', \''.$dir.'\');" style="cursor:pointer;">'; 
  1329. } else { 
  1330. $th = '<th scope="col" id="gf-col-'.$form_id.'-'.$field_id.'" class="manage-column">'; 
  1331. ?> 
  1332. <tfoot> 
  1333. <tr> 
  1334. <?php 
  1335. $addressesExist = false; 
  1336. foreach($columns as $field_id => $field_info) { 
  1337. $dir = $field_id == 0 ? "DESC" : "ASC"; //default every field so ascending sorting except date_created (id=0) 
  1338. if($field_id == $sort_field) { //reverting direction if clicking on the currently sorted field 
  1339. $dir = $sort_direction == "ASC" ? "DESC" : "ASC"; 
  1340. if(is_array($adminonlycolumns) && !in_array($field_id, $adminonlycolumns) || (is_array($adminonlycolumns) && in_array($field_id, $adminonlycolumns) && $showadminonly) || !$showadminonly) { 
  1341. if($field_info['type'] == 'address' && $appendaddress && $hideaddresspieces) { $addressesExist = true; continue; } 
  1342.  
  1343. echo $th; 
  1344.  
  1345. if($field_info['type'] == 'id' && $entry) { $label = $entryth; } 
  1346. else { $label = $field_info["label"]; } 
  1347.  
  1348. $label = apply_filters('kws_gf_directory_th', apply_filters('kws_gf_directory_th_'.$field_id, apply_filters('kws_gf_directory_th_'.sanitize_title($label), $label))); 
  1349. echo esc_html($label) 
  1350.  
  1351. ?></th> 
  1352. <?php 
  1353. if($appendaddress && $addressesExist) { 
  1354. ?> 
  1355. <th scope="col" id="gf-col-<?php echo $form_id.'-'.$field_id ?>" class="manage-column" onclick="Search('<?php echo $search_query ?>', '<?php echo $field_id ?>', '<?php echo $dir ?>');" style="cursor:pointer;"><?php 
  1356. $label = apply_filters('kws_gf_directory_th', apply_filters('kws_gf_directory_th_address', 'Address')); 
  1357. echo esc_html($label) 
  1358.  
  1359. ?></th> 
  1360. <?php 
  1361. ?> 
  1362. </tr> 
  1363. <?php if(!empty($credit)) { self::get_credit_link(sizeof($columns), $options); } ?> 
  1364. </tfoot> 
  1365. <?php } ?> 
  1366. </table> 
  1367. <?php 
  1368.  
  1369. do_action('kws_gf_after_directory_before_nav', do_action('kws_gf_after_directory_before_nav_form_'.$form_id, $form, $leads, compact("approved", "sort_field", "sort_direction", "search_query", "first_item_index", "page_size", "star", "read", "is_numeric", "start_date", "end_date"))); 
  1370.  
  1371.  
  1372. //Displaying paging links if appropriate 
  1373.  
  1374. if($lead_count > 0 && $showcount || $page_links) { 
  1375. if($lead_count == 0) { $first_item_index--; } 
  1376. ?> 
  1377. <div class="tablenav"> 
  1378. <div class="tablenav-pages"> 
  1379. <?php if($showcount) { 
  1380. if(($first_item_index + $page_size) > $lead_count || $page_size <= 0) { 
  1381. $second_part = $lead_count; 
  1382. } else { 
  1383. $second_part = $first_item_index + $page_size; 
  1384. ?> 
  1385. <span class="displaying-num"><?php printf(__("Displaying %d - %d of %d", "gravity-forms-addons"), $first_item_index + 1, $second_part, $lead_count) ?></span> 
  1386. <?php } if($page_links) { echo $page_links; } ?> 
  1387. </div> 
  1388. <div class="clear"></div> 
  1389. </div> 
  1390. <?php 
  1391.  
  1392. ?> 
  1393. </div> 
  1394. <?php 
  1395. if(empty($credit)) { 
  1396. echo "\n".'<!-- Directory generated by Gravity Forms Directory & Addons : http://wordpress.org/extend/plugins/gravity-forms-addons/ -->'."\n"; 
  1397.  
  1398. do_action('kws_gf_after_directory', do_action('kws_gf_after_directory_form_'.$form_id, $form, $leads, compact("approved", "sort_field", "sort_direction", "search_query", "first_item_index", "page_size", "star", "read", "is_numeric", "start_date", "end_date"))); 
  1399.  
  1400. $content = ob_get_contents(); // Get the output 
  1401. ob_end_clean(); // Clear the cache 
  1402.  
  1403. // If the form is form #2, two filters are applied: `kws_gf_directory_output_2` and `kws_gf_directory_output` 
  1404. $content = apply_filters('kws_gf_directory_output', apply_filters('kws_gf_directory_output_'.$form_id, self::html_display_type_filter($content, $directoryview))); 
  1405.  
  1406. return $content; // Return it! 
  1407.  
  1408. /** 
  1409. * Render image link HTML 
  1410. * 
  1411. * @since 3.7 
  1412. * @param [type] $url [description] 
  1413. * @param string $title [description] 
  1414. * @param string $caption [description] 
  1415. * @param string $description [description] 
  1416. * @return [type] [description] 
  1417. */ 
  1418. static private function render_image_link( $url, $lead, $options, $title = '', $caption = '', $description = '' ) { 
  1419.  
  1420. extract($options); 
  1421.  
  1422. $target = ($linknewwindow && empty($lightboxsettings['images'])) ? ' target="_blank"' : ''; 
  1423.  
  1424. $size = false; 
  1425. if( !empty( $options['getimagesize'] )) { 
  1426. $size = @getimagesize( $url ); 
  1427.  
  1428. //displaying thumbnail (if file is an image) or an icon based on the extension 
  1429. $icon = GFEntryList::get_icon_url($url); 
  1430. if(!preg_match('/icon\_image\.gif/ism', $icon)) { 
  1431. $src = $icon; 
  1432. if(!empty($size)) { 
  1433. $img = "<img src='$src' {$size[3]}/>"; 
  1434. } else { 
  1435. $img = "<img src='$src' />"; 
  1436. } else { // No thickbox for non-images please 
  1437. switch( strtolower( trim( $options['postimage'] ) ) ) { 
  1438. case 'image': 
  1439. $src = $url; 
  1440. break; 
  1441. case 'icon': 
  1442. default: 
  1443. $src = $icon; 
  1444. break; 
  1445. $img = array( 
  1446. 'src' => $src,  
  1447. 'size' => $size,  
  1448. 'title' => $title,  
  1449. 'caption' => $caption,  
  1450. 'description' => $description,  
  1451. 'url' => esc_url_raw( $url ),  
  1452. 'code' => isset($size[3]) ? "<img src='$src' {$size[3]} />" : "<img src='$src' />" 
  1453. ); 
  1454. $img = apply_filters('kws_gf_directory_lead_image', apply_filters('kws_gf_directory_lead_image_'.$options['postimage'], apply_filters('kws_gf_directory_lead_image_'.$lead['id'], $img))); 
  1455.  
  1456. $lightboxclass = ''; 
  1457.  
  1458. if(!empty($lightboxsettings['images']) && self::is_image_file( $url ) ) { 
  1459. if(wp_script_is('colorbox', 'registered')) { 
  1460. $lightboxclass = ' class="colorbox lightbox"'; 
  1461. } else if(wp_script_is('thickbox', 'registered')) { 
  1462. $lightboxclass = ' class="thickbox lightbox"'; 
  1463.  
  1464. if(in_array('images', $lightboxsettings) || !empty($lightboxsettings['images'])) { 
  1465. $lightboxclass .= ' rel="directory_all directory_images"'; 
  1466.  
  1467. $value = "<a href='{$url}'{$target}{$lightboxclass}>{$img['code']}</a>"; 
  1468.  
  1469. $value = apply_filters( 'kws_gf_directory_render_image_link', $value, $url, $lead, $options, $title, $caption, $description ); 
  1470.  
  1471. return $value; 
  1472.  
  1473. /** 
  1474. * Verify that the src URL matches image patterns. 
  1475. * 
  1476. * 
  1477. * @return boolean True: matches pattern; False: does not match pattern. 
  1478. */ 
  1479. public static function is_image_file( $src ) { 
  1480.  
  1481. $info = pathinfo( $src ); 
  1482.  
  1483. $image_exts = apply_filters('kws_gf_directory_image_extensions', array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico' )); 
  1484.  
  1485. return isset( $info['extension'] ) && in_array(strtolower( $info['extension'] ), $image_exts); 
  1486.  
  1487. /** 
  1488. * render_search_dropdown function. 
  1489. * 
  1490. * @since 3.5 
  1491. * @access private 
  1492. * @static 
  1493. * @param string $label (default: '') search field label 
  1494. * @param string $name (default: '') input name attribute 
  1495. * @param array $choices 
  1496. * @return field dropdown html 
  1497. */ 
  1498. static private function render_search_dropdown( $label = '', $name = '', $choices ) { 
  1499.  
  1500. if( empty( $choices ) || !is_array( $choices ) || empty( $name ) ) { 
  1501. return ''; 
  1502.  
  1503. $current_value = isset( $_GET[ $name ] ) ? $_GET[ $name ] : ''; 
  1504.  
  1505. $output = '<div class="search-box">'; 
  1506. $output .= '<label for=search-box-'.$name.'>' . $label . '</label>'; 
  1507. $output .= '<select name="'.$name.'" id="search-box-'.$name.'">'; 
  1508. $output .= '<option value="" '. selected( '', $current_value, false ) .'>---</option>'; 
  1509. foreach( $choices as $choice ) { 
  1510. $output .= '<option value="'. $choice['value'] .'" '. selected( $choice['value'], $current_value, false ) .'>'. $choice['text'] .'</option>'; 
  1511. $output .= '</select>'; 
  1512. $output .= '</div>'; 
  1513.  
  1514. return $output; 
  1515.  
  1516.  
  1517.  
  1518. /** 
  1519. * render_search_input function. 
  1520. * 
  1521. * @since 3.5 
  1522. * @access private 
  1523. * @static 
  1524. * @param string $label (default: '') search field label 
  1525. * @param string $name (default: '') input name attribute 
  1526. * @return field input html 
  1527. */ 
  1528. static private function render_search_input( $label = '', $name = '' ) { 
  1529.  
  1530. if( empty( $name ) ) { 
  1531. return ''; 
  1532.  
  1533. $current_value = isset( $_GET[ $name ] ) ? $_GET[ $name ] : ''; 
  1534.  
  1535. $output = '<div class="search-box">'; 
  1536. $output .= '<label for=search-box-'.$name.'>' . $label . '</label>'; 
  1537. $output .= '<input type="text" name="'.$name.'" id="search-box-'.$name.'" value="'.$current_value.'">'; 
  1538. $output .= '</div>'; 
  1539.  
  1540. return $output; 
  1541.  
  1542.  
  1543.  
  1544. static public function get_credit_link( $columns = 1, $options = array() ) { 
  1545. global $post;// prevents calling before <HTML> 
  1546. if(empty($post) || is_admin()) { return; } 
  1547.  
  1548. $settings = self::get_settings(); 
  1549.  
  1550. // Only show credit link if the user has saved settings; 
  1551. // this prevents existing directories adding a link without user action. 
  1552. if(isset($settings['version'])) { 
  1553. echo "<tr><th colspan='{$columns}'>". self::attr($options) ."</th></tr>"; 
  1554.  
  1555. static public function get_version() { 
  1556. return self::$version; 
  1557.  
  1558. static public function return_7776000() { 
  1559. return 7776000; // extend the cache to 90 days 
  1560.  
  1561. static public function attr($options, $default = '<span class="kws_gf_credit" style="font-weight:normal; text-align:center; display:block; margin:0 auto;">Powered by <a href="http://katz.co/gravity-forms-addons/">Gravity Forms Directory</a></span>') { 
  1562. include_once(ABSPATH . WPINC . '/feed.php'); 
  1563. add_filter( 'wp_feed_cache_transient_lifetime' , array('GFDirectory', 'return_7776000')); 
  1564. $rss = fetch_feed(add_query_arg(array('site' => htmlentities(substr(get_bloginfo('url'), is_ssl() ? 8 : 7)), 'from' => 'kws_gf_addons', 'version' => self::$version, 'credit' => !empty($options['credit'])), 'http://www.katzwebservices.com/development/attribution.php')); 
  1565. remove_filter( 'wp_feed_cache_transient_lifetime' , array('GFDirectory', 'return_7776000')); 
  1566. if($rss && !is_wp_error($rss)) { 
  1567. // We want to strip all tags except for 'style', 'id', and 'class' so that the return value is always safe for the site. 
  1568. $strip = array('bgsound', 'expr', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc'); 
  1569. $rss->strip_attributes($strip); $rss_items = $rss->get_items(0, 1); 
  1570. foreach ( $rss_items as $item ) { 
  1571. return str_replace(array("\n", "\r"), ' ', $item->get_description()); 
  1572.  
  1573. return $default; 
  1574.  
  1575.  
  1576. static public function add_lead_approved_hidden_input($value, $lead, $field = '') { 
  1577. global $_gform_directory_processed_meta, $_gform_directory_approvedcolumn; 
  1578.  
  1579. if(!in_array($lead['id'], $_gform_directory_processed_meta)) { 
  1580. $_gform_directory_processed_meta[] = $lead['id']; 
  1581. if(empty($_gform_directory_approvedcolumn)) { 
  1582. $forms = RGFormsModel::get_forms(null, "title"); 
  1583. $_gform_directory_approvedcolumn = self::globals_get_approved_column($forms[0]->id); 
  1584. if(self::check_approval($lead, $_gform_directory_approvedcolumn)) { 
  1585. echo '<td style="display:none;"><input type="hidden" class="lead_approved" id="lead_approved_'.$lead['id'].'" value="true" /></td>'; 
  1586.  
  1587. return $value; 
  1588.  
  1589.  
  1590. static public function globals_get_approved_column($formID = 0) { 
  1591. global $_gform_directory_processed_meta, $_gform_directory_approvedcolumn, $_gform_directory_activeform; 
  1592.  
  1593. $_gform_directory_processed_meta = array(); 
  1594.  
  1595. if(empty($formID)) { 
  1596. $formID = RGForms::get("id"); 
  1597.  
  1598. if(empty($formID)) { 
  1599. $forms = RGFormsModel::get_forms(null, "title"); 
  1600. $formID = $forms[0]->id; 
  1601.  
  1602. if(!empty($formID)) { 
  1603. $_gform_directory_activeform = RGFormsModel::get_form_meta($formID); 
  1604. } else if(isset($_GET['id'])) { 
  1605. $_gform_directory_activeform = RGFormsModel::get_form_meta($_GET['id']); 
  1606.  
  1607. $_gform_directory_approvedcolumn = self::get_approved_column($_gform_directory_activeform); 
  1608.  
  1609. return $_gform_directory_approvedcolumn; 
  1610.  
  1611. static public function get_approved_column($form) { 
  1612. if(!is_array($form)) { return false; } 
  1613.  
  1614. foreach(@$form['fields'] as $key=>$col) { 
  1615. if(isset($col['inputs']) && is_array($col['inputs'])) { 
  1616. foreach($col['inputs'] as $key2=>$input) { 
  1617. if(strtolower($input['label']) == 'approved' && $col['type'] == 'checkbox' && !empty($col['adminOnly'])) { 
  1618. return $input['id']; 
  1619.  
  1620. foreach(@$form['fields'] as $key=>$col) { 
  1621. if(isset($col['label']) && strtolower($col['label']) == 'approved' && $col['type'] == 'checkbox') { 
  1622. if(isset($col['inputs'][0]['id'])) 
  1623. return $key; 
  1624.  
  1625. return null; 
  1626.  
  1627.  
  1628. static public function directory_update_approved_hook() { 
  1629. global $_gform_directory_approvedcolumn; 
  1630. check_ajax_referer('rg_update_approved', 'rg_update_approved'); 
  1631. if(!empty($_POST["lead_id"])) { 
  1632. $_gform_directory_approvedcolumn = empty($_gform_directory_approvedcolumn) ? self::globals_get_approved_column($_POST['form_id']) : $_gform_directory_approvedcolumn; 
  1633. self::directory_update_approved((int)$_POST["lead_id"], $_POST["approved"], (int)$_POST['form_id'], $_gform_directory_approvedcolumn); 
  1634.  
  1635. static public function settings_link( $links, $file ) { 
  1636. static $this_plugin; 
  1637. if( ! $this_plugin ) $this_plugin = plugin_basename(__FILE__); 
  1638. if ( $file == $this_plugin ) { 
  1639. $settings_link = '<a href="' . admin_url( 'admin.php?page=gf_settings&addon=Directory+%26+Addons' ) . '">' . __('Settings', 'gravity-forms-addons') . '</a>'; 
  1640. array_unshift( $links, $settings_link ); // before other links 
  1641. return $links; 
  1642.  
  1643. //Returns true if the current page is an Feed pages. Returns false if not 
  1644. private static function is_directory_page() { 
  1645. if(empty($_GET["pagenum"])) { return false; } 
  1646. $current_page = trim(strtolower($_GET["pagenum"])); 
  1647. $directory_pages = array("gf_directory"); 
  1648.  
  1649. return in_array($current_page, $directory_pages); 
  1650.  
  1651. static public function get_settings() { 
  1652. return get_option("gf_addons_settings", array( 
  1653. "directory" => true,  
  1654. "directory_defaults" => array(),  
  1655. "referrer" => false,  
  1656. "modify_admin" => array( 
  1657. 'expand' => true,  
  1658. 'toggle' => true,  
  1659. 'edit' => true,  
  1660. 'ids' => true 
  1661. ),  
  1662. "saved" => false,  
  1663. "version" => self::$version 
  1664. ); 
  1665.  
  1666. public static function disable_directory() { 
  1667. delete_option("gf_directory_oid"); 
  1668.  
  1669. public static function uninstall() { 
  1670.  
  1671. if(!GFDirectory::has_access("gravityforms_directory_uninstall")) 
  1672. (__("You don't have adequate permission to uninstall Directory Add-On.", "gravity-forms-addons")); 
  1673.  
  1674. //removing options 
  1675. delete_option("gf_addons_settings"); 
  1676.  
  1677. //Deactivating plugin 
  1678. $plugin = "gravity-forms-addons/gravity-forms-addons.php"; 
  1679. deactivate_plugins($plugin); 
  1680. update_option('recently_activated', array($plugin => time()) + (array)get_option('recently_activated')); 
  1681.  
  1682. private static function is_gravityforms_supported() { 
  1683. if(class_exists("GFCommon")) { 
  1684. $is_correct_version = version_compare(GFCommon::$version, self::$min_gravityforms_version, ">="); 
  1685. return $is_correct_version; 
  1686. else{ 
  1687. return false; 
  1688.  
  1689. protected static function get_has_access($required_permission) { 
  1690. $has_members_plugin = function_exists('members_get_capabilities'); 
  1691. $has_access = $has_members_plugin ? current_user_can($required_permission) : current_user_can("level_7"); 
  1692. if($has_access) 
  1693. return $has_members_plugin ? $required_permission : "level_7"; 
  1694. else 
  1695. return false; 
  1696.  
  1697. public static function has_access($required_permission) { 
  1698. return self::get_has_access($required_permission); 
  1699.  
  1700. //Returns the url of the plugin's root folder 
  1701. static public function get_base_url() { 
  1702. return plugins_url(null, __FILE__); 
  1703.  
  1704.  
  1705. /** 
  1706. * get_search_filters function. 
  1707. * 
  1708. * @since 3.5 
  1709. * @access public 
  1710. * @static 
  1711. * @param mixed $form 
  1712. * @return array search fields ids 
  1713. */ 
  1714. public static function get_search_filters( $form ) { 
  1715. if( empty($form['fields']) ) { 
  1716. return array(); 
  1717.  
  1718. $search_fields = array(); 
  1719.  
  1720. foreach( $form['fields'] as $field ) { 
  1721. if( !empty( $field['isSearchFilter'] ) ) { 
  1722. $search_fields[] = $field['id']; 
  1723.  
  1724. return $search_fields; 
  1725.  
  1726. /** 
  1727. * get_leads function. 
  1728. * 
  1729. * @access public 
  1730. * @static 
  1731. * @param int $form_id 
  1732. * @param int $sort_field_number (default: 0) 
  1733. * @param string $sort_direction (default: 'DESC') 
  1734. * @param string $search (default: '') 
  1735. * @param int $offset (default: 0) 
  1736. * @param int $page_size (default: 30) 
  1737. * @param mixed $star (default: null) 
  1738. * @param mixed $read (default: null) 
  1739. * @param bool $is_numeric_sort (default: false) 
  1740. * @param mixed $start_date (default: null) 
  1741. * @param mixed $end_date (default: null) 
  1742. * @param string $status (default: 'active') 
  1743. * @param mixed $approvedcolumn (default: null) 
  1744. * @param bool $limituser (default: false) 
  1745. * @param array $search_criterias, since 3.5 
  1746. * @return array Leads results 
  1747. */ 
  1748. public static function get_leads($form_id, $sort_field_number=0, $sort_direction='DESC', $search='', $offset=0, $page_size=30, $star=null, $read=null, $is_numeric_sort = false, $start_date=null, $end_date=null, $status='active', $approvedcolumn = null, $limituser = false, $search_criterias ) { 
  1749.  
  1750. global $wpdb; 
  1751.  
  1752. if($sort_field_number == 0) 
  1753. $sort_field_number = "date_created"; 
  1754.  
  1755. //since 3.5 
  1756. if( empty( $search_criterias ) ) { 
  1757. $search_criterias = array(); 
  1758.  
  1759. // Retrieve the leads based on whether it's sorted or not. 
  1760. if( is_numeric( $sort_field_number ) ) { 
  1761. $sql = self::sort_by_custom_field_query($form_id, $sort_field_number, $sort_direction, $search, $search_criterias, $offset, $page_size, $star, $read, $is_numeric_sort, $status, $approvedcolumn, $limituser ); 
  1762. } else { 
  1763. $sql = self::sort_by_default_field_query($form_id, $sort_field_number, $sort_direction, $search, $search_criterias, $offset, $page_size, $star, $read, $is_numeric_sort, $start_date, $end_date, $status, $approvedcolumn, $limituser ); 
  1764.  
  1765. //initializing rownum 
  1766. $wpdb->query("select @rownum:=0"); 
  1767.  
  1768. //getting results 
  1769.  
  1770. $results = $wpdb->get_results($sql); 
  1771.  
  1772.  
  1773. $return = ''; 
  1774. if(function_exists('gform_get_meta')) { 
  1775. $return = RGFormsModel::build_lead_array($results); // This is a private function until 1.6 
  1776.  
  1777. // Used by at least the show_only_user_entries() method 
  1778. $return = apply_filters( 'kws_gf_directory_lead_filter', $return, compact("approved", "sort_field_number", "sort_direction", "search_query", "search_criterias", "first_item_index", "page_size", "star", "read", "is_numeric", "start_date", "end_date", "status", "approvedcolumn", "limituser") ); 
  1779.  
  1780. return $return; 
  1781.  
  1782. static function is_current_user( $lead = array()) { 
  1783. global $current_user; 
  1784. get_currentuserinfo(); 
  1785. return ( (int)$current_user->ID === (int)$lead["created_by"]) ; 
  1786.  
  1787. static function show_only_user_entries($leads = array(), $settings = array()) { 
  1788. if(empty($settings['limituser'])) { return $leads; } 
  1789. return array_filter($leads, array('GFDirectory', 'is_current_user')); 
  1790.  
  1791.  
  1792. /** 
  1793. * sort_by_custom_field_query function. 
  1794. * 
  1795. * A copy of the Gravity Forms method, but adding $approvedcolumns and $limituser args 
  1796. * 
  1797. * @access private 
  1798. * @static 
  1799. * @param mixed $form_id 
  1800. * @param int $sort_field_number (default: 0) 
  1801. * @param string $sort_direction (default: 'DESC') 
  1802. * @param string $search (default: '') 
  1803. * @param array $search_criterias, since 3.5 
  1804. * @param int $offset (default: 0) 
  1805. * @param int $page_size (default: 30) 
  1806. * @param mixed $star (default: null) 
  1807. * @param mixed $read (default: null) 
  1808. * @param bool $is_numeric_sort (default: false) 
  1809. * @param string $status (default: 'active') 
  1810. * @param mixed $approvedcolumn (default: null) 
  1811. * @param bool $limituser (default: false) 
  1812. * @return void 
  1813. */ 
  1814. private static function sort_by_custom_field_query($form_id, $sort_field_number=0, $sort_direction='DESC', $search='', $search_criterias, $offset=0, $page_size=30, $star=null, $read=null, $is_numeric_sort = false, $status='active', $approvedcolumn = null, $limituser = false ) { 
  1815. global $wpdb, $current_user; 
  1816. if(!is_numeric($form_id) || !is_numeric($sort_field_number)|| !is_numeric($offset)|| !is_numeric($page_size)) 
  1817. return ""; 
  1818.  
  1819. $lead_detail_table_name = RGFormsModel::get_lead_details_table_name(); 
  1820. $lead_table_name = RGFormsModel::get_lead_table_name(); 
  1821.  
  1822. $orderby = $is_numeric_sort ? "ORDER BY query, (value+0) $sort_direction" : "ORDER BY query, value $sort_direction"; 
  1823.  
  1824. //$search = empty($search) ? "" : "WHERE d.value LIKE '%$search%' "; 
  1825. $search_term = "%$search%"; 
  1826. $search_filter = empty($search) ? "" : $wpdb->prepare("WHERE d.value LIKE %s", $search_term); 
  1827.  
  1828. //starred clause 
  1829. $where = empty($search) ? "WHERE" : "AND"; 
  1830. $search_filter .= $star !== null && $status == 'active' ? $wpdb->prepare("$where is_starred=%d AND status='active' ", $star) : ""; 
  1831.  
  1832. //read clause 
  1833. $where = empty($search) ? "WHERE" : "AND"; 
  1834. $search_filter .= $read !== null && $status == 'active' ? $wpdb->prepare("$where is_read=%d AND status='active' ", $read) : ""; 
  1835.  
  1836. //status clause 
  1837. if(function_exists('gform_get_meta')) { 
  1838. $where = empty($search) ? "WHERE" : "AND"; 
  1839. $search_filter .= $wpdb->prepare("$where status=%s ", $status); 
  1840.  
  1841. // new search criterias since 3.5 
  1842. $in_search_criteria = ''; 
  1843. if( !empty( $search_criterias ) ) { 
  1844. foreach( $search_criterias as $field_id => $value ) { 
  1845. $value = "%$value%"; 
  1846. $in_search_criteria .= $wpdb->prepare(" l.id IN (SELECT lead_id from $lead_detail_table_name WHERE field_number = %s AND value LIKE %s) AND ", $field_id, $value ); 
  1847. $where = empty($search_filter) ? "WHERE " : "AND "; 
  1848. $in_search_criteria = ( !empty($in_search_criteria) ) ? $where . substr( $in_search_criteria, 0, -4 ) : ''; // to add where/and and remove the last AND 
  1849.  
  1850.  
  1851. if($limituser) { 
  1852. get_currentuserinfo(); 
  1853. if((int)$current_user->ID !== 0 || ($current_user->ID === 0 && apply_filters('kws_gf_show_entries_if_not_logged_in', apply_filters('kws_gf_treat_not_logged_in_as_user', true)))) { 
  1854. $where = empty($search_filter) ? "WHERE" : "AND"; 
  1855. if((int)$current_user->ID === 0) { 
  1856. $search_filter .= $wpdb->prepare("$where (created_by IS NULL OR created_by=%d)", $current_user->ID); 
  1857. } else { 
  1858. $search_filter .= $wpdb->prepare("$where l.created_by=%d ", $current_user->ID); 
  1859. } else { 
  1860. return false; 
  1861.  
  1862. $field_number_min = $sort_field_number - 0.001; 
  1863. $field_number_max = $sort_field_number + 0.001; 
  1864.  
  1865. $in_filter = ""; 
  1866. if(!empty($approvedcolumn)) { 
  1867. $in_filter = $wpdb->prepare("WHERE l.id IN (SELECT lead_id from $lead_detail_table_name WHERE field_number BETWEEN %f AND %f)", $approvedcolumn - 0.001, $approvedcolumn + 0.001); 
  1868. // This will work once all the fields are converted to the meta_key after 1.6 
  1869. #$search_filter .= $wpdb->prepare(" AND m.meta_key = 'is_approved' AND m.meta_value = %s", 1); 
  1870.  
  1871. $limit_filter = ''; //paging is done later since 3.5 to allow multisort 
  1872. if( !apply_filters( 'kws_gf_directory_want_multisort', false ) ) { 
  1873. if($page_size > 0) { $limit_filter = "LIMIT $offset, $page_size"; } 
  1874.  
  1875. $sql = " 
  1876. SELECT filtered.sort, l.*, d.field_number, d.value 
  1877. FROM $lead_table_name l 
  1878. INNER JOIN $lead_detail_table_name d ON d.lead_id = l.id 
  1879. INNER JOIN ( 
  1880. SELECT distinct sorted.sort, l.id 
  1881. FROM $lead_table_name l 
  1882. INNER JOIN $lead_detail_table_name d ON d.lead_id = l.id 
  1883. INNER JOIN ( 
  1884. SELECT @rownum:=@rownum+1 as sort, id FROM ( 
  1885. SELECT 0 as query, lead_id as id, value 
  1886. FROM $lead_detail_table_name 
  1887. WHERE form_id=$form_id 
  1888. AND field_number between $field_number_min AND $field_number_max 
  1889.  
  1890. UNION ALL 
  1891.  
  1892. SELECT 1 as query, l.id, d.value 
  1893. FROM $lead_table_name l 
  1894. LEFT OUTER JOIN $lead_detail_table_name d ON d.lead_id = l.id AND field_number between $field_number_min AND $field_number_max 
  1895. WHERE l.form_id=$form_id 
  1896. AND d.lead_id IS NULL 
  1897.  
  1898. ) sorted1 
  1899. $orderby 
  1900. ) sorted ON d.lead_id = sorted.id 
  1901. $search_filter 
  1902. $in_search_criteria 
  1903. $limit_filter 
  1904. ) filtered ON filtered.id = l.id 
  1905. $in_filter 
  1906. ORDER BY filtered.sort"; 
  1907.  
  1908. return $sql; 
  1909.  
  1910.  
  1911. /** 
  1912. * sort_by_default_field_query function. 
  1913. * 
  1914. * A copy of the Gravity Forms method, but adding $approvedcolumns and $limituser args 
  1915. * 
  1916. * @access private 
  1917. * @static 
  1918. * @param mixed $form_id 
  1919. * @param mixed $sort_field 
  1920. * @param string $sort_direction (default: 'DESC') 
  1921. * @param string $search (default: '') 
  1922. * @param array $search_criterias - since 3.5 
  1923. * @param int $offset (default: 0) 
  1924. * @param int $page_size (default: 30) 
  1925. * @param mixed $star (default: null) 
  1926. * @param mixed $read (default: null) 
  1927. * @param bool $is_numeric_sort (default: false) 
  1928. * @param mixed $start_date (default: null) 
  1929. * @param mixed $end_date (default: null) 
  1930. * @param string $status (default: 'active') 
  1931. * @param mixed $approvedcolumn (default: null) 
  1932. * @param bool $limituser (default: false) 
  1933. * @return void 
  1934. */ 
  1935. private static function sort_by_default_field_query($form_id, $sort_field, $sort_direction='DESC', $search='', $search_criterias, $offset=0, $page_size=30, $star=null, $read=null, $is_numeric_sort = false, $start_date=null, $end_date=null, $status='active', $approvedcolumn = null, $limituser = false) { 
  1936. global $wpdb, $current_user; 
  1937.  
  1938. if(!is_numeric($form_id) || !is_numeric($offset)|| !is_numeric($page_size)) { 
  1939. return ""; 
  1940.  
  1941. $lead_detail_table_name = RGFormsModel::get_lead_details_table_name(); 
  1942. $lead_table_name = RGFormsModel::get_lead_table_name(); 
  1943.  
  1944. $search_term = "%$search%"; 
  1945. $search_filter = empty($search) ? "" : $wpdb->prepare(" AND value LIKE %s", $search_term); 
  1946.  
  1947. // new search criterias since 3.5 
  1948. $in_search_criteria = ''; 
  1949. if( !empty( $search_criterias ) ) { 
  1950. foreach( $search_criterias as $field_id => $value ) { 
  1951. $value = "%$value%"; 
  1952. $in_search_criteria .= $wpdb->prepare(" AND l.id IN (SELECT lead_id from $lead_detail_table_name WHERE field_number = %s AND value LIKE %s)", $field_id, $value ); 
  1953.  
  1954. $star_filter = $star !== null && $status == 'active' ? $wpdb->prepare(" AND is_starred=%d AND status='active' ", $star) : ""; 
  1955. $read_filter = $read !== null && $status == 'active' ? $wpdb->prepare(" AND is_read=%d AND status='active' ", $read) : ""; 
  1956. if(function_exists('gform_get_meta')) { 
  1957. $status_filter = $wpdb->prepare(" AND status=%s ", $status); 
  1958. } else { 
  1959. $status_filter = ''; 
  1960.  
  1961. $start_date_filter = empty($start_date) ? "" : " AND datediff(date_created, '$start_date') >=0"; 
  1962. $end_date_filter = empty($end_date) ? "" : " AND datediff(date_created, '$end_date') <=0"; 
  1963.  
  1964. $in_filter = ""; 
  1965. if(!empty($approvedcolumn)) { 
  1966. $in_filter = $wpdb->prepare("l.id IN (SELECT lead_id from $lead_detail_table_name WHERE field_number BETWEEN %f AND %f) AND", $approvedcolumn - 0.001, $approvedcolumn + 0.001); 
  1967. // This will work once all the fields are converted to the meta_key after 1.6 
  1968. #$search_filter .= $wpdb->prepare(" AND m.meta_key = 'is_approved' AND m.meta_value = %s", 1); 
  1969.  
  1970. $user_filter = ''; 
  1971. if($limituser) { 
  1972. get_currentuserinfo(); 
  1973. if((int)$current_user->ID !== 0 || ($current_user->ID === 0 && apply_filters('kws_gf_show_entries_if_not_logged_in', apply_filters('kws_gf_treat_not_logged_in_as_user', true)))) { 
  1974. if((int)$current_user->ID === 0) { 
  1975. $user_filter = $wpdb->prepare(" AND (created_by IS NULL OR created_by=%d)", $current_user->ID); 
  1976. } else { 
  1977. $user_filter = $wpdb->prepare(" AND created_by=%d ", $current_user->ID); 
  1978. } else { 
  1979. return false; 
  1980.  
  1981. $limit_filter = ''; //paging is done later since 3.5 to allow multisort 
  1982. if( !apply_filters( 'kws_gf_directory_want_multisort', false ) ) { 
  1983. if($page_size > 0) { $limit_filter = "LIMIT $offset, $page_size"; } 
  1984.  
  1985. $sql = " 
  1986. SELECT filtered.sort, l.*, d.field_number, d.value 
  1987. FROM $lead_table_name l 
  1988. INNER JOIN $lead_detail_table_name d ON d.lead_id = l.id 
  1989. INNER JOIN 
  1990. SELECT @rownum:=@rownum + 1 as sort, id 
  1991. FROM 
  1992. SELECT distinct l.id 
  1993. FROM $lead_table_name l 
  1994. INNER JOIN $lead_detail_table_name d ON d.lead_id = l.id 
  1995. WHERE $in_filter 
  1996. l.form_id=$form_id 
  1997. $search_filter 
  1998. $in_search_criteria 
  1999. $star_filter 
  2000. $read_filter 
  2001. $user_filter 
  2002. $status_filter 
  2003. $start_date_filter 
  2004. $end_date_filter 
  2005. ORDER BY $sort_field $sort_direction 
  2006. $limit_filter 
  2007. ) page 
  2008. ) filtered ON filtered.id = l.id 
  2009. ORDER BY filtered.sort"; 
  2010.  
  2011. return $sql; 
  2012.  
  2013. static function directory_anchor_text($value = null) { 
  2014.  
  2015. if(apply_filters('kws_gf_directory_anchor_text_striphttp', true)) { 
  2016. $value = str_replace('http://', '', $value); 
  2017. $value = str_replace('https://', '', $value); 
  2018.  
  2019. if(apply_filters('kws_gf_directory_anchor_text_stripwww', true)) { 
  2020. $value = str_replace('www.', '', $value); 
  2021. if(apply_filters('kws_gf_directory_anchor_text_rootonly', true)) { 
  2022. $value = preg_replace('/(.*?)\/(.+)/ism', '$1', $value); 
  2023. if(apply_filters('kws_gf_directory_anchor_text_nosubdomain', true)) { 
  2024. $value = preg_replace('/((.*?)\.)+(.*?)\.(.*?)/ism', '$3.$4', $value); 
  2025. if(apply_filters('kws_gf_directory_anchor_text_noquerystring', true)) { 
  2026. $ary = explode("?", $value); 
  2027. $value = $ary[0]; 
  2028. return $value; 
  2029.  
  2030. static public function r($content, $die = false) { 
  2031. echo '<pre>'.print_r($content, true).'</pre>'; 
  2032. if($die) { die(); } 
  2033.  
  2034. static private function get_entrylink_column($form, $entry = false) { 
  2035. if(!is_array($form)) { return false; } 
  2036.  
  2037. $columns = empty($entry) ? array() : array('id' => 'id'); 
  2038. foreach(@$form['fields'] as $key=>$col) { 
  2039. if(!empty($col['useAsEntryLink'])) { 
  2040. $columns[$col['id']] = $col['useAsEntryLink']; 
  2041.  
  2042. return empty($columns) ? false : $columns; 
  2043.  
  2044. static private function prep_address_field($field) { 
  2045. return !empty($field) ? GFCommon::trim_all($field) : ''; 
  2046.  
  2047. static function format_address($address = array(), $linknewwindow = false) { 
  2048. $address_field_id = @self::prep_address_field($address['id']); 
  2049. $street_value = @self::prep_address_field($address[$address_field_id . ".1"]); 
  2050. $street2_value = @self::prep_address_field($address[$address_field_id . ".2"]); 
  2051. $city_value = @self::prep_address_field($address[$address_field_id . ".3"]); 
  2052. $state_value = @self::prep_address_field($address[$address_field_id . ".4"]); 
  2053. $zip_value = @self::prep_address_field($address[$address_field_id . ".5"]); 
  2054. $country_value = @self::prep_address_field($address[$address_field_id . ".6"]); 
  2055.  
  2056. $address = $street_value; 
  2057. $address .= !empty($address) && !empty($street2_value) ? "<br />$street2_value" : $street2_value; 
  2058. $address .= !empty($address) && (!empty($city_value) || !empty($state_value)) ? "<br />$city_value" : $city_value; 
  2059. $address .= !empty($address) && !empty($city_value) && !empty($state_value) ? ", $state_value" : $state_value; 
  2060. $address .= !empty($address) && !empty($zip_value) ? " $zip_value" : $zip_value; 
  2061. $address .= !empty($address) && !empty($country_value) ? "<br />$country_value" : $country_value; 
  2062.  
  2063. //adding map link 
  2064. if(!empty($address) && apply_filters('kws_gf_directory_td_address_map', 1)) { 
  2065. $address_qs = str_replace("<br />", " ", $address); //replacing <br/> with spaces 
  2066. $address_qs = urlencode($address_qs); 
  2067. $target = ''; if($linknewwindow) { $target = ' target="_blank"'; } 
  2068. $address .= "<br/>".apply_filters('kws_gf_directory_map_link', "<a href='http://maps.google.com/maps?q=$address_qs'".$target." class='map-it-link'>".__('Map It')."</a>"); 
  2069. return $address; 
  2070.  
  2071. static public function html_display_type_filter($content = null, $type = 'table', $single = false) { 
  2072. switch($type) { 
  2073. case 'table': 
  2074. return $content; 
  2075. break; 
  2076. case 'ul': 
  2077. $content = self::convert_to_ul($content, $single); 
  2078. break; 
  2079. case 'dl': 
  2080. $content = self::convert_to_dl($content, $single); 
  2081. break; 
  2082. return $content; 
  2083.  
  2084. static public function convert_to_ul($content = null, $singleUL = false) { 
  2085.  
  2086. $strongHeader = apply_filters('kws_gf_convert_to_ul_strong_header', 1); 
  2087.  
  2088. // Directory View 
  2089. if(!$singleUL) { 
  2090. $content = preg_replace("/<table([^>]*)>/ism", "<ul$1>", $content); 
  2091. $content = preg_replace("/<\/table([^>]*)>/ism", "</ul>", $content); 
  2092. if($strongHeader) { 
  2093. $content = preg_replace("/<tr([^>]*)>\s+/", "\n\t\t\t\t\t\t\t\t\t\t\t\t<li$1><ul>", $content); 
  2094. $content = preg_replace("/<th([^>]*)\>(.*?)\<\/th\>/", "$2</strong>\n\t\t\t\t\t\t\t\t\t\t\t\t\t<ul>", $content); 
  2095. } else { 
  2096. $content = preg_replace("/<tr([^>]*)>\s+/", "\n\t\t\t\t\t\t\t\t\t\t\t\t<li$1>", $content); 
  2097. $content = preg_replace("/<th([^>]*)\>(.*?)\<\/th\>/", "$2\n\t\t\t\t\t\t\t\t\t\t\t\t\t<ul>", $content); 
  2098. $content = preg_replace("/<\/tr[^>]*>/", "\t\t\t\t\t</ul>\n\t\t\t\t\t\t\t\t\t\t\t\t</li>", $content); 
  2099. // Single listing view 
  2100. else { 
  2101. $content = preg_replace("/<table([^>]*)>/ism", "<ul$1>", $content); 
  2102. $content = preg_replace("/<\/table([^>]*)>/ism", "</ul>", $content); 
  2103. if($strongHeader) { 
  2104. $content = preg_replace("/<tr([^>]*)>\s+/", "\n\t\t\t\t\t\t\t\t\t\t\t\t<li$1><strong>", $content); 
  2105. $content = preg_replace("/<th([^>]*)\>(.*?)\<\/th\>/", "$2</strong>\n\t\t\t\t\t\t\t\t\t\t\t\t\t<ul>", $content); 
  2106. } else { 
  2107. $content = preg_replace("/<tr([^>]*)>\s+/", "\n\t\t\t\t\t\t\t\t\t\t\t\t<li$1>", $content); 
  2108. $content = preg_replace("/<th([^>]*)\>(.*?)\<\/th\>/", "$2\n\t\t\t\t\t\t\t\t\t\t\t\t\t<ul>", $content); 
  2109. $content = preg_replace("/<\/tr[^>]*>/", "\t\t\t\t\t</ul>\n\t\t\t\t\t\t\t\t\t\t\t\t</li>", $content); 
  2110. # $content = preg_replace("/\<\/p\>\s+\<\/li/ism", "\<\/p\>\<\/li", $content); 
  2111. $content = preg_replace("/(?:\s+)?(valign\=\"(?:.*?)\"|width\=\"(?:.*?)\"|cellspacing\=\"(?:.*?)\")(?:\s+)?/ism", ' ', $content); 
  2112. $content = preg_replace("/<\/?tbody[^>]*>/", "", $content); 
  2113. $content = preg_replace("/<thead[^>]*>.*<\/thead>|<tfoot[^>]*>.*<\/tfoot>/is", "", $content); 
  2114. $content = preg_replace("/\<td([^>]*)\>(\ |)\<\/td\>/", "", $content); 
  2115. $content = preg_replace("/\<td([^>]*)\>/", "\t\t\t\t\t<li$1>", $content); 
  2116. $content = preg_replace("/<\/td[^>]*>/", "</li>", $content); 
  2117. $content = preg_replace('/\s?colspan\="([^>]*?)"\s?/ism', ' ', $content); 
  2118. return $content; 
  2119.  
  2120. static public function convert_to_dl($content, $singleDL = false) { 
  2121. $back = ''; 
  2122. // Get the back link, if it exists 
  2123. preg_match("/\<p\sclass=\"entryback\"\>(.*?)\<\/p\>/", $content, $matches); 
  2124. if(isset($matches[0])) { $back = $matches[0]; } 
  2125. $content = preg_replace("/\<p\sclass=\"entryback\"\>(.*?)\<\/p\>/", "", $content); 
  2126. $content = preg_replace("/<\/?table[^>]*>|<\/?tbody[^>]*>/", "", $content); 
  2127. $content = preg_replace("/<thead[^>]*>.*<\/thead>|<tfoot[^>]*>.*<\/tfoot>/is", "", $content); 
  2128. if(!$singleDL) { 
  2129. $content = preg_replace("/<tr([^>]*)>/", "<dl$1>", $content); 
  2130. $content = preg_replace("/<\/tr[^>]*>/", "</dl>", $content); 
  2131. } else { 
  2132. $content = preg_replace("/<tr([^>]*)>/", "", $content); 
  2133. $content = preg_replace("/<\/tr[^>]*>/", "", $content); 
  2134. $content = preg_replace("/\<td([^>]*)\>(\ |)\<\/td\>/", "", $content); 
  2135. $content = preg_replace("/\<th([^>]*)\>(.*?)<\/th\>/ism", "<dt$1>$2</dt>", $content); 
  2136. $content = preg_replace('/<td(.*?)(title="(.*?)")?>(.*?)<\/td[^>]*>/ism', "<dt$1>$3</dt><dd>$4</dd>", $content); 
  2137. $output = $back; 
  2138. $output .= "\n\t\t\t\t\t\t\t\t".'<dl>'; 
  2139. $output .= $content; 
  2140. $output .= "\t\t\t\t\t\t".'</dl>'; 
  2141. return $output; 
  2142.  
  2143. static public function make_entry_link($options = array(), $link = false, $lead_id = '', $form_id = '', $field_id = '', $field_label = '', $linkClass = '') { 
  2144. global $wp_rewrite, $post, $wp; 
  2145. extract($options); 
  2146. $entrylink = (empty($link) || $link === ' ') ? $field_label : $link; //$entrylink; 
  2147.  
  2148. $entrytitle = apply_filters('kws_gf_directory_detail_title', apply_filters('kws_gf_directory_detail_title_'.$lead_id, $entrytitle)); 
  2149.  
  2150. if(!empty($lightboxsettings['entry'])) { 
  2151. $href = wp_nonce_url(plugins_url( "/entry-details.php?leadid=$lead_id&form={$form_id}&post={$post->ID}", __FILE__), sprintf('view-%d-%d', $lead_id, $form_id), 'view'); 
  2152. if(wp_script_is('colorbox', 'registered')) { 
  2153. $linkClass = ' class="colorbox lightbox" rel="directory_all directory_entry"'; 
  2154. } else if(wp_script_is('thickbox', 'registered')) { 
  2155. $linkClass = ' class="thickbox lightbox" rel="directory_all directory_entry"'; 
  2156. } else { 
  2157. $multisite = (function_exists('is_multisite') && is_multisite() && $wpdb->blogid == 1); 
  2158. if($wp_rewrite->using_permalinks()) { 
  2159. // example.com/example-directory/entry/4/14/ 
  2160. if(isset($post->ID)) { 
  2161. $url = get_permalink($post->ID); 
  2162. } else { 
  2163. $url = parse_url(add_query_arg(array(), home_url())); 
  2164. $url = $url['path']; 
  2165. $href = trailingslashit($url).sanitize_title(apply_filters('kws_gf_directory_endpoint', 'entry')).'/'.$form_id.apply_filters('kws_gf_directory_endpoint_separator', '/').$lead_id.'/'; 
  2166. #if(!empty($url['query'])) { $href .= '?'.$url['query']; } 
  2167. $href = add_query_arg(array('gf_search' => !empty($_REQUEST['gf_search']) ? $_REQUEST['gf_search'] : null, 'sort' => isset($_REQUEST['sort']) ? $_REQUEST['sort'] : null, 'dir' => isset($_REQUEST['dir']) ? $_REQUEST['dir'] : null, 'pagenum' => isset($_REQUEST['pagenum']) ? $_REQUEST['pagenum'] : null, 'start_date' => isset($_REQUEST['start_date']) ? $_REQUEST['start_date'] : null, 'end_date' => isset($_REQUEST['start_date']) ? $_REQUEST['end_date'] : null), $href); 
  2168. } else { 
  2169. // example.com/?page_id=24&leadid=14&form=4 
  2170. $href = wp_nonce_url(add_query_arg(array('leadid'=>$lead_id, 'form' => $form_id)), sprintf('view-%d-%d', $lead_id, $form_id), 'view'); 
  2171.  
  2172. // If this is a preview, add preview arguments to the link. 
  2173. // @since 3.5 
  2174. if(!empty($_GET['preview']) && !empty($_GET['preview_id']) && !empty($_GET['preview_nonce'])) { 
  2175. if(current_user_can( 'edit_posts' )) { 
  2176. $href = add_query_arg(array('preview' => $_GET['preview'], 'preview_id' => $_GET['preview_id'], 'preview_nonce' => $_GET['preview_nonce']), $href); 
  2177.  
  2178. $value = '<a href="'. esc_url( $href ) .'"'.$linkClass.' title="'.$entrytitle.'">'.$entrylink.'</a>'; 
  2179. return $value; 
  2180.  
  2181. static function get_lead_count($form_id, $search, $star=null, $read=null, $column, $approved = false, $leads = array(), $start_date = null, $end_date = null, $limituser = false, $search_criterias ) { 
  2182. global $wpdb, $current_user; 
  2183.  
  2184. if(!is_numeric($form_id)) 
  2185. return ""; 
  2186.  
  2187. $detail_table_name = RGFormsModel::get_lead_details_table_name(); 
  2188. $lead_table_name = RGFormsModel::get_lead_table_name(); 
  2189.  
  2190. $star_filter = $star !== null ? $wpdb->prepare("AND is_starred=%d ", $star) : ""; 
  2191. $read_filter = $read !== null ? $wpdb->prepare("AND is_read=%d ", $read) : ""; 
  2192. if(function_exists('gform_get_meta')) { 
  2193. $status_filter = $wpdb->prepare(" AND status=%s ", 'active'); 
  2194. } else { 
  2195. $status_filter = ''; 
  2196. $start_date_filter = empty($start_date) ? "" : " AND datediff(date_created, '$start_date') >=0"; 
  2197. $end_date_filter = empty($end_date) ? "" : " AND datediff(date_created, '$end_date') <=0"; 
  2198.  
  2199. $search_term = "%$search%"; 
  2200. $search_filter = empty($search) ? "" : $wpdb->prepare("AND ld.value LIKE %s", $search_term ); 
  2201.  
  2202. // new search criterias since 3.5 
  2203. $in_search_criteria = ''; 
  2204. if( !empty( $search_criterias ) ) { 
  2205. foreach( $search_criterias as $field_id => $value ) { 
  2206. $value = "%$value%"; 
  2207. $in_search_criteria .= $wpdb->prepare(" AND l.id IN (SELECT lead_id from $detail_table_name WHERE field_number = %s AND value LIKE %s)", $field_id, $value ); 
  2208.  
  2209.  
  2210. $user_filter = ''; 
  2211. if($limituser) { 
  2212. get_currentuserinfo(); 
  2213. if((int)$current_user->ID !== 0 || ($current_user->ID === 0 && apply_filters('kws_gf_show_entries_if_not_logged_in', apply_filters('kws_gf_treat_not_logged_in_as_user', true)))) { 
  2214. if(!empty($current_user->ID)) { 
  2215. $user_filter = $wpdb->prepare(" AND l.created_by=%d ", $current_user->ID); 
  2216. } else { 
  2217. $user_filter = $wpdb->prepare(" AND (created_by IS NULL OR created_by=%d)", $current_user->ID); 
  2218. } else { 
  2219. return false; 
  2220.  
  2221.  
  2222. $in_filter = ""; 
  2223. if($approved) { 
  2224. $in_filter = $wpdb->prepare("l.id IN (SELECT lead_id from $detail_table_name WHERE field_number BETWEEN %f AND %f) AND", $column - 0.001, $column + 0.001); 
  2225. // This will work once all the fields are converted to the meta_key after 1.6 
  2226. #$search_filter .= $wpdb->prepare(" AND m.meta_key = 'is_approved' AND m.meta_value = %s", 1); 
  2227.  
  2228. $sql = "SELECT count(distinct l.id) FROM $lead_table_name as l,  
  2229. $detail_table_name as ld"; 
  2230. # $sql .= function_exists('gform_get_meta') ? " INNER JOIN wp_rg_lead_meta m ON l.id = m.lead_id " : ""; // After 1.6 
  2231. $sql .= " 
  2232. WHERE $in_filter 
  2233. l.form_id=$form_id 
  2234. AND ld.form_id=$form_id 
  2235. AND l.id = ld.lead_id 
  2236. $star_filter 
  2237. $read_filter 
  2238. $status_filter 
  2239. $user_filter 
  2240. $start_date_filter 
  2241. $end_date_filter 
  2242. $search_filter 
  2243. $in_search_criteria"; 
  2244.  
  2245. return $wpdb->get_var($sql); 
  2246.  
  2247. static function check_meta_approval($lead_id) { 
  2248. return gform_get_meta($lead_id, 'is_approved'); 
  2249.  
  2250. static function check_approval($lead, $column) { 
  2251. return self::check_meta_approval($lead['id']); 
  2252.  
  2253. static function hide_in_directory($form, $field_id) { 
  2254. return self::check_hide_in('hideInDirectory', $form, $field_id); 
  2255.  
  2256. static function hide_in_single( $form, $field_id ) { 
  2257. return self::check_hide_in('hideInSingle', $form, $field_id); 
  2258.  
  2259. static function check_hide_in($type, $form, $field_id) { 
  2260. foreach( $form['fields'] as $field ) { 
  2261. # echo $field['label'] . ' / ' . floor($field['id']).' / '.floor($field_id).' / <strong>'.$field["{$type}"].'</strong><br />'; 
  2262. if(floor($field_id) === floor($field['id']) && !empty($field["{$type}"])) { 
  2263. return true; 
  2264.  
  2265. return false; 
  2266.  
  2267. /** 
  2268. * get field property value, for a specific field_id on a $form 
  2269. * 
  2270. * @since 3.5 
  2271. */ 
  2272. static function get_field_property( $property, $form, $field_id = '' ) { 
  2273. if( empty( $property ) || empty( $form ) || '' === $field_id ) { 
  2274. return false; 
  2275. foreach( $form['fields'] as $field ) { 
  2276.  
  2277. if( floor( $field_id ) === floor( $field['id'] ) && !empty( $field[ $property ] ) ) { 
  2278. return $field[ $property ]; 
  2279.  
  2280. return false; 
  2281.  
  2282. /** 
  2283. * get field properties, for a specific field_id on a $form 
  2284. * 
  2285. * @since 3.5 
  2286. * @param array $form GF Form array 
  2287. * @param string $field_id Field ID 
  2288. * @return boolean|array If the field matches the searched-for field ID, return the field array. Otherwise, return false. 
  2289. */ 
  2290. static function get_field_properties( $form, $field_id = '' ) { 
  2291. if( empty( $form ) || '' === $field_id ) { 
  2292. return false; 
  2293.  
  2294. foreach( $form['fields'] as $field ) { 
  2295. if( floor( $field_id ) === floor( $field['id'] ) ) { 
  2296. return $field; 
  2297. return false; 
  2298.  
  2299. /** 
  2300. * Deprecated. 
  2301. * 
  2302. * @deprecated 3.5 
  2303. */ 
  2304. static function remove_admin_only() {} 
  2305.  
  2306. static function remove_approved_column($type = 'form', $fields, $approvedcolumn) { 
  2307.  
  2308. foreach($fields as $key => $column) { 
  2309. if((int)floor($column['id']) === (int)floor($approvedcolumn)) { 
  2310. unset($fields["{$key}"]); 
  2311.  
  2312. return $fields; 
  2313.  
  2314.  
  2315. /** 
  2316. * Filter columns and fields when generating directory or single entry view based on Admin Only fields, or "hide from directory" fields or (since 3.5) only visible if user is logged in. 
  2317. * 
  2318. * This method replaces GFDirectory::remove_admin_only() in 3.5 
  2319. * 
  2320. * @since 3.5 
  2321. * @access public 
  2322. * @static 
  2323. * @param mixed $leads 
  2324. * @param mixed $admin_only 
  2325. * @param mixed $approved 
  2326. * @param mixed $is_leads 
  2327. * @param bool $is_single (default: false) 
  2328. * @param bool $show_admin_only (default: false) 
  2329. * @param mixed $form 
  2330. * @return void 
  2331. */ 
  2332. static function remove_hidden_fields( $leads, $admin_only, $approved, $is_leads, $is_single = false, $show_admin_only = false, $form ) { 
  2333.  
  2334. if( empty( $admin_only ) || !is_array( $admin_only ) ) { $admin_only = array(); } 
  2335.  
  2336. if( empty( $leads ) || !is_array( $leads ) ) { return $leads; } 
  2337.  
  2338. if( $is_leads ) { 
  2339.  
  2340. foreach( $leads as $index => $lead ) { 
  2341. // the field_ids are the numeric array keys of a lead 
  2342. $field_ids = array_filter( array_keys( $lead ), 'is_int' ); 
  2343.  
  2344. foreach( $field_ids as $id ) { 
  2345. if( self::check_hide_field_conditions( $id, $admin_only, $approved, $is_single, $show_admin_only, $form ) ) { 
  2346. unset( $leads[ $index ][ $id ] ); 
  2347.  
  2348.  
  2349. return $leads; 
  2350.  
  2351. } else { 
  2352.  
  2353. // the KEY = field_id (to be used to check directory columns) 
  2354. foreach( $leads as $key => $column) { 
  2355.  
  2356. if( self::check_hide_field_conditions( $key, $admin_only, $approved, $is_single, $show_admin_only, $form ) ) { 
  2357. unset( $leads[ $key ] ); 
  2358.  
  2359.  
  2360. return $leads; 
  2361.  
  2362.  
  2363.  
  2364. /** returns true if field should be hidden / returns false if not , since 3.5 */ 
  2365. static function check_hide_field_conditions( $field_id, $admin_only, $approved, $is_single = false, $show_admin_only = false, $form ) { 
  2366.  
  2367.  
  2368. $properties = self::get_field_properties( $form, $field_id ); 
  2369. if( empty( $properties ) ) { 
  2370. return false; 
  2371.  
  2372. //check if set to be hidden in directory or in single entry view 
  2373. if( ( $is_single && !empty( $properties['hideInSingle'] ) ) || ( !$is_single && !empty( $properties['hideInDirectory'] ) ) ) { 
  2374. return true; 
  2375.  
  2376. // check if is and admin only field and remove if not authorized to be shown 
  2377. if( !$show_admin_only && @in_array( $field_id, $admin_only ) && $field_id != $approved && $field_id != floor($approved) ) { 
  2378. return true; 
  2379.  
  2380. //check if field is only visible for logged in users, and in that case, check capabilities level 
  2381. if( !empty( $properties['visibleToLoggedIn'] ) && !current_user_can( $properties['visibleToLoggedInCap'] ) ) { 
  2382. return true; 
  2383.  
  2384. return false; 
  2385.  
  2386.  
  2387. /** 
  2388. * Adapted from forms_model.php, RGFormsModel::save_lead($Form, $lead) 
  2389. * @param array $form Form object. 
  2390. * @param array $lead Lead object 
  2391. * @return void 
  2392. */ 
  2393. public static function save_lead($form, &$lead ) { 
  2394. global $wpdb; 
  2395.  
  2396. if(IS_ADMIN && !GFCommon::current_user_can_any("gravityforms_edit_entries")) 
  2397. die(__("You don't have adequate permission to edit entries.", "gravityforms")); 
  2398.  
  2399. $lead_detail_table = RGFormsModel::get_lead_details_table_name(); 
  2400.  
  2401. //Inserting lead if null 
  2402. if($lead == null) { 
  2403. global $current_user; 
  2404. $user_id = $current_user && $current_user->ID ? $current_user->ID : 'NULL'; 
  2405.  
  2406. $lead_table = RGFormsModel::get_lead_table_name(); 
  2407. $user_agent = RGFormsModel::truncate($_SERVER["HTTP_USER_AGENT"], 250); 
  2408. $currency = GFCommon::get_currency(); 
  2409. $source_url = RGFormsModel::truncate(RGFormsModel::get_current_page_url(), 200); 
  2410.  
  2411. $wpdb->query($wpdb->prepare("INSERT INTO $lead_table(form_id, ip, source_url, date_created, user_agent, currency, created_by) VALUES(%d, %s, %s, utc_timestamp(), %s, %s, {$user_id})", $form["id"], RGFormsModel::get_ip(), $source_url, $user_agent, $currency)); 
  2412.  
  2413.  
  2414. //reading newly created lead id 
  2415. $lead_id = $wpdb->insert_id; 
  2416. $lead = array("id" => $lead_id); 
  2417.  
  2418.  
  2419. $current_fields = $wpdb->get_results($wpdb->prepare("SELECT id, field_number FROM $lead_detail_table WHERE lead_id=%d", $lead["id"])); 
  2420. $original_post_id = rgget("post_id", $lead); 
  2421.  
  2422. $total_fields = array(); 
  2423. $calculation_fields = array(); 
  2424. $recalculate_total = false; 
  2425.  
  2426. foreach($form["fields"] as $field) { 
  2427.  
  2428. //Ignore fields that are marked as display only 
  2429. if(rgget("displayOnly", $field) && $field["type"] != "password") { 
  2430. continue; 
  2431.  
  2432. //ignore pricing fields in the entry detail 
  2433. if(RG_CURRENT_VIEW == "entry" && GFCommon::is_pricing_field($field["type"])) { 
  2434. continue; 
  2435.  
  2436.  
  2437. //process total field after all fields have been saved 
  2438. if($field["type"] == "total") { 
  2439. $total_fields[] = $field; 
  2440. continue; 
  2441.  
  2442. //only save fields that are not hidden (except on entry screen) 
  2443. if(RG_CURRENT_VIEW == "entry" || !RGFormsModel::is_field_hidden($form, $field, array(), $lead ) ) { 
  2444. // process calculation fields after all fields have been saved (moved after the is hidden check) 
  2445. if( GFCommon::has_field_calculation($field) ) { 
  2446. $calculation_fields[] = $field; 
  2447. continue; 
  2448.  
  2449. if($field['type'] == 'post_category') 
  2450. $field = GFCommon::add_categories_as_choices($field, ''); 
  2451.  
  2452. if(isset($field["inputs"]) && is_array($field["inputs"])) { 
  2453.  
  2454. foreach($field["inputs"] as $input) 
  2455. RGFormsModel::save_input($form, $field, $lead, $current_fields, $input["id"]); 
  2456. else{ 
  2457. RGFormsModel::save_input($form, $field, $lead, $current_fields, $field["id"]); 
  2458.  
  2459. //Refresh lead to support conditionals (not optimal but...) 
  2460. $lead = RGFormsModel::get_lead( $lead['id'] ); 
  2461.  
  2462. if(!empty($calculation_fields)) { 
  2463. foreach($calculation_fields as $calculation_field) { 
  2464.  
  2465. if(isset($calculation_field["inputs"]) && is_array($calculation_field["inputs"])) { 
  2466. foreach($calculation_field["inputs"] as $input) { 
  2467. RGFormsModel::save_input($form, $calculation_field, $lead, $current_fields, $input["id"]); 
  2468. RGFormsModel::refresh_lead_field_value($lead["id"], $input["id"]); 
  2469. else{ 
  2470. RGFormsModel::save_input($form, $calculation_field, $lead, $current_fields, $calculation_field["id"]); 
  2471. RGFormsModel::refresh_lead_field_value($lead["id"], $calculation_field["id"]); 
  2472.  
  2473. RGFormsModel::refresh_product_cache($form, $lead = RGFormsModel::get_lead($lead['id'])); 
  2474.  
  2475. //saving total field as the last field of the form. 
  2476. if(!empty($total_fields)) { 
  2477. foreach($total_fields as $total_field) { 
  2478. GFCommon::log_debug("Saving total field."); 
  2479. RGFormsModel::save_input($form, $total_field, $lead, $current_fields, $total_field["id"]); 
  2480.  
  2481.  
  2482.  
  2483.  
  2484. function kws_gf_load_functions() { 
  2485.  
  2486. // If Gravity Forms is installed and exists 
  2487. if(defined('RG_CURRENT_PAGE')) { 
  2488.  
  2489. function gf_field_value($leadid, $fieldid, $form = array()) { 
  2490. echo get_gf_field_value($leadid, $fieldid, $form); 
  2491.  
  2492.  
  2493. // To retrieve textarea inputs from a lead 
  2494. // Example: get_gf_field_value_long(22, '14'); 
  2495. function get_gf_field_value_long($leadid, $fieldid, $form = array(), $apply_filter=true) { 
  2496. return RGFormsModel::get_field_value_long($leadid, $fieldid, $form, $apply_filter); 
  2497.  
  2498. // To retrieve textarea inputs from a lead 
  2499. // Example: get_gf_field_value_long(22, '14'); 
  2500. function get_gf_field_value($leadid, $fieldid, $form = array()) { 
  2501. $lead = RGFormsModel::get_lead($leadid); 
  2502. $fieldid = floatval($fieldid); 
  2503. if(is_numeric($fieldid)) { 
  2504. $result = $lead["$fieldid"]; 
  2505.  
  2506. $max_length = GFORMS_MAX_FIELD_LENGTH; 
  2507.  
  2508. if(strlen($result) >= ($max_length - 50)) { 
  2509. $result = get_gf_field_value_long($lead["id"], $fieldid, $form); 
  2510. $result = trim($result); 
  2511.  
  2512. if(!empty($result)) { return $result; } 
  2513. return false; 
  2514.  
  2515. function gf_field_value_long($leadid, $fieldid, $form = array()) { 
  2516. echo get_gf_field_value_long($leadid, $fieldid, $form); 
  2517.  
  2518.  
  2519. // Gives you the label for a form input (such as First Name). Enter in the form and the field ID to access the label. 
  2520. // Example: echo get_gf_field_label(1, 1.3); 
  2521. // Gives you the label for a form input (such as First Name). Enter in the form and the field ID to access the label. 
  2522. // Example: echo get_gf_field_label(1, 1.3); 
  2523. function get_gf_field_label($form_id, $field_id) { 
  2524. $form = RGFormsModel::get_form_meta($form_id); 
  2525. foreach($form["fields"] as $field) { 
  2526. if($field['id'] == $field_id) { 
  2527. # $output = RGForms::escape_text($field['label']); // No longer used 
  2528. $output = esc_html($field['label']); // Using esc_html(), a WP function 
  2529. }elseif(is_array($field['inputs'])) { 
  2530. foreach($field["inputs"] as $input) { 
  2531. if($input['id'] == $field_id) { 
  2532. if(class_exists('GFCommon')) { 
  2533. $output = esc_html(GFCommon::get_label($field, $field_id)); 
  2534. } else { 
  2535. #$output = RGForms::escape_text(RGForms::get_label($field, $field_id)); // No longer used 
  2536. $output = esc_html(RGForms::get_label($field, $field_id)); // No longer used 
  2537. return $output; 
  2538. function gf_field_label($form_id, $field_id) { 
  2539. echo get_gf_field_label($form_id, $field_id); 
  2540.  
  2541. // Returns a form using php instead of shortcode 
  2542. function get_gf_form($id, $display_title=true, $display_description=true, $force_display=false, $field_values=null) { 
  2543. if(class_exists('GFFormDisplay')) { 
  2544. return GFFormDisplay::get_form($id, $display_title=true, $display_description=true, $force_display=false, $field_values=null); 
  2545. } else { 
  2546. return RGFormsModel::get_form($id, $display_title, $display_description); 
  2547. function gf_form($id, $display_title=true, $display_description=true, $force_display=false, $field_values=null) { 
  2548. echo get_gf_form($id, $display_title, $display_description, $force_display, $field_values); 
  2549.  
  2550. // Returns array of leads for a specific form 
  2551. function get_gf_leads($form_id, $sort_field_number=0, $sort_direction='DESC', $search='', $offset=0, $page_size=3000, $star=null, $read=null, $is_numeric_sort = false, $start_date=null, $end_date=null, $status = 'active', $approvedcolumn = false, $limituser = false) { 
  2552. return GFDirectory::get_leads($form_id, $sort_field_number, $sort_direction, $search, $offset, $page_size, $star, $read, $is_numeric_sort, $start_date, $end_date, $status, $approvedcolumn, $limituser); 
  2553.  
  2554. function gf_leads($form_id, $sort_field_number=0, $sort_direction='DESC', $search='', $offset=0, $page_size=3000, $star=null, $read=null, $is_numeric_sort = false, $start_date=null, $end_date=null) { 
  2555. echo get_gf_leads($form_id, $sort_field_number, $sort_direction, $search, $offset, $page_size, $star, $read, $is_numeric_sort, $start_date, $end_date); 
  2556.  
  2557. function kws_gf_directory($atts) { 
  2558. GFDirectory::make_directory($atts); 
  2559.  
  2560.  
  2561. if(!function_exists('kws_print_r')) { 
  2562. function kws_print_r($content, $die = false) { 
  2563. echo '<pre>'.print_r($content, true).'</pre>'; 
  2564. if($die) { die(); } 
  2565. return $content; 
  2566.  
  2567.  
  2568. /** Ending ?> left out intentionally */ 
.