Passed
Push — develop ( 39ebd6...fc1005 )
by Paul
02:53
created

Archive::getPostType()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 9.4285
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
		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
	/**
37
	 * @return void
38
	 * @action admin_menu
39
	 */
40
	public function addPage()
41
	{
42
		foreach( $this->getPostTypesWithArchive() as $type => $page ) {
43
			$labels = get_post_type_labels( get_post_type_object( $type ));
44
			$this->hooks[$type] = call_user_func_array( 'add_submenu_page', $this->filter( 'page', [
45
				$page,
46
				sprintf( _x( '%s Archive', 'post archive', 'pollux' ), $labels->singular_name ),
47
				sprintf( _x( '%s Archive', 'post archive', 'pollux' ), $labels->singular_name ),
48
				'edit_theme_options',
49
				sprintf( '%s_archive', $type ),
50
				[$this, 'renderPage'],
51
			]));
52
		}
53
	}
54
55
	/**
56
	 * @return string
57
	 * @filter pollux/{static::ID}/before/instructions
58
	 */
59
	public function filterBeforeInstructions()
60
	{
61
		return sprintf( '<pre class="my-sites nav-tab-active misc-pub-section">%s</pre>',
62
			array_reduce( ['title', 'content', 'featured'], function( $instructions, $id ) {
63
				return $instructions . $this->filterInstruction( null, ['slug' => $id], ['slug' => $this->getPostType()] ) . PHP_EOL;
64
			})
65
		);
66
	}
67
68
	/**
69
	 * @param string $instruction
70
	 * @return string
71
	 * @action pollux/{static::ID}/instruction
72
	 */
73
	public function filterInstruction( $instruction, array $field, array $metabox )
74
	{
75
		return sprintf( "ArchiveMeta::%s('%s');", $metabox['slug'], $field['slug'] );
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;
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...
90
	 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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( $key, $fallback, $group );
111
	}
112
113
	/**
114
	 * @return void
115
	 * @action current_screen
116
	 */
117
	public function register()
118
	{
119
		$screenId = ( new 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 pollux/archives/editor
139
	 */
140
	public function renderEditor( $content, $type )
141
	{
142
		wp_editor( $content, 'content', [
143
			'_content_editor_dfw' => true,
144
			'drag_drop_upload' => true,
145
			'editor_height' => 300,
146
			'tabfocus_elements' => 'content-html, publishing-action',
147
			'textarea_name' => sprintf( '%s[%s][content]', static::id(), $type ),
148
			'tinymce' => [
149
				'add_unload_trigger' => false,
150
				'resize' => false,
151
				'wp_autoresize_on' => true,
152
			],
153
		]);
154
	}
155
156
	/**
157
	 * @return void
158
	 * @callback add_meta_box
159
	 */
160
	public function renderFeaturedImageMetaBox( $imageId = null )
161
	{
162
		if( !is_numeric( $imageId )) {
163
			$imageId = ArchiveMeta::get( 'featured', -1, $this->getPostType() );
164
		}
165
		$imageSize = isset( wp_get_additional_image_sizes()['post-thumbnail'] )
166
			? 'post-thumbnail'
167
			: [266, 266];
168
		$thumbnail = get_post( $imageId )
169
			? wp_get_attachment_image( $imageId, $imageSize )
170
			: __( 'Set Featured Image', 'pollux' );
171
172
		$this->render( 'archive/featured', [
173
			'edit_image' => __( 'Click the image to edit or update', 'pollux' ),
174
			'id' => static::id(),
175
			'image_id' => $imageId,
176
			'post_type' => $this->getPostType(),
177
			'remove_image' => __( 'Remove featured image', 'pollux' ),
178
			'thumbnail' => $thumbnail,
179
		]);
180
	}
181
182
	/**
183
	 * @return void
184
	 * @callback add_menu_page
185
	 */
186
	public function renderPage()
187
	{
188
		$type = $this->getPostType();
189
		if( empty( $type ))return;
190
		$labels = get_post_type_labels( get_post_type_object( $type ));
191
		$this->render( 'archive/index', [
192
			'columns' => get_current_screen()->get_columns(),
193
			'content' => ArchiveMeta::get( 'content', '', $type ),
194
			'heading' => sprintf( _x( '%s Archive', 'post archive', 'pollux' ), $labels->singular_name ),
195
			'id' => static::id(),
196
			'post_type' => $type,
197
			'title' => ArchiveMeta::get( 'title', '', $type ),
198
		]);
199
	}
200
201
	/**
202
	 * @return array
203
	 */
204
	protected function getDefaults()
205
	{
206
		return [];
207
	}
208
209
	/**
210
	 * @return string
211
	 */
212
	protected function getPostType()
213
	{
214
		$type = array_search( $this->hook, $this->hooks );
215
		if( !empty( $type ) && is_string( $type )) {
216
			static::$current = $type;
217
		}
218
		return static::$current;
219
	}
220
221
	/**
222
	 * @return array
223
	 */
224
	protected function getPostTypesWithArchive()
225
	{
226
		$types = array_map( function( $value ) {
227
			return sprintf( 'edit.php?post_type=%s', $value );
228
		}, get_post_types( ['has_archive' => 1] ));
229
		return array_merge( $types, ['post' => 'edit.php'] );
230
	}
231
232
	/**
233
	 * @return array
234
	 */
235
	protected function getSettings()
236
	{
237
		return (array) ArchiveMeta::all();
238
	}
239
}
240