Completed
Push — develop ( 765c38...eb44bf )
by David
03:19
created

Wordlift_Admin_Term_Adapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $taxonomy is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $tt_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $taxonomy is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
}