Last active
October 4, 2016 15:49
-
-
Save mboynes/bf7811a2958bb67cdde8 to your computer and use it in GitHub Desktop.
Use fieldmanager to create meta boxes on a subpage from a term
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( class_exists( 'Fieldmanager_Context_Term' ) ) { | |
/** | |
* Use fieldmanager to create meta boxes on a subpage from a term | |
*/ | |
class Fieldmanager_Context_Termpage extends Fieldmanager_Context_Term { | |
/** | |
* @var string|null $submit_button_label | |
*/ | |
protected $submit_button_label = null; | |
/** | |
* Add a context to a fieldmanager | |
* @param string $title Currently both menu and page titles; should add an option later. | |
* @param string|string[] $taxonomies | |
* @param string $parent | |
* @param Fieldmanager_Field $fm | |
*/ | |
public function __construct( $title, $taxonomies, $parent = '', $fm = null ) { | |
// Populate the list of taxonomies for which to add this meta box with the given settings | |
if ( ! is_array( $taxonomies ) ) { | |
$taxonomies = array( $taxonomies ); | |
} | |
// Set the class variables | |
$this->title = $title; | |
$this->taxonomies = $taxonomies; | |
$this->show_on_add = false; | |
$this->show_on_edit = false; | |
$this->parent = $parent; | |
$this->fm = $fm; | |
// If this should use FM Term meta, uncomment the next line. Otherwise, this will | |
// use core's term meta. | |
// $this->use_fm_meta = true; | |
// Iterate through the taxonomies and add the fields to the requested forms | |
// Also add handlers for saving the fields and which forms to validate (if enabled) | |
foreach ( $taxonomies as $taxonomy ) { | |
add_action( "{$taxonomy}_edit_form_fields", array( $this, 'show_term_subpage_link' ), 10, 2 ); | |
add_filter( "{$taxonomy}_row_actions", array( $this, 'show_term_row_link' ), 10, 2 ); | |
// Also handle removing data when a term is deleted | |
add_action( 'delete_term', array( $this, 'delete_term_fields'), 10, 4 ); | |
} | |
add_action( 'admin_menu', array( $this, 'register_submenu_page' ) ); | |
add_action( 'admin_init', array( $this, 'handle_submenu_save' ) ); | |
} | |
/** | |
* Register the taxonomy submenu pages with WordPress. | |
*/ | |
public function register_submenu_page() { | |
foreach ( $this->taxonomies as $taxonomy ) { | |
$tax_obj = get_taxonomy( $taxonomy ); | |
add_submenu_page( 'edit-tags.php', $this->title, $this->title, $tax_obj->cap->manage_terms, $this->get_menu_page_slug( $taxonomy ), array( $this, 'render_submenu_page' ) ); | |
} | |
} | |
/** | |
* Get the submenu page slug for a taxonomy for this context. | |
* | |
* @param string $taxonomy | |
* @return string | |
*/ | |
public function get_menu_page_slug( $taxonomy ) { | |
return "{$taxonomy}_submenu_{$this->fm->name}"; | |
} | |
/** | |
* Get the URL to edit the fields for a term. | |
* | |
* @param int $term_id | |
* @param string $taxonomy | |
* @return string | |
*/ | |
public function url( $term_id, $taxonomy ) { | |
return admin_url( "edit-tags.php?page={$this->get_menu_page_slug( $taxonomy )}&term_id={$term_id}&taxonomy={$taxonomy}" ); | |
} | |
/** | |
* Output the markup for the term submenu form. | |
*/ | |
public function render_submenu_page() { | |
if ( empty( $_GET['term_id'] ) || empty( $_GET['taxonomy'] ) || empty( $_GET['page'] ) ) { | |
return; | |
} | |
$msg = empty( $_GET['msg'] ) ? null : sanitize_text_field( $_GET['msg'] ); | |
$term_id = absint( $_GET['term_id'] ); | |
$taxonomy = sanitize_title( $_GET['taxonomy'] ); | |
$term = get_term( $term_id, $taxonomy ); | |
?> | |
<div class="wrap"> | |
<?php if ( 'success' === $msg ) : ?> | |
<div class="updated success"><p><?php esc_html_e( 'Options updated', 'fieldmanager' ); ?></p></div> | |
<?php endif ?> | |
<h2><?php echo esc_html( $this->title ) ?>: <?php echo esc_html( $term->name ); ?></h2> | |
<p> | |
<a href="<?php echo esc_url( get_edit_term_link( $term_id, $taxonomy ) ); ?>"> | |
<?php echo esc_html( sprintf( __( 'Edit %s', 'fieldmanager' ), $term->name ) ); ?> | |
</a> | |
</p> | |
<form method="POST" id="<?php echo esc_attr( $this->uniqid ) ?>"> | |
<input type="hidden" name="fm-term-subpage" value="<?php echo sanitize_title( $this->get_menu_page_slug( $taxonomy ) ) ?>" /> | |
<input type="hidden" name="term_id" value="<?php echo absint( $term_id ); ?>" /> | |
<input type="hidden" name="taxonomy" value="<?php echo esc_attr( $taxonomy ); ?>" /> | |
<table class="form-table"> | |
<tbody> | |
<?php $this->edit_term_fields( $term, $taxonomy ); ?> | |
</tbody> | |
</table> | |
<?php submit_button( $this->submit_button_label, 'primary', 'fm-submit' ) ?> | |
</form> | |
</div> | |
<?php | |
} | |
/** | |
* Save a term submenu page's values. | |
*/ | |
public function handle_submenu_save() { | |
if ( empty( $_POST['term_id'] ) || empty( $_GET['taxonomy'] ) || empty( $_POST['fm-term-subpage'] ) ) { | |
return; | |
} | |
// Make sure that our nonce field arrived intact. | |
if ( ! $this->is_valid_nonce() ) { | |
return; | |
} | |
$term_id = absint( $_POST['term_id'] ); | |
$taxonomy = sanitize_title( $_POST['taxonomy'] ); | |
$this->save_term_fields( $term_id, null, $taxonomy ); | |
// TODO: Other functions will require a bit of refactoring for us to be able to properly conditionally show this message. | |
wp_redirect( esc_url_raw( add_query_arg( array( 'msg' => 'success' ), $this->url( $term_id, $taxonomy ) ) ) ); | |
exit; | |
} | |
/** | |
* Add the link to the submenu page in the edit term form. | |
* | |
* @param WP_Term $term | |
* @param string $taxonomy | |
*/ | |
public function show_term_subpage_link( $term, $taxonomy ) { | |
$label = empty( $this->title ) ? __( 'Fields', 'fieldmanager' ) : $this->title; | |
?> | |
<tr class="form-field"> | |
<th scope="row" valign="top"></th> | |
<td> | |
<a href="<?php echo esc_url( $this->url( $term->term_id, $taxonomy ) ); ?>"> | |
<?php echo esc_html( sprintf( __( 'Edit %s', 'fieldmanager' ), $label ) ); ?> | |
</a> | |
</td> | |
</tr> | |
<?php | |
} | |
/** | |
* Add the link to the row actions in the list of terms. | |
* | |
* @param array $actions | |
* @param WP_Term $term | |
* @return array | |
*/ | |
public function show_term_row_link( $actions, $term ) { | |
$tax_obj = get_taxonomy( $term->taxonomy ); | |
if ( current_user_can( $tax_obj->cap->manage_terms, $term->term_id ) ) { | |
$actions[ "edit_{$this->fm->name}" ] = '<a href="' . esc_url( $this->url( $term->term_id, $term->taxonomy ) ) . '">' . esc_html( sprintf( __( 'Edit %s', 'fieldmanager' ), $this->title ) ) . '</a>'; | |
} | |
return $actions; | |
} | |
/** | |
* Helper to register an admin bar link for a termpage. | |
* | |
* @param string $label Label to display on the button | |
* @param string|array $taxonomies Taxonomies to display the button on | |
* @param string $fm_name Name field of the real FM instance. | |
*/ | |
public static function register_admin_bar_link( $label, $taxonomies, $fm_name ) { | |
if ( ! is_admin() ) { | |
// We just need a minimal pseudo-context for the menu buttons. | |
// Not very DRY when invoking it, but should be suitable for now. | |
add_action( 'init', array( | |
new Fieldmanager_Context_Termpage( | |
$label, | |
$taxonomies, | |
'', | |
new Fieldmanager_TextField( array( 'name' => $fm_name ) ) | |
), | |
'add_to_admin_bar', | |
) ); | |
} | |
} | |
/** | |
* Tell this context to add a button to the admin bar. After you | |
* instantiate this class, call this method to "enable" this featured. | |
*/ | |
public function add_to_admin_bar() { | |
add_action( 'admin_bar_menu', array( $this, 'admin_bar_button' ), 999 ); | |
add_action( 'wp_footer', array( $this, 'add_admin_bar_css' ), 999 ); | |
add_action( 'admin_footer', array( $this, 'add_admin_bar_css' ), 999 ); | |
} | |
/** | |
* Add a link to the termpage if we're looking at a term. | |
* | |
* @param WP_Admin_Bar $wp_admin_bar Admin bar instance. | |
*/ | |
public function admin_bar_button( $wp_admin_bar ) { | |
if ( is_tax() || is_tag() || is_category() ) { | |
$term = get_queried_object(); | |
} | |
if ( ! isset( $term->term_id ) || ! in_array( $term->taxonomy, $this->taxonomies ) ) { | |
return; | |
} | |
$tax_obj = get_taxonomy( $term->taxonomy ); | |
if ( current_user_can( $tax_obj->cap->manage_terms, $term->term_id ) ) { | |
$wp_admin_bar->add_node( array( | |
'id' => 'termpage-' . sanitize_title( $this->fm->name ), | |
'title' => $this->title, | |
'href' => $this->url( $term->term_id, $term->taxonomy ), | |
) ); | |
} | |
} | |
/** | |
* Add dashicon to termpage link in the Admin Bar. | |
*/ | |
public function add_admin_bar_css() { | |
if ( is_user_logged_in() ) : | |
?> | |
<style> | |
#wp-admin-bar-termpage-<?php echo sanitize_title( $this->fm->name ) ?> a:before { font-family: 'dashicons'; content: "\f111" !important; margin-top: 2px; } | |
</style> | |
<?php | |
endif; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment