GravityView  1.19.4
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-entry-link-shortcode.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Handle the [gv_entry_link] shortcode
5  *
6  * Replaces [gv_edit_entry_link] and [gv_delete_entry_link] shortcodes
7  *
8  * @since 1.15
9  */
11 
12  /**
13  * @type array Entry fetched using the $atts['entry_id'] shortcode setting.
14  * @since 1.15
15  */
16  private $entry = array();
17 
18  /**
19  * @type int If set, generate a link to the entry for this View ID. Required when used outside a View. Otherwise, current View ID is used.
20  * @since 1.15
21  */
22  private $view_id = 0;
23 
24  /**
25  * @type array The accepted shortcode attribute pairs, with defaults set
26  * @since 1.15
27  */
28  static private $defaults = array(
29  'action' => 'read',
30  'view_id' => 0,
31  'entry_id' => 0,
32  'post_id' => 0,
33  'link_atts' => '',
34  'return' => 'html',
35  'field_values' => '',
36  );
37 
38  /**
39  * @type array The final settings for the shortcode, after merging passed $atts with self::$defaults
40  * @since 1.15
41  */
42  private $settings = array();
43 
44  function __construct() {
45  $this->add_hooks();
46  }
47 
48  /**
49  * Add shortcodes
50  *
51  * @since 1.15
52  */
53  private function add_hooks() {
54  add_shortcode( 'gv_entry_link', array( $this, 'read_shortcode' ) );
55  add_shortcode( 'gv_edit_entry_link', array( $this, 'edit_shortcode' ) );
56  add_shortcode( 'gv_delete_entry_link', array( $this, 'delete_shortcode' ) );
57  }
58 
59  /**
60  * @since 1.15
61  * @copydoc GravityView_Entry_Link_Shortcode::shortcode
62  */
63  public function read_shortcode( $atts, $content = null, $context = 'gv_entry_link' ) {
64  return $this->shortcode( $atts, $content, $context );
65  }
66 
67  /**
68  * Backward compatibility for existing `gv_edit_entry_link` shortcode
69  * Forces $atts['action'] to "edit"
70  *
71  * @since 1.15
72  * @copydoc GravityView_Entry_Link_Shortcode::shortcode
73  */
74  public function edit_shortcode( $atts, $content = null, $context = 'gv_edit_entry_link' ) {
75  $atts['action'] = 'edit';
76 
77  return $this->shortcode( $atts, $content, $context );
78  }
79 
80  /**
81  * Backward compatibility for existing `gv_delete_entry_link` shortcodes
82  * Forces $atts['action'] to "delete"
83  *
84  * @since 1.15
85  * @copydoc GravityView_Entry_Link_Shortcode::shortcode
86  */
87  public function delete_shortcode( $atts, $content = null, $context = 'gv_delete_entry_link' ) {
88  $atts['action'] = 'delete';
89 
90  return $this->shortcode( $atts, $content, $context );
91  }
92 
93  /**
94  * Generate a link to an entry. The link can be an edit, delete, or standard link.
95  *
96  * @since 1.15
97  *
98  * @param array $atts {
99  * @type string $action What type of link to generate. Options: `read`, `edit`, and `delete`. Default: `read`
100  * @type string $view_id Define the ID for the View. If not set, use current View ID, if exists.
101  * @type string $entry_id ID of the entry to edit. If undefined, uses the current entry ID, if exists.
102  * @type string $post_id ID of the base post or page to use for an embedded View
103  * @type string $link_atts Pass anchor tag attributes (`target=_blank` to open Edit Entry link in a new window, for example)
104  * @type string $return What should the shortcode return: link HTML (`html`) or the URL (`url`). Default: `html`
105  * @type string $field_values Only used for `action="edit"`. Parameters to pass in to the prefill data in Edit Entry form. Uses the same format as Gravity Forms "Allow field to be populated dynamically" {@see https://www.gravityhelp.com/documentation/article/allow-field-to-be-populated-dynamically/ }
106  * }
107  *
108  * @param string|null $content Used as link anchor text, if specified.
109  * @param string $context Current shortcode being called. Not used.
110  *
111  * @return null|string If admin or an error occurred, returns null. Otherwise, returns entry link output. If `$atts['return']` is 'url', the entry link URL. Otherwise, entry link `<a>` HTML tag.
112  */
113  private function shortcode( $atts, $content = null, $context = 'gv_entry_link' ) {
114 
115  // Don't process when saving post. Keep processing if it's admin-ajax.php
116  if ( ! class_exists( 'GravityView_Plugin' ) || GravityView_Plugin::is_admin() ) {
117  return null;
118  }
119 
120  // Make sure GV is loaded
121  if ( ! class_exists( 'GravityView_frontend' ) || ! class_exists( 'GravityView_View' ) ) {
122  do_action( 'gravityview_log_error', __METHOD__ . ' GravityView_frontend or GravityView_View do not exist.' );
123 
124  return null;
125  }
126 
127  $this->settings = shortcode_atts( self::$defaults, $atts, $context );
128 
129  $this->view_id = empty( $this->settings['view_id'] ) ? GravityView_View::getInstance()->getViewId() : absint( $this->settings['view_id'] );
130 
131  if ( empty( $this->view_id ) ) {
132  do_action( 'gravityview_log_error', __METHOD__ . ' A View ID was not defined and we are not inside a View' );
133 
134  return null;
135  }
136 
137  $this->entry = $this->get_entry( $this->settings['entry_id'] );
138 
139  do_action( 'gravityview_log_debug', __METHOD__ . ' ' . $context . ' $atts: ', $atts );
140 
141  if ( ! $this->has_cap() ) {
142  do_action( 'gravityview_log_error', __METHOD__ . ' User does not have the capability to ' . esc_attr( $this->settings['action'] ) . ' this entry: ' . $this->entry['id'] );
143 
144  return null;
145  }
146 
147  $url = $this->get_url();
148 
149  if ( ! $url ) {
150  do_action( 'gravityview_log_error', __METHOD__ . ' Link returned false; View or Post may not exist.' );
151 
152  return false;
153  }
154 
155  // Get just the URL, not the tag
156  if ( 'url' === $this->settings['return'] ) {
157  return $url;
158  }
159 
160  $link_atts = $this->get_link_atts();
161 
162  $link_text = $this->get_anchor_text( $content );
163 
165  }
166 
167  /**
168  * Parse shortcode atts to fetch `link_atts`, which will be added to the output of the HTML anchor tag generated by shortcode
169  * Only used when `return` value of shortcode is not "url"
170  *
171  * @since 1.15
172  * @see gravityview_get_link() See acceptable attributes here
173  * @return array Array of attributes to be added
174  */
175  private function get_link_atts() {
176 
177  wp_parse_str( $this->settings['link_atts'], $link_atts );
178 
179  if ( 'delete' === $this->settings['action'] ) {
180  $link_atts['onclick'] = isset( $link_atts['onclick'] ) ? $link_atts['onclick'] : GravityView_Delete_Entry::get_confirm_dialog();
181  }
182 
183  return (array) $link_atts;
184  }
185 
186  /**
187  * Get the anchor text for the link. If content inside shortcode is defined, use that as the text. Otherwise, use default values.
188  *
189  * Only used when `return` value of shortcode is not "url"
190  *
191  * @since 1.15
192  *
193  * @param string|null $content Content inside shortcode, if defined
194  *
195  * @return string Text to use for HTML anchor
196  */
197  private function get_anchor_text( $content = null ) {
198 
199  if ( $content ) {
200  return $content;
201  }
202 
203  switch ( $this->settings['action'] ) {
204  case 'edit':
205  $anchor_text = __( 'Edit Entry', 'gravityview' );
206  break;
207  case 'delete':
208  $anchor_text = __( 'Delete Entry', 'gravityview' );
209  break;
210  default:
211  $anchor_text = __( 'View Details', 'gravityview' );
212  }
213 
214  return $anchor_text;
215  }
216 
217  /**
218  * Get the URL for the entry.
219  *
220  * Uses the `post_id`, `view_id` params as defined in the shortcode attributes.
221  *
222  * @since 1.15
223  *
224  * @param string|null $content Content inside shortcode, if defined
225  *
226  * @return string|boolean If URL is fetched, the URL to the entry link. If not found, returns false.
227  */
228  private function get_url() {
229 
230  // if post_id is not defined, default to view_id
231  $post_id = empty( $this->settings['post_id'] ) ? $this->view_id : absint( $this->settings['post_id'] );
232 
233  switch ( $this->settings['action'] ) {
234  case 'edit':
235  $url = GravityView_Edit_Entry::get_edit_link( $this->entry, $this->view_id, $post_id );
236  break;
237  case 'delete':
238  $url = GravityView_Delete_Entry::get_delete_link( $this->entry, $this->view_id, $post_id );
239  break;
240  case 'read':
241  default:
242  $url = GravityView_API::entry_link( $this->entry, $post_id );
243  }
244 
246 
247  return $url;
248  }
249 
250  /**
251  * Check whether the user has the capability to see the shortcode output, depending on the action ('read', 'edit', 'delete')
252  *
253  * @since 1.15
254  * @return bool True: has cap.
255  */
256  private function has_cap() {
257 
258  switch ( $this->settings['action'] ) {
259  case 'edit':
260  $has_cap = GravityView_Edit_Entry::check_user_cap_edit_entry( $this->entry, $this->view_id );
261  break;
262  case 'delete':
263  $has_cap = GravityView_Delete_Entry::check_user_cap_delete_entry( $this->entry, array(), $this->view_id );
264  break;
265  case 'read':
266  default:
267  $has_cap = true; // TODO: add cap check for read_gravityview
268  }
269 
270  return $has_cap;
271  }
272 
273  /**
274  * Get entry array from `entry_id` parameter. If no $entry_id
275  *
276  * @since 1.15
277  * @uses GVCommon::get_entry
278  * @uses GravityView_frontend::getSingleEntry
279  *
280  * @param int $entry_id Gravity Forms Entry ID. If not passed, current View's current entry ID will be used, if found.
281  *
282  * @return array|bool Gravity Forms array, if found. Otherwise, false.
283  */
284  private function get_entry( $entry_id = 0 ) {
285 
286  $backup_entry = GravityView_frontend::getInstance()->getSingleEntry() ? GravityView_frontend::getInstance()->getEntry() : GravityView_View::getInstance()->getCurrentEntry();
287 
288  if ( empty( $entry_id ) ) {
289  if ( ! $backup_entry ) {
290  do_action( 'gravityview_log_error', __METHOD__ . ' No entry defined (or entry id not valid number)', $this->settings );
291 
292  return false;
293  }
294  $entry = $backup_entry;
295  } else {
296  $entry = wp_cache_get( 'gv_entry_link_entry_' . $entry_id, 'gravityview_entry_link_shortcode' );
297  if ( false === $entry ) {
298  $entry = GVCommon::get_entry( $entry_id, true, false );
299  wp_cache_add( 'gv_entry_link_entry_' . $entry_id, $entry, 'gravityview_entry_link_shortcode' );
300  }
301  }
302 
303  // No search results
304  if ( false === $entry ) {
305  do_action( 'gravityview_log_error', __METHOD__ . ' No entries match the entry ID defined', $entry_id );
306 
307  return false;
308  }
309 
310  return $entry;
311  }
312 
313  /**
314  * Allow passing URL params to dynamically populate the Edit Entry form
315  * If `field_values` key is set, run it through `parse_str()` and add the values to $url
316  *
317  * @since 1.15
318  *
319  * @param string $href URL
320  */
322 
323  if ( $url && ! empty( $this->settings['field_values'] ) ) {
324 
325  wp_parse_str( $this->settings['field_values'], $field_values );
326 
327  $url = add_query_arg( $field_values, $url );
328  }
329 
330  return $url;
331  }
332 }
333 
$url
Definition: post_image.php:25
Handle the [gv_entry_link] shortcode.
$settings
array The final settings for the shortcode, after merging passed $atts with self::$defaults ...
get_anchor_text($content=null)
Get the anchor text for the link.
edit_shortcode($atts, $content=null, $context= 'gv_edit_entry_link')
Backward compatibility for existing gv_edit_entry_link shortcode Forces $atts[&#39;action&#39;] to "edit"...
$entry
array Entry fetched using the $atts[&#39;entry_id&#39;] shortcode setting.
$view_id
int If set, generate a link to the entry for this View ID.
has_cap()
Check whether the user has the capability to see the shortcode output, depending on the action (&#39;read...
static check_user_cap_delete_entry($entry, $field=array(), $view_id=0)
checks if user has permissions to view the link or delete a specific entry
static get_delete_link($entry, $view_id=0, $post_id=null)
Generate a nonce link with the base URL of the current View embed.
add_hooks()
Add shortcodes.
static check_user_cap_edit_entry($entry, $view_id=0)
checks if user has permissions to edit a specific entry
static get_confirm_dialog()
Get the onclick attribute for the confirm dialogs that warns users before they delete an entry...
static entry_link($entry, $post_id=NULL, $add_directory_args=true)
return href for single entry
Definition: class-api.php:642
delete_shortcode($atts, $content=null, $context= 'gv_delete_entry_link')
Backward compatibility for existing gv_delete_entry_link shortcodes Forces $atts[&#39;action&#39;] to "delete...
gravityview_get_link($href= '', $anchor_text= '', $atts=array())
Generate an HTML anchor tag with a list of supported attributes.
maybe_add_field_values_query_args($url)
Allow passing URL params to dynamically populate the Edit Entry form If field_values key is set...
get_link_atts()
Parse shortcode atts to fetch link_atts, which will be added to the output of the HTML anchor tag gen...
get_entry($entry_id=0)
Get entry array from entry_id parameter.
static get_entry($entry_slug, $force_allow_ids=false, $check_entry_display=true)
Return a single entry object.
static is_admin()
Check if is_admin(), and make sure not DOING_AJAX.
get_url()
Get the URL for the entry.
static $defaults
array The accepted shortcode attribute pairs, with defaults set
static getInstance($passed_post=NULL)
static get_edit_link($entry, $view_id, $post_id=null, $field_values= '')
The edit entry link creates a secure link with a nonce.
__construct()
new GravityView_Entry_Link_Shortcode
if(empty($field_settings['content'])) $content
Definition: custom.php:37
read_shortcode($atts, $content=null, $context= 'gv_entry_link')
Generate a link to an entry.
shortcode($atts, $content=null, $context= 'gv_entry_link')
Generate a link to an entry.
static getInstance()
Get the one true instantiated self.