Passed
Push — develop ( 37b65a...9b346b )
by Paul
03:39
created

Archive::getPostTypesWithArchive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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 $hooks = [];
18
19
	/**
20
	 * {@inheritdoc}
21
	 */
22
	public function init()
23
	{
24
		parent::init();
25
26
		add_action( 'pollux/archives/init',              [$this, 'registerFeaturedImageMetaBox'] );
27
		add_action( 'pollux/archives/editor',            [$this, 'renderEditor'], 10, 2 );
28
		add_filter( 'pollux/archives/metabox/submit',    [$this, 'filterSubmitMetaBox'] );
29
		add_filter( 'pollux/archives/show/instructions', '__return_true' );
30
	}
31
32
	/**
33
	 * @return void
34
	 * @action admin_menu
35
	 */
36
	public function addPage()
37
	{
38
		foreach( $this->getPostTypesWithArchive() as $type => $page ) {
39
			$labels = get_post_type_labels( get_post_type_object( $type ));
40
			$this->hooks[$type] = call_user_func_array( 'add_submenu_page', $this->filter( 'page', [
41
				$page,
42
				sprintf( _x( '%s Archive', 'post archive', 'pollux' ), $labels->singular_name ),
43
				__( 'Archive', 'pollux' ),
44
				'edit_theme_options',
45
				sprintf( '%s_archive', $type ),
46
				[$this, 'renderPage'],
47
			]));
48
		}
49
	}
50
51
	/**
52
	 * @return string
53
	 * @filter pollux/{static::ID}/before/instructions
54
	 */
55
	public function filterBeforeInstructions()
56
	{
57
		return sprintf( '<pre class="my-sites nav-tab-active misc-pub-section">%s</pre>',
58
			array_reduce( ['title', 'content', 'featured'], function( $instructions, $id ) {
59
				return $instructions . $this->filterInstruction( null, ['slug' => $id], ['slug' => $this->getPostType()] ) . PHP_EOL;
60
			})
61
		);
62
	}
63
64
	/**
65
	 * @param string $instruction
66
	 * @return string
67
	 * @action pollux/{static::ID}/instruction
68
	 */
69
	public function filterInstruction( $instruction, array $field, array $metabox )
70
	{
71
		return sprintf( "ArchiveMeta::%s('%s');", $metabox['slug'], $field['slug'] );
72
	}
73
74
	/**
75
	 * @return array
76
	 * @action pollux/{static::ID}/metabox/submit
77
	 */
78
	public function filterSubmitMetaBox( array $args )
79
	{
80
		$args[1] = __( 'Save Archive', 'pollux' );
81
		return $args;
82
	}
83
84
	/**
85
	 * @return void
86
	 * @action current_screen
87
	 */
88
	public function register()
89
	{
90
		$screenId = ( new Helper )->getCurrentScreen()->id;
91
		if( in_array( $screenId, $this->hooks )) {
92
			$this->hook = $screenId;
93
		}
94
		parent::register();
95
	}
96
97
	/**
98
	 * @return void
99
	 * @action pollux/archives/init
100
	 */
101
	public function registerFeaturedImageMetaBox()
102
	{
103
		if( !current_user_can( 'upload_files' ))return;
104
		add_meta_box( 'postimagediv', __( 'Featured Image', 'pollux' ), [$this, 'renderFeaturedImageMetaBox'], null, 'side', 'low' );
105
	}
106
107
	/**
108
	 * @return void
109
	 * @action pollux/archives/editor
110
	 */
111
	public function renderEditor( $content, $type )
112
	{
113
		wp_editor( $content, 'content', [
114
			'_content_editor_dfw' => true,
115
			'drag_drop_upload' => true,
116
			'editor_height' => 300,
117
			'tabfocus_elements' => 'content-html, publishing-action',
118
			'textarea_name' => sprintf( '%s[%s][content]', static::id(), $type ),
119
			'tinymce' => [
120
				'add_unload_trigger' => false,
121
				'resize' => false,
122
				'wp_autoresize_on' => true,
123
			],
124
		]);
125
	}
126
127
	/**
128
	 * @return void
129
	 * @callback add_meta_box
130
	 */
131
	public function renderFeaturedImageMetaBox()
132
	{
133
		$imageId = ArchiveMeta::get( 'featured', -1, $this->getPostType() );
134
		$imageSize = isset( wp_get_additional_image_sizes()['post-thumbnail'] )
135
			? 'post-thumbnail'
136
			: [266, 266];
137
		$thumbnail = get_post( $imageId )
138
			? wp_get_attachment_image( $imageId, $imageSize )
139
			: __( 'Set Featured Image', 'pollux' );
140
141
		$this->render( 'archive/featured', [
142
			'edit_image' => __( 'Click the image to edit or update', 'pollux' ),
143
			'id' => static::id(),
144
			'image_id' => $imageId,
145
			'post_type' => $this->getPostType(),
146
			'remove_image' => __( 'Remove featured image', 'pollux' ),
147
			'thickbox_url' => '',
148
			'thumbnail' => $thumbnail,
149
		]);
150
	}
151
152
	/**
153
	 * @return void
154
	 * @callback add_menu_page
155
	 */
156
	public function renderPage()
157
	{
158
		$type = $this->getPostType();
159
		if( empty( $type ))return;
160
		$labels = get_post_type_labels( get_post_type_object( $type ));
161
		$this->render( 'archive/index', [
162
			'columns' => get_current_screen()->get_columns(),
163
			'content' => ArchiveMeta::get( 'content', '', $type ),
164
			'heading' => sprintf( _x( '%s Archive', 'post archive', 'pollux' ), $labels->singular_name ),
165
			'id' => static::id(),
166
			'post_type' => $type,
167
			'title' => ArchiveMeta::get( 'title', '', $type ),
168
		]);
169
	}
170
171
	/**
172
	 * @return array
173
	 */
174
	protected function getDefaults()
175
	{
176
		return [];
177
	}
178
179
	/**
180
	 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|string?

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...
181
	 */
182
	protected function getPostType()
183
	{
184
		$type = array_search( $this->hook, $this->hooks );
185
		return !empty( $type )
186
			? $type
187
			: '';
188
	}
189
190
	/**
191
	 * @return array
192
	 */
193
	protected function getPostTypesWithArchive()
194
	{
195
		$types = array_map( function( $value ) {
196
			return sprintf( 'edit.php?post_type=%s', $value );
197
		}, get_post_types( ['has_archive' => 1] ));
198
		return array_merge( $types, ['post' => 'edit.php'] );
199
	}
200
201
	/**
202
	 * @return array
203
	 */
204
	protected function getSettings()
205
	{
206
		return (array) ArchiveMeta::all();
207
	}
208
209
	/**
210
	 * @return string|array
211
	 */
212
	protected function getValue( $key, $group )
213
	{
214
		return ArchiveMeta::get( $key, '', $group );
215
	}
216
}
217