GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-admin-add-shortcode.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Adds a button to add the View shortcode into the post content
4  *
5  * @package GravityView
6  * @license GPL2+
7  * @author GravityView <[email protected]>
8  * @link http://gravityview.co
9  * @copyright Copyright 2014, Katz Web Services, Inc.
10  *
11  * @since 1.0.0
12  */
13 
14 /** If this file is called directly, abort. */
15 if ( ! defined( 'ABSPATH' ) ) {
16  die;
17 }
18 
20 
21  function __construct() {
22 
23  add_action( 'media_buttons', array( $this, 'add_shortcode_button'), 30);
24 
25  add_action( 'admin_footer', array( $this, 'add_shortcode_popup') );
26 
27  // adding styles and scripts
28  add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts_and_styles') );
29 
30  // ajax - populate sort fields based on the selected view
31  add_action( 'wp_ajax_gv_sortable_fields', array( $this, 'get_sortable_fields' ) );
32  }
33 
34 
35  /**
36  * check if screen post editor and is not related with post type 'gravityview'
37  *
38  * @return bool
39  */
40  function is_post_editor_screen() {
41  global $current_screen, $pagenow;
42  return !empty( $current_screen->post_type ) && 'gravityview' != $current_screen->post_type && in_array( $pagenow , array( 'post.php' , 'post-new.php' ) );
43  }
44 
45 
46  /**
47  * Add shortcode button to the Add Media right
48  *
49  * @return void
50  */
51  function add_shortcode_button() {
52 
53  /**
54  * @since 1.15.3
55  */
56  if( ! GVCommon::has_cap( array( 'publish_gravityviews' ) ) ) {
57  return;
58  }
59 
60  if( !$this->is_post_editor_screen() ) {
61  return;
62  }
63  ?>
64  <a href="#TB_inline?width=600&amp;height=800&amp;inlineId=select_gravityview_view" class="thickbox hide-if-no-js button gform_media_link" id="add_gravityview" title="<?php esc_attr_e("Insert View", 'gk-gravityview'); ?>"><span class="icon gv-icon-astronaut-head"></span><?php esc_html_e( 'Add View', 'gk-gravityview' ); ?></a>
65  <?php
66 
67  }
68 
69 
70 
71  /**
72  * Add shortcode popup div
73  *
74  * @return void
75  */
76  function add_shortcode_popup() {
77  global $post;
78 
79  if( !$this->is_post_editor_screen() ) {
80  return;
81  }
82 
83  $post_type = get_post_type_object($post->post_type);
84 
85  $views = get_posts( array('post_type' => 'gravityview', 'posts_per_page' => -1 ) );
86 
87  // If there are no views set up yet, we get outta here.
88  if( empty( $views ) ) {
89  echo '<div id="select_gravityview_view"><div class="wrap">' . GravityView_Admin::no_views_text() . '</div></div>';
90  return;
91  }
92 
93  ?>
94  <div id="select_gravityview_view">
95  <form action="#" method="get" id="select_gravityview_view_form">
96  <div class="wrap">
97 
98  <h2 class=""><?php esc_html_e( 'Embed a View', 'gk-gravityview' ); ?></h2>
99  <p class="subtitle"><?php printf( esc_attr ( __( 'Use this form to embed a View into this %s. %sLearn more about using shortcodes.%s', 'gk-gravityview') ), $post_type->labels->singular_name, '<a href="https://docs.gravityview.co/article/73-using-the-shortcode" target="_blank" rel="noopener noreferrer">', '</a>' ); ?></p>
100 
101  <div>
102  <h3><label for="gravityview_id"><?php esc_html_e( 'Select a View', 'gk-gravityview' ); ?></label></h3>
103 
104  <select name="gravityview_id" id="gravityview_id">
105  <option value=""><?php esc_html_e( '&mdash; Select a View to Insert &mdash;', 'gk-gravityview' ); ?></option>
106  <?php
107  foreach( $views as $view ) {
108  $title = empty( $view->post_title ) ? __('(no title)', 'gk-gravityview') : $view->post_title;
109  echo '<option value="'. $view->ID .'">'. esc_html( sprintf('%s #%d', $title, $view->ID ) ) .'</option>';
110  }
111  ?>
112  </select>
113  </div>
114 
115  <table class="form-table hide-if-js">
116 
117  <caption><?php esc_html_e( 'View Settings', 'gk-gravityview' ); ?></caption>
118 
119  <?php
120 
122 
123  foreach ( $settings as $key => $setting ) {
124 
125  if( empty( $setting['show_in_shortcode'] ) ) { continue; }
126 
127  GravityView_Render_Settings::render_setting_row( $key, array(), NULL, 'gravityview_%s', 'gravityview_%s' );
128  }
129  ?>
130 
131  </table>
132 
133  <div class="submit">
134  <input type="submit" class="button button-primary button-large alignleft hide-if-js" value="<?php esc_attr_e('Insert View', 'gk-gravityview' ); ?>" id="insert_gravityview_view" />
135  <input class="button button-secondary alignright" type="submit" onclick="tb_remove(); return false;" value="<?php esc_attr_e("Cancel", 'gk-gravityview'); ?>" />
136  </div>
137 
138  </div>
139  </form>
140  </div>
141  <?php
142 
143  }
144 
145 
146 
147 
148  /**
149  * Enqueue scripts and styles
150  *
151  * @return void
152  */
154 
155  if( ! $this->is_post_editor_screen() ) {
156  return;
157  }
158 
159  wp_enqueue_style( 'dashicons' );
160 
161  // date picker
162  wp_enqueue_script( 'jquery-ui-datepicker' );
163 
164  $protocol = is_ssl() ? 'https://' : 'http://';
165 
166  wp_enqueue_style( 'jquery-ui-datepicker', $protocol.'ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/smoothness/jquery-ui.css', array(), GV_PLUGIN_VERSION );
167 
168  //enqueue styles
169  wp_register_style( 'gravityview_postedit_styles', plugins_url('assets/css/admin-post-edit.css', GRAVITYVIEW_FILE), array(), GV_PLUGIN_VERSION );
170  wp_enqueue_style( 'gravityview_postedit_styles' );
171 
172  $script_debug = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';
173 
174  // custom js
175  wp_register_script( 'gravityview_postedit_scripts', plugins_url('assets/js/admin-post-edit'.$script_debug.'.js', GRAVITYVIEW_FILE), array( 'jquery', 'jquery-ui-datepicker' ), GV_PLUGIN_VERSION );
176  wp_enqueue_script( 'gravityview_postedit_scripts' );
177  wp_localize_script('gravityview_postedit_scripts', 'gvGlobals', array(
178  'nonce' => wp_create_nonce( 'gravityview_ajaxaddshortcode'),
179  'loading_text' => esc_html__( 'Loading&hellip;', 'gk-gravityview' ),
180  'alert_1' => esc_html__( 'Please select a View', 'gk-gravityview'),
181  ));
182 
183  }
184 
185 
186 
187  /**
188  * Ajax
189  * Given a View id, calculates the assigned form, and returns the form fields (only the sortable ones )
190  *
191  * @return void
192  */
193  function get_sortable_fields() {
194 
195  // Not properly formatted request
196  if ( empty( $_POST['viewid'] ) || !is_numeric( $_POST['viewid'] ) ) {
197  exit( false );
198  }
199 
200  // Not valid request
201  if( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'gravityview_ajaxaddshortcode' ) ) {
202  exit( false );
203  }
204 
205  $viewid = (int)$_POST['viewid'];
206 
207  // fetch form id assigned to the view
208  $formid = gravityview_get_form_id( $viewid );
209 
210  // Get the default sort field for the view
211  $sort_field = gravityview_get_template_setting( $viewid, 'sort_field' );
212 
213  // Generate the output `<option>`s
214  $response = gravityview_get_sortable_fields( $formid, $sort_field );
215 
216  exit( $response );
217  }
218 
219 }
220 
const GV_PLUGIN_VERSION(! GravityKit\GravityView\Foundation\meets_min_php_version_requirement(__FILE__, '7.2.0'))
Constants.
Definition: gravityview.php:34
is_post_editor_screen()
check if screen post editor and is not related with post type &#39;gravityview&#39;
add_shortcode_popup()
Add shortcode popup div.
static render_setting_row( $key='', $current_settings=array(), $override_input=null, $name='template_settings[%s]', $id='gravityview_se_%s')
Output a table row for view settings.
get_sortable_fields()
Ajax Given a View id, calculates the assigned form, and returns the form fields (only the sortable on...
gravityview_get_template_setting( $post_id, $key)
Get the setting for a View.
add_scripts_and_styles()
Enqueue scripts and styles.
global $post
Definition: delete-entry.php:7
add_shortcode_button()
Add shortcode button to the Add Media right.
static no_views_text()
Get text for no views found.
Definition: class-admin.php:55
new GravityView_Admin_Add_Shortcode
gravityview_get_form_id( $view_id)
Get the connected form ID from a View ID.
const GRAVITYVIEW_FILE
Full path to the GravityView file "GRAVITYVIEW_FILE" "./gravityview.php".
Definition: gravityview.php:40
gravityview_get_sortable_fields( $formid, $current='')
Render dropdown (select) with the list of sortable fields from a form ID.
static defaults( $detailed=false, $group=null)
Retrieve the default View settings.
static has_cap( $caps='', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()
If this file is called directly, abort.
$title