|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Adapters: Term Adapter. |
|
4
|
|
|
* |
|
5
|
|
|
* Hooks to the Term edit screen to extend it with an autocomplete select to bind entities to entity terms. |
|
6
|
|
|
* |
|
7
|
|
|
* @since 3.20.0 |
|
8
|
|
|
* @package Wordlift |
|
9
|
|
|
* @subpackage Wordlift/admin |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Define the Wordlift_Admin_Term_Adapter class. |
|
14
|
|
|
* |
|
15
|
|
|
* @since 3.20.0 |
|
16
|
|
|
*/ |
|
17
|
|
|
class Wordlift_Admin_Term_Adapter { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The meta key holding the entity id. |
|
21
|
|
|
* |
|
22
|
|
|
* @since 3.20.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
const META_KEY = '_wl_entity_id'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Create a Wordlift_Admin_Term_Adapter instance. |
|
28
|
|
|
* |
|
29
|
|
|
* @since 3.20.0 |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct() { |
|
32
|
|
|
|
|
33
|
|
|
add_action( 'registered_taxonomy', array( $this, 'add_action', ) ); |
|
34
|
|
|
add_action( 'edit_term', array( $this, 'edit_term', ), 10, 3 ); |
|
35
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Add the form fields to the entity edit screen. |
|
40
|
|
|
* |
|
41
|
|
|
* @since 3.20.0 |
|
42
|
|
|
* |
|
43
|
|
|
* @param object $tag Current taxonomy term object. |
|
44
|
|
|
* @param string $taxonomy Current taxonomy slug. |
|
45
|
|
|
*/ |
|
46
|
|
|
public function edit_form_fields( $tag, $taxonomy ) { |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
// Enqueue the JavaScript app. |
|
49
|
|
|
wp_enqueue_script( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.js', array( 'wp-util', ), Wordlift::get_instance()->get_version(), true ); |
|
50
|
|
|
wp_enqueue_style( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.css', array(), Wordlift::get_instance()->get_version() ); |
|
51
|
|
|
|
|
52
|
|
|
$values = get_term_meta( $tag->term_id, self::META_KEY ); |
|
53
|
|
|
|
|
54
|
|
|
?> |
|
55
|
|
|
<tr class="form-field term-name-wrap"> |
|
56
|
|
|
<th scope="row"><label for="wl-entity-id"><?php _ex( 'Entity', 'term entity', 'wordlift' ); ?></label></th> |
|
57
|
|
|
<td> |
|
58
|
|
|
<?php foreach ( $values as $value ) { ?> |
|
59
|
|
|
<input type="text" name="wl_entity_id[]" value="<?php echo esc_attr( $value ); ?>"/> |
|
60
|
|
|
<?php } ?> |
|
61
|
|
|
<div id="wl-term-entity-id"></div> |
|
62
|
|
|
<p class="description"><?php _e( 'The entity bound to the term.', 'wordlift' ); ?></p> |
|
63
|
|
|
</td> |
|
64
|
|
|
</tr> |
|
65
|
|
|
<?php |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Bind the new fields to the edit term screen. |
|
70
|
|
|
* |
|
71
|
|
|
* @since 3.20.0 |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $taxonomy The taxonomy name. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function add_action( $taxonomy ) { |
|
76
|
|
|
|
|
77
|
|
|
add_action( "{$taxonomy}_edit_form_fields", array( $this, 'edit_form_fields' ), 10, 2 ); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Hook to the edit term to handle our own custom fields. |
|
82
|
|
|
* |
|
83
|
|
|
* @since 3.20.0 |
|
84
|
|
|
* |
|
85
|
|
|
* @param int $term_id The term id. |
|
86
|
|
|
* @param int $tt_id The term taxonomy id. |
|
87
|
|
|
* @param string $taxonomy The taxonomy. |
|
88
|
|
|
*/ |
|
89
|
|
|
public function edit_term( $term_id, $tt_id, $taxonomy ) { |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
// Bail if the action isn't related to the term currently being edited. |
|
92
|
|
|
if ( ! isset( $_POST['tag_ID'] ) || $term_id !== (int) (int) $_POST['tag_ID'] ) { |
|
93
|
|
|
return; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// Delete. |
|
97
|
|
|
if ( ! isset( $_POST['wl_entity_id'] ) || ! is_array( $_POST['wl_entity_id'] ) || empty( $_POST['wl_entity_id'] ) ) { |
|
98
|
|
|
delete_term_meta( $term_id, self::META_KEY ); |
|
99
|
|
|
|
|
100
|
|
|
return; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// Update. |
|
104
|
|
|
// |
|
105
|
|
|
// Only use mb_* functions when mbstring is available. |
|
106
|
|
|
// |
|
107
|
|
|
// See https://github.com/insideout10/wordlift-plugin/issues/693. |
|
108
|
|
View Code Duplication |
if ( extension_loaded( 'mbstring' ) ) { |
|
|
|
|
|
|
109
|
|
|
mb_regex_encoding( 'UTF-8' ); |
|
110
|
|
|
|
|
111
|
|
|
$merged = array_reduce( (array) $_POST['wl_entity_id'], function ( $carry, $item ) { |
|
112
|
|
|
return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) ); |
|
113
|
|
|
}, array() ); |
|
114
|
|
|
} else { |
|
115
|
|
|
$merged = array_reduce( (array) $_POST['wl_entity_id'], function ( $carry, $item ) { |
|
116
|
|
|
return array_merge( $carry, preg_split( "/\x{2063}/u", wp_unslash( $item ) ) ); |
|
117
|
|
|
}, array() ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
delete_term_meta( $term_id, self::META_KEY ); |
|
121
|
|
|
foreach ( array_unique( array_filter( $merged ) ) as $single ) { |
|
122
|
|
|
add_term_meta( $term_id, self::META_KEY, $single ); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.