cptui_admin_ui

Custom Post Type UI Admin UI.

Defined (1)

The class is defined in the following location(s).

/classes/class.cptui_admin_ui.php  
  1. class cptui_admin_ui { 
  2.  
  3. /** 
  4. * Return an opening `<tr>` tag. 
  5. * @since 1.0.0 
  6. * @return string $value Opening `<tr>` tag with attributes. 
  7. */ 
  8. public function get_tr_start() { 
  9. return '<tr valign="top">'; 
  10.  
  11. /** 
  12. * Return a closing `</tr>` tag. 
  13. * @since 1.0.0 
  14. * @return string $value Closing `</tr>` tag. 
  15. */ 
  16. public function get_tr_end() { 
  17. return '</tr>'; 
  18.  
  19. /** 
  20. * Return an opening `<th>` tag. 
  21. * @since 1.0.0 
  22. * @return string $value Opening `<th>` tag with attributes. 
  23. */ 
  24. public function get_th_start() { 
  25. return '<th scope="row">'; 
  26.  
  27. /** 
  28. * Return a closing `</th>` tag. 
  29. * @since 1.0.0 
  30. * @return string $value Closing `</th>` tag. 
  31. */ 
  32. public function get_th_end() { 
  33. return '</th>'; 
  34.  
  35. /** 
  36. * Return an opening `<td>` tag. 
  37. * @since 1.0.0 
  38. * @return string $value Opening `<td>` tag. 
  39. */ 
  40. public function get_td_start() { 
  41. return '<td>'; 
  42.  
  43. /** 
  44. * Return a closing `</td>` tag. 
  45. * @since 1.0.0 
  46. * @return string $value Closing `</td>` tag. 
  47. */ 
  48. public function get_td_end() { 
  49. return '</td>'; 
  50.  
  51. /** 
  52. * Return an opening `<fieldset>` tag. 
  53. * @since 1.2.0 
  54. * @since 1.3.0 Added $args parameter. 
  55. * @param array $args Array of arguments. 
  56. * @return string $value Opening `<fieldset>` tag. 
  57. */ 
  58. public function get_fieldset_start( $args = array() ) { 
  59. $fieldset = '<fieldset'; 
  60.  
  61. if ( ! empty( $args['id'] ) ) { 
  62. $fieldset .= ' id="' . esc_attr( $args['id'] ) . '"'; 
  63.  
  64. if ( ! empty( $args['classes'] ) ) { 
  65. $classes = 'class="' . implode( ' ', $args['classes'] ) . '"'; 
  66. $fieldset .= ' ' . $classes; 
  67.  
  68. if ( ! empty( $args['aria-expanded'] ) ) { 
  69. $fieldset .= ' aria-expanded="' . $args['aria-expanded'] . '"'; 
  70.  
  71. $fieldset .= ' tabindex="0">'; 
  72.  
  73. return $fieldset; 
  74.  
  75. /** 
  76. * Return an closing `<fieldset>` tag. 
  77. * @since 1.2.0 
  78. * @return string $value Closing `<fieldset>` tag. 
  79. */ 
  80. public function get_fieldset_end() { 
  81. return '</fieldset>'; 
  82.  
  83. /** 
  84. * Return an opening `<legend>` tag. 
  85. * @since 1.3.0 
  86. * @return string 
  87. */ 
  88. public function get_legend_start() { 
  89. return '<legend>'; 
  90.  
  91. /** 
  92. * Return a closing `</legend>` tag. 
  93. * @since 1.3.0 
  94. * @return string 
  95. */ 
  96. public function get_legend_end() { 
  97. return '</legend>'; 
  98.  
  99. /** 
  100. * Return string wrapped in a `<p>` tag. 
  101. * @since 1.0.0 
  102. * @param string $text Content to wrap in a `<p>` tag. 
  103. * @return string $value Content wrapped in a `<p>` tag. 
  104. */ 
  105. public function get_p( $text = '' ) { 
  106. return '<p>' . $text . '</p>'; 
  107.  
  108. /** 
  109. * Return a form <label> with for attribute. 
  110. * @since 1.0.0 
  111. * @param string $label_for Form input to associate `<label>` with. 
  112. * @param string $label_text Text to display in the `<label>` tag. 
  113. * @return string $value `<label>` tag with filled out parts. 
  114. */ 
  115. public function get_label( $label_for = '', $label_text = '' ) { 
  116. return '<label for="' . esc_attr( $label_for ) . '">' . strip_tags( $label_text ) . '</label>'; 
  117.  
  118. /** 
  119. * Return an html attribute denoting a required field. 
  120. * @since 1.3.0 
  121. * @param bool $required Whether or not the field is required. 
  122. * @return string `Required` attribute. 
  123. */ 
  124. public function get_required_attribute( $required = false ) { 
  125. $attr = ''; 
  126. if ( $required ) { 
  127. $attr .= 'required="true"'; 
  128. return $attr; 
  129.  
  130. /** 
  131. * Return a `<span>` to indicate required status, with class attribute. 
  132. * @since 1.0.0 
  133. * @return string Span tag. 
  134. */ 
  135. public function get_required_span() { 
  136. return ' <span class="required">*</span>'; 
  137.  
  138. /** 
  139. * Return an aria-required attribute set to true. 
  140. * @since 1.3.0 
  141. * @param bool $required Whether or not the field is required. 
  142. * @return string Aria required attribute 
  143. */ 
  144. public function get_aria_required( $required = false ) { 
  145. $attr = ( $required ) ? 'true' : 'false'; 
  146. return 'aria-required="' . $attr . '"'; 
  147.  
  148. /** 
  149. * Return an `<a>` tag with title attribute holding help text. 
  150. * @since 1.0.0 
  151. * @param string $help_text Text to use in the title attribute. 
  152. * @return string <a> tag with filled out parts. 
  153. */ 
  154. public function get_help( $help_text = '' ) { 
  155. return '<a href="#" class="cptui-help dashicons-before dashicons-editor-help" title="' . esc_attr( $help_text ) . '"></a>'; 
  156.  
  157. /** 
  158. * Return a `<span>` tag with the help text. 
  159. * @since 1.3.0 
  160. * @param string $help_text Text to display after the input. 
  161. * @return string 
  162. */ 
  163. public function get_description( $help_text = '' ) { 
  164. return '<span class="cptui-field-description">' . $help_text . '</span>'; 
  165.  
  166. /** 
  167. * Return a maxlength HTML attribute with a specified length. 
  168. * @since 1.0.0 
  169. * @param string $length How many characters the max length should be set to. 
  170. * @return string $value Maxlength HTML attribute. 
  171. */ 
  172. public function get_maxlength( $length = '' ) { 
  173. return 'maxlength="' . esc_attr( $length ) . '"'; 
  174.  
  175. /** 
  176. * Return a onblur HTML attribute for a specified value. 
  177. * @since 1.0.0 
  178. * @param string $text Text to place in the onblur attribute. 
  179. * @return string $value Onblur HTML attribute. 
  180. */ 
  181. public function get_onblur( $text = '' ) { 
  182. return 'onblur="' . esc_attr( $text ) . '"'; 
  183.  
  184. /** 
  185. * Return a placeholder HTML attribtue for a specified value. 
  186. * @since 1.3.0 
  187. * @param string $text Text to place in the placeholder attribute. 
  188. * @return string $value Placeholder HTML attribute. 
  189. */ 
  190. public function get_placeholder( $text = '' ) { 
  191. return 'placeholder="' . esc_attr( $text ) . '"'; 
  192.  
  193. /** 
  194. * Return a span that will only be visible for screenreaders. 
  195. * @since 1.3.0 
  196. * @param string $text Text to visually hide. 
  197. * @return string $value Visually hidden text meant for screen readers. 
  198. */ 
  199. public function get_hidden_text( $text = '' ) { 
  200. return '<span class="visuallyhidden">' . $text . '</span>'; 
  201.  
  202. /** 
  203. * Return a populated `<select>` input. 
  204. * @since 1.0.0 
  205. * @param array $args Arguments to use with the `<select>` input. 
  206. * @return string $value Complete <select> input with options and selected attribute. 
  207. */ 
  208. public function get_select_input( $args = array() ) { 
  209. $defaults = $this->get_default_input_parameters( 
  210. array( 'selections' => array() ) 
  211. ); 
  212.  
  213. $args = wp_parse_args( $args, $defaults ); 
  214.  
  215. $value = ''; 
  216. if ( $args['wrap'] ) { 
  217. $value = $this->get_tr_start(); 
  218. $value .= $this->get_th_start(); 
  219. $value .= $this->get_label( $args['name'], $args['labeltext'] ); 
  220. if ( $args['required'] ) { $value .= $this->get_required_span(); } 
  221. if ( ! empty( $args['helptext'] ) ) { $value .= $this->get_help( $args['helptext'] ); } 
  222. $value .= $this->get_th_end(); 
  223. $value .= $this->get_td_start(); 
  224.  
  225. $value .= '<select id="' . $args['name'] . '" name="' . $args['namearray'] . '[' . $args['name'] . ']">'; 
  226. if ( ! empty( $args['selections']['options'] ) && is_array( $args['selections']['options'] ) ) { 
  227. foreach ( $args['selections']['options'] as $val ) { 
  228. $result = ''; 
  229. $bool = disp_boolean( $val['attr'] ); 
  230.  
  231. if ( is_numeric( $args['selections']['selected'] ) ) { 
  232. $selected = disp_boolean( $args['selections']['selected'] ); 
  233. } elseif ( in_array( $args['selections']['selected'], array( 'true', 'false' ) ) ) { 
  234. $selected = $args['selections']['selected']; 
  235.  
  236. if ( ( ! empty( $selected ) ) && $selected === $bool ) { 
  237. $result = ' selected="selected"'; 
  238. } else { 
  239. if ( array_key_exists( 'default', $val ) && ! empty( $val['default'] ) ) { 
  240. if ( empty( $selected ) ) { 
  241. $result = ' selected="selected"'; 
  242.  
  243. if ( ! is_numeric( $args['selections']['selected'] ) && ( ! empty( $args['selections']['selected'] ) && $args['selections']['selected'] === $val['attr'] ) ) { 
  244. $result = ' selected="selected"'; 
  245.  
  246. $value .= '<option value="' . $val['attr'] . '"' . $result . '>' . $val['text'] . '</option>'; 
  247. $value .= '</select>'; 
  248.  
  249. if ( ! empty( $args['aftertext'] ) ) { 
  250. $value .= ' ' . $this->get_description( $args['aftertext'] ); 
  251.  
  252. if ( $args['wrap'] ) { 
  253. $value .= $this->get_td_end(); 
  254. $value .= $this->get_tr_end(); 
  255.  
  256. return $value; 
  257.  
  258. /** 
  259. * Return a text input. 
  260. * @since 1.0.0 
  261. * @param array $args Arguments to use with the text input. 
  262. * @return string Complete text `<input>` with proper attributes. 
  263. */ 
  264. public function get_text_input( $args = array() ) { 
  265. $defaults = $this->get_default_input_parameters( 
  266. array( 
  267. 'maxlength' => '',  
  268. 'onblur' => '',  
  269. ); 
  270. $args = wp_parse_args( $args, $defaults ); 
  271.  
  272. $value = ''; 
  273. if ( $args['wrap'] ) { 
  274. $value .= $this->get_tr_start(); 
  275. $value .= $this->get_th_start(); 
  276. $value .= $this->get_label( $args['name'], $args['labeltext'] ); 
  277. if ( $args['required'] ) { $value .= $this->get_required_span(); } 
  278. $value .= $this->get_th_end(); 
  279. $value .= $this->get_td_start(); 
  280.  
  281. $value .= '<input type="text" id="' . $args['name'] . '" name="' . $args['namearray'] . '[' . $args['name'] . ']" value="' . $args['textvalue'] . '"'; 
  282.  
  283. if ( $args['maxlength'] ) { 
  284. $value .= ' ' . $this->get_maxlength( $args['maxlength'] ); 
  285.  
  286. if ( $args['onblur'] ) { 
  287. $value .= ' ' . $this->get_onblur( $args['onblur'] ); 
  288.  
  289. $value .= ' ' . $this->get_aria_required( $args['required'] ); 
  290.  
  291. $value .= ' ' . $this->get_required_attribute( $args['required'] ); 
  292.  
  293. if ( ! empty( $args['aftertext'] ) ) { 
  294. if ( $args['placeholder'] ) { 
  295. $value .= ' ' . $this->get_placeholder( $args['aftertext'] ); 
  296.  
  297. $value .= ' />'; 
  298.  
  299. if ( ! empty( $args['aftertext'] ) ) { 
  300. $value .= $this->get_hidden_text( $args['aftertext'] ); 
  301.  
  302. if ( $args['helptext'] ) { 
  303. $value .= '<br/>' . $this->get_description( $args['helptext'] ); 
  304.  
  305. if ( $args['wrap'] ) { 
  306. $value .= $this->get_td_end(); 
  307. $value .= $this->get_tr_end(); 
  308.  
  309. return $value; 
  310.  
  311. /** 
  312. * Return a `<textarea>` input. 
  313. * @since 1.0.0 
  314. * @param array $args Arguments to use with the textarea input. 
  315. * @return string $value Complete <textarea> input with proper attributes. 
  316. */ 
  317. public function get_textarea_input( $args = array() ) { 
  318. $defaults = $this->get_default_input_parameters( 
  319. array( 
  320. 'rows' => '',  
  321. 'cols' => '',  
  322. ); 
  323. $args = wp_parse_args( $args, $defaults ); 
  324.  
  325. $value = ''; 
  326.  
  327. if ( $args['wrap'] ) { 
  328. $value .= $this->get_tr_start(); 
  329. $value .= $this->get_th_start(); 
  330. $value .= $this->get_label( $args['name'], $args['labeltext'] ); 
  331. if ( $args['required'] ) { $value .= $this->get_required_span(); } 
  332. $value .= $this->get_th_end(); 
  333. $value .= $this->get_td_start(); 
  334.  
  335. $value .= '<textarea id="' . $args['name'] . '" name="' . $args['namearray'] . '[' . $args['name'] . ']" rows="' . $args['rows'] . '" cols="' . $args['cols'] . '">' . $args['textvalue'] . '</textarea>'; 
  336.  
  337. if ( ! empty( $args['aftertext'] ) ) { 
  338. $value .= $args['aftertext']; 
  339.  
  340. if ( $args['helptext'] ) { 
  341. $value .= '<br/>' . $this->get_description( $args['helptext'] ); 
  342.  
  343. if ( $args['wrap'] ) { 
  344. $value .= $this->get_td_end(); 
  345. $value .= $this->get_tr_end(); 
  346.  
  347. return $value; 
  348.  
  349. /** 
  350. * Return a checkbox `<input>`. 
  351. * @since 1.0.0 
  352. * @param array $args Arguments to use with the checkbox input. 
  353. * @return string $value Complete checkbox `<input>` with proper attributes. 
  354. */ 
  355. public function get_check_input( $args = array() ) { 
  356. $defaults = $this->get_default_input_parameters( 
  357. array( 
  358. 'checkvalue' => '',  
  359. 'checked' => 'true',  
  360. 'checklisttext' => '',  
  361. 'default' => false,  
  362. ); 
  363.  
  364. $args = wp_parse_args( $args, $defaults ); 
  365.  
  366. $value = ''; 
  367. if ( $args['wrap'] ) { 
  368. $value .= $this->get_tr_start(); 
  369. $value .= $this->get_th_start(); 
  370. $value .= $args['checklisttext']; 
  371. if ( $args['required'] ) { $value .= $this->get_required_span(); } 
  372. $value .= $this->get_th_end(); 
  373. $value .= $this->get_td_start(); 
  374.  
  375. if ( isset( $args['checked'] ) && 'false' === $args['checked'] ) { 
  376. $value .= '<input type="checkbox" id="' . $args['name'] . '" name="' . $args['namearray'] . '[]" value="' . $args['checkvalue'] . '" />'; 
  377. } else { 
  378. $value .= '<input type="checkbox" id="' . $args['name'] . '" name="' . $args['namearray'] . '[]" value="' . $args['checkvalue'] . '" checked="checked" />'; 
  379. $value .= $this->get_label( $args['name'], $args['labeltext'] ); 
  380. $value .= '<br/>'; 
  381.  
  382. if ( $args['wrap'] ) { 
  383. $value .= $this->get_td_end(); 
  384. $value .= $this->get_tr_end(); 
  385.  
  386. return $value; 
  387.  
  388. /** 
  389. * Return a button `<input>`. 
  390. * @since 1.3.0 
  391. * @param array $args Arguments to use with the button input. 
  392. * @return string Complete button `<input>`. 
  393. */ 
  394. public function get_button( $args = array() ) { 
  395. $value = ''; 
  396. $value .= '<input id="' . $args['id'] . '" class="button" type="button" value="' . $args['textvalue'] . '" />'; 
  397.  
  398. return $value; 
  399.  
  400. /** 
  401. * Return some array_merged default arguments for all input types. 
  402. * @since 1.0.0 
  403. * @param array $additions Arguments array to merge with our defaults. 
  404. * @return array $value Merged arrays for our default parameters. 
  405. */ 
  406. public function get_default_input_parameters( $additions = array() ) { 
  407. return array_merge( 
  408. array( 
  409. 'namearray' => '',  
  410. 'name' => '',  
  411. 'textvalue' => '',  
  412. 'labeltext' => '',  
  413. 'aftertext' => '',  
  414. 'helptext' => '',  
  415. 'helptext_after' => false,  
  416. 'required' => false,  
  417. 'wrap' => true,  
  418. 'placeholder' => true,  
  419. ),  
  420. (array) $additions 
  421. );