1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\Pollux\PostType; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\Pollux\Application; |
6
|
|
|
use GeminiLabs\Pollux\Facades\ArchiveMeta; |
7
|
|
|
use GeminiLabs\Pollux\Helper; |
8
|
|
|
use GeminiLabs\Pollux\Settings\Settings; |
9
|
|
|
|
10
|
|
|
class Archive extends Settings |
11
|
|
|
{ |
12
|
|
|
CONST ID = 'archives'; |
13
|
|
|
|
14
|
|
|
const CAPABILITY = 'edit_others_posts'; |
15
|
|
|
const DEPENDENCY = ''; |
16
|
|
|
|
17
|
|
|
public static $current; |
18
|
|
|
|
19
|
|
|
public $hooks = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
public function init() |
25
|
|
|
{ |
26
|
|
|
parent::init(); |
27
|
|
|
|
28
|
|
|
add_action( 'wp_ajax_pollux/archives/featured/html', [$this, 'getFeaturedImageHtml'] ); |
29
|
|
|
add_action( 'pollux/archives/init', [$this, 'registerFeaturedImageMetaBox'] ); |
30
|
|
|
add_action( 'pollux/archives/editor', [$this, 'renderEditor'], 10, 2 ); |
31
|
|
|
add_action( 'wp_ajax_pollux/archives/featured', [$this, 'setFeaturedImage'] ); |
32
|
|
|
add_filter( 'pollux/archives/metabox/submit', [$this, 'filterSubmitMetaBox'] ); |
33
|
|
|
add_filter( 'pollux/archives/show/instructions', '__return_true' ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function canProceed() |
37
|
|
|
{ |
38
|
|
|
return $this->app->gatekeeper->hasDependency( static::DEPENDENCY ) |
39
|
|
|
&& $this->app->config->enable_archive_page; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
* @filter pollux/{static::ID}/before/instructions |
45
|
|
|
*/ |
46
|
|
|
public function filterBeforeInstructions() |
47
|
|
|
{ |
48
|
|
|
return sprintf( '<pre class="my-sites nav-tab-active misc-pub-section">%s</pre>', |
49
|
|
|
array_reduce( ['title', 'content', 'featured'], function( $instructions, $id ) { |
50
|
|
|
return $instructions . $this->filterInstruction( null, ['slug' => $id], ['slug' => $this->getPostType()] ) . PHP_EOL; |
51
|
|
|
}) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $instruction |
57
|
|
|
* @return string |
58
|
|
|
* @action pollux/{static::ID}/instruction |
59
|
|
|
*/ |
60
|
|
|
public function filterInstruction( $instruction, array $field, array $metabox ) |
61
|
|
|
{ |
62
|
|
|
return sprintf( "ArchiveMeta::%s('%s');", $metabox['slug'], $field['slug'] ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param null|array $settings |
67
|
|
|
* @return array |
68
|
|
|
* @callback register_setting |
69
|
|
|
*/ |
70
|
|
|
public function filterSavedSettings( $settings ) |
71
|
|
|
{ |
72
|
|
|
return $this->filter( 'save', array_intersect_key( |
73
|
|
|
array_merge( $this->getSettings(), Helper::toArray( $settings )), |
74
|
|
|
$this->getPostTypesWithArchive() |
75
|
|
|
)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
* @action pollux/{static::ID}/metabox/submit |
81
|
|
|
*/ |
82
|
|
|
public function filterSubmitMetaBox( array $args ) |
83
|
|
|
{ |
84
|
|
|
$args[1] = __( 'Save Archive', 'pollux' ); |
85
|
|
|
return $args; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @todo: Use gatekeeper to check capability, wp_die(-1) on fail; |
90
|
|
|
* @return string|null |
91
|
|
|
* @action wp_ajax_pollux/archives/featured/html |
92
|
|
|
*/ |
93
|
|
|
public function getFeaturedImageHtml() |
94
|
|
|
{ |
95
|
|
|
check_ajax_referer( sprintf( '%s-options', static::id() )); |
96
|
|
|
static::$current = filter_input( INPUT_POST, 'post_type' ); |
97
|
|
|
ob_start(); |
98
|
|
|
$this->renderFeaturedImageMetaBox( intval( filter_input( INPUT_POST, 'thumbnail_id' ))); |
99
|
|
|
wp_send_json_success( ob_get_clean() ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $key |
104
|
|
|
* @param mixed $fallback |
105
|
|
|
* @param string $group |
106
|
|
|
* @return string|array |
107
|
|
|
*/ |
108
|
|
|
public function getMetaValue( $key, $fallback = '', $group = '' ) |
109
|
|
|
{ |
110
|
|
|
return ArchiveMeta::get( $group, $key, $fallback ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return void |
115
|
|
|
* @action current_screen |
116
|
|
|
*/ |
117
|
|
|
public function register() |
118
|
|
|
{ |
119
|
|
|
$screenId = Helper::getCurrentScreen()->id; |
120
|
|
|
if( in_array( $screenId, $this->hooks )) { |
121
|
|
|
$this->hook = $screenId; |
122
|
|
|
} |
123
|
|
|
parent::register(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return void |
128
|
|
|
* @action pollux/archives/init |
129
|
|
|
*/ |
130
|
|
|
public function registerFeaturedImageMetaBox() |
131
|
|
|
{ |
132
|
|
|
if( !current_user_can( 'upload_files' ))return; |
133
|
|
|
add_meta_box( 'postimagediv', __( 'Featured Image', 'pollux' ), [$this, 'renderFeaturedImageMetaBox'], null, 'side', 'low' ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return void |
138
|
|
|
* @action admin_menu |
139
|
|
|
*/ |
140
|
|
|
public function registerPage() |
141
|
|
|
{ |
142
|
|
|
foreach( $this->getPostTypesWithArchive() as $type => $page ) { |
143
|
|
|
$labels = get_post_type_labels( get_post_type_object( $type )); |
144
|
|
|
$this->hooks[$type] = call_user_func_array( 'add_submenu_page', $this->filter( 'page', [ |
145
|
|
|
$page, |
146
|
|
|
sprintf( _x( '%s Archive', 'post archive', 'pollux' ), $labels->singular_name ), |
147
|
|
|
sprintf( _x( '%s Archive', 'post archive', 'pollux' ), $labels->singular_name ), |
148
|
|
|
static::CAPABILITY, |
149
|
|
|
sprintf( '%s_archive', $type ), |
150
|
|
|
[$this, 'renderPage'], |
151
|
|
|
])); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return void |
157
|
|
|
* @action pollux/archives/editor |
158
|
|
|
*/ |
159
|
|
|
public function renderEditor( $content, $type ) |
160
|
|
|
{ |
161
|
|
|
wp_editor( $content, 'content', [ |
162
|
|
|
'_content_editor_dfw' => true, |
163
|
|
|
'drag_drop_upload' => true, |
164
|
|
|
'editor_height' => 300, |
165
|
|
|
'tabfocus_elements' => 'content-html, publishing-action', |
166
|
|
|
'textarea_name' => sprintf( '%s[%s][content]', static::id(), $type ), |
167
|
|
|
'tinymce' => [ |
168
|
|
|
'add_unload_trigger' => false, |
169
|
|
|
'resize' => false, |
170
|
|
|
'wp_autoresize_on' => true, |
171
|
|
|
], |
172
|
|
|
]); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return void |
177
|
|
|
* @callback add_meta_box |
178
|
|
|
*/ |
179
|
|
|
public function renderFeaturedImageMetaBox( $imageId = null ) |
180
|
|
|
{ |
181
|
|
|
if( !is_numeric( $imageId )) { |
182
|
|
|
$imageId = ArchiveMeta::get( $this->getPostType(), 'featured', -1 ); |
183
|
|
|
} |
184
|
|
|
$imageSize = isset( wp_get_additional_image_sizes()['post-thumbnail'] ) |
185
|
|
|
? 'post-thumbnail' |
186
|
|
|
: [266, 266]; |
187
|
|
|
$thumbnail = get_post( $imageId ) |
188
|
|
|
? wp_get_attachment_image( $imageId, $imageSize ) |
189
|
|
|
: __( 'Set Featured Image', 'pollux' ); |
190
|
|
|
|
191
|
|
|
$this->app->render( 'archive/featured', [ |
192
|
|
|
'edit_image' => __( 'Click the image to edit or update', 'pollux' ), |
193
|
|
|
'id' => static::id(), |
194
|
|
|
'image_id' => $imageId, |
195
|
|
|
'post_type' => $this->getPostType(), |
196
|
|
|
'remove_image' => __( 'Remove featured image', 'pollux' ), |
197
|
|
|
'thumbnail' => $thumbnail, |
198
|
|
|
]); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return void |
203
|
|
|
* @callback add_submenu_page |
204
|
|
|
*/ |
205
|
|
|
public function renderPage() |
206
|
|
|
{ |
207
|
|
|
$type = $this->getPostType(); |
208
|
|
|
if( empty( $type ))return; |
209
|
|
|
$labels = get_post_type_labels( get_post_type_object( $type )); |
210
|
|
|
$this->app->render( 'archive/index', [ |
211
|
|
|
'columns' => get_current_screen()->get_columns(), |
|
|
|
|
212
|
|
|
'content' => ArchiveMeta::get( $type, 'content', '' ), |
213
|
|
|
'heading' => sprintf( _x( '%s Archive', 'post archive', 'pollux' ), $labels->singular_name ), |
214
|
|
|
'id' => static::id(), |
215
|
|
|
'post_type' => $type, |
216
|
|
|
'title' => ArchiveMeta::get( $type, 'title', '' ), |
217
|
|
|
]); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return array |
222
|
|
|
*/ |
223
|
|
|
protected function getDefaults() |
224
|
|
|
{ |
225
|
|
|
return []; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return string |
230
|
|
|
*/ |
231
|
|
|
protected function getPostType() |
232
|
|
|
{ |
233
|
|
|
$type = array_search( $this->hook, $this->hooks ); |
234
|
|
|
if( !empty( $type ) && is_string( $type )) { |
235
|
|
|
static::$current = $type; |
236
|
|
|
} |
237
|
|
|
return static::$current; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return array |
242
|
|
|
*/ |
243
|
|
|
protected function getPostTypesWithArchive() |
244
|
|
|
{ |
245
|
|
|
$types = array_map( function( $value ) { |
246
|
|
|
return sprintf( 'edit.php?post_type=%s', $value ); |
247
|
|
|
}, get_post_types( ['has_archive' => 1] )); |
248
|
|
|
return array_merge( $types, ['post' => 'edit.php'] ); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @return array |
253
|
|
|
*/ |
254
|
|
|
protected function getSettings() |
255
|
|
|
{ |
256
|
|
|
return (array) ArchiveMeta::all(); |
|
|
|
|
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.