@@ -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