Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.36 KB | None | 0 0
  1. // Priority of the metabox placement.
  2. $custom_tax_mb->set( 'priority', 'low' );
  3.  
  4. // 'normal' to move it under the post content.
  5. $custom_tax_mb->set( 'context', 'normal' );
  6.  
  7. // Custom title for your metabox
  8. $custom_tax_mb->set( 'metabox_title', __( 'Custom Metabox Title', 'your-text-domain' ) );
  9.  
  10. // Makes a selection required.
  11. $custom_tax_mb->set( 'force_selection', true );
  12.  
  13. // Will keep radio elements from indenting for child-terms.
  14. $custom_tax_mb->set( 'indented', false );
  15.  
  16. // Allows adding of new terms from the metabox
  17. $custom_tax_mb->set( 'allow_new_terms', true );
  18.  
  19. <?php
  20.  
  21. if ( ! class_exists( 'Taxonomy_Single_Term' ) ) :
  22. /**
  23. * Removes and replaces the built-in taxonomy metabox with <select> or series of <input type="radio" />
  24. *
  25. * Usage:
  26. *
  27. * $custom_tax_mb = new Taxonomy_Single_Term( 'custom-tax-slug', array( 'post_type' ), 'type' ); // 'type' can be 'radio' or 'select' (default: radio)
  28. *
  29. * Update optional properties:
  30. *
  31. * $custom_tax_mb->set( 'priority', 'low' );
  32. * $custom_tax_mb->set( 'context', 'normal' );
  33. * $custom_tax_mb->set( 'metabox_title', __( 'Custom Metabox Title', 'yourtheme' ) );
  34. * $custom_tax_mb->set( 'force_selection', true );
  35. * $custom_tax_mb->set( 'indented', false );
  36. * $custom_tax_mb->set( 'allow_new_terms', true );
  37. *
  38. * @link http://codex.wordpress.org/Function_Reference/add_meta_box#Parameters
  39. * @link https://github.com/WebDevStudios/Taxonomy_Single_Term/blob/master/README.md
  40. * @version 0.2.1
  41. */
  42. class Taxonomy_Single_Term {
  43.  
  44. /**
  45. * Post types where metabox should be replaced (defaults to all post_types associated with taxonomy)
  46. * @since 0.1.0
  47. * @var array
  48. */
  49. protected $post_types = array();
  50.  
  51. /**
  52. * Taxonomy slug
  53. * @since 0.1.0
  54. * @var string
  55. */
  56. protected $slug = '';
  57.  
  58. /**
  59. * Taxonomy object
  60. * @since 0.1.0
  61. * @var object
  62. */
  63. protected $taxonomy = false;
  64.  
  65. /**
  66. * Taxonomy_Single_Term_Walker object
  67. * @since 0.1.0
  68. * @var object
  69. */
  70. protected $walker = false;
  71.  
  72. /**
  73. * New metabox title. Defaults to Taxonomy name
  74. * @since 0.1.0
  75. * @var string
  76. */
  77. protected $metabox_title = '';
  78.  
  79. /**
  80. * Metabox priority. (vertical placement)
  81. * 'high', 'core', 'default' or 'low'
  82. * @since 0.1.0
  83. * @var string
  84. */
  85. protected $priority = 'high';
  86.  
  87. /**
  88. * Metabox position. (column placement)
  89. * 'normal', 'advanced', or 'side'
  90. * @since 0.1.0
  91. * @var string
  92. */
  93. protected $context = 'side';
  94.  
  95. /**
  96. * Set to true to hide "None" option & force a term selection
  97. * @since 0.1.1
  98. * @var boolean
  99. */
  100. protected $force_selection = false;
  101.  
  102. /**
  103. * Whether hierarchical taxonomy inputs should be indented to represent hierarchy
  104. * @since 0.1.2
  105. * @var boolean
  106. */
  107. protected $indented = true;
  108.  
  109. /**
  110. * Checks if there is a bulk-edit term to set
  111. * @var boolean|term object
  112. */
  113. protected $to_set = false;
  114.  
  115. /**
  116. * Array of post ids whose terms have been reset from bulk-edit. (prevents recursion)
  117. * @var array
  118. */
  119. protected $single_term_set = array();
  120.  
  121. /**
  122. * What input element to use in the taxonomy meta box (radio or select)
  123. * @var array
  124. */
  125. protected $input_element = 'radio';
  126.  
  127. /**
  128. * Whether adding new terms via the metabox is permitted
  129. * @since 0.2.0
  130. * @var boolean
  131. */
  132. protected $allow_new_terms = false;
  133.  
  134. /**
  135. * Initiates our metabox action
  136. * @since 0.1.0
  137. * @param string $tax_slug Taxonomy slug
  138. * @param array $post_types post-types to display custom metabox
  139. */
  140. public function __construct( $tax_slug, $post_types = array(), $type = 'radio' ) {
  141.  
  142. $this->slug = $tax_slug;
  143. $this->post_types = is_array( $post_types ) ? $post_types : array( $post_types );
  144. $this->input_element = in_array( (string) $type, array( 'radio', 'select' ) ) ? $type : $this->input_element;
  145.  
  146. add_action( 'add_meta_boxes', array( $this, 'add_input_element' ) );
  147. add_action( 'admin_footer', array( $this, 'js_checkbox_transform' ) );
  148. add_action( 'wp_ajax_taxonomy_single_term_add', array( $this, 'ajax_add_term' ) );
  149.  
  150. // Handle bulk-editing
  151. if ( isset( $_REQUEST['bulk_edit'] ) && 'Update' == $_REQUEST['bulk_edit'] ) {
  152. $this->bulk_edit_handler();
  153. }
  154. }
  155.  
  156. /**
  157. * Removes and replaces the built-in taxonomy metabox with our own.
  158. * @since 0.1.0
  159. */
  160. public function add_input_element() {
  161.  
  162. // test the taxonomy slug construtor is an actual taxonomy
  163. if ( ! $this->taxonomy() ) {
  164. return;
  165. }
  166.  
  167. foreach ( $this->post_types() as $key => $cpt ) {
  168. // remove default category type metabox
  169. remove_meta_box( $this->slug . 'div', $cpt, 'side' );
  170. // remove default tag type metabox
  171. remove_meta_box( 'tagsdiv-' . $this->slug, $cpt, 'side' );
  172. // add our custom radio box
  173. add_meta_box( $this->slug . '_input_element', $this->metabox_title(), array( $this, 'input_element' ), $cpt, $this->context, $this->priority );
  174. }
  175. }
  176.  
  177. /**
  178. * Displays our taxonomy input metabox
  179. * @since 0.1.0
  180. * @todo Abstract inline javascript to it's own file and localize it
  181. */
  182. public function input_element() {
  183.  
  184. // uses same noncename as default box so no save_post hook needed
  185. wp_nonce_field( 'taxonomy_'. $this->slug, 'taxonomy_noncename' );
  186.  
  187. $class = $this->indented ? 'taxonomydiv' : 'not-indented';
  188. $class .= 'category' !== $this->slug ? ' ' . $this->slug . 'div' : '';
  189. $class .= ' tabs-panel';
  190.  
  191. $this->namefield = 'category' == $this->slug ? 'post_category' : 'tax_input[' . $this->slug . ']';
  192. $this->namefield = $this->taxonomy()->hierarchical ? $this->namefield . '[]' : $this->namefield;
  193.  
  194. $el_open_cb = $this->input_element . '_open';
  195. $el_close_cb = $this->input_element . '_close';
  196.  
  197. ?>
  198. <div id="taxonomy-<?php echo $this->slug; ?>" class="<?php echo $class; ?>">
  199. <?php $this->{$el_open_cb}() ?>
  200. <?php $this->term_fields_list(); ?>
  201. <?php $this->{$el_close_cb}() ?>
  202. <?php if ( $this->allow_new_terms ) {
  203. $this->terms_adder_button();
  204. } ?>
  205. <div style="clear:both;"></div>
  206. </div>
  207. <?php
  208. }
  209.  
  210. /**
  211. * Select wrapper open
  212. * @since 0.2.0
  213. */
  214. public function select_open() {
  215. ?>
  216. <select style="display:block;width:100%;margin-top:12px;" name="<?php echo $this->namefield; ?>" id="<?php echo $this->slug; ?>checklist" class="form-no-clear">
  217. <?php if ( ! $this->force_selection ) : ?>
  218. <option value="0"><?php echo esc_html( apply_filters( 'taxonomy_single_term_select_none', __( 'None' ) ) ); ?></option>
  219. <?php endif;
  220. }
  221.  
  222. /**
  223. * Radio wrapper open
  224. * @since 0.2.0
  225. */
  226. public function radio_open() {
  227. ?>
  228. <ul id="<?php echo $this->slug; ?>checklist" data-wp-lists="list:<?php echo $this->slug; ?>" class="categorychecklist form-no-clear">
  229. <?php if ( ! $this->force_selection ) : ?>
  230. <li style="display:none;">
  231. <input id="taxonomy-<?php echo $this->slug; ?>-clear" type="radio" name="<?php echo $this->namefield; ?>" value="0" />
  232. </li>
  233. <?php endif;
  234. }
  235.  
  236. /**
  237. * Select wrapper close
  238. * @since 0.2.0
  239. */
  240. public function select_close() {
  241. ?>
  242. </select>
  243. <?php
  244. }
  245.  
  246. /**
  247. * Radio wrapper close
  248. * @since 0.2.0
  249. */
  250. public function radio_close() {
  251. ?>
  252. </ul>
  253. <p style="margin-bottom:0;float:left;width:50%;">
  254. <a class="button" id="taxonomy-<?php echo $this->slug; ?>-trigger-clear" href="#"><?php _e( 'Clear' ); ?></a>
  255. </p>
  256. <script type="text/javascript">
  257. jQuery(document).ready(function($){
  258. $('#taxonomy-<?php echo $this->slug; ?>-trigger-clear').click(function(){
  259. $('#taxonomy-<?php echo $this->slug; ?> input:checked').prop( 'checked', false );
  260. $('#taxonomy-<?php echo $this->slug; ?>-clear').prop( 'checked', true );
  261. return false;
  262. });
  263. });
  264. </script>
  265. <?php
  266. }
  267.  
  268. /**
  269. * wp_terms_checklist wrapper which outputs the terms list
  270. * @since 0.2.0
  271. */
  272. public function term_fields_list() {
  273. wp_terms_checklist( get_the_ID(), array(
  274. 'taxonomy' => $this->slug,
  275. 'selected_cats' => false,
  276. 'popular_cats' => false,
  277. 'checked_ontop' => false,
  278. 'walker' => $this->walker(),
  279. ) );
  280. }
  281.  
  282. /**
  283. * Adds button (and associated JS) for adding new terms
  284. * @since 0.2.0
  285. */
  286. public function terms_adder_button() {
  287. ?>
  288. <p style="margin-bottom:0;float:right;width:50%;text-align:right;">
  289. <a class="button-secondary" id="taxonomy-<?php echo $this->slug; ?>-new" href="#"<?php if ( 'radio' == $this->input_element ) : ?> style="display:inline-block;margin-top:0.4em;"<?php endif; ?>><?php _e( 'Add New' ); ?></a>
  290. </p>
  291. <script type="text/javascript">
  292. jQuery(document).ready(function($){
  293. $('#taxonomy-<?php echo $this->slug; ?>-new').click(function(e){
  294. e.preventDefault();
  295.  
  296. var termName = prompt( "Add New <?php echo esc_attr( $this->taxonomy()->labels->singular_name ); ?>", "New <?php echo esc_attr( $this->taxonomy()->labels->singular_name ); ?>" );
  297.  
  298. if( ! termName ) {
  299. return;
  300. }
  301. if(termName != null) {
  302. var data = {
  303. 'action' : 'taxonomy_single_term_add',
  304. 'term_name' : termName,
  305. 'taxonomy' : '<?php echo $this->slug; ?>',
  306. 'nonce' : '<?php echo wp_create_nonce( 'taxonomy_'. $this->slug, '_add_term' ); ?>'
  307. };
  308. $.post( ajaxurl, data, function(response) {
  309. window.console.log( 'response', response );
  310. if( response.success ){
  311. <?php if ( 'radio' == $this->input_element ) : ?>
  312. $('#taxonomy-<?php echo $this->slug; ?> input:checked').prop( 'checked', false );
  313. <?php else : ?>
  314. $('#taxonomy-<?php echo $this->slug; ?> option').prop( 'selected', false );
  315. <?php endif; ?>
  316. $('#<?php echo $this->slug; ?>checklist').append( response.data );
  317. } else {
  318. window.alert( '<?php printf( __( 'There was a problem adding a new %s' ), esc_attr( $this->taxonomy()->labels->singular_name ) ); ?>: ' + "n" + response.data );
  319. }
  320. });
  321. }
  322. });
  323. });
  324. </script>
  325. <?php
  326. }
  327.  
  328. /**
  329. * AJAX callback to add terms inline
  330. * @since 0.2.0
  331. */
  332. function ajax_add_term() {
  333. $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
  334. $term_name = isset( $_POST['term_name'] ) ? sanitize_text_field( $_POST['term_name'] ) : false;
  335. $taxonomy = isset( $_POST['taxonomy'] ) ? sanitize_text_field( $_POST['taxonomy'] ) : false;
  336.  
  337. $friendly_taxonomy = $this->taxonomy()->labels->singular_name;
  338.  
  339. // Ensure user is allowed to add new terms
  340. if( !$this->allow_new_terms ) {
  341. wp_send_json_error( __( "New $friendly_taxonomy terms are not allowed" ) );
  342. }
  343.  
  344. if( !taxonomy_exists( $taxonomy ) ) {
  345. wp_send_json_error( __( "Taxonomy $friendly_taxonomy does not exist. Cannot add term" ) );
  346. }
  347.  
  348. if( !wp_verify_nonce( $nonce, 'taxonomy_' . $taxonomy, '_add_term' ) ) {
  349. wp_send_json_error( __( "Cheatin' Huh? Could not verify security token" ) );
  350. }
  351.  
  352. if( term_exists( $term_name, $taxonomy ) ) {
  353. wp_send_json_error( __( "The term '$term_name' already exists in $friendly_taxonomy" ) );
  354. }
  355.  
  356. $result = wp_insert_term( $term_name, $taxonomy );
  357.  
  358. if ( is_wp_error( $result ) ) {
  359. wp_send_json_error( $result->get_error_message() );
  360. }
  361.  
  362. $term = get_term_by( 'id', $result['term_id'], $taxonomy );
  363.  
  364. if ( ! isset( $term->term_id ) ) {
  365. wp_send_json_error();
  366. }
  367.  
  368.  
  369. $field_name = $taxonomy == 'category'
  370. ? 'post_category'
  371. : 'tax_input[' . $taxonomy . ']';
  372.  
  373. $field_name = $this->taxonomy()->hierarchical
  374. ? $field_name . '[]'
  375. : $field_name;
  376.  
  377. $args = array(
  378. 'id' => $taxonomy . '-' . $term->term_id,
  379. 'name' => $field_name,
  380. 'value' => $this->taxonomy()->hierarchical ? $term->term_id : $term->slug,
  381. 'checked' => ' checked="checked"',
  382. 'selected' => ' selected="selected"',
  383. 'disabled' => '',
  384. 'label' => esc_html( apply_filters( 'the_category', $term->name ) ),
  385. );
  386.  
  387. $output = '';
  388. $output .= 'radio' == $this->input_element
  389. ? $this->walker()->start_el_radio( $args )
  390. : $this->walker()->start_el_select( $args );
  391.  
  392. // $output is handled by reference
  393. $this->walker()->end_el( $output, $term );
  394.  
  395. wp_send_json_success( $output );
  396.  
  397. }
  398.  
  399. /**
  400. * Add some JS to the post listing page to transform the quickedit inputs
  401. * @since 0.1.3
  402. */
  403. public function js_checkbox_transform() {
  404. $screen = get_current_screen();
  405. $taxonomy = $this->taxonomy();
  406.  
  407. if (
  408. empty( $taxonomy ) || empty( $screen )
  409. || ! isset( $taxonomy->object_type )
  410. || ! isset( $screen->post_type )
  411. || ! in_array( $screen->post_type, $taxonomy->object_type )
  412. )
  413. return;
  414.  
  415. ?>
  416. <script type="text/javascript">
  417. // Handles changing input types to radios for WDS_Taxonomy_Radio
  418. jQuery(document).ready(function($){
  419. var $postsFilter = $('#posts-filter');
  420. var $theList = $postsFilter.find('#the-list');
  421.  
  422. // Handles changing the input type attributes
  423. var changeToRadio = function( $context ) {
  424. $context = $context ? $context : $theList;
  425. var $taxListInputs = $context.find( '.<?php echo $this->slug; ?>-checklist li input' );
  426. if ( $taxListInputs.length ) {
  427. // loop and switch input types
  428. $taxListInputs.each( function() {
  429. $(this).attr( 'type', 'radio' ).addClass('transformed-to-radio');
  430. });
  431. }
  432. };
  433.  
  434. $postsFilter
  435. // Handle converting radios in bulk-edit row
  436. .on( 'click', '#doaction, #doaction2', function(){
  437. var name = $(this).attr('id').substr(2);
  438. if ( 'edit' === $( 'select[name="' + name + '"]' ).val() ) {
  439. setTimeout( function() {
  440. changeToRadio( $theList.find('#bulk-edit') );
  441. }, 50 );
  442. }
  443. })
  444. // when clicking new radio inputs, be sure to uncheck all but the one clicked
  445. .on( 'change', '.transformed-to-radio', function() {
  446. var $this = $(this);
  447. $siblings = $this.parents( '.<?php echo $this->slug; ?>-checklist' ).find( 'li .transformed-to-radio' ).prop( 'checked', false );
  448. $this.prop( 'checked', true );
  449. });
  450.  
  451. // Handle converting radios in inline-edit rows
  452. $theList.find('.editinline').on( 'click', function() {
  453. var $this = $(this);
  454. setTimeout( function() {
  455. var $editRow = $this.parents( 'tr' ).next().next();
  456. changeToRadio( $editRow );
  457. }, 50 );
  458. });
  459.  
  460. });
  461. </script>
  462. <?php
  463. }
  464.  
  465. /**
  466. * Handles checking if object terms need to be set when bulk-editing posts
  467. * @since 0.2.1
  468. */
  469. public function bulk_edit_handler() {
  470. // Get wp tax name designation
  471. $name = $this->slug;
  472.  
  473. if ( 'category' == $name ) {
  474. $name = 'post_category';
  475. }
  476.  
  477. if ( 'tag' == $name ) {
  478. $name = 'post_tag';
  479. }
  480.  
  481. // If this tax name exists in the query arg
  482. if ( isset( $_REQUEST[ $name ] ) && is_array( $_REQUEST[ $name ] ) ) {
  483. $this->to_set = end( $_REQUEST[ $name ] );
  484. } elseif ( isset( $_REQUEST['tax_input'][ $name ] ) && is_array( $_REQUEST['tax_input'][ $name ] ) ) {
  485. $this->to_set = end( $_REQUEST['tax_input'][ $name ] );
  486. }
  487.  
  488. // Then get it's term object
  489. if ( $this->to_set ) {
  490. $this->to_set = get_term( $this->to_set, $this->slug );
  491. // And hook in our re-save action
  492. add_action( 'set_object_terms', array( $this, 'maybe_resave_terms' ), 10, 5 );
  493. }
  494. }
  495.  
  496. /**
  497. * Handles resaving terms to post when bulk-editing so that only one term will be applied
  498. * @since 0.1.4
  499. * @param int $object_id Object ID.
  500. * @param array $terms An array of object terms.
  501. * @param array $tt_ids An array of term taxonomy IDs.
  502. * @param string $taxonomy Taxonomy slug.
  503. * @param bool $append Whether to append new terms to the old terms.
  504. * @param array $old_tt_ids Old array of term taxonomy IDs.
  505. */
  506. public function maybe_resave_terms( $object_id, $terms, $tt_ids, $taxonomy, $append ) {
  507. if (
  508. // if the terms being edited are not this taxonomy
  509. $taxonomy != $this->slug
  510. // or we already did our magic
  511. || in_array( $object_id, $this->single_term_set, true )
  512. ) {
  513. // Then bail
  514. return;
  515. }
  516.  
  517. // Prevent recursion
  518. $this->single_term_set[] = $object_id;
  519. // Replace terms with the one term
  520. wp_set_object_terms( $object_id, $this->to_set->slug, $taxonomy, $append );
  521. }
  522.  
  523. /**
  524. * Gets the taxonomy object from the slug
  525. * @since 0.1.0
  526. * @return object Taxonomy object
  527. */
  528. public function taxonomy() {
  529. $this->taxonomy = $this->taxonomy ? $this->taxonomy : get_taxonomy( $this->slug );
  530. return $this->taxonomy;
  531. }
  532.  
  533. /**
  534. * Gets the taxonomy's associated post_types
  535. * @since 0.1.0
  536. * @return array Taxonomy's associated post_types
  537. */
  538. public function post_types() {
  539. $this->post_types = !empty( $this->post_types ) ? $this->post_types : $this->taxonomy()->object_type;
  540. return $this->post_types;
  541. }
  542.  
  543. /**
  544. * Gets the metabox title from the taxonomy object's labels (or uses the passed in title)
  545. * @since 0.1.0
  546. * @return string Metabox title
  547. */
  548. public function metabox_title() {
  549. $this->metabox_title = !empty( $this->metabox_title ) ? $this->metabox_title : $this->taxonomy()->labels->name;
  550. return $this->metabox_title;
  551. }
  552.  
  553. /**
  554. * Gets the Taxonomy_Single_Term_Walker object for use in term_fields_list and ajax_add_term
  555. * @since 0.2.0
  556. * @return object Taxonomy_Single_Term_Walker object
  557. */
  558. public function walker() {
  559. if ( $this->walker ) {
  560. return $this->walker;
  561. }
  562. require_once( 'walker.taxonomy-single-term.php' );
  563. $this->walker = new Taxonomy_Single_Term_Walker( $this->taxonomy()->hierarchical, $this->input_element );
  564.  
  565. return $this->walker;
  566. }
  567.  
  568. /**
  569. * Set the object properties.
  570. *
  571. * @since 0.2.1
  572. *
  573. * @param string $property Property in object. Must be set in object.
  574. * @param mixed $value Value of property.
  575. *
  576. * @return Taxonomy_Single_Term Returns Taxonomy_Single_Term object, allows for chaining.
  577. */
  578. public function set( $property, $value ) {
  579.  
  580. if ( property_exists( $this, $property ) ) {
  581. $this->$property = $value;
  582. }
  583.  
  584. return $this;
  585. }
  586.  
  587. /**
  588. * Magic getter for our object.
  589. *
  590. * @since 0.2.1
  591. *
  592. * @param string Property in object to retrieve.
  593. * @throws Exception Throws an exception if the field is invalid.
  594. *
  595. * @return mixed Property requested.
  596. */
  597. public function __get( $property ) {
  598. if ( property_exists( $this, $value ) ) {
  599. return $this->{$property};
  600. } else {
  601. throw new Exception( 'Invalid '. __CLASS__ .' property: ' . $field );
  602. }
  603. }
  604.  
  605. }
  606.  
  607. endif; // class_exists check
  608.  
  609. <?php
  610.  
  611. if ( ! class_exists( 'Taxonomy_Single_Term_Walker' ) && class_exists( 'Walker' ) ) :
  612.  
  613. /**
  614. * Walker to output an unordered list of taxonomy radio <input> elements.
  615. *
  616. * @see Walker
  617. * @see wp_category_checklist()
  618. * @see wp_terms_checklist()
  619. * @since 0.1.2
  620. */
  621. class Taxonomy_Single_Term_Walker extends Walker {
  622. public $tree_type = 'category';
  623. public $db_fields = array( 'parent' => 'parent', 'id' => 'term_id' ); //TODO: decouple this
  624.  
  625. public function __construct( $hierarchical, $input_element ) {
  626. $this->hierarchical = $hierarchical;
  627. $this->input_element = $input_element;
  628. }
  629.  
  630. /**
  631. * Starts the list before the elements are added.
  632. *
  633. * @see Walker:start_lvl()
  634. *
  635. * @since 0.1.2
  636. *
  637. * @param string $output Passed by reference. Used to append additional content.
  638. * @param int $depth Depth of category. Used for tab indentation.
  639. * @param array $args An array of arguments. @see wp_terms_checklist()
  640. */
  641. public function start_lvl( &$output, $depth = 0, $args = array() ) {
  642. if ( 'radio' == $this->input_element ) {
  643. $indent = str_repeat("t", $depth);
  644. $output .= "$indent<ul class='children'>n";
  645. }
  646. }
  647.  
  648. /**
  649. * Ends the list of after the elements are added.
  650. *
  651. * @see Walker::end_lvl()
  652. *
  653. * @since 0.1.2
  654. *
  655. * @param string $output Passed by reference. Used to append additional content.
  656. * @param int $depth Depth of category. Used for tab indentation.
  657. * @param array $args An array of arguments. @see wp_terms_checklist()
  658. */
  659. public function end_lvl( &$output, $depth = 0, $args = array() ) {
  660. if ( 'radio' == $this->input_element ) {
  661. $indent = str_repeat("t", $depth);
  662. $output .= "$indent</ul>n";
  663. }
  664. }
  665.  
  666. /**
  667. * Start the element output.
  668. *
  669. * @see Walker::start_el()
  670. *
  671. * @since 0.1.2
  672. *
  673. * @param string $output Passed by reference. Used to append additional content.
  674. * @param object $term The current term object.
  675. * @param int $depth Depth of the term in reference to parents. Default 0.
  676. * @param array $args An array of arguments. @see wp_terms_checklist()
  677. * @param int $id ID of the current term.
  678. */
  679. public function start_el( &$output, $term, $depth = 0, $args = array(), $id = 0 ) {
  680.  
  681. $taxonomy = empty( $args['taxonomy'] ) ? 'category' : $args['taxonomy'];
  682. $name = $taxonomy == 'category' ? 'post_category' : 'tax_input['.$taxonomy.']';
  683. // input name
  684. $name = $this->hierarchical ? $name .'[]' : $name;
  685. // input value
  686. $value = $this->hierarchical ? $term->term_id : $term->slug;
  687.  
  688. $selected_cats = empty( $args['selected_cats'] ) ? array() : $args['selected_cats'];
  689. $in_selected = in_array( $term->term_id, $selected_cats );
  690.  
  691. $args = array(
  692. 'id' => $taxonomy .'-'. $term->term_id,
  693. 'name' => $name,
  694. 'value' => $value,
  695. 'checked' => checked( $in_selected, true, false ),
  696. 'selected' => selected( $in_selected, true, false ),
  697. 'disabled' => disabled( empty( $args['disabled'] ), false, false ),
  698. 'label' => esc_html( apply_filters('the_category', $term->name ) )
  699. );
  700.  
  701. $output .= 'radio' == $this->input_element
  702. ? $this->start_el_radio( $args )
  703. : $this->start_el_select( $args );
  704. }
  705.  
  706. /**
  707. * Creates the opening markup for the radio input
  708. *
  709. * @since 0.2.0
  710. *
  711. * @param array $args Array of arguments for creating the element
  712. *
  713. * @return string Opening li element and radio input
  714. */
  715. public function start_el_radio( $args ) {
  716. return "n".sprintf(
  717. '<li id="%s"><label class="selectit"><input value="%s" type="radio" name="%s" id="in-%s" %s %s/>%s</label>',
  718. $args['id'],
  719. $args['value'],
  720. $args['name'],
  721. $args['id'],
  722. $args['checked'],
  723. $args['disabled'],
  724. $args['label']
  725. );
  726. }
  727.  
  728. /**
  729. * Creates the opening markup for the select input
  730. *
  731. * @since 0.2.0
  732. *
  733. * @param array $args Array of arguments for creating the element
  734. *
  735. * @return string Opening option element and option text
  736. */
  737. public function start_el_select( $args ) {
  738. return "n".sprintf(
  739. '<option %s %s id="%s" value="%s" class="class-single-term">%s',
  740. $args['selected'],
  741. $args['disabled'],
  742. $args['id'],
  743. $args['value'],
  744. $args['label']
  745. );
  746. }
  747.  
  748. /**
  749. * Ends the element output, if needed.
  750. *
  751. * @see Walker::end_el()
  752. *
  753. * @since 0.1.2
  754. *
  755. * @param string $output Passed by reference. Used to append additional content.
  756. * @param object $term The current term object.
  757. * @param int $depth Depth of the term in reference to parents. Default 0.
  758. * @param array $args An array of arguments. @see wp_terms_checklist()
  759. */
  760. public function end_el( &$output, $term, $depth = 0, $args = array() ) {
  761. if ( 'radio' == $this->input_element ) {
  762. $output .= "</li>n";
  763. } else {
  764. $output .= "</option>n";
  765. }
  766. }
  767.  
  768. }
  769.  
  770. endif; // class_exists check
  771.  
  772. var $editRow = $this.parents( 'tr' ).next().next();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement