-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathEasy_Photography_Portfolio.php
312 lines (232 loc) · 6.76 KB
/
Easy_Photography_Portfolio.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
use Photography_Portfolio\Admin_View\Admin_View;
use Photography_Portfolio\Core\Initialize_Layout_Registry;
use Photography_Portfolio\Core\Query;
use Photography_Portfolio\Frontend\Layout_Factory;
use Photography_Portfolio\Frontend\Layout_Registry;
use Photography_Portfolio\Frontend\Enqueue_Assets;
use Photography_Portfolio\Frontend\Popup_Gallery\Popup_Gallery_Factory;
use Photography_Portfolio\Frontend\Template;
use Photography_Portfolio\Settings\General_Portfolio_Settings;
use Photography_Portfolio\Settings\Setting_Registry;
/**
* Easy_Photography_Portfolio
* @package Easy_Photography_Portfolio
* @type `Singleton` God Object, the worst kind. Yet serves its function.
*
*/
class Easy_Photography_Portfolio {
/**
* This is a Singleton class
* Singletons are almost always bad, surely not this time. Right?....
*/
protected static $_instance = NULL;
/**
* All plugin options will be added in the settings
* @var Setting_Registry $settings
*/
public $settings;
/**
* @var Layout_Registry $layouts
* Contains all available portfolio layouts
*/
public $layouts;
/**
* @var $query Query
* Determines if portfolio is active
*/
public $query;
/**
* @var $layout - The current Layout settings
*/
public $layout;
/**
* Easy Photography Portfolio Version
*
* @var string
*/
private $version = '1.5.1';
/**
* Constructor.
*/
public function __construct() {
define( 'CLM_VERSION', $this->version );
/**
* Everything else is handled in $this->boot(), so that `phort_instance()` is available if needed
*/
}
public function prepare_view() {
/**
* == Boot ==
* Either the front or backend
*/
if ( is_admin() ) {
new Admin_View();
}
else {
new Enqueue_Assets( Popup_Gallery_Factory::create_instance() );
}
// Trigger `phort/core/loaded` as soon as the plugin is fully loaded
do_action( 'phort/core/loaded', $this );
}
/**
* Main Instance.
*
* Ensures only one instance of Easy_Photography_Portfolio is loaded or can be loaded.
*
* @static
* @return Easy_Photography_Portfolio instance
*
* Very Heavily inspired by WooCommerce
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
// Create instance
self::$_instance = new self();
// Boot immediately
self::$_instance->initialize();
}
return self::$_instance;
}
public function load_translations() {
load_plugin_textdomain( 'photography-portfolio', false, dirname( CLM_ABSPATH ) . '/languages' );
}
/**
* Cloning is forbidden.
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, esc_html__( "Can't do this thing.", 'photography-portfolio' ), '2.1' );
}
/**
* Unserializing instances of this class is forbidden.
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, esc_html__( "Can't do this thing.", 'photography-portfolio' ), '2.1' );
}
/**
* Attach "Settings" in plugin action links (in the plugin list)
*
* @param $links
*
* @return mixed
*/
public function action_links( $links ) {
$url = get_admin_url( NULL, 'edit.php?post_type=phort_post&page=phort_options' );
array_unshift(
$links,
'<a href="' . esc_url( $url ) . '">' . esc_html__(
'Settings',
'photography-portfolio'
) . '</a>'
);
return $links;
}
/**
* Setup the plugin setting registry and register the available options
*/
public function setup_settings() {
$this->settings = new Setting_Registry();
// Setup General Settings
$general_settings = new General_Portfolio_Settings( $this->layouts );
// Add all settings to the registry
$this->settings->add_all( $general_settings->get_all() );
}
/**
* Maybe start loading portfolio template files instead of whatever WordPress was going to do.
* This is run @filter `template_include`
*
* @param $template - Current template path
*
* @return string
*/
public function maybe_load_portfolio_template_files( $template ) {
/**
* Exceptions
*/
if ( is_embed() ) {
return $template;
}
/**
* If the current query says that this is a portfolio -
* Load the portfolio view
*/
if ( $this->query->is_portfolio() ) {
/*
* Prepare the layout data
*/
$this->layout = Layout_Factory::autoload();
/*
* Return the initial template file
*/
$located_template = Template::locate( [ 'wrapper.php', 'photography-portfolio.php' ] );
if ( $located_template ) {
return $located_template;
}
}
/**
* Return $template if this is not the portfolio or a template was not located
*/
return $template;
}
/**
* Constructor is only going to set up the core
*/
protected function initialize() {
// If there is anything you want to do before the plugin configures itself
do_action( 'phort/core/prepare', $this );
$this->query = new Query();
// Register Layouts
$this->layouts = Initialize_Layout_Registry::with_defaults();
$this->register_hooks();
}
protected function register_hooks() {
/**
*
* Register Post Types
*
* post_type: `phort_post`
* taxonomy: `phort_post_category`
* Turns out taxonomies have to be registered before the
* post type is registered to get pretty URLs like `/portfolio/category/%cat`
*
* @link https://cnpagency.com/blog/the-right-way-to-do-wordpress-custom-taxonomy-rewrites/
*
* `add_action` order is improtant here:
*/
add_action( 'init', [ 'Photography_Portfolio\Core\Post_Type_Portfolio', 'register_taxonomy' ], 5 );
add_action( 'init', [ 'Photography_Portfolio\Core\Post_Type_Portfolio', 'register_post_type' ], 5 );
/*
* Setup the settings
* Crucial for `phort_get_option` to work properly, which is almost at the core of everything else further down the pipe
*/
add_action( 'init', [ $this, 'setup_settings' ], 7 );
// Load Translations:
add_action( 'init', [ $this, 'load_translations' ] );
/**
* Load `Admin_View` or `Enqueue_Assets`
*/
add_action( 'init', [ $this, 'prepare_view' ], 30 );
/*
*
* This at the core of loading all of the Easy Photography Portfolio template files
*/
add_filter( 'template_include', [ $this, 'maybe_load_portfolio_template_files' ], 150 );
/**
* Modify the WP_Query before posts are queried.
*/
if ( ! is_admin() ) {
add_action( 'pre_get_posts', [ $this->query, 'store_original_query' ], 2 );
add_action( 'pre_get_posts', [ $this->query, 'set_variables' ], 5 );
add_action( 'pre_get_posts', [ $this->query, 'increase_ppp_limit' ], 25 );
}
/**
*
* == Miscellaneous ==
*
*/
// Maybe add video support
add_action( 'init', [ 'Photography_Portfolio\Core\Gallery_Attachment_Video_Support', 'maybe_add_hooks' ] );
// Add "Settings" to plugin links in plugin page
add_filter( 'plugin_action_links_' . CLM_PLUGIN_BASENAME, [ $this, 'action_links' ] );
}
}