plugins_url() . '/academic-bloggers-toolkit',
'plugins_url' => plugins_url(),
'wp_upload_dir' => wp_get_upload_dir(),
'home_url' => home_url(),
);
}
/**
* 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 [array] 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,
);
}
/**
* Sets up all actions and filters for the backend class
*/
public function __construct() {
add_action('admin_notices', array($this, 'user_alert'));
add_action('admin_head', array($this, 'init_tinymce'));
add_filter('mce_css', array($this, 'load_tinymce_css'));
add_action('add_meta_boxes', array($this, 'add_metaboxes'));
add_action('save_post', array($this, 'save_meta'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
}
/**
* 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( '
%2$s
', $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 array $plugin_array Array of TinyMCE plugins
* @return array Array of TinyMCE plugins with plugins added
*/
public function register_tinymce_plugins($plugin_array) {
$plugin_array['academic_bloggers_toolkit'] = plugins_url('academic-bloggers-toolkit/lib/js/tinymce/index.js');
$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/lib/css/citations.css');
return $mce_css;
}
/**
* Adds metaboxes to posts and pages.
*
* @param string $post_type The post type
*/
public function add_metaboxes($post_type) {
$invalid_post_type = in_array($post_type, array('attachment', 'acf', 'um_form'));
if ($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.
*/
public function render_reference_list($post) {
$ABT_i18n = abt_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_Reflist_State', $state);
wp_localize_script('abt-reflist', 'ABT_i18n', $ABT_i18n);
wp_localize_script('abt-reflist', 'ABT_CitationStyles', $this->get_citation_styles());
wp_localize_script('abt-reflist', 'ABT_wp', $this->localize_wordpress_constants());
wp_localize_script('abt-reflist', 'ABT_Custom_CSL', $this->get_user_defined_csl($opts['citation_style']['custom_url']));
echo "";
}
/**
* Saves the Peer Review meta fields to the database.
*
* @param string $post_id The post ID
*/
public function save_meta($post_id) {
$is_autosave = wp_is_post_autosave($post_id);
$is_revision = wp_is_post_revision($post_id);
$is_valid_nonce = (isset($_POST[ 'abt_nonce' ]) && wp_verify_nonce($_POST['abt_nonce'], basename(__FILE__))) ? true : false;
if ($is_autosave || $is_revision || !$is_valid_nonce) return;
$reflist_state = $_POST['abt-reflist-state'];
update_post_meta($post_id, '_abt-reflist-state', $reflist_state);
}
/**
* Registers and enqueues all required scripts.
*/
public function enqueue_admin_scripts() {
global $post_type;
$invalid_post_type = in_array($post_type, array('attachment', 'acf', 'um_form'));
if ($invalid_post_type) return;
wp_dequeue_script('autosave');
wp_enqueue_style('dashicons');
wp_enqueue_style('abt-admin-css', plugins_url('academic-bloggers-toolkit/lib/css/admin.css'), array('dashicons'), ABT_VERSION);
wp_enqueue_script('abt-citeproc', plugins_url('academic-bloggers-toolkit/vendor/citeproc.js'), array(), ABT_VERSION, true);
wp_enqueue_script('abt-vendors', plugins_url('academic-bloggers-toolkit/vendor/vendor.bundle.js'), array(), ABT_VERSION, true);
wp_enqueue_script('abt-reflist', plugins_url('academic-bloggers-toolkit/lib/js/reference-list/index.js'), array('abt-citeproc', 'abt-vendors'), ABT_VERSION, true);
}
}