-
Notifications
You must be signed in to change notification settings - Fork 0
/
brafton_wordpress_plugin.php
370 lines (352 loc) · 21.9 KB
/
brafton_wordpress_plugin.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
<?php
/*
Plugin Name: Brafton WordPress Plugin
Plugin URI: http://www.brafton.com/support/wordpress
version: 1.5.0
Requires at least: 3.8
Tested up to: 3.9.1
Description: Automates Brafton Inc, ContentLEAD, and Castleford Media content publishing.
Author: Brafton Inc
Author URI: http://www.brafton.com/support/wordpress
GitHub Plugin URI: ContentLEAD/brafton-wordpress-plugin
GitHub Plugin URI: https://github.com/ContentLEAD/brafton-wordpress-plugin
*/
if( !class_exists( 'WP_Brafton_Article_Importer' ) )
{
if ( !defined( 'BRAFTON_PLUGIN_VERSION_KEY' ) )
define( 'BRAFTON_PLUGIN_VERSION_KEY', 'brafton_importer_version' );
if ( !defined( 'MYPLUGIN_VERSION_NUM' ) )
define( 'BRAFTON_PLUGIN_VERSION_NUM', '1.0.0' );
include_once( plugin_dir_path( __FILE__ ) .'/src/brafton_article_helper.php' );
include_once( plugin_dir_path( __FILE__ ) . '/src/brafton_taxonomy.php' );
include_once( plugin_dir_path( __FILE__ ) . '/src/brafton_image_handler.php' );
include_once( plugin_dir_path( __FILE__ ) . '/src/brafton_article_importer.php' );
include_once( plugin_dir_path( __FILE__ ) . '/src/brafton_errors.php' );
include_once( plugin_dir_path( __FILE__ ) . '/src/brafton_video_helper.php' );
include_once( plugin_dir_path( __FILE__ ) . '/src/brafton_video_importer.php' );
include_once( plugin_dir_path( __FILE__ ) . '/src/brafton_validator.php' );
include_once( plugin_dir_path( __FILE__ ) . '/src/brafton_hooks.php' );
class WP_Brafton_Article_Importer
{
public $brafton_options;
/**
* Construct the plugin object
*/
public function __construct()
{
// Initialize Settings
require_once( sprintf( "%s/src/brafton_errors.php", dirname( __FILE__ ) ) );
require_once( sprintf( "%s/src/admin/brafton_options.php", dirname( __FILE__ ) ) );
require_once( sprintf( "%s/src/admin/brafton_fields.php", dirname( __FILE__ ) ) );
require_once( sprintf( "%s/src/admin/brafton_admin_notice.php", dirname( __FILE__ ) ) );
$brafton_fields = new Brafton_fields();
$brafton_options = Brafton_options::get_instance();
require_once( sprintf( "%s/src/admin/brafton_settings.php", dirname( __FILE__ ) ) );
$brafton_importer_settings = new WP_Brafton_Article_Importer_Settings( $brafton_options, $brafton_fields );
// Register custom post types
require_once( sprintf( "%s/src/post-types/brafton_content_template.php", dirname( __FILE__ ) ) );
$article_post_type = $brafton_options->options['brafton_article_post_type'];
$article_post_type_name = $brafton_options->brafton_get_post_type( $article_post_type );
if( $article_post_type ){
$Brafton_Article_Template = new Brafton_Article_Template( $brafton_options, $article_post_type_name, array( 'singular' => 'Article', 'plural' => 'Articles' ), array( 'brafton_id', 'photo_id',) );
//only log when importer is executed.
if( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == true )
brafton_log( array( 'message' => "Article post type is ready for more content. Article custom post type id: " . $article_post_type) );
}
$video_post_type = $brafton_options->options['brafton_video_post_type'];
$video_post_type_name = $brafton_options->brafton_get_post_type( $video_post_type );
if( $video_post_type ){
$brafton_Video_Template = new Brafton_Article_Template( $brafton_options, $video_post_type_name, array( 'singular' => 'Video', 'plural' => 'Videos' ), array( 'brafton_id', 'photo_id', 'brafton_pause_cta_link', 'brafton_pause_cta_text', 'brafton_end_cta_title' , 'brafton_end_cta_subtitle', 'brafton_end_cta_button_text', 'brafton_end_cta_button_link', 'brafton_video_presplash' ) );
if( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == true )
brafton_log( array( 'message' => "Video post type is ready for more content. Video custom post type id: " . $video_post_type) );
}
} // END public function __construct
/**
* Activate the plugin
*/
public static function activate()
{
//add actions and filters here:
// Do nothing
} // END public static function activate
/**
* Deactivate the plugin
*/
public static function deactivate()
{
$brafton_options = Brafton_options::get_instance();
//remove scheduled hook.
brafton_import_clear_crons( 'brafton_import_trigger_hook' );
if( $brafton_options->options['brafton_purge'] == 'posts' )
{
brafton_log( array( 'message' => "attempting to delete articles" ) );
$brafton_options->purge_articles();
}
if( $brafton_options->options['brafton_purge'] == 'all' )
{
$brafton_options->purge_articles();
//$brafton_optinos->purge_options();
}
//Flush rewrite rules if custom post types are enabled.
if( $brafton_options->options['brafton_article_post_type'] != "" || $brafton_options->options['brafton_video_post_type'] != "" )
flush_rewrite_rules();
// Do nothing
} // END public static function deactivate
} // END class WP_Brafton_Article_Importer
} // END if(!class_exists('WP_Brafton_Article_Importer'))
if( class_exists( 'WP_Brafton_Article_Importer' ) )
{
// Installation and uninstallation hooks
register_activation_hook( __FILE__, array( 'WP_Brafton_Article_Importer', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'WP_Brafton_Article_Importer', 'deactivate' ) );
// instantiate the plugin class
$WP_Brafton_Article_Importer = new WP_Brafton_Article_Importer();
/* This is the scheduling hook for our plugin that is triggered by cron */
#add_action('brafton_import_trigger_hook', 'run_import', 10, 2);
// Add a link to the settings page onto the plugin page
if( isset( $WP_Brafton_Article_Importer ) )
{
// Add the settings link to the plugins page
function plugin_settings_link( $links )
{
$settings_link = '<a href="options-general.php?page=WP_Brafton_Article_Importer">Settings</a>';
array_unshift( $links, $settings_link );
return $links;
}
$plugin = plugin_basename( __FILE__ );
add_filter( "plugin_action_links_$plugin", 'plugin_settings_link' );
//Manually run importer when settings are saved.
add_action( 'load-toplevel_page_WP_Brafton_Article_Importer', 'run_article_import' );
add_action( 'load-toplevel_page_WP_Brafton_Article_Importer', 'run_video_import' );
//Run video and article importers when archives form is saved
add_action( 'load-brafton_page_brafton_archives', 'run_article_import' );
require_once plugin_dir_path( __FILE__ ) . '/vendors/tgm-activation.php';
add_action( 'load-brafton_page_WP_Brafton_Article_Importer', 'brafton_admin_notice' );
add_action( 'tgmpa_register', 'brafton_setup_recommended_plugins' );
function brafton_setup_recommended_plugins(){
$brafton_options = Brafton_options::get_instance();
$plugins = array(
array(
'name' => $brafton_options->brafton_get_product() . ' Analytics Dashboard', // The plugin name
'slug' => 'brafton-analytics-dashboard', // The plugin slug (typically the folder name)
'source' => plugin_dir_path( __FILE__ ) . 'vendors/recommended-plugins/brafton-analytics-dashboard.zip', // The plugin source
'required' => false, // If false, the plugin is only 'recommended' instead of required
'force_activation' => false, // If true, plugin is activated upon theme activation and cannot be deactivated until theme switch
),
array(
'name' => 'WordPress SEO',
'slug' => 'wordpress-seo',
'required' => false,
),
array(
'name' => 'Github Updater',
'slug' => 'github-updater-master',
'source' => 'https://github.com/afragen/github-updater/archive/master.zip',
'required' => true,
'force_activation' => true,
),
array(
'name' => 'Google Analytics for WordPress',
'slug' => 'google-analytics-for-wordpress',
'required' => false,
),
array(
'name' => 'Google Sitemap Generator',
'slug' => 'google-sitemap-generator',
'required' => false,
),
array(
'name' => 'Contact Form 7',
'slug' => 'contact-form-7',
'required' => false,
)
);
$config = array(
'id' => 'brafton_tgmpa', // Unique ID for hashing notices for multiple instances of TGMPA.
'default_path' => '', // Default absolute path to pre-packaged plugins.
'menu' => 'tgmpa-install-plugins', // Menu slug.
'has_notices' => true, // Show admin notices or not.
'dismissable' => false, // If false, a user cannot dismiss the nag message.
'dismiss_msg' => $brafton_options->brafton_get_product() . ' recommended plugins not installed.', // If 'dismissable' is false, this message will be output at top of nag.
'is_automatic' => true, // Automatically activate plugins after installation or not.
'message' => 'Brafton Inc. recommends the following wp plugins.', // Message to output right before the plugins table.
'strings' => array(
'notice_can_install_required' => _n_noop( $brafton_options->brafton_get_product() .' plugin requires the following plugin: %1$s.', 'Brafton plugin requires the following plugins: %1$s.', 'tgmpa' ),
'notice_can_install_recommended' => _n_noop( $brafton_options->brafton_get_product() . ' recommends the following plugin: %1$s.', 'Brafton Inc. recommends the following plugins: %1$s.', 'tgmpa' ),
'notice_cannot_install' => _n_noop( 'Sorry, but you do not have the correct permissions to install the %s plugin. Contact the administrator of this site for help on getting the plugin installed.', 'Sorry, but you do not have the correct permissions to install the %s plugins. Contact the administrator of this site for help on getting the plugins installed.', 'tgmpa' ), // %1$s = plugin name(s).
'notice_can_activate_required' => _n_noop( 'The following required plugin is currently inactive: %1$s.', 'The following required plugins are currently inactive: %1$s.', 'tgmpa' ), // %1$s = plugin name(s).
'notice_can_activate_recommended' => _n_noop( 'The following recommended plugin is currently inactive: %1$s.', 'The following recommended plugins are currently inactive: %1$s.', 'tgmpa' ), // %1$s = plugin name(s).
'notice_cannot_activate' => _n_noop( 'Sorry, but you do not have the correct permissions to activate the %s plugin. Contact the administrator of this site for help on getting the plugin activated.', 'Sorry, but you do not have the correct permissions to activate the %s plugins. Contact the administrator of this site for help on getting the plugins activated.', 'tgmpa' ), // %1$s = plugin name(s).
'notice_ask_to_update' => _n_noop( 'The following plugin needs to be updated to its latest version to ensure maximum compatibility with the Brafton Plugin: %1$s.', 'The following plugins need to be updated to their latest version to ensure maximum compatibility with the Brafton Plugin: %1$s.', 'tgmpa' ), // %1$s = plugin name(s).
'notice_cannot_update' => _n_noop( 'Sorry, but you do not have the correct permissions to update the %s plugin. Contact the administrator of this site for help on getting the plugin updated.', 'Sorry, but you do not have the correct permissions to update the %s plugins. Contact the administrator of this site for help on getting the plugins updated.', 'tgmpa' ), // %1$s = plugin name(s).
'install_link' => _n_noop( 'Begin installing plugin', 'Begin installing plugins', 'tgmpa' ),
'activate_link' => _n_noop( 'Begin activating plugin', 'Begin activating plugins', 'tgmpa' ),
'return' => __( 'Return to Required Plugins Installer', 'tgmpa' ),
'plugin_activated' => __( 'Plugin activated successfully.', 'tgmpa' ),
'complete' => __( 'All plugins installed and activated successfully. %s', 'tgmpa' ), // %s = dashboard link.
'nag_type' => 'updated' // Determines admin notice type - can only be 'updated', 'update-nag' or 'error'.
)
);
tgmpa( $plugins, $config );
}
/**
* Run the article importer
*/
function run_article_import( $cron = null ){
//Wait until settings are saved or cron is triggered before attempting to import articles
if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == true || isset( $_FILES['brafton-archive']['tmp_name'] ) || $cron )
{
//Grab saved options.
$brafton_options = Brafton_options::get_instance();
//If article importing is disabled - do nothing
if( $brafton_options->options['brafton_import_articles'] === 'off' ) {
brafton_log( array( 'message' => "Article importing is disabled." ) );
return;
}
//If api key isn't set - do nothing
if( $brafton_options->options['brafton_api_key'] === "" ) {
brafton_log( array( 'message' => " Brafton Api key is not set." ) );
return;
}
//if brafton error reporting is enabled - log importing.
brafton_log( array( 'message' => 'Starting to import articles.' ) );
$validator = new Brafton_Validator();
$brafton_cats = new Brafton_Taxonomy( $brafton_options );
$brafton_tags = new Brafton_Taxonomy( $brafton_options );
$brafton_image = new Brafton_Image_Handler( $brafton_options, $validator );
$brafton_article = new Brafton_Article_Helper( $brafton_options );
$brafton_article_importer = new Brafton_Article_Importer(
$brafton_image,
$brafton_cats,
$brafton_tags,
$brafton_article,
$brafton_options
);
//run anything attached to this hook.
brafton_before_article_import_hook( $brafton_article_importer );
$log = array(
'count' => 0, //number of reports stored. Empty initially.
'limit' => 2000, //ingeger -limit log entries capacity
'priority' => 0, //0 - log entries only when brafton errors enabled; 1 -log entries always
'entries' => array() //array of report objects
);
brafton_initialize_log( 'brafton_import_status', $log );
//Import articles.
$brafton_article_importer->import_articles();
//run anything attached to this hook.
brafton_after_article_import_hook( $brafton_article_importer );
echo brafton_render_log( array(2), 'brafton_import_status' );
//Schedule importer to run the next hour.
brafton_schedule_import();
}
}
/**
* Run importer for video articles
*
*
* @param bool $cron
*/
function run_video_import( $cron = null )
{
//Wait until settings are saved or cron is triggered before attempting to import articles
if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == true || $cron )
{
$brafton_options = Brafton_options::get_instance();
if( $brafton_options->options['brafton_enable_video'] === 'off' ) {
brafton_log( array( 'message' => 'Video importing is disabled') );
return;
}
brafton_log( array( 'message' => 'Starting to import videos.' ) );
$brafton_cats = new Brafton_Taxonomy( $brafton_options );
$brafton_tags = new Brafton_Taxonomy( $brafton_options );
$brafton_image = new Brafton_Image_Handler( $brafton_options );
$brafton_video = new Brafton_Video_Helper( $brafton_options );
$brafton_video_importer = new Brafton_Video_Importer(
$brafton_image,
$brafton_cats,
$brafton_video,
$brafton_options
);
brafton_before_video_import_hook( $brafton_video_importer );
$brafton_video_importer->import_videos();
brafton_before_video_import_hook( $Brafton_Video_Importer );
//Schedule importer.
brafton_schedule_import();
}
}
#run duplicate killer if version is not appropriate
}
/**
* Update video embed codes.
*/
add_action( 'init', 'update_video_embed' );
function update_video_embed(){
$options = Brafton_Options::get_instance();
$brafton_video = new Brafton_Video_Helper( $options );
add_action( 'save_post', array( &$brafton_video , 'update_video_embed_code' ), 10, 2 );
}
//Add video player scripts and css to site <head>. Executive decision - only support Atlantis.js
//Include Video Js for legacy clients with videojs embed codes.
function brafton_enqueue_video_scripts() {
$brafton_options = Brafton_options::get_instance();
//support atlantisjs embed codes
$player = $brafton_options->options['brafton_video_player'];
switch( $player ) {
case "atlantis":
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'atlantisjs', 'http://p.ninjacdn.co.uk/atlantisjs/v0.11.7/atlantis.js', array( 'jquery' ) );
//wp_enqueue_script( 'videojs', '//vjs.zencdn.net/4.3/video.js', array( 'jquery' ) );
wp_enqueue_style( 'atlantis', 'http://p.ninjacdn.co.uk/atlantisjs/v0.11.7/atlantisjs.css' );
//wp_enqueue_style( 'videocss', '//vjs.zencdn.net/4.3/video-js.css' );
break;
}
}
add_action( 'wp_enqueue_scripts', 'brafton_enqueue_video_scripts' );
//Used to preview video in admin page.
add_action( 'admin_init', 'brafton_enqueue_video_scripts' );
function brafton_import_clear_crons($hook)
{
$crons = _get_cron_array();
if ( empty( $crons ) )
return;
foreach ( $crons as $timestamp => $cron )
if ( !empty( $cron[$hook] ) )
unset($crons[$timestamp][$hook]);
_set_cron_array( $crons );
}
/**
* This is the scheduling hook for our plugin that is triggered by cron
*/
add_action('brafton_import_trigger_hook', 'brafton_run_hourly_import', 10, 0 );
function brafton_run_hourly_import()
{
$cron = true;
brafton_log( array( 'message' => "Import successfully triggered by wp cron." ) );
run_article_import( $cron );
run_video_import( $cron );
brafton_schedule_import();
$brafton_options = Brafton_options::get_instance();
$brafton_options->update_option( "brafton_options", "brafton_import_trigger_count", $brafton_options->get_option( "brafton_options", "brafton_import_trigger_count") + 1, 0);
//update_option("brafton_import_trigger_count", get_option("brafton_import_trigger_count") + 1);
// HACK: posts are duplicated due to a lack of cron lock resolution (see http://core.trac.wordpress.org/ticket/19700)
// this is fixed in wp versions >= 3.4.
// if ( version_compare( $wpVersion, '3.4', '<') )
// duplicateKiller();
}
function brafton_schedule_import(){
//Use wp_next_scheduled to check if import is already scheduled
$timestamp = wp_next_scheduled( "brafton_import_trigger_hook" );
//If $timestamp == false schedule hourly imports since it hasn't been done previously
if( $timestamp == false ){
//Schedule the event for right now, then hourly until importing is disabled.
wp_schedule_event( time() + 3600, "hourly", "brafton_import_trigger_hook" );
}
}
//Load the admin page Stylesheet.
function wp_brafton_article_importer_settings_style() {
$siteurl = get_option( 'siteurl' );
$url = $siteurl . '/wp-content/plugins/' . basename(dirname(__FILE__)) . '/css/settings.css';
echo "<link rel='stylesheet' type='text/css' href='$url' />\n";
}
add_action( 'admin_head', 'wp_brafton_article_importer_settings_style' );
}