Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. <?php
  2.  
  3. class AFA_Post_Notice {
  4.  
  5. public function __construct($editor) {
  6. $editor->initialize();
  7. }
  8.  
  9. public function initialize() {
  10. add_action( 'admin_enqueue_scripts', array($this, 'enqueue_styles') );
  11. add_action( 'admin_enqueue_scripts', array($this, 'enqueue_scripts') );
  12.  
  13. add_action('admin_menu', array($this, 'register_admin_page') );
  14. add_action('admin_init', array($this, 'register_plugin_settings_options') );
  15. }
  16.  
  17. public function register_admin_page() {
  18. add_options_page(
  19. 'Post Notice Settings', // Title
  20. 'AFA Post Notice', // Name
  21. 'manage_options', // Capabilities
  22. 'afa-post-notice', // Slug
  23. array($this, 'plugin_options_page') // Callback
  24. );
  25. }
  26.  
  27. public function plugin_options_page() {
  28. require_once (WP_PLUGIN_DIR . '/afa-post-notice/views/afa-post-notice-options.php');
  29. }
  30.  
  31. public function register_plugin_settings_options() {
  32. register_setting( 'afa-post-notice-option', 'post_notice_screens' );
  33. register_setting( 'afa-post-notice-option', 'where_to_show' );
  34. }
  35.  
  36. public function enqueue_styles() {
  37. wp_enqueue_style(
  38. 'afa-post-notice-admin',
  39. plugins_url( 'afa-post-notice/assets/css/admin.css' ),
  40. array(),
  41. '0.1.0'
  42. );
  43. }
  44.  
  45. public function enqueue_scripts() {
  46. wp_enqueue_script(
  47. 'afa-post-notice-admin',
  48. plugins_url( 'afa-post-notice/assets/js/admin.js' ),
  49. array('jquery'),
  50. '0.1.0'
  51. );
  52. }
  53. }
  54.  
  55.  
  56.  
  57. ******
  58.  
  59.  
  60. <div>
  61. <h2>Post Notice Settings</h2>
  62. <form action="options.php" method="post">
  63. <?php settings_fields('afa-post-notice-option'); ?>
  64. <?php do_settings_sections('afa-post-notice-option'); ?>
  65. <table class="form-table">
  66. <tr valign="top">
  67. <th scope="row">Select Screen</th>
  68. <td>
  69. <?php
  70. $args = array(
  71. 'public' => true
  72. );
  73. ?>
  74. <?php $post_types = get_post_types($args); ?>
  75. <select name="post_notice_screens[]" id="post_notice_screens" multiple>
  76. <?php foreach($post_types as $key => $postType) : ?>
  77. <option
  78. value="<?php echo $key; ?>"
  79. <?php echo ( get_option( 'post_notice_screens') && in_array($key, get_option( 'post_notice_screens')) ) ? 'selected="selected"' : null; ?>
  80. ><?php echo $key; ?></option>
  81. <?php endforeach; ?>
  82. </select>
  83. </tr>
  84. <tr valign="top">
  85. <th scope="row">Where To Show</th>
  86. <td>
  87. <?php $beforeToShow = esc_attr( get_option('where_to_show') ); ?>
  88. <input type="radio" name="where_to_show" value="before" <?php echo ($beforeToShow == 'before') ? 'checked="checkded"' : null; ?>> Before <br>
  89. <input type="radio" name="where_to_show" value="after" <?php echo ($beforeToShow == 'after') ? 'checked="checkded"' : null; ?>> After
  90. </td>
  91. </tr>
  92. </table>
  93. <?php submit_button(); ?>
  94. </form>
  95. </div>
  96.  
  97. ******
  98.  
  99. public function display_notice($content) {
  100. $post_notice = get_post_meta( get_the_ID(), 'afa-post-notice-display', true );
  101.  
  102. if ( $post_notice != '' ) {
  103.  
  104. $whereToShow = esc_attr( get_option('where_to_show') );
  105. $notice_html = '<div class="afa-post-notice-display">' . $post_notice . '</div>';
  106.  
  107. if($whereToShow == 'before' || !$whereToShow) {
  108. $content = $notice_html . $content;
  109. } else {
  110. $content = $content . $notice_html;
  111. }
  112.  
  113. }
  114.  
  115. return $content;
  116. }
  117.  
  118. *****
  119.  
  120.  
  121. function add_meta_box() {
  122.  
  123. $allowedScreens = get_option('post_notice_screens');
  124. $screen = get_current_screen();
  125.  
  126. if( ! in_array($screen->id, $allowedScreens) ) {
  127. return;
  128. }
  129.  
  130. add_meta_box(
  131. 'afa-post-notice',
  132. 'Post Notice',
  133. array( $this, 'post_notice_display' ),
  134. $screen->id,
  135. 'normal',
  136. 'high'
  137. );
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement