ap_settings = get_option('ap_settings'); register_activation_hook( __FILE__, array( $this, 'load_default_settings' ) );//loads default settings for the plugin while activating the plugin add_action('init',array($this,'plugin_text_domain'));//loads text domain for translation ready add_action('init',array($this,'session_init')); //starts session if not started add_action('template_redirect',array($this,'submit_form'));//captures all the form values before printing any other html add_action('admin_post_ap_settings_action',array($this,'ap_settings_action'));//settings action add_action('admin_menu',array($this,'add_ap_menu'));//adds plugin menu in wp-admin add_action('admin_enqueue_scripts',array($this,'register_admin_assets'));//register plugin scripts and css in wp-admin add_shortcode('ap-form',array($this,'ap_form'));//adds the plugin shortcode add_shortcode('ap-form-message',array($this,'ap_form_message'));//add the shortcode to display the post submission message in redirected page. add_action('wp_enqueue_scripts',array($this,'register_frontend_assets'));//registers scripts and styles for front end add_action('pre_get_posts',array($this,'restrict_media_library'));//restricts user to see only uploaded by logged in user add_action('admin_post_ap_restore_default',array($this,'ap_restore_default'));//restores default settings } //load the text domain for language translation function plugin_text_domain() { load_plugin_textdomain( 'anonymous-post', FALSE, AP_LANG_DIR ); } //grabes the posted form data and save post accordingly function submit_form() { if(isset($_POST['ap_form_submit_btn'],$_POST['ap_form_nonce'])) { //$this->print_array($_POST); //$this->print_array($_FILES); include_once('inc/cores/save-post.php'); } } //registers all the necessary css and js for wp-admin function register_admin_assets() { //including the plugin's css and js only in plugin's settings page if(isset($_GET['page']) && $_GET['page']=='anonymous-post') { wp_enqueue_script('ap-admin-script',AP_JS_DIR.'/admin-script.js',array('jquery')); wp_enqueue_style('ap-admin-style',AP_CSS_DIR.'/admin-style.css'); } } //registers css and js for frontend function register_frontend_assets() { $ap_settings = $this->ap_settings; //including plugin only if admin has selected the option to show if($ap_settings['plugin_styles']==1) { wp_enqueue_style('ap-front-styles',AP_CSS_DIR.'/frontend-style.css'); } wp_enqueue_script('ap-frontend-js',AP_JS_DIR.'/frontend.js',array('jquery'),'',true); wp_localize_script('ap-frontend-js','ap_form_required_message',__('This field is required','anonymous-post')); wp_localize_script('ap-frontend-js','ap_captcha_error_message',__('Sum is not correct.','anonymous-post')); } //Adds admin menu function add_ap_menu() { add_menu_page('AccessPress Anonymoust Post Settings','AccessPress Anonymous Post','manage_options','anonymous-post',array($this,'ap_settings'),AP_IMAGE_DIR.'/ap-icon.png'); } //returns the ID of the first user function get_first_user_id() { $users = get_users(); foreach($users as $user) { return $user->ID; exit; } } //starts the session with the call of init hook function session_init() { if(!session_id()) { session_start(); } } //Load default settings during plugin activation function load_default_settings() { $ap_settings = array();//array for saving all the plugin's settings in single array $ap_settings['form_title'] = 'Anonymous Post'; $ap_settings['publish_status'] = 'draft'; $ap_settings['admin_notification'] = 1; $ap_settings['login_check'] = 0; $ap_settings['login_message'] = 'Please login to submit the post.'; $ap_settings['login_link_text'] = ''; $ap_settings['post_author'] = $this->get_first_user_id(); $ap_settings['plugin_styles'] = 1; $ap_settings['post_submission_message'] = ''; $ap_settings['form_included_fields'] = array('post_title','post_content'); $ap_settings['form_required_fields'] = array('post_title','post_content'); $ap_settings['taxonomy_reference'] = 'category,post_tag'; $ap_settings['editor_type'] = 'rich'; $ap_settings['media_upload'] = 0; $ap_settings['form_included_taxonomy'] = array(); $ap_settings['post_category'] = ''; $ap_settings['post_title_label'] = ''; $ap_settings['post_excerpt_label'] = ''; $ap_settings['post_content_label'] = ''; $ap_settings['post_image_label'] = ''; $ap_settings['author_name_label'] = ''; $ap_settings['author_url_label'] = ''; $ap_settings['author_email_label'] = ''; $ap_settings['post_submit_label'] = ''; $ap_settings['category_label'] = ''; $ap_settings['post_tag_label'] = ''; $ap_settings['captcha_settings'] = '1'; $ap_settings['math_captcha_label'] = ''; $ap_settings['editor_type'] = 'rich'; $ap_settings['redirect_url'] = ''; $ap_settings['admin_email_list'] = array(); $ap_settings['math_captcha_error_message'] = ''; if(!get_option('ap_settings')) { update_option('ap_settings',$ap_settings);//update as default option while activating for the first time. } } //plugin backend settings page function ap_settings() { include_once('inc/settings.php'); } //prints array in pre format function print_array($array) { echo "
";
print_r($array);
echo "";
}
//Sanitizes field values for saving in db
function filter_field($field)
{
//$field = trim($field);
//$field = strip_tags($field);
//$field = stripslashes($field);
return sanitize_text_field($field);
}
//Sanitizes field by converting line breaks to