/** * Enqueue Scripts and Styles for DataTable View Type * * @filter gravityview_datatables_loading_text Modify the text shown while the DataTable is loading */ function add_scripts_and_styles() { //global $gravityview_view; $post = get_post(); if (!is_a($post, 'WP_Post')) { do_action('gravityview_log_debug', 'GV_Extension_DataTables_Data[add_scripts_and_styles] not a post...leaving', $post); return; } // Get all the views on the current post/page/view $views = gravityview_get_current_views(); do_action('gravityview_log_debug', 'GV_Extension_DataTables_Data[add_scripts_and_styles] Get current views. Found:', $views); // Check whether there's a view with a datatables template $is_datatables = false; $dt_configs = array(); foreach ($views as $key => $view_data) { if (empty($view_data['template_id']) || 'datatables_table' !== $view_data['template_id']) { continue; } $is_datatables = true; $ajax_settings = array('action' => 'gv_datatables_data', 'view_id' => $view_data['id'], 'post_id' => $post->ID, 'nonce' => wp_create_nonce('gravityview_datatables_data'), 'getData' => json_encode((array) $_GET)); // Prepare DataTables init config $dt_config = array('processing' => true, 'deferRender' => true, 'serverSide' => true, 'retrieve' => true, 'stateSave' => true, 'stateDuration' => -1, 'lengthMenu' => $this->get_length_menu($view_data), 'language' => $this->get_language(), 'ajax' => array('url' => $this->get_ajax_url(), 'type' => 'POST', 'data' => $ajax_settings)); // page size, if defined if (!empty($view_data['atts']['page_size']) && is_numeric($view_data['atts']['page_size'])) { $dt_config['pageLength'] = intval($view_data['atts']['page_size']); } /** * Set the columns to be displayed * * @link https://datatables.net/reference/option/columns */ $columns = array(); if (!empty($view_data['fields']['directory_table-columns'])) { foreach ($view_data['fields']['directory_table-columns'] as $field) { $columns[] = array('name' => 'gv_' . $field['id'], 'width' => $this->get_column_width($field)); } $dt_config['columns'] = $columns; } // set default order if (!empty($view_data['atts']['sort_field'])) { foreach ($columns as $key => $column) { if ($column['name'] === 'gv_' . $view_data['atts']['sort_field']) { $dir = !empty($view_data['atts']['sort_direction']) ? $view_data['atts']['sort_direction'] : 'asc'; $dt_config['order'] = array(array($key, strtolower($dir))); } } } // filter init DataTables options $dt_config = apply_filters('gravityview_datatables_js_options', $dt_config, $view_data['id'], $post); $dt_configs[] = $dt_config; } // major FOREACH just to test // is the View requested a Datatables view ? if (empty($is_datatables)) { do_action('gravityview_log_debug', 'GV_Extension_DataTables_Data[add_scripts_and_styles] DataTables view not requested.'); return; } do_action('gravityview_log_debug', 'GV_Extension_DataTables_Data[add_scripts_and_styles] DataTables configuration: ', $dt_configs); $script_debug = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; $path = plugins_url('assets/datatables/media/', GV_DT_FILE); /** * Include DataTables core script * Use your own DataTables core script by using the `gravityview_datatables_script_src` filter */ wp_enqueue_script('gv-datatables', apply_filters('gravityview_datatables_script_src', $path . 'js/jquery.dataTables' . $script_debug . '.js'), array('jquery'), GV_Extension_DataTables::version, true); /** * Use your own DataTables stylesheet by using the `gravityview_datatables_style_src` filter */ wp_enqueue_style('gravityview_style_datatables_table'); /** * Register the featured entries script so that if active, the Featured Entries extension can use it. */ wp_register_style('gv-datatables-featured-entries', plugins_url('assets/css/featured-entries.css', GV_DT_FILE), array('gravityview_style_datatables_table'), GV_Extension_DataTables::version, 'all'); // include DataTables custom script wp_enqueue_script('gv-datatables-cfg', plugins_url('assets/js/datatables-views' . $script_debug . '.js', GV_DT_FILE), array('gv-datatables'), GV_Extension_DataTables::version, true); wp_localize_script('gv-datatables-cfg', 'gvDTglobals', $dt_configs); // Extend datatables by including other scripts and styles do_action('gravityview_datatables_scripts_styles', $dt_configs, $views, $post); }
/** * @internal Make sure this test is above the test_directory_link() test so that one doesn't pollute $post */ public function test_gravityview_get_current_views() { $fe = GravityView_frontend::getInstance(); // Clear the data so that gravityview_get_current_views() runs parse_content() $fe->gv_output_data = null; $view_post_type_id = $this->_get_new_view_id(); global $post; $post = get_post($view_post_type_id); $this->assertEquals($view_post_type_id, $post->ID); $current_views = gravityview_get_current_views(); // Check if the view post is set $this->assertTrue(isset($current_views[$view_post_type_id])); // When the view is added, the key is set to the View ID and the `id` is also set to that $this->assertEquals($view_post_type_id, $current_views[$view_post_type_id]['id']); // Just one View $this->assertEquals(1, count($current_views)); $second_view_post_type_id = $this->_get_new_view_id(); $fe->gv_output_data->add_view($second_view_post_type_id); $second_current_views = gravityview_get_current_views(); // Check to make sure add_view worked properly $this->assertEquals($second_view_post_type_id, $second_current_views[$second_view_post_type_id]['id']); // Now two Views $this->assertEquals(2, count($second_current_views)); }