Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class AFA_Post_Notice {
- public function __construct($editor) {
- $editor->initialize();
- }
- public function initialize() {
- add_action( 'admin_enqueue_scripts', array($this, 'enqueue_styles') );
- add_action( 'admin_enqueue_scripts', array($this, 'enqueue_scripts') );
- add_action('admin_menu', array($this, 'register_admin_page') );
- add_action('admin_init', array($this, 'register_plugin_settings_options') );
- }
- public function register_admin_page() {
- add_options_page(
- 'Post Notice Settings', // Title
- 'AFA Post Notice', // Name
- 'manage_options', // Capabilities
- 'afa-post-notice', // Slug
- array($this, 'plugin_options_page') // Callback
- );
- }
- public function plugin_options_page() {
- require_once (WP_PLUGIN_DIR . '/afa-post-notice/views/afa-post-notice-options.php');
- }
- public function register_plugin_settings_options() {
- register_setting( 'afa-post-notice-option', 'post_notice_screens' );
- register_setting( 'afa-post-notice-option', 'where_to_show' );
- }
- public function enqueue_styles() {
- wp_enqueue_style(
- 'afa-post-notice-admin',
- plugins_url( 'afa-post-notice/assets/css/admin.css' ),
- array(),
- '0.1.0'
- );
- }
- public function enqueue_scripts() {
- wp_enqueue_script(
- 'afa-post-notice-admin',
- plugins_url( 'afa-post-notice/assets/js/admin.js' ),
- array('jquery'),
- '0.1.0'
- );
- }
- }
- ******
- <div>
- <h2>Post Notice Settings</h2>
- <form action="options.php" method="post">
- <?php settings_fields('afa-post-notice-option'); ?>
- <?php do_settings_sections('afa-post-notice-option'); ?>
- <table class="form-table">
- <tr valign="top">
- <th scope="row">Select Screen</th>
- <td>
- <?php
- $args = array(
- 'public' => true
- );
- ?>
- <?php $post_types = get_post_types($args); ?>
- <select name="post_notice_screens[]" id="post_notice_screens" multiple>
- <?php foreach($post_types as $key => $postType) : ?>
- <option
- value="<?php echo $key; ?>"
- <?php echo ( get_option( 'post_notice_screens') && in_array($key, get_option( 'post_notice_screens')) ) ? 'selected="selected"' : null; ?>
- ><?php echo $key; ?></option>
- <?php endforeach; ?>
- </select>
- </tr>
- <tr valign="top">
- <th scope="row">Where To Show</th>
- <td>
- <?php $beforeToShow = esc_attr( get_option('where_to_show') ); ?>
- <input type="radio" name="where_to_show" value="before" <?php echo ($beforeToShow == 'before') ? 'checked="checkded"' : null; ?>> Before <br>
- <input type="radio" name="where_to_show" value="after" <?php echo ($beforeToShow == 'after') ? 'checked="checkded"' : null; ?>> After
- </td>
- </tr>
- </table>
- <?php submit_button(); ?>
- </form>
- </div>
- ******
- public function display_notice($content) {
- $post_notice = get_post_meta( get_the_ID(), 'afa-post-notice-display', true );
- if ( $post_notice != '' ) {
- $whereToShow = esc_attr( get_option('where_to_show') );
- $notice_html = '<div class="afa-post-notice-display">' . $post_notice . '</div>';
- if($whereToShow == 'before' || !$whereToShow) {
- $content = $notice_html . $content;
- } else {
- $content = $content . $notice_html;
- }
- }
- return $content;
- }
- *****
- function add_meta_box() {
- $allowedScreens = get_option('post_notice_screens');
- $screen = get_current_screen();
- if( ! in_array($screen->id, $allowedScreens) ) {
- return;
- }
- add_meta_box(
- 'afa-post-notice',
- 'Post Notice',
- array( $this, 'post_notice_display' ),
- $screen->id,
- 'normal',
- 'high'
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement