GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-metabox-tab.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * The class for a metabox tab in the GravityView View Settings metabox
5  *
6  * @see https://gist.github.com/zackkatz/6cc381bcf54849f2ed41 For example
7  *
8  * @since 1.8
9  */
11 
12  /**
13  * String prepended to the $id when registering the metabox
14  * @since 1.8
15  * @var string
16  */
17  private $prefix = 'gravityview_';
18 
19  /**
20  * String for use in the 'id' attribute of tags.
21  * @since 1.8
22  * @param string
23  */
24  public $id = '';
25 
26  /**
27  * Title of the meta box.
28  * @since 1.8
29  * @param string
30  */
31  public $title = '';
32 
33  /**
34  * Function that fills the box with the desired content. The function should echo its output.
35  * @since 1.8
36  * @param callback
37  */
38  protected $callback = '';
39 
40  /**
41  * Optional. The screen on which to show the box (like a post type, 'link', or 'comment'). Default is the current screen.
42  *
43  * @since 1.8
44  * @param string|WP_Screen
45  */
46  protected $screen = '';
47 
48  /**
49  * Optional. The context within the screen where the boxes should display.
50  * Available contexts vary from screen to screen. Post edit screen contexts include 'normal', 'side', and 'advanced'.
51  * Comments screen contexts include 'normal' and 'side'.
52  * Menus meta boxes (accordion sections) all use the 'side' context. Global default is 'advanced'.
53  *
54  * @since 1.8
55  * @param string
56  */
57  protected $context = 'advanced';
58 
59  /**
60  * Optional. The priority within the context where the boxes should show ('high', 'low'). Default 'default'.
61  * @since 1.8
62  * @param string
63  */
64  protected $priority = '';
65 
66  /**
67  * Optional. Data that should be set as the $args property of the box array (which is the second parameter passed to your callback). Default null.
68  * @since 1.8
69  * @param array
70  */
71  protected $callback_args = array();
72 
73  /**
74  * Define a file stored in the partials directory to render the output
75  * @since 1.8
76  * @var string
77  */
78  protected $render_template_file = '';
79 
80  /**
81  * CSS class for the tab icon
82  * @since 1.8
83  * @var string
84  */
85  public $icon_class_name = '';
86 
87 
88  /**
89  * Create a new metabox tab
90  *
91  * @since 1.8
92  * @param $id string Metabox HTML ID, without `gravityview_` prefix
93  * @param string $title Name of the metabox. Shown in the tab.
94  * @param string $file The file name of a file stored in the /gravityview/includes/admin/metaboxes/views/ directory to render the metabox output, or the full path to a file. If defined, `callback` is not used.
95  * @param string $icon_class_name Icon class used in vertical tabs. Supports non-dashicon. If dashicons, no need for `dashicons ` prefix
96  * @param string $callback Function to render the metabox, if $file is not defined.
97  * @param array $callback_args Arguments passed to the callback
98  * @return void
99  */
100  function __construct( $id, $title = '', $file = '', $icon_class_name = '', $callback = '', $callback_args = array() ) {
101 
102  $this->id = $this->prefix.$id;
103  $this->title = $title;
104  $this->render_template_file = $file;
105  $this->callback = $callback;
106  $this->callback_args = $callback_args;
107  $this->icon_class_name = $this->parse_icon_class_name( $icon_class_name );
108  }
109 
110  /**
111  * If the name of the CSS class has dashicon in it, add the `dashicons` prefix
112  *
113  * @since 1.8
114  *
115  * @param string $icon_class_name Passed class name
116  *
117  * @return string sanitized CSS class
118  */
120 
121  if( preg_match( '/dashicon/i', $icon_class_name ) ) {
122  $icon_class_name = 'dashicons ' . $icon_class_name;
123  }
124 
125  return esc_attr( $icon_class_name );
126  }
127 
128  /**
129  * Render the tab
130  *
131  * If the $file parameter was passed when registering a new GravityView_Metabox_Tab,
132  * then the file is included.
133  *
134  * The include checks for a full file path first, and if a file exists, use that file.
135  * If the file doesn't exist, then the code looks inside the [gravityview]/includes/admin/metaboxes/views/ dir.
136  *
137  * Finally, if there's no file specified, but there's a $callback parameter specified, use the callback.
138  *
139  * @since 1.8
140  *
141  * @param WP_Post $post Currently edited post object
142  */
143  function render( $post ) {
144 
145  if( !empty( $this->render_template_file ) ) {
146 
148 
149  // If the full path exists, use it
150  if( file_exists( $file ) ) {
151  $path = $file;
152  } else {
153  $path = GRAVITYVIEW_DIR .'includes/admin/metaboxes/views/'.$file;
154  }
155 
156  if( file_exists( $path ) ) {
157  include $path;
158  } else {
159  gravityview()->log->error( 'Metabox template file not found', array( 'data' => $this ) );
160  }
161 
162  } else if( !empty( $this->callback ) ) {
163 
164  if( is_callable( $this->callback ) ) {
165 
166  /** @see do_accordion_sections() */
167  call_user_func( $this->callback, $post, (array) $this );
168 
169  } else {
170  gravityview()->log->error( 'Metabox callback was not callable', array( 'data' => $this ) );
171  }
172 
173  } else {
174  gravityview()->log->error( 'Metabox file and callback were not found', array( 'data' => $this ) );
175  }
176  }
177 
178 }
const GRAVITYVIEW_DIR
"GRAVITYVIEW_DIR" "./" The absolute path to the plugin directory, with trailing slash ...
Definition: gravityview.php:49
__construct( $id, $title='', $file='', $icon_class_name='', $callback='', $callback_args=array())
Create a new metabox tab.
global $post
Definition: delete-entry.php:7
$id
String for use in the &#39;id&#39; attribute of tags.
$callback
Function that fills the box with the desired content.
The class for a metabox tab in the GravityView View Settings metabox.
gravityview()
The main GravityView wrapper function.
parse_icon_class_name( $icon_class_name='')
If the name of the CSS class has dashicon in it, add the dashicons prefix.