Completed
Pull Request — develop (#1082)
by Naveen
02:29
created

Wordlift_Admin_Term_Adapter::add_settings()   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
		$this->add_settings();
36
37
	}
38
39
	/**
40
	 * Hook in to WordLift admin settings and add the term page specific
41
	 * settings.
42
	 * @since 3.26.1
43
	 */
44
	public function add_settings() {
45
		add_filter( 'wl_admin_settings', function ( $params ) {
46
			$params['show_local_entities'] = true;
47
			return $params;
48
		} );
49
	}
50
51
	/**
52
	 * Add the form fields to the entity edit screen.
53
	 *
54
	 * @param object $tag Current taxonomy term object.
55
	 * @param string $taxonomy Current taxonomy slug.
56
	 *
57
	 * @since 3.20.0
58
	 *
59
	 */
60
	public function edit_form_fields( $tag, $taxonomy ) {
61
62
		global $wp_version;
63
64
		// Enqueue the JavaScript app.
65
		if ( version_compare( $wp_version, '5.0', '>=' ) ) {
66
			$term_asset = include plugin_dir_path( dirname( __FILE__ ) ) . 'js/dist/term.asset.php';
67
			wp_enqueue_script( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.js', array_merge( array( 'wp-util' ), $term_asset['dependencies'] ), Wordlift::get_instance()->get_version(), true );
68
		} else {
69
			wp_enqueue_script( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.full.js', array( 'wp-util' ), Wordlift::get_instance()->get_version(), true );
70
		}
71
72
		wp_enqueue_style( 'wl-term', plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/term.css', array(), Wordlift::get_instance()->get_version() );
73
74
		$values = get_term_meta( $tag->term_id, self::META_KEY );
75
76
		?>
77
        <tr class="form-field term-name-wrap">
78
            <th scope="row"><label for="wl-entity-id"><?php _ex( 'Entity', 'term entity', 'wordlift' ); ?></label></th>
79
            <td>
80
				<?php foreach ( $values as $value ) { ?>
81
                    <input type="text" name="wl_entity_id[]" value="<?php echo esc_attr( $value ); ?>"/>
82
				<?php } ?>
83
                <div id="wl-term-entity-id"></div>
84
                <p class="description"><?php _e( 'The entity bound to the term.', 'wordlift' ); ?></p>
85
            </td>
86
        </tr>
87
		<?php
88
	}
89
90
	/**
91
	 * Bind the new fields to the edit term screen.
92
	 *
93
	 * @since 3.20.0
94
	 *
95
	 * @param string $taxonomy The taxonomy name.
96
	 */
97
	public function add_action( $taxonomy ) {
98
99
		add_action( "{$taxonomy}_edit_form_fields", array( $this, 'edit_form_fields' ), 10, 2 );
100
	}
101
102
	/**
103
	 * Hook to the edit term to handle our own custom fields.
104
	 *
105
	 * @since 3.20.0
106
	 *
107
	 * @param int    $term_id The term id.
108
	 * @param int    $tt_id The term taxonomy id.
109
	 * @param string $taxonomy The taxonomy.
110
	 */
111
	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...
112
113
		// Bail if the action isn't related to the term currently being edited.
114
		if ( ! isset( $_POST['tag_ID'] ) || $term_id !== (int) (int) $_POST['tag_ID'] ) {
115
			return;
116
		}
117
118
		// Delete.
119
		if ( ! isset( $_POST['wl_entity_id'] ) || ! is_array( $_POST['wl_entity_id'] ) || empty( $_POST['wl_entity_id'] ) ) {
120
			delete_term_meta( $term_id, self::META_KEY );
121
122
			return;
123
		}
124
125
		// Update.
126
		//
127
		// Only use mb_* functions when mbstring is available.
128
		//
129
		// See https://github.com/insideout10/wordlift-plugin/issues/693.
130 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...
131
			mb_regex_encoding( 'UTF-8' );
132
133
			$merged = array_reduce( (array) $_POST['wl_entity_id'], function ( $carry, $item ) {
134
				return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) );
135
			}, array() );
136
		} else {
137
			$merged = array_reduce( (array) $_POST['wl_entity_id'], function ( $carry, $item ) {
138
				return array_merge( $carry, preg_split( "/\x{2063}/u", wp_unslash( $item ) ) );
139
			}, array() );
140
		}
141
142
		delete_term_meta( $term_id, self::META_KEY );
143
		foreach ( array_unique( array_filter( $merged ) ) as $single ) {
144
			add_term_meta( $term_id, self::META_KEY, $single );
145
		}
146
147
	}
148
149
}
150