Passed
Push — master ( 51995c...0bccda )
by Paul
06:07 queued 02:50
created

Archive   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 232
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 26
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 232
rs 10

16 Methods

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