|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Elements: Author Element. |
|
4
|
|
|
* |
|
5
|
|
|
* A complex element that displays the current person/organization entity |
|
6
|
|
|
* associated with a User and enables selecting a new one. |
|
7
|
|
|
* |
|
8
|
|
|
* @since 3.14.0 |
|
9
|
|
|
* @package Wordlift |
|
10
|
|
|
* @subpackage Wordlift/admin |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Define the {@link Wordlift_Admin_Person_Element} class. |
|
15
|
|
|
* |
|
16
|
|
|
* @since 3.14.0 |
|
17
|
|
|
* @package Wordlift |
|
18
|
|
|
* @subpackage Wordlift/admin |
|
19
|
|
|
*/ |
|
20
|
|
|
class Wordlift_Admin_Author_Element implements Wordlift_Admin_Element { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The {@link Wordlift_Publisher_Service} instance. |
|
24
|
|
|
* |
|
25
|
|
|
* @since 3.14.0 |
|
26
|
|
|
* @access private |
|
27
|
|
|
* @var \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
28
|
|
|
*/ |
|
29
|
|
|
private $publisher_service; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* A {@link Wordlift_Admin_Select2_Element} instance. |
|
33
|
|
|
* |
|
34
|
|
|
* @since 3.14.0 |
|
35
|
|
|
* @access private |
|
36
|
|
|
* @var \Wordlift_Admin_Select2_Element $select_element A {@link Wordlift_Admin_Select2_Element} instance. |
|
37
|
|
|
*/ |
|
38
|
|
|
private $select_element; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Create a {@link Wordlift_Admin_Person_Element} instance. |
|
42
|
|
|
* |
|
43
|
|
|
* @since 3.14.0 |
|
44
|
|
|
* |
|
45
|
|
|
* @param \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
46
|
|
|
* @param \Wordlift_Admin_Select2_Element $select_element The {@link Wordlift_Admin_Select_Element} instance. |
|
47
|
|
|
*/ |
|
48
|
|
|
function __construct( $publisher_service, $select_element ) { |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
$this->publisher_service = $publisher_service; |
|
51
|
|
|
|
|
52
|
|
|
// Child elements. |
|
53
|
|
|
$this->select_element = $select_element; |
|
54
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @inheritdoc |
|
59
|
|
|
*/ |
|
60
|
|
|
public function render( $args ) { |
|
61
|
|
|
|
|
62
|
|
|
// Parse the arguments and merge with default values. |
|
63
|
|
|
$params = wp_parse_args( $args, array( |
|
64
|
|
|
'id' => uniqid( 'wl-input-' ), |
|
65
|
|
|
'name' => uniqid( 'wl-input-' ), |
|
66
|
|
|
'current_entity' => 0, |
|
67
|
|
|
) ); |
|
68
|
|
|
|
|
69
|
|
|
$current_entity_id = $params['current_entity']; |
|
70
|
|
|
$data = $this->publisher_service->query(); |
|
71
|
|
|
|
|
72
|
|
|
// Set a default to show when no entity is associated and a way to unassign. |
|
73
|
|
|
array_unshift( $data, array( |
|
74
|
|
|
'id' => '0', |
|
75
|
|
|
'text' => __( '<em>(none)</em>', 'wordlift' ), |
|
76
|
|
|
'type' => '', |
|
77
|
|
|
'thumbnail_url' => plugin_dir_url( dirname( __FILE__ ) ) . 'images/pixel.png', |
|
78
|
|
|
) ); |
|
79
|
|
|
|
|
80
|
|
|
// Finally do the render, passing along also the current selected entity |
|
81
|
|
|
// id and the options data. |
|
82
|
|
|
return $this->do_render( $params, $current_entity_id, $data ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Render the `select` using the provided parameters. |
|
87
|
|
|
* |
|
88
|
|
|
* @since 3.14.0 |
|
89
|
|
|
* |
|
90
|
|
|
* @param array $params The array of parameters from the `render` function. |
|
91
|
|
|
* @param int $current_post_id The currently selected {@link WP_Post} `id`. |
|
92
|
|
|
* @param array $data An array of Select2 options. |
|
93
|
|
|
* |
|
94
|
|
|
* @return \Wordlift_Admin_Author_Element $this Return this element. |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function do_render( $params, $current_post_id, $data ) { |
|
97
|
|
|
|
|
98
|
|
|
// Queue the script which will initialize the select and style it. |
|
99
|
|
|
wp_enqueue_script( 'wl-author-element', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/wordlift-author-element.bundle.js', array( 'wordlift-select2' ) ); |
|
100
|
|
|
|
|
101
|
|
|
// Prepare the URLs for entities which don't have logos. |
|
102
|
|
|
$person_thumbnail_url = plugin_dir_url( dirname( __FILE__ ) ) . 'images/person.png'; |
|
103
|
|
|
$organization_thumbnail_url = plugin_dir_url( dirname( __FILE__ ) ) . 'images/organization.png'; |
|
104
|
|
|
|
|
105
|
|
|
// Get the current post. |
|
106
|
|
|
$current_post = $current_post_id ? get_post( $current_post_id ) : null; |
|
107
|
|
|
|
|
108
|
|
|
// Finally render the Select. |
|
109
|
|
|
$this->select_element->render( array( |
|
110
|
|
|
// Id. |
|
111
|
|
|
'id' => $params['id'], |
|
112
|
|
|
// Name. |
|
113
|
|
|
'name' => $params['name'], |
|
114
|
|
|
// The selected id. |
|
115
|
|
|
'value' => $current_post_id, |
|
116
|
|
|
// The selected item (must be in the options for Select2 to display it). |
|
117
|
|
|
'options' => $current_post ? array( $current_post->ID => $current_post->post_title ) : array(), |
|
118
|
|
|
// The list of available options. |
|
119
|
|
|
'data' => $data, |
|
120
|
|
|
// The HTML template for each option. |
|
121
|
|
|
'template-result' => "<div class='wl-select2-result'><span class='wl-select2-thumbnail' style='background-image: url( <%= obj.thumbnail_url || ( 'Organization' === obj.type ? '$organization_thumbnail_url' : '$person_thumbnail_url' ) %> );'> </span><span class='wl-select2'><%= obj.text %></span><span class='wl-select2-type'><%= obj.type %></span></div>", |
|
122
|
|
|
// The HTML template for the selected option. |
|
123
|
|
|
'template-selection' => "<div class='wl-select2-selection'><span class='wl-select2-thumbnail' style='background-image: url( <%= obj.thumbnail_url || ( 'Organization' === obj.type ? '$organization_thumbnail_url' : '$person_thumbnail_url' ) %> );'> </span><span class='wl-select2'><%= obj.text %></span><span class='wl-select2-type'><%= obj.type %></span></div>", |
|
124
|
|
|
) ); |
|
125
|
|
|
|
|
126
|
|
|
// Finally return the element instance. |
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.