Passed
Push — master ( 0bccda...b35e9b )
by Paul
05:49 queued 02:55
created

Archive   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 236
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 236
rs 10
c 1
b 0
f 0
wmc 27
lcom 1
cbo 4

17 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 11 1
A canProceed() 0 5 2
A filterBeforeInstructions() 0 8 1
A filterInstruction() 0 4 1
A filterSubmitMetaBox() 0 5 1
A getFeaturedImageHtml() 0 8 1
A getMetaValue() 0 4 1
A register() 0 8 2
A registerFeaturedImageMetaBox() 0 5 2
A registerPage() 0 14 2
A renderEditor() 0 15 1
A renderFeaturedImageMetaBox() 0 21 4
A renderPage() 0 14 2
A getDefaults() 0 4 1
A getPostType() 0 8 3
A getPostTypesWithArchive() 0 7 1
A getSettings() 0 4 1
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
	 * @return array
67
	 * @action pollux/{static::ID}/metabox/submit
68
	 */
69
	public function filterSubmitMetaBox( array $args )
70
	{
71
		$args[1] = __( 'Save Archive', 'pollux' );
72
		return $args;
73
	}
74
75
	/**
76
	 * @todo: Use gatekeeper to check capability, wp_die(-1) on fail;
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
77
	 * @return string|null
78
	 * @action wp_ajax_pollux/archives/featured/html
79
	 */
80
	public function getFeaturedImageHtml()
81
	{
82
		check_ajax_referer( sprintf( '%s-options', static::id() ));
83
		static::$current = filter_input( INPUT_POST, 'post_type' );
84
		ob_start();
85
		$this->renderFeaturedImageMetaBox( intval( filter_input( INPUT_POST, 'thumbnail_id' )));
86
		wp_send_json_success( ob_get_clean() );
87
	}
88
89
	/**
90
	 * @param string $key
91
	 * @param mixed $fallback
92
	 * @param string $group
93
	 * @return string|array
94
	 */
95
	public function getMetaValue( $key, $fallback = '', $group = '' )
96
	{
97
		return ArchiveMeta::get( $key, $fallback, $group );
98
	}
99
100
	/**
101
	 * @return void
102
	 * @action current_screen
103
	 */
104
	public function register()
105
	{
106
		$screenId = ( new Helper )->getCurrentScreen()->id;
107
		if( in_array( $screenId, $this->hooks )) {
108
			$this->hook = $screenId;
109
		}
110
		parent::register();
111
	}
112
113
	/**
114
	 * @return void
115
	 * @action pollux/archives/init
116
	 */
117
	public function registerFeaturedImageMetaBox()
118
	{
119
		if( !current_user_can( 'upload_files' ))return;
120
		add_meta_box( 'postimagediv', __( 'Featured Image', 'pollux' ), [$this, 'renderFeaturedImageMetaBox'], null, 'side', 'low' );
121
	}
122
123
	/**
124
	 * @return void
125
	 * @action admin_menu
126
	 */
127
	public function registerPage()
128
	{
129
		foreach( $this->getPostTypesWithArchive() as $type => $page ) {
130
			$labels = get_post_type_labels( get_post_type_object( $type ));
131
			$this->hooks[$type] = call_user_func_array( 'add_submenu_page', $this->filter( 'page', [
132
				$page,
133
				sprintf( _x( '%s Archive', 'post archive', 'pollux' ), $labels->singular_name ),
134
				sprintf( _x( '%s Archive', 'post archive', 'pollux' ), $labels->singular_name ),
135
				static::CAPABILITY,
136
				sprintf( '%s_archive', $type ),
137
				[$this, 'renderPage'],
138
			]));
139
		}
140
	}
141
142
	/**
143
	 * @return void
144
	 * @action pollux/archives/editor
145
	 */
146
	public function renderEditor( $content, $type )
147
	{
148
		wp_editor( $content, 'content', [
149
			'_content_editor_dfw' => true,
150
			'drag_drop_upload' => true,
151
			'editor_height' => 300,
152
			'tabfocus_elements' => 'content-html, publishing-action',
153
			'textarea_name' => sprintf( '%s[%s][content]', static::id(), $type ),
154
			'tinymce' => [
155
				'add_unload_trigger' => false,
156
				'resize' => false,
157
				'wp_autoresize_on' => true,
158
			],
159
		]);
160
	}
161
162
	/**
163
	 * @return void
164
	 * @callback add_meta_box
165
	 */
166
	public function renderFeaturedImageMetaBox( $imageId = null )
167
	{
168
		if( !is_numeric( $imageId )) {
169
			$imageId = ArchiveMeta::get( 'featured', -1, $this->getPostType() );
170
		}
171
		$imageSize = isset( wp_get_additional_image_sizes()['post-thumbnail'] )
172
			? 'post-thumbnail'
173
			: [266, 266];
174
		$thumbnail = get_post( $imageId )
175
			? wp_get_attachment_image( $imageId, $imageSize )
176
			: __( 'Set Featured Image', 'pollux' );
177
178
		$this->app->render( 'archive/featured', [
179
			'edit_image' => __( 'Click the image to edit or update', 'pollux' ),
180
			'id' => static::id(),
181
			'image_id' => $imageId,
182
			'post_type' => $this->getPostType(),
183
			'remove_image' => __( 'Remove featured image', 'pollux' ),
184
			'thumbnail' => $thumbnail,
185
		]);
186
	}
187
188
	/**
189
	 * @return void
190
	 * @callback add_submenu_page
191
	 */
192
	public function renderPage()
193
	{
194
		$type = $this->getPostType();
195
		if( empty( $type ))return;
196
		$labels = get_post_type_labels( get_post_type_object( $type ));
197
		$this->app->render( 'archive/index', [
198
			'columns' => get_current_screen()->get_columns(),
199
			'content' => ArchiveMeta::get( 'content', '', $type ),
200
			'heading' => sprintf( _x( '%s Archive', 'post archive', 'pollux' ), $labels->singular_name ),
201
			'id' => static::id(),
202
			'post_type' => $type,
203
			'title' => ArchiveMeta::get( 'title', '', $type ),
204
		]);
205
	}
206
207
	/**
208
	 * @return array
209
	 */
210
	protected function getDefaults()
211
	{
212
		return [];
213
	}
214
215
	/**
216
	 * @return string
217
	 */
218
	protected function getPostType()
219
	{
220
		$type = array_search( $this->hook, $this->hooks );
221
		if( !empty( $type ) && is_string( $type )) {
222
			static::$current = $type;
223
		}
224
		return static::$current;
225
	}
226
227
	/**
228
	 * @return array
229
	 */
230
	protected function getPostTypesWithArchive()
231
	{
232
		$types = array_map( function( $value ) {
233
			return sprintf( 'edit.php?post_type=%s', $value );
234
		}, get_post_types( ['has_archive' => 1] ));
235
		return array_merge( $types, ['post' => 'edit.php'] );
236
	}
237
238
	/**
239
	 * @return array
240
	 */
241
	protected function getSettings()
242
	{
243
		return (array) ArchiveMeta::all();
244
	}
245
}
246