GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gv-shortcode.php
Go to the documentation of this file.
1 <?php
2 namespace GV;
3 
4 /** If this file is called directly, abort. */
5 if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6  die();
7 }
8 
9 /**
10  * The base \GV\Shortcode class.
11  *
12  * Contains some unitility methods, base class for all GV Shortcodes.
13  */
14 class Shortcode {
15  /*
16  * @var array All GravityView-registered and loaded shortcodes can be found here.
17  */
18  private static $shortcodes;
19 
20  /**
21  * @var array The parsed attributes of this shortcode.
22  */
23  public $atts;
24 
25  /**
26  * @var string The parsed name of this shortcode.
27  */
28  public $name;
29 
30  /**
31  * @var string The parsed content between tags of this shortcode.
32  */
33  public $content;
34 
35  /**
36  * The WordPress Shortcode API callback for this shortcode.
37  *
38  * @param array $atts The attributes passed.
39  * @param string $content The content inside the shortcode.
40  * @param string $tag The tag.
41  *
42  * @return string The output.
43  */
44  public function callback( $atts, $content = '', $tag = '' ) {
45  gravityview()->log->error( '[{shortcode}] shortcode {class}::callback method not implemented.', array( 'shortcode' => $this->name, 'class' => get_class( $this ) ) );
46  return '';
47  }
48 
49  /**
50  * Register this shortcode class with the WordPress Shortcode API.
51  *
52  * @internal
53  *
54  * @since develop
55  * @param string $name A shortcode name override. Default: self::$name
56  *
57  * @return \GV\Shortcode|null The only internally registered instance of this shortcode, or null on error.
58  */
59  public static function add( $name = null ) {
60  $shortcode = new static();
61  $name = $name ? $name : $shortcode->name;
62  if ( shortcode_exists( $name ) ) {
63  if ( empty( self::$shortcodes[ $name ] ) ) {
64  gravityview()->log->error( 'Shortcode [{shortcode}] has already been registered elsewhere.', array( 'shortcode' => $name ) );
65  return null;
66  }
67  } else {
68  add_shortcode( $name, array( $shortcode, 'callback' ) );
69  self::$shortcodes[ $name ] = $shortcode;
70  }
71 
72  return self::$shortcodes[ $name ];
73  }
74 
75  /**
76  * Unregister this shortcode.
77  *
78  * @internal
79  *
80  * @return void
81  */
82  public static function remove() {
83  $shortcode = new static();
84  unset( self::$shortcodes[$shortcode->name] );
85  remove_shortcode( $shortcode->name );
86  }
87 
88  /**
89  * Parse a string of content and figure out which ones there are.
90  *
91  * Only registered shortcodes (via add_shortcode) will show up.
92  * Returned order is not guaranteed.
93  *
94  * @param string $content Some post content to search through.
95  *
96  * @internal
97  *
98  * @return \GV\Shortcode[] An array of \GV\Shortcode (and subclass) instances.
99  */
100  public static function parse( $content ) {
101  $shortcodes = array();
102 
103  /**
104  * The matches contains:
105  *
106  * 1 - An extra [ to allow for escaping shortcodes with double [[]]
107  * 2 - The shortcode name
108  * 3 - The shortcode argument list
109  * 4 - The self closing /
110  * 5 - The content of a shortcode when it wraps some content.
111  * 6 - An extra ] to allow for escaping shortcodes with double [[]]
112  */
113  preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER );
114 
115  foreach ( $matches as $shortcode ) {
116  $shortcode_name = $shortcode[2];
117 
118  $shortcode_atts = shortcode_parse_atts( $shortcode[3] );
119  $shortcode_content = $shortcode[5];
120 
121  /** This is a registered GravityView shortcode. */
122  if ( !empty( self::$shortcodes[$shortcode_name] ) ) {
123  $shortcode = clone self::$shortcodes[$shortcode_name];
124  } else {
125  /** This is some generic shortcode. */
126  $shortcode = new self;
127  $shortcode->name = $shortcode_name;
128  }
129 
130  $shortcode->atts = $shortcode_atts;
131  $shortcode->content = $shortcode_content;
132 
133  /** Merge inner shortcodes. */
134  $shortcodes = array_merge( $shortcodes, array( $shortcode ), self::parse( $shortcode_content ) );
135  }
136 
137  return $shortcodes;
138  }
139 }
static add( $name=null)
Register this shortcode class with the WordPress Shortcode API.
if(empty( $field_settings['content'])) $content
Definition: custom.php:37
If this file is called directly, abort.
callback( $atts, $content='', $tag='')
The WordPress Shortcode API callback for this shortcode.
gravityview()
The main GravityView wrapper function.
static parse( $content)
Parse a string of content and figure out which ones there are.