Skip to content

Commit 4d3a684

Browse files
committed
Merge pull request alleyinteractive#401 from alleyinteractive/core-term-meta
Add support for core term meta
2 parents f69242f + ba6ebaf commit 4d3a684

8 files changed

+601
-62
lines changed

fieldmanager.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
* Fieldmanager Base Plugin File.
44
*
55
* @package Fieldmanager
6-
* @version 1.0.0-beta.2
6+
* @version 1.0.0-beta.3
77
*/
88

99
/*
1010
Plugin Name: Fieldmanager
1111
Plugin URI: https://github.com/alleyinteractive/wordpress-fieldmanager
1212
Description: Add fields to content types programatically.
1313
Author: Austin Smith
14-
Version: 1.0.0-beta.2
14+
Version: 1.0.0-beta.3
1515
Author URI: http://www.alleyinteractive.com/
1616
*/
1717

1818
/**
1919
* Current version of Fieldmanager.
2020
*/
21-
define( 'FM_VERSION', '1.0.0-beta.2' );
21+
define( 'FM_VERSION', '1.0.0-beta.3' );
2222

2323
/**
2424
* Filesystem path to Fieldmanager.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "Fieldmanager",
33
"description": "Fieldmanager is a comprehensive toolkit for building forms, metaboxes, and custom admin screens for WordPress.",
4-
"version": "1.0.0-beta.2",
4+
"version": "1.0.0-beta.3",
55
"repository" : {
66
"type" : "git",
77
"url" : "https://github.com/alleyinteractive/wordpress-fieldmanager"

php/class-fieldmanager-field.php

+44-1
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,11 @@ public function add_page_form( $uniqid ) {
971971

972972
/**
973973
* Add a form on a term add/edit page
974+
*
975+
* @deprecated 1.0.0-beta.3 Replaced by {@see Fieldmanager_Field::add_term_meta_box()}.
976+
*
974977
* @see Fieldmanager_Context_Term
978+
*
975979
* @param string $title
976980
* @param string|array $taxonomies The taxonomies on which to display this form
977981
* @param boolean $show_on_add Whether or not to show the fields on the add term form
@@ -980,7 +984,46 @@ public function add_page_form( $uniqid ) {
980984
*/
981985
public function add_term_form( $title, $taxonomies, $show_on_add = true, $show_on_edit = true, $parent = '' ) {
982986
$this->require_base();
983-
return new Fieldmanager_Context_Term( $title, $taxonomies, $show_on_add, $show_on_edit, $parent, $this );
987+
return new Fieldmanager_Context_Term( array(
988+
'title' => $title,
989+
'taxonomies' => $taxonomies,
990+
'show_on_add' => $show_on_add,
991+
'show_on_edit' => $show_on_edit,
992+
'parent' => $parent,
993+
// Use the deprecated FM Term Meta instead of core's term meta
994+
'use_fm_meta' => true,
995+
'field' => $this,
996+
) );
997+
}
998+
999+
/**
1000+
* Add fields to the term add/edit page
1001+
*
1002+
* @see Fieldmanager_Context_Term
1003+
*
1004+
* @param string $title
1005+
* @param string|array $taxonomies The taxonomies on which to display this form
1006+
* @param boolean $show_on_add Whether or not to show the fields on the add term form
1007+
* @param boolean $show_on_edit Whether or not to show the fields on the edit term form
1008+
* @param int $parent Only show this field on child terms of this parent term ID
1009+
*/
1010+
public function add_term_meta_box( $title, $taxonomies, $show_on_add = true, $show_on_edit = true, $parent = '' ) {
1011+
// Bail if term meta table is not installed.
1012+
if ( get_option( 'db_version' ) < 34370 ) {
1013+
_doing_it_wrong( __METHOD__, esc_html__( 'This method requires WordPress 4.4 or above', 'fieldmanager' ), 'Fieldmanager-1.0.0-beta.3' );
1014+
return false;
1015+
}
1016+
1017+
$this->require_base();
1018+
return new Fieldmanager_Context_Term( array(
1019+
'title' => $title,
1020+
'taxonomies' => $taxonomies,
1021+
'show_on_add' => $show_on_add,
1022+
'show_on_edit' => $show_on_edit,
1023+
'parent' => $parent,
1024+
'use_fm_meta' => false,
1025+
'field' => $this,
1026+
) );
9841027
}
9851028

9861029
/**

php/context/class-fieldmanager-context-term.php

+118-31
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,22 @@ class Fieldmanager_Context_Term extends Fieldmanager_Context_Storable {
4545
public $reserved_fields = array( 'name', 'slug', 'description' );
4646

4747
/**
48-
* @var Fieldmanager_Group
48+
* Use FM term meta or WordPres core term meta. The default is a bit
49+
* confusing: technically, it's to use core's term meta, but if the class is
50+
* instantiated using the now-deprecated separated arguments, this gets set
51+
* to true for backwards-compatibility purposes.
52+
*
53+
* This should be false whenever possible to instead use core's built-in
54+
* term meta (introduced in WordPress 4.4).
55+
*
56+
* @var boolean
57+
*/
58+
public $use_fm_meta = false;
59+
60+
/**
4961
* Base field
62+
*
63+
* @var Fieldmanager_Field
5064
*/
5165
public $fm = '';
5266

@@ -58,29 +72,77 @@ class Fieldmanager_Context_Term extends Fieldmanager_Context_Storable {
5872

5973

6074
/**
61-
* Add a context to a fieldmanager
62-
* @param string|string[] $taxonomies
63-
* @param boolean $show_on_add Whether or not to show the fields on the add term form
64-
* @param boolean $show_on_edit Whether or not to show the fields on the edit term form
65-
* @param Fieldmanager_Field $fm
75+
* Instantiate this context. You can either pass an array of all args
76+
* (preferred), or pass them individually (deprecated).
77+
*
78+
* @param array|string $args {
79+
* Array of arguments.
80+
*
81+
* If a string (deprecated), this will be used as the $title.
82+
*
83+
* @type string $title The context/meta box title.
84+
* @type string|array $taxonomies The taxonomy/taxonomies to which to
85+
* add this field.
86+
* @type bool $show_on_add Optional. Should this field show on the "Add
87+
* Term" screen? Defaults to yes (true).
88+
* @type bool $show_on_edit Optional. Should this field show on the
89+
* "Edit Term" screen? Defaults to yes (true).
90+
* @type int $parent Optional. Should this field only show if its parent
91+
* matches this term ID?
92+
* @type bool $use_fm_meta Optional. Should this context store its data
93+
* using FM term meta (true, deprecated) or
94+
* WordPress core term meta (false). Defaults to
95+
* false.
96+
* @type Fieldmanager_Field $field Optional. The field to which to
97+
* attach this context.
98+
* }
99+
* @param string|array $taxonomies Optional. Deprecated. Required if $args
100+
* is a string.
101+
* @param boolean $show_on_add Optional. Deprecated.
102+
* @param boolean $show_on_edit Optional. Deprecated.
103+
* @param string $parent Optional. Deprecated.
104+
* @param Fieldmanager_Field $fm Optional. Deprecated.
66105
*/
67-
public function __construct( $title, $taxonomies, $show_on_add = true, $show_on_edit = true, $parent = '', $fm = null ) {
68-
// Populate the list of taxonomies for which to add this meta box with the given settings
69-
if ( ! is_array( $taxonomies ) ) {
70-
$taxonomies = array( $taxonomies );
71-
}
106+
public function __construct( $args, $taxonomies = array(), $show_on_add = true, $show_on_edit = true, $parent = '', $fm = null ) {
107+
if ( is_array( $args ) ) {
108+
$args = wp_parse_args( $args, array(
109+
'show_on_add' => true,
110+
'show_on_edit' => true,
111+
'parent' => '',
112+
'use_fm_meta' => false,
113+
'field' => null,
114+
) );
115+
if ( ! isset( $args['title'], $args['taxonomies'] ) ) {
116+
throw new FM_Developer_Exception( esc_html__( '"title" and "taxonomies" are required values for Fieldmanager_Context_Term', 'fieldmanager' ) );
117+
}
72118

73-
// Set the class variables
74-
$this->title = $title;
75-
$this->taxonomies = $taxonomies;
76-
$this->show_on_add = $show_on_add;
77-
$this->show_on_edit = $show_on_edit;
78-
$this->parent = $parent;
79-
$this->fm = $fm;
119+
$this->title = $args['title'];
120+
$this->taxonomies = (array) $args['taxonomies'];
121+
$this->show_on_add = $args['show_on_add'];
122+
$this->show_on_edit = $args['show_on_edit'];
123+
$this->parent = $args['parent'];
124+
$this->use_fm_meta = $args['use_fm_meta'];
125+
$this->fm = $args['field'];
126+
} elseif ( empty( $taxonomies ) ) {
127+
throw new FM_Developer_Exception( esc_html__( '"title" and "taxonomies" are required values for Fieldmanager_Context_Term', 'fieldmanager' ) );
128+
} else {
129+
// Instantiating Fieldmanager_Context_Term using individual
130+
// arguments is deprecated as of Fieldmanager-1.0.0-beta.3; you
131+
// should pass an array of arguments instead.
132+
133+
// Set the class variables
134+
$this->title = $args;
135+
$this->taxonomies = (array) $taxonomies;
136+
$this->show_on_add = $show_on_add;
137+
$this->show_on_edit = $show_on_edit;
138+
$this->parent = $parent;
139+
$this->use_fm_meta = true;
140+
$this->fm = $fm;
141+
}
80142

81143
// Iterate through the taxonomies and add the fields to the requested forms
82144
// Also add handlers for saving the fields and which forms to validate (if enabled)
83-
foreach ( $taxonomies as $taxonomy ) {
145+
foreach ( $this->taxonomies as $taxonomy ) {
84146
if ( $this->show_on_add ) {
85147
add_action( $taxonomy . '_add_form_fields', array( $this, 'add_term_fields' ), 10, 1 );
86148
add_action( 'created_term', array( $this, 'save_term_fields'), 10, 3 );
@@ -91,8 +153,10 @@ public function __construct( $title, $taxonomies, $show_on_add = true, $show_on_
91153
add_action( 'edited_term', array( $this, 'save_term_fields'), 10, 3 );
92154
}
93155

94-
// Also handle removing data when a term is deleted
95-
add_action( 'delete_term', array( $this, 'delete_term_fields'), 10, 4 );
156+
if ( $this->use_fm_meta ) {
157+
// Handle removing FM term meta when a term is deleted
158+
add_action( 'delete_term', array( $this, 'delete_term_fields'), 10, 4 );
159+
}
96160
}
97161
}
98162

@@ -229,14 +293,17 @@ public function save_to_term_meta( $term_id, $taxonomy, $data = null ) {
229293
}
230294

231295
/**
232-
* Saves custom fields for the sport taxonomy
296+
* Saves custom fields for the sport taxonomy.
297+
*
298+
* @deprecated Fieldmanager-1.0.0-beta.3 This is not necessary if you're
299+
* using core's term meta.
233300
*
234301
* @access public
302+
*
235303
* @param int $term_id
236304
* @param int $tt_id
237305
* @param string $taxonomy
238306
* @param WP_term $deleted_term
239-
* @return void
240307
*/
241308
public function delete_term_fields( $term_id, $tt_id, $taxonomy, $deleted_term ) {
242309
// Get an instance of the term meta class
@@ -249,37 +316,57 @@ public function delete_term_fields( $term_id, $tt_id, $taxonomy, $deleted_term )
249316
/**
250317
* Callback to get term meta for the given term ID and current taxonomy.
251318
*
252-
* @see Fieldmanager_Util_Term_Meta::get_term_meta()
319+
* @see get_term_meta().
320+
* @see Fieldmanager_Util_Term_Meta::get_term_meta() (Deprecated).
253321
*/
254322
protected function get_data( $term_id, $meta_key, $single = false ) {
255-
return fm_get_term_meta( $term_id, $this->current_taxonomy, $meta_key, $single );
323+
if ( $this->use_fm_meta ) {
324+
return fm_get_term_meta( $term_id, $this->current_taxonomy, $meta_key, $single );
325+
} else {
326+
return get_term_meta( $term_id, $meta_key, $single );
327+
}
256328
}
257329

258330
/**
259331
* Callback to add term meta for the given term ID and current taxonomy.
260332
*
261-
* @see Fieldmanager_Util_Term_Meta::add_term_meta()
333+
* @see add_term_meta().
334+
* @see Fieldmanager_Util_Term_Meta::add_term_meta() (Deprecated).
262335
*/
263336
protected function add_data( $term_id, $meta_key, $meta_value, $unique = false ) {
264-
return fm_add_term_meta( $term_id, $this->current_taxonomy, $meta_key, $meta_value, $unique );
337+
if ( $this->use_fm_meta ) {
338+
return fm_add_term_meta( $term_id, $this->current_taxonomy, $meta_key, $meta_value, $unique );
339+
} else {
340+
return add_term_meta( $term_id, $meta_key, $meta_value, $unique );
341+
}
265342
}
266343

267344
/**
268345
* Callback to update term meta for the given term ID and current taxonomy.
269346
*
270-
* @see Fieldmanager_Util_Term_Meta::update_term_meta()
347+
* @see update_term_meta().
348+
* @see Fieldmanager_Util_Term_Meta::update_term_meta() (Deprecated).
271349
*/
272350
protected function update_data( $term_id, $meta_key, $meta_value, $meta_prev_value = '' ) {
273-
return fm_update_term_meta( $term_id, $this->current_taxonomy, $meta_key, $meta_value, $meta_prev_value );
351+
if ( $this->use_fm_meta ) {
352+
return fm_update_term_meta( $term_id, $this->current_taxonomy, $meta_key, $meta_value, $meta_prev_value );
353+
} else {
354+
return update_term_meta( $term_id, $meta_key, $meta_value, $meta_prev_value );
355+
}
274356
}
275357

276358
/**
277359
* Callback to delete term meta for the given term ID and current taxonomy.
278360
*
279-
* @see Fieldmanager_Util_Term_Meta::delete_term_meta()
361+
* @see delete_term_meta().
362+
* @see Fieldmanager_Util_Term_Meta::delete_term_meta() (Deprecated).
280363
*/
281364
protected function delete_data( $term_id, $meta_key, $meta_value = '' ) {
282-
return fm_delete_term_meta( $term_id, $this->current_taxonomy, $meta_key, $meta_value );
365+
if ( $this->use_fm_meta ) {
366+
return fm_delete_term_meta( $term_id, $this->current_taxonomy, $meta_key, $meta_value );
367+
} else {
368+
return delete_term_meta( $term_id, $meta_key, $meta_value );
369+
}
283370
}
284371

285372
}

0 commit comments

Comments
 (0)