/** * Theme function to get a GravityView view * * @access public * @param string $view_id (default: '') * @param array $atts (default: array()) * @return string HTML of the output. Empty string if $view_id is empty. */ function get_gravityview($view_id = '', $atts = array()) { if (!empty($view_id)) { $atts['id'] = $view_id; $args = wp_parse_args($atts, GravityView_View_Data::get_default_args()); $GravityView_frontend = GravityView_frontend::getInstance(); $GravityView_frontend->setGvOutputData(GravityView_View_Data::getInstance($view_id)); $GravityView_frontend->set_context_view_id($view_id); $GravityView_frontend->set_entry_data(); return $GravityView_frontend->render_view($args); } return ''; }
/** * Validate attributes passed to the [gravityview] shortcode. Supports {get} Merge Tags values. * * Attributes passed to the shortcode are compared to registered attributes {@see GravityView_View_Data::get_default_args} * Only attributes that are defined will be allowed through. * * Then, {get} merge tags are replaced with their $_GET values, if passed * * Then, attributes are sanitized based on the type of setting (number, checkbox, select, radio, text) * * @since 1.15.1 * * @see GravityView_View_Data::get_default_args Only attributes defined in get_default_args() are valid to be passed via the shortcode * * @param array $passed_atts Attribute pairs defined to render the View * * @return array Valid and sanitized attribute pairs */ private function parse_and_sanitize_atts($passed_atts) { $defaults = GravityView_View_Data::get_default_args(true); $supported_atts = array_fill_keys(array_keys($defaults), ''); // Whittle down the attributes to only valid pairs $filtered_atts = shortcode_atts($supported_atts, $passed_atts, 'gravityview'); // Only keep the passed attributes after making sure that they're valid pairs $filtered_atts = function_exists('array_intersect_key') ? array_intersect_key($passed_atts, $filtered_atts) : $filtered_atts; $atts = array(); foreach ($filtered_atts as $key => $passed_value) { // Allow using GravityView merge tags in shortcode attributes, like {get} and {created_by} $passed_value = GravityView_Merge_Tags::replace_variables($passed_value); switch ($defaults[$key]['type']) { /** * Make sure number fields are numeric. * Also, convert mixed number strings to numbers * @see http://php.net/manual/en/function.is-numeric.php#107326 */ case 'number': if (is_numeric($passed_value)) { $atts[$key] = $passed_value + 0; } break; // Checkboxes should be 1 or 0 // Checkboxes should be 1 or 0 case 'checkbox': $atts[$key] = gv_empty($passed_value) ? 0 : 1; break; /** * Only allow values that are defined in the settings */ /** * Only allow values that are defined in the settings */ case 'select': case 'radio': $options = isset($defaults[$key]['choices']) ? $defaults[$key]['choices'] : $defaults[$key]['options']; if (in_array($passed_value, array_keys($options))) { $atts[$key] = $passed_value; } break; case 'text': default: $atts[$key] = $passed_value; break; } } return $atts; }
/** * @param $args * * @return int|WP_Error */ function create_object($args) { $args = wp_parse_args($args, $this->default_generation_definitions); $form_id = $args['form_id']; $template_id = isset($args['template_id']) ? $args['template_id'] : 'preset_business_data'; $settings = isset($args['settings']) ? $args['settings'] : GravityView_View_Data::get_default_args(); $fields = isset($args['fields']) ? serialize($args['fields']) : 'a:1:{s:23:"directory_table-columns";a:3:{s:13:"535d63d1488b0";a:9:{s:2:"id";s:1:"4";s:5:"label";s:13:"Business Name";s:10:"show_label";s:1:"1";s:12:"custom_label";s:0:"";s:12:"custom_class";s:0:"";s:12:"show_as_link";s:1:"0";s:13:"search_filter";s:1:"0";s:13:"only_loggedin";s:1:"0";s:17:"only_loggedin_cap";s:4:"read";}s:13:"535d63d379a3c";a:9:{s:2:"id";s:2:"12";s:5:"label";s:20:"Business Description";s:10:"show_label";s:1:"1";s:12:"custom_label";s:0:"";s:12:"custom_class";s:0:"";s:12:"show_as_link";s:1:"0";s:13:"search_filter";s:1:"0";s:13:"only_loggedin";s:1:"0";s:17:"only_loggedin_cap";s:4:"read";}s:13:"535d63dc735a6";a:9:{s:2:"id";s:1:"2";s:5:"label";s:7:"Address";s:10:"show_label";s:1:"1";s:12:"custom_label";s:0:"";s:12:"custom_class";s:0:"";s:12:"show_as_link";s:1:"0";s:13:"search_filter";s:1:"0";s:13:"only_loggedin";s:1:"0";s:17:"only_loggedin_cap";s:4:"read";}}}'; $insert_post_response = parent::create_object($args); if ($insert_post_response && !is_wp_error($insert_post_response)) { $view_meta = array('_gravityview_form_id' => $form_id, '_gravityview_template_settings' => $settings, '_gravityview_directory_template' => $template_id, '_gravityview_directory_widgets' => 'a:0:{}', '_gravityview_directory_fields' => $fields); foreach ($view_meta as $meta_key => $meta_value) { $meta_value = maybe_unserialize($meta_value); update_post_meta($insert_post_response, $meta_key, $meta_value); } } return $insert_post_response; }
/** * Get all the settings for a View * * @uses GravityView_View_Data::get_default_args() Parses the settings with the plugin defaults as backups. * @param int $post_id View ID * @return array Associative array of settings with plugin defaults used if not set by the View */ public static function get_template_settings($post_id) { $settings = get_post_meta($post_id, '_gravityview_template_settings', true); if (class_exists('GravityView_View_Data')) { $defaults = GravityView_View_Data::get_default_args(); return wp_parse_args((array) $settings, $defaults); } // Backup, in case GravityView_View_Data isn't loaded yet. return $settings; }
/** * Add tooltip text for use throughout the UI * @param array $tooltips Array of Gravity Forms tooltips * @return array Modified tooltips array */ public function tooltips($tooltips = array()) { $gv_tooltips = array(); // Generate tooltips for View settings $default_args = GravityView_View_Data::get_default_args(true); foreach ($default_args as $key => $arg) { // If an arg has `tooltip` defined, but it's false, don't display a tooltip if (isset($arg['tooltip']) && empty($arg['tooltip'])) { continue; } // By default, use `tooltip` if defined. $tooltip = empty($arg['tooltip']) ? NULL : $arg['tooltip']; // Otherwise, use the description as a tooltip. if (empty($tooltip) && !empty($arg['desc'])) { $tooltip = $arg['desc']; } // If there's no tooltip set, continue if (empty($tooltip)) { continue; } // Add the tooltip $gv_tooltips['gv_' . $key] = array('title' => $arg['label'], 'value' => $tooltip); } $gv_tooltips['gv_css_merge_tags'] = array('title' => __('CSS Merge Tags', 'gravityview'), 'value' => sprintf(__('Developers: The CSS classes will be sanitized using the %ssanitize_title_with_dashes()%s function.', 'gravityview'), '<code>', '</code>')); /** * @filter `gravityview_tooltips` The tooltips GravityView adds to the Gravity Forms tooltip array * @param array $gv_tooltips Associative array with unique keys containing array of `title` and `value` keys, as expected by `gform_tooltips` filter */ $gv_tooltips = apply_filters('gravityview_tooltips', $gv_tooltips); foreach ($gv_tooltips as $key => $tooltip) { $title = empty($tooltip['title']) ? '' : '<h6>' . esc_html($tooltip['title']) . '</h6>'; $tooltips[$key] = $title . wpautop(esc_html($tooltip['value'])); } return $tooltips; }
/** * Add shortcode popup div * * @access public * @return void */ function add_shortcode_popup() { global $post; if (!$this->is_post_editor_screen()) { return; } $post_type = get_post_type_object($post->post_type); $views = get_posts(array('post_type' => 'gravityview', 'posts_per_page' => -1)); // If there are no views set up yet, we get outta here. if (empty($views)) { echo '<div id="select_gravityview_view"><div class="wrap">' . GravityView_Post_Types::no_views_text() . '</div></div>'; return; } ?> <div id="select_gravityview_view"> <form action="#" method="get" id="select_gravityview_view_form"> <div class="wrap"> <h2 class=""><?php esc_html_e('Embed a View', 'gravityview'); ?> </h2> <p class="subtitle"><?php printf(esc_attr(__('Use this form to embed a View into this %s. %sLearn more about using shortcodes.%s', 'gravityview')), $post_type->labels->singular_name, '<a href="http://gravityview.co/support/documentation/202934188/" target="_blank">', '</a>'); ?> </p> <div> <h3><label for="gravityview_id"><?php esc_html_e('Select a View', 'gravityview'); ?> </label></h3> <select name="gravityview_id" id="gravityview_id"> <option value=""><?php esc_html_e('— Select a View to Insert —', 'gravityview'); ?> </option> <?php foreach ($views as $view) { $title = empty($view->post_title) ? __('(no title)', 'gravityview') : $view->post_title; echo '<option value="' . $view->ID . '">' . esc_html(sprintf('%s #%d', $title, $view->ID)) . '</option>'; } ?> </select> </div> <table class="form-table hide-if-js"> <caption><?php esc_html_e('View Settings', 'gravityview'); ?> </caption> <?php $settings = GravityView_View_Data::get_default_args(true); foreach ($settings as $key => $setting) { if (empty($setting['show_in_shortcode'])) { continue; } GravityView_Render_Settings::render_setting_row($key, array(), NULL, 'gravityview_%s', 'gravityview_%s'); } ?> </table> <div class="submit"> <input type="submit" class="button button-primary button-large alignleft hide-if-js" value="<?php esc_attr_e('Insert View', 'gravityview'); ?> " id="insert_gravityview_view" /> <input class="button button-secondary alignright" type="submit" onclick="tb_remove(); return false;" value="<?php esc_attr_e("Cancel", 'gravityview'); ?> " /> </div> </div> </form> </div> <?php }
/** * main AJAX logic to retrieve DataTables data */ function get_datatables_data() { global $gravityview_view; if (empty($_POST)) { return; } // Prevent error output ob_start(); // Send correct headers $this->do_ajax_headers('application/javascript'); $this->check_ajax_nonce(); if (empty($_POST['view_id'])) { do_action('gravityview_log_debug', '[DataTables] AJAX request - View ID check failed'); exit(false); } // Prevent emails from being encrypted add_filter('gravityview_email_prevent_encrypt', '__return_true'); do_action('gravityview_log_debug', '[DataTables] AJAX Request ($_POST)', $_POST); // include some frontend logic if (class_exists('GravityView_Plugin') && !class_exists('GravityView_View')) { GravityView_Plugin::getInstance()->frontend_actions(); } // Pass $_GET variables to the View functions, since they're relied on heavily // for searching and filtering, for example the A-Z widget $_GET = json_decode(stripslashes($_POST['getData']), true); $view_id = intval($_POST['view_id']); // create the view object based on the post_id $GravityView_View_Data = GravityView_View_Data::getInstance((int) $_POST['post_id']); // get the view data $view_data = $GravityView_View_Data->get_view($view_id); $view_data['atts']['id'] = $view_id; $atts = $view_data['atts']; // check for order/sorting if (isset($_POST['order'][0]['column'])) { $order_index = $_POST['order'][0]['column']; if (!empty($_POST['columns'][$order_index]['name'])) { // remove prefix 'gv_' $atts['sort_field'] = substr($_POST['columns'][$order_index]['name'], 3); $atts['sort_direction'] = !empty($_POST['order'][0]['dir']) ? strtoupper($_POST['order'][0]['dir']) : 'ASC'; } } // check for search if (!empty($_POST['search']['value'])) { $atts['search_value'] = esc_attr(stripslashes_deep($_POST['search']['value'])); } // Paging/offset $atts['page_size'] = isset($_POST['length']) ? intval($_POST['length']) : ''; $atts['offset'] = isset($_POST['start']) ? intval($_POST['start']) : 0; // prepare to get entries $atts = wp_parse_args($atts, GravityView_View_Data::get_default_args()); // check if someone requested the full filtered data (eg. TableTools print button) if ($atts['page_size'] == '-1') { $mode = 'all'; $atts['page_size'] = PHP_INT_MAX; } else { // regular mode - get view entries $mode = 'page'; } $view_data['atts'] = $atts; $gravityview_view = new GravityView_View($view_data); if (class_exists('GravityView_Cache')) { // We need to fetch the search criteria and pass it to the Cache so that the search is used when generating the cache transient key. $search_criteria = GravityView_frontend::get_search_criteria($atts, $view_data['form_id']); // make sure to allow late filter ( used on Advanced Filter extension ) $criteria = apply_filters('gravityview_search_criteria', array('search_criteria' => $search_criteria), $view_data['form_id'], $_POST['view_id']); $atts['search_criteria'] = $criteria['search_criteria']; // Cache key should also depend on the View assigned fields $atts['directory_table-columns'] = !empty($view_data['fields']['directory_table-columns']) ? $view_data['fields']['directory_table-columns'] : array(); // cache depends on user session $atts['user_session'] = $this->get_user_session(); $Cache = new GravityView_Cache($view_data['form_id'], $atts); if ($output = $Cache->get()) { do_action('gravityview_log_debug', '[DataTables] Cached output found; using cache with key ' . $Cache->get_key()); // update DRAW (mr DataTables is very sensitive!) $temp = json_decode($output, true); $temp['draw'] = intval($_POST['draw']); $output = json_encode($temp); exit($output); } } $view_entries = GravityView_frontend::get_view_entries($atts, $view_data['form_id']); $data = $this->get_output_data($view_entries, $view_data); // wrap all $output = array('draw' => intval($_POST['draw']), 'recordsTotal' => intval($view_entries['count']), 'recordsFiltered' => intval($view_entries['count']), 'data' => $data); do_action('gravityview_log_debug', '[DataTables] Ajax request answer', $output); $json = json_encode($output); if (class_exists('GravityView_Cache')) { do_action('gravityview_log_debug', '[DataTables] Setting cache'); // Cache results $Cache->set($json, 'datatables_output'); } // End prevent error output ob_end_clean(); exit($json); }
public function _get_new_view_id() { $view_array = array('post_content' => '', 'post_type' => 'gravityview', 'post_status' => 'publish'); // Add the View $view_post_type_id = wp_insert_post($view_array); // Set the form ID update_post_meta($view_post_type_id, '_gravityview_form_id', $this->form_id); // Set the View settigns update_post_meta($view_post_type_id, '_gravityview_template_settings', GravityView_View_Data::get_default_args()); // Set the template to be table update_post_meta($view_post_type_id, '_gravityview_directory_template', 'default_table'); return $view_post_type_id; }