post_type;
$disabled_post_types = apply_filters('abt_disabled_post_types', array('acf', 'um_form'));
$is_invalid_post_type = in_array(
$post_type,
array_merge(
array('attachment'),
is_array($disabled_post_types) ? $disabled_post_types : array()
)
);
if ($is_invalid_post_type) {
return;
}
add_action('add_meta_boxes', array($this, 'add_metaboxes'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
add_action('admin_head', array($this, 'init_tinymce'));
add_action('admin_notices', array($this, 'user_alert'));
add_action('save_post', array($this, 'save_meta'));
add_filter('mce_css', array($this, 'load_tinymce_css'));
}
public static function init() {
$class = __CLASS__;
new $class;
}
/**
* Alerts the user that the plugin will not work if he/she doesn't have 'Rich Editing' enabled.
*/
public function user_alert() {
if ('true' === get_user_option('rich_editing')) {
return;
}
$class = 'notice notice-warning is-dismissible';
$message = __("Notice: Rich editing must be enabled to use the Academic Blogger's Toolkit plugin", 'academic-bloggers-toolkit');
printf('
', $class, $message);
}
/**
* Instantiates the TinyMCE plugin.
*/
public function init_tinymce() {
if ('true' === get_user_option('rich_editing')) {
add_filter('mce_external_plugins', array($this, 'register_tinymce_plugins'));
echo '';
}
}
/**
* Registers the TinyMCE plugins + loads fonts.
*
* @param string[] $plugin_array Array of TinyMCE plugins
*
* @return string[] Array of TinyMCE plugins with plugins added
*/
public function register_tinymce_plugins($plugin_array) {
$plugin_array['noneditable'] = plugins_url('academic-bloggers-toolkit/vendor/noneditable.js');
return $plugin_array;
}
/**
* Loads the required stylesheet into TinyMCE (required for proper citation parsing).
*
* @param string $mce_css CSS string
*
* @return string CSS string + custom CSS appended
*/
public function load_tinymce_css($mce_css) {
if (!empty($mce_css)) {
$mce_css .= ',';
}
$mce_css .= plugins_url('academic-bloggers-toolkit/css/editors/tinymce.css');
return $mce_css;
}
/**
* Adds metaboxes to posts and pages.
*
* @param string $post_type The post type
*/
public function add_metaboxes($post_type) {
$disabled_post_types = apply_filters('abt_disabled_post_types', array('acf', 'um_form'));
$is_invalid_post_type = in_array(
$post_type,
array_merge(
array('attachment'),
is_array($disabled_post_types) ? $disabled_post_types : array()
)
);
if ($is_invalid_post_type) {
return;
}
$all_types = get_post_types();
add_meta_box(
'abt-reflist',
__('Reference List', 'academic-bloggers-toolkit'),
array($this, 'render_reference_list'),
$all_types,
'side',
'high'
);
}
/**
* Renders the HTML for React to mount into.
*
* @param mixed $post
*/
public function render_reference_list($post) {
$ABT_i18n = i18n\generate_translations();
wp_nonce_field(basename(__FILE__), 'abt_nonce');
$state = json_decode(get_post_meta($post->ID, '_abt-reflist-state', true), true);
$opts = get_option('abt_options');
$custom_preferred = $opts['citation_style']['prefer_custom'] === true;
$custom_valid = file_exists($opts['citation_style']['custom_url']);
$style = $custom_preferred && $custom_valid ? 'abt-user-defined' : $opts['citation_style']['style'];
if (empty($state)) {
$state = array(
'cache' => array(
'style' => $style,
'links' => $opts['display_options']['links'],
'locale' => get_locale(),
),
'citationByIndex' => array(),
'CSL' => (object)array(),
);
}
$state['bibOptions'] = array(
'heading' => $opts['display_options']['bib_heading'],
'headingLevel' => $opts['display_options']['bib_heading_level'],
'style' => $opts['display_options']['bibliography'],
);
// Fix legacy post meta
if (array_key_exists('processorState', $state)) {
$state['CSL'] = $state['processorState'];
unset($state['processorState']);
}
if (array_key_exists('citations', $state)) {
$state['citationByIndex'] = $state['citations']['citationByIndex'];
unset($state['citations']);
}
wp_localize_script('abt-reflist', 'ABT', array(
'state' => $state,
'i18n' => $ABT_i18n,
'styles' => $this->get_citation_styles(),
'wp' => $this->localize_wordpress_constants(),
'custom_csl' => $this->get_user_defined_csl($opts['citation_style']['custom_url']),
)); ?>
plugins_url() . '/academic-bloggers-toolkit',
'home_url' => home_url(),
'plugins_url' => plugins_url(),
'wp_upload_dir' => wp_get_upload_dir(),
'info' => array(
'site' => array(
'language' => get_bloginfo('language'),
'name' => get_bloginfo('name'),
'plugins' => get_option('active_plugins'),
'theme' => get_template(),
'url' => get_bloginfo('url'),
),
'versions' => array(
'abt' => ABT_VERSION,
'php' => PHP_VERSION,
'wordpress' => get_bloginfo('version'),
),
),
);
}
/**
* Checks to see if custom CSL XML is saved and available. If so, returns an
* array containing the XML, label, and value. If not, returns an array
* containing only the key 'value' with the value of null.
*
* @param string $path path to CSL XML file
*
* @return mixed[] array as described above
*/
private function get_user_defined_csl($path) {
if (!file_exists($path)) {
return array('value' => null);
}
$contents = file_get_contents($path);
$xml = new SimpleXMLElement($contents);
$label = $xml->info->title->__toString() !== ''
? $xml->info->title->__toString()
: 'ABT Custom Style';
return array(
'label' => $label,
'value' => 'abt-user-defined',
'CSL' => $contents,
);
}
}