-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpressroom.php
408 lines (367 loc) · 9.83 KB
/
pressroom.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
/*
Plugin Name: Pressroom
Plugin URI: http://press-room.io/
Description: PressRoom turns Wordpress into a multi channel publishing environment.
Version: 1.3
Author: thePrintLabs Ltd
Author URI: http://theprintlabs.com
License: GPLv2
*/
if (!defined( 'ABSPATH' )) exit; // Exit if accessed directly
require_once __DIR__ . '/autoload.php';
class TPL_Pressroom
{
public $configs;
public $edition;
public $preview;
public function __construct() {
if ( !is_admin() ) {
return;
}
$this->_load_configs();
$this->_hooks();
$this->_actions();
$this->_filters();
$this->_create_edition();
$this->_create_preview();
}
/**
* Activation plugin:
* Setup database tables and filesystem structure
*
* @void
*/
public function plugin_activation() {
$response = PR_Setup::install();
if ( false !== $response ) {
$html = '<h1>' . __('Pressroom') . '</h1>
<p><b>' .__( 'An error occurred during activation. Please see details below.', 'pressroom_setup' ). '</b></p>
<ul><li>' .implode( "</li><li>", $response ). '</li></ul>';
wp_die( $html, __( 'Pressroom activation error', 'pressroom_setup' ), ('back_link=true') );
}
do_action( 'press_flush_rules' );
flush_rewrite_rules();
}
/**
* Deactivation plugin
*
* @void
*/
public function plugin_deactivation() {
flush_rewrite_rules();
PR_Cron::disable();
}
/**
* Add connection between
* the edition and other allowed post type.
*
* @void
*/
public function register_post_connection() {
$types = $this->get_allowed_post_types();
p2p_register_connection_type( array(
'name' => P2P_EDITION_CONNECTION,
'from' => $types,
'to' => PR_EDITION,
'admin_column' => 'any',
'admin_dropdown' => 'from',
'sortable' => false,
'title' => array(
'from' => __( 'Included into issue', 'pressroom' )
),
'to_labels' => array(
'singular_name' => __( 'Issue', 'pressroom' ),
'search_items' => __( 'Search issue', 'pressroom' ),
'not_found' => __( 'No issue found.', 'pressroom' ),
'create' => __( 'Select an issue', 'pressroom' ),
),
'admin_box' => array(
'show' => 'from',
'context' => 'side',
'priority' => 'high',
),
'fields' => array(
'status' => array(
'title' => __( 'Visible', 'pressroom' ),
'type' => 'checkbox',
'default' => 1,
),
'template' => array(
'title' => '',
'type' => 'hidden',
'values' => array(),
),
'order' => array(
'title' => '',
'type' => 'hidden',
'default' => 0,
'values' => array(),
),
)
) );
}
/**
* Add default theme template to post connection
*
* @param int $p2p_id
* @void
*/
public function post_connection_add_default_theme( $p2p_id ) {
$connection = p2p_get_connection( $p2p_id );
if ( $connection->p2p_type == P2P_EDITION_CONNECTION ) {
$themes = PR_Theme::get_themes();
$selected_theme = get_post_meta( $connection->p2p_to, '_pr_theme_select', true );
if ( !empty( $themes ) && $selected_theme ) {
$pages = $themes[$selected_theme];
foreach ( $pages as $page ) {
if ( isset( $page['rule'] ) && $page['rule'] == 'post' ) {
p2p_add_meta( $p2p_id, 'template', $page['path'] );
}
}
}
}
}
/**
* Check admin notices and display
*
* @echo
*/
public function check_pressroom_notice() {
if ( isset( $_GET['pmtype'], $_GET['pmcode'] ) ) {
$msg_type = $_GET['pmtype'];
$msg_code = $_GET['pmcode'];
$msg_param = isset( $_GET['pmparam'] ) ? urldecode( $_GET['pmparam'] ) : '';
echo '<div class="pr-alert ' . $msg_type . '"><p>';
switch ( $msg_code ) {
case 'theme':
echo _e( '<b>Error:</b> You must specify a theme for issue!', 'pressroom_notice' );
break;
case 'duplicate_entry':
echo _e( sprintf('<b>Error:</b> Duplicate entry for <b>%s</b>. It must be unique', $msg_param ) );
break;
case 'failed_activated_license':
echo _e( sprintf('<b>Error during activation:</b> %s', $msg_param ) );
break;
case 'success_activated_license':
echo _e( sprintf('<b>Activation successfully:</b> %s', $msg_param ) );
break;
case 'failed_deactivated_license':
echo _e( sprintf('<b>Error during deactivation:</b> %s', $msg_param ) );
break;
case 'success_deactivated_license':
echo _e( '<b>License Deactivated.</b>' );
break;
case 'themes_cache_flushed':
echo _e( '<b>Themes cache flushed successfully</b>' );
break;
}
echo '</p></div>';
}
}
/**
* Unset theme root to exclude custom filter override
*
* @param string $path
* return string
*/
public function set_theme_root( $path ) {
update_option( 'pr_theme_root', $path );
if ( isset( $_GET['pr_no_theme'] ) ) {
return PR_THEMES_PATH;
}
return $path;
}
public function set_theme_uri( $uri ) {
update_option( 'pr_theme_uri', $uri );
if ( isset( $_GET['pr_no_theme'] ) ) {
return PR_THEME_URI;
}
return $uri;
}
/**
* Override default wordpress theme
*
* @param string $name
* return string
*/
public function set_template_name( $name ) {
if ( isset( $_GET['pr_no_theme'], $_GET['edition_id'] ) ) {
$name = PR_Theme::get_theme_path( $_GET['edition_id'], false );
}
return $name;
}
/*
* Get all allowed post types
*
* @return array
*/
public function get_allowed_post_types() {
$types = array( 'post', 'page' );
$custom_types = $this->_load_custom_post_types();
$types = array_merge( $types, $custom_types );
return $types;
}
/**
* Check if is add or edit page
*
* @param string $new_edit
* @return boolean
*/
public static function is_edit_page() {
global $pagenow;
if ( !is_admin() ) {
return false;
}
return in_array( $pagenow, array( 'post.php' ) );
}
/**
* Render admin notice for permalink
*
* @void
*/
public function permalink_notice() {
$setting_page_url = admin_url() . 'options-permalink.php';
echo '
<div class="error pr-alert">
<p>' . __( sprintf( 'Pressroom: PressRoom require <i>Post Name</i> format for permalink. You can set it in <a href="%s">setting page</a>', $setting_page_url ), 'pressroom' ). '</p>
</div>';
}
/**
* Load add-ons exporters
*
* @void
*/
public function load_exporters() {
do_action_ref_array( 'pressroom/add_ons', array() );
}
/**
* Get all core exporters from relative dir
*
* @return boolean
*/
public function load_core_exporters() {
$exporters = PR_Utils::search_dir( PR_PACKAGER_EXPORTERS_PATH );
if ( !empty( $exporters ) ) {
foreach ( $exporters as $exporter ) {
$file = trailingslashit( PR_PACKAGER_EXPORTERS_PATH . $exporter ) . "index.php";
if ( is_file( $file ) ) {
require_once $file;
}
}
return true;
}
return false;
}
/**
* Add jQuery datepicker script and css styles
* @void
*/
public function register_pressroom_styles() {
wp_register_style( 'pressroom', PR_ASSETS_URI . 'css/pressroom.css' );
wp_enqueue_style( 'pressroom' );
}
/*
* Register hooks
* @void
*/
protected function _hooks() {
register_activation_hook( __FILE__, array( $this, 'plugin_activation' ) );
register_deactivation_hook( __FILE__, array( $this, 'plugin_deactivation' ) );
}
/**
* Register actions
* @void
*/
protected function _actions() {
if ( !$this->_check_permalink_structure() ) {
add_action( 'admin_notices', array( $this, 'permalink_notice' ) );
}
add_action( 'admin_notices', array( $this, 'check_pressroom_notice' ), 20 );
add_action( 'p2p_init', array( $this, 'register_post_connection' ) );
/* Once all plugin are loaded, load core and external exporters */
add_action( 'plugins_loaded', array( $this, 'load_exporters' ) );
add_action( 'plugins_loaded', array( $this, 'load_core_exporters' ), 20 );
add_action( 'admin_init', array( $this, 'register_pressroom_styles' ), 20 );
}
/**
* Register filters
* @void
*/
protected function _filters() {
add_filter( 'p2p_created_connection', array( $this, 'post_connection_add_default_theme' ) );
/* Override default wordpress theme path */
add_filter( 'theme_root', array( $this, 'set_theme_root' ), 10 );
add_filter( 'theme_root_uri', array( $this, 'set_theme_uri' ), 10 );
add_filter( 'template', array( $this, 'set_template_name'), 10 );
add_filter( 'stylesheet', array( $this, 'set_template_name'), 10 );
}
/**
* Check if permalink structure is set to Post Name
* ( required for Editorial Project endpoint )
*
* @return boolean
*/
protected function _check_permalink_structure() {
$structure = get_option('permalink_structure');
if( $structure != "/%postname%/" ) {
return false;
}
return true;
}
/**
* Load plugin configuration settings
*
* @void
*/
protected function _load_configs() {
if ( is_null( $this->configs ) ) {
$this->configs = get_option('pr_settings', array(
'pr_custom_post_type' => array()
));
}
}
/**
* Load custom post types configured in settings page
*
* @return array - custom post types
*/
protected function _load_custom_post_types() {
$types = array();
if ( !empty( $this->configs ) && isset( $this->configs['pr_custom_post_type'] ) ) {
$custom_types = $this->configs['pr_custom_post_type'];
if ( is_array( $custom_types ) ) {
foreach ( $custom_types as $post_type ) {
if ( strlen( $post_type ) ) {
array_push( $types, $post_type );
}
}
} elseif ( is_string( $custom_types ) && strlen( $custom_types ) ) {
array_push( $types, $custom_types );
}
}
return $types;
}
/**
* Instance a new edition object
*
* @void
*/
protected function _create_edition() {
if ( is_null( $this->edition ) ) {
$this->edition = new PR_Edition();
}
}
/**
* Instance a new preview object
*
* @void
*/
protected function _create_preview() {
if ( is_null( $this->preview ) ) {
$this->preview = new PR_Preview;
}
}
}
// Instantiate the plugin class
$tpl_pressroom = new TPL_Pressroom();