plugin_meta = get_plugin_meta_wpcomment();
//getting saved settings
$this -> plugin_settings = get_option($this->plugin_meta['shortname'].'_settings');
// populating $inputs with NM_Inputs object
$this->inputs = $this->get_all_inputs ();
/*
* [2]
* TODO: update scripts array for SHIPPED scripts
* only use handlers
*/
//setting shipped scripts
$this -> wp_shipped_scripts = array('jquery');
/*
* [3]
* TODO: update scripts array for custom scripts/styles
*/
//setting plugin settings
$this -> plugin_scripts = array(array( 'script_name' => 'scripts',
'script_source' => '/js/script.js',
'localized' => true,
'type' => 'js'
),
array( 'script_name' => 'styles',
'script_source' => '/plugin.styles.css',
'localized' => false,
'type' => 'style'
),
array( 'script_name' => 'bootstrap-grid',
'script_source' => '/assets/css/bootstrap-grid.css',
'localized' => false,
'type' => 'style'
),
);
/*
* [4]
* TODO: localized array that will be used in JS files
* Localized object will always be your pluginshortname_vars
* e.g: pluginshortname_vars.ajaxurl
*/
$this -> localized_vars = array('ajaxurl' => admin_url( 'admin-ajax.php' ),
'plugin_url' => $this->plugin_meta['url'],
'settings' => $this -> plugin_settings);
/*
* [5]
* TODO: this array will grow as plugin grow
* all functions which need to be called back MUST be in this array
* setting callbacks
*/
//following array are functions name and ajax callback handlers
$this -> ajax_callbacks = array('save_settings', //do not change this action, is for admin
'save_file_meta',);
/*
* plugin localization being initiated here
*/
add_action('init', array($this, 'wpp_textdomain'));
/*
* hooking up scripts for front-end
*/
add_action('wp_enqueue_scripts', array($this, 'load_scripts'));
/*
* registering callbacks
*/
$this -> do_callbacks();
/*
* Action hooks for comment fields
*/
add_action('comment_form_after_fields', array($this, 'render_comments_meta_fields'));
add_action('comment_form_logged_in_after', array($this, 'render_comments_meta_fields'));
/*
* Saving comment meta
*/
add_action( 'comment_post', array($this, 'save_comment_meta_fields') );
/*
* adding meta box in comments edit page
*/
add_action('add_meta_boxes_comment', array($this, 'render_comment_meta_admin_box'), 1);
/**
* adding comment meta in front view
*/
add_filter('comment_text', array($this, 'render_comment_meta_front'),100);
/**
* validating comments
*/
add_filter( 'preprocess_comment', array($this, 'verify_comment_meta_data') );
}
// i18n and l10n support here
// plugin localization
function wpp_textdomain() {
$locale_dir = $this->plugin_meta['path'] . '/locale/';
load_plugin_textdomain('nm-wpcomments', false, $locale_dir);
}
/*
* =============== NOW do your JOB ===========================
*
*/
/*
* Callbacks for comments fields
*/
function render_comments_meta_fields(){
global $post;
$custom_posttypes = $this->get_option ( '_comment_posttypes' );
if( $custom_posttypes != ''){
$postTypes = explode(',', $custom_posttypes);
$postTypes = array_map('trim', $postTypes);
if( ! in_array($post->post_type, $postTypes) )
return;
}
$this -> load_template('render.comment.meta.php');
}
function save_comment_meta_fields($comment_id){
$comment_meta = get_option('wpcomment_meta');
//wpcomment_pa($_POST); exit;
foreach ($comment_meta as $index => $meta){
$comment_meta_key = $meta['data_name'];
if( is_array($_POST[$comment_meta_key]) ) {
$comment_meta_val = implode(',', $_POST[$comment_meta_key]);
}else{
$comment_meta_val = sanitize_text_field($_POST[$comment_meta_key]);
}
add_comment_meta( $comment_id, $comment_meta_key, $comment_meta_val );
}
}
/*
* Verify comment meta
*/
function verify_comment_meta_data( $commentdata ) {
$comment_meta = get_option('wpcomment_meta');
//wpcomment_pa($_POST); exit;
if($comment_meta){
foreach ($comment_meta as $index => $meta){
$comment_meta_key = $meta['data_name'];
$comment_meta_val = esc_attr($_POST[$comment_meta_key]);
if($meta['required'] == 'on' && $comment_meta_val == ''){
$default_message = sprintf(__('%s is a required field'), esc_attr($meta['title']) );
$message = ($meta['error_message'] != '' ? esc_attr($meta['error_message']) : $default_message);
$message .= '
back';
wp_die( sprintf(__('%s'), stripslashes($message)) );
}
}
}
return $commentdata;
}
/**
* rendering comment meta BOX in comments admin page
*/
function render_comment_meta_admin_box($comment)
{
add_meta_box('nm_comment_meta_box', __('Comment Meta'), array($this, 'render_comment_meta_admin'), 'comment', 'normal');
}
/**
* rendering comment META in comments admin page
*/
function render_comment_meta_admin($comment) {
$comment_meta = get_option('wpcomment_meta');
?>
'.sprintf(__('%s'), $comment_meta_heading).'
'; $comment .= '