Passed
Push — develop ( f91847...084281 )
by Paul
03:07
created

Archive::getMetaValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 4
rs 10
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
	 * @param string $key
86
	 * @param mixed $fallback
87
	 * @param string $group
88
	 * @return string|array
89
	 */
90
	public function getMetaValue( $key, $fallback = '', $group = '' )
91
	{
92
		return ArchiveMeta::get( $key, $fallback, $group );
93
	}
94
95
	/**
96
	 * @return void
97
	 * @action current_screen
98
	 */
99
	public function register()
100
	{
101
		$screenId = ( new Helper )->getCurrentScreen()->id;
102
		if( in_array( $screenId, $this->hooks )) {
103
			$this->hook = $screenId;
104
		}
105
		parent::register();
106
	}
107
108
	/**
109
	 * @return void
110
	 * @action pollux/archives/init
111
	 */
112
	public function registerFeaturedImageMetaBox()
113
	{
114
		if( !current_user_can( 'upload_files' ))return;
115
		add_meta_box( 'postimagediv', __( 'Featured Image', 'pollux' ), [$this, 'renderFeaturedImageMetaBox'], null, 'side', 'low' );
116
	}
117
118
	/**
119
	 * @return void
120
	 * @action pollux/archives/editor
121
	 */
122
	public function renderEditor( $content, $type )
123
	{
124
		wp_editor( $content, 'content', [
125
			'_content_editor_dfw' => true,
126
			'drag_drop_upload' => true,
127
			'editor_height' => 300,
128
			'tabfocus_elements' => 'content-html, publishing-action',
129
			'textarea_name' => sprintf( '%s[%s][content]', static::id(), $type ),
130
			'tinymce' => [
131
				'add_unload_trigger' => false,
132
				'resize' => false,
133
				'wp_autoresize_on' => true,
134
			],
135
		]);
136
	}
137
138
	/**
139
	 * @return void
140
	 * @callback add_meta_box
141
	 */
142
	public function renderFeaturedImageMetaBox()
143
	{
144
		$imageId = ArchiveMeta::get( 'featured', -1, $this->getPostType() );
145
		$imageSize = isset( wp_get_additional_image_sizes()['post-thumbnail'] )
146
			? 'post-thumbnail'
147
			: [266, 266];
148
		$thumbnail = get_post( $imageId )
149
			? wp_get_attachment_image( $imageId, $imageSize )
150
			: __( 'Set Featured Image', 'pollux' );
151
152
		$this->render( 'archive/featured', [
153
			'edit_image' => __( 'Click the image to edit or update', 'pollux' ),
154
			'id' => static::id(),
155
			'image_id' => $imageId,
156
			'post_type' => $this->getPostType(),
157
			'remove_image' => __( 'Remove featured image', 'pollux' ),
158
			'thickbox_url' => '',
159
			'thumbnail' => $thumbnail,
160
		]);
161
	}
162
163
	/**
164
	 * @return void
165
	 * @callback add_menu_page
166
	 */
167
	public function renderPage()
168
	{
169
		$type = $this->getPostType();
170
		if( empty( $type ))return;
171
		$labels = get_post_type_labels( get_post_type_object( $type ));
172
		$this->render( 'archive/index', [
173
			'columns' => get_current_screen()->get_columns(),
174
			'content' => ArchiveMeta::get( 'content', '', $type ),
175
			'heading' => sprintf( _x( '%s Archive', 'post archive', 'pollux' ), $labels->singular_name ),
176
			'id' => static::id(),
177
			'post_type' => $type,
178
			'title' => ArchiveMeta::get( 'title', '', $type ),
179
		]);
180
	}
181
182
	/**
183
	 * @return array
184
	 */
185
	protected function getDefaults()
186
	{
187
		return [];
188
	}
189
190
	/**
191
	 * @return string
192
	 */
193
	protected function getPostType()
194
	{
195
		$type = array_search( $this->hook, $this->hooks );
196
		return !empty( $type ) && is_string( $type )
197
			? $type
198
			: '';
199
	}
200
201
	/**
202
	 * @return array
203
	 */
204
	protected function getPostTypesWithArchive()
205
	{
206
		$types = array_map( function( $value ) {
207
			return sprintf( 'edit.php?post_type=%s', $value );
208
		}, get_post_types( ['has_archive' => 1] ));
209
		return array_merge( $types, ['post' => 'edit.php'] );
210
	}
211
212
	/**
213
	 * @return array
214
	 */
215
	protected function getSettings()
216
	{
217
		return (array) ArchiveMeta::all();
218
	}
219
}
220