init($base);
self::$dsnmanager = $this;
return $this;
}
function init($base) {
// add translation support
load_plugin_textdomain(DSNMANAGER_TEXTDOMAIN, PLUGINDIR . 'admin-dashboard-site-notes/languages', 'admin-dashboard-site-notes/languages' );
// define all of the capabilities even though at this point there's just one capability used
$this->capabilities = array(
'publish_posts' => 'publish_'.$this->post_type_name_cap,
'edit_posts' => 'edit_'.$this->post_type_name_cap,
'edit_others_posts' => 'edit_others_'.$this->post_type_name_cap,
'delete_posts' => 'delete_'.$this->post_type_name_cap,
'delete_others_posts' => 'delete_others_'.$this->post_type_name_cap,
'read_private_posts' => 'read_private_'.$this->post_type_name_cap,
'edit_post' => 'edit_'.$this->post_type_name,
'delete_post' => 'delete_'.$this->post_type_name,
'read_post' => 'read_'.$this->post_type_name,
);
// cache our options before doing anything, since everything else depends on them
$this->options = $this->get_all_options();
// check if we need to run any upgrade scripts and if so, do it
$this->handle_upgrades();
// base name should be plugin_basename(__FILE__) to check when WP is referring to this plugin, but since this is an include, we need to have it passed in by the actual file
$this->plugin_base = $base;
// create the site note content type
if(!defined('DSN_DISABLE_CHANGES')) {
$this->add_content_type();
}
// add hooks
add_filter('plugin_row_meta',array($this,'extra_plugin_links'),10,2);
add_action('all_admin_notices',array($this,'all_admin_notices'));
add_action('admin_init',array($this,'admin_init'));
// add styles/scripts
add_action('admin_enqueue_scripts',array($this,'enqueue_includes'));
// add dashboard widget notes if there are any
if($this->has_dashboard_notes()) {
add_action('wp_dashboard_setup', array($this,'setup_dashboard'));
}
// add instruction manual page if entries exist
if($this->has_instruction_notes()) {
add_action( 'admin_menu', array($this,'admin_menu') );
}
// add the options page if this user can manage the options
if($this->user_has_admin()) {
if(!defined('DSN_DISABLE_CHANGES')) {
add_action('admin_menu', array($this,'add_config_menu'));
add_action('admin_init', array($this,'register_settings'));
}
}
// check if we can add contextual help (added in WP v3.3) and if so, add that action
$wp_version = get_bloginfo('version');
if( version_compare($wp_version, '3.3.0') >= 0) {
add_action('current_screen',array($this,'current_screen'),999);
}
}
private function handle_upgrades() {
global $wpdb;
$prefix = $this->custom_field_prefix;
// if there's no version set in the database, it's a pre-1.3 version, so run the "upgrade" code and then create the version
if(!isset($this->options['version']) || version_compare($this->options['version'], '1.3.1', '<')) {
// set instruction_manual to the opposite of instructions_exclude and rename it
$sql = "
UPDATE {$wpdb->postmeta} wppm
SET meta_value = NOT meta_value, meta_key = '{$prefix}instruction_manual'
WHERE meta_key = '{$prefix}instructions_exclude'
";
$updated_rows = $wpdb->query($sql);
$this->options['updated_rows_instruction_manual'] = $updated_rows;
$this->options['version'] = $this->plugin_version;
update_option($this->plugin_id, $this->options);
// Create "admin_notice" meta key for each existing dsn_note, since it's now required
$sql = "
INSERT IGNORE INTO {$wpdb->postmeta} (post_id, meta_key, meta_value)
SELECT {$wpdb->posts}.ID, '{$prefix}admin_notice', '1'
FROM {$wpdb->posts}
WHERE {$wpdb->posts}.post_type = '{$this->post_type_name}'
";
$updated_admin_notices = $wpdb->query($sql);
$this->options['updated_admin_notices'] = $updated_admin_notices;
// Create "contextual_help" meta key for each existing dsn_note
$sql = "
INSERT IGNORE INTO {$wpdb->postmeta} (post_id, meta_key, meta_value)
SELECT {$wpdb->posts}.ID, '{$prefix}contextual_help', '0'
FROM {$wpdb->posts}
WHERE {$wpdb->posts}.post_type = '{$this->post_type_name}'
";
$updated_admin_notices = $wpdb->query($sql);
$this->options['updated_admin_notices'] = $updated_admin_notices;
// rename "loc_dashboard" to "dashboard_widget" in anticipation of having a dashboard page location
// (to allow dashboard tabs/notices instead of just a widget)
$sql = "
UPDATE {$wpdb->postmeta} wppm
SET meta_key = '{$prefix}dashboard_widget'
WHERE meta_key = '{$prefix}loc_dashboard'
";
$updated_rows = $wpdb->query($sql);
// delete all meta keys attached incorrectly to non-dsn_note post types
// to minimize risk of deleting meta keys created by other plugins, we'll be as specific as possible rather than just using '_dsn_%'
$old_prefix = '_dsn_'; // hardcoded to _dsn_ because it was used in prior versions
$pre_role = $old_prefix . "role_"; // role meta keys
$pre_loc = $old_prefix . "loc_"; // locations meta keys
$pre_hide = $old_prefix . "hide_title"; // hide_title meta keys
$pre_instr = $old_prefix . "instructions_exclude"; // instructions_exclude meta keys
$sql = "
DELETE FROM wppm
USING {$wpdb->postmeta} AS wppm
WHERE
(wppm.meta_key LIKE '{$pre_role}%' OR wppm.meta_key LIKE '{$pre_loc}%' OR wppm.meta_key LIKE '{$pre_hide}%' OR wppm.meta_key LIKE '{$pre_instr}%')
AND NOT EXISTS (
SELECT ID
FROM {$wpdb->posts} AS wpp
WHERE ID = wppm.post_id AND wpp.post_type = 'dsn_note')";
$deleted_rows = $wpdb->query($sql);
$this->options['deleted_rows_metabug'] = $deleted_rows;
// NOTE: for future versions, make sure to allow previous scripts to run too in case someone skips an update. watch out about updating version in here.
// store this version as the current plugin version in the options
$this->options['version'] = $this->plugin_version;
update_option($this->plugin_id, $this->options);
}
}
public function current_screen() {
$screen = get_current_screen();
// TODO: fix this since it's not actually filtering by contextual_help
$args = array('format'=>'contextual_help');
$posts = $this->get_notes($args);
if(count($posts)) {
foreach($posts as $post) {
$c = $this->get_content($post);
$c = "
{$c}
";
$t = $post->post_title;
$id = $post->ID;
$screen->add_help_tab( array(
'id' => "dsn-postid-{$id}",
'title' => $t,
'content' => $c,
) );
}
}
}
public function shortcode($atts=array(), $content='') {
extract( shortcode_atts( array(
'id' => '',
'depth' => 99,
'excerpt' => false,
'content' => true,
'title' => true,
), $atts ) );
// make sure this note actually allows use in a shortcode
if($id && get_post_meta($id, '_dsn_shortcodable', true) == true) {
$post = get_post($id);
if($post) {
return $this->note_with_children($post,'',0,!($excerpt),$depth);
}
}
return '';
}
public function enqueue_includes() {
wp_register_style($this->plugin_stylesheet_id, plugins_url('admin-dashboard-site-notes/admin-styles-1.3.0.css'));
wp_enqueue_style($this->plugin_stylesheet_id);
wp_register_script($this->plugin_script_id, plugins_url('admin-dashboard-site-notes/admin-scripts.js'), array('jquery') );
wp_enqueue_script($this->plugin_script_id);
}
// check if user is allowed to configure the plugin
// only super admins are allowed, unless DSN_ADMIN_CONFIG is true, in which case any admin is allowed
private function user_has_admin() {
return is_super_admin() || (defined('DSN_ADMIN_CONFIG') && current_user_can('manage_options'));
}
// get all wordpress roles, allowing them to be filtered by other plugins
private function get_roles() {
global $wp_roles;
if(!$wp_roles) {
return array();
}
$all_roles = $wp_roles->roles;
$editable_roles = apply_filters('editable_roles', $all_roles);
return $editable_roles;
}
// add the content type to the admin navigation
public function admin_menu() {
add_dashboard_page($this->options['manual_title'], $this->options['manual_nav'], 'read', $this->plugin_id, array($this,'admin_page'));
}
// echo the instruction manual
public function admin_page() {
echo "
';
return $output;
}
// recursively get the post and its children
public function note_with_children($post,$format,$depth=0,$full_post=false,$max_depth=5) {
$output = "
";
foreach($this->post_types as $type=>$type_obj) {
if($ct++ % 2 == 0) $class = ' even ';
else $class = ' odd ';
if($type_obj->name == 'revision') {
$ct--;
// we'll add the special case at the end instead to make it look better
}
else {
$output .= "