PluginProbe
Post Lockdown / 2.1
Post Lockdown v2.1
trunk 1.0.0 1.0.1 1.1 1.1.1 2.0 2.0.1 2.0.2 2.0.3 2.1 3.0 3.0.1 3.0.13 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 4.0 4.0.2 4.0.3 4.0.4 4.0.5 4.1.0 All 26 releases
post-lockdown / classes / class-postlockdown-optionspage.php

class-postlockdown-optionspage.php in Post Lockdown 2.1, at classes/class-postlockdown-optionspage.php

174 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class PostLockdown_OptionsPage {
4 const PAGE_TITLE = 'Post Lockdown';
5 /** @var string Page hook returned by add_options_page(). */
6 private $page_hook;
7 /** @var PostLockdown */
8 private $postlockdown;
9
10 public function __construct( PostLockdown $postlockdown ) {
11 $this->postlockdown = $postlockdown;
12
13 add_action( 'admin_init', array( $this, '_register_setting' ) );
14 add_action( 'admin_menu', array( $this, '_add_options_page' ) );
15
16 add_action( 'admin_enqueue_scripts', array( $this, '_enqueue_scripts' ) );
17 add_action( 'wp_ajax_pl_autocomplete', array( $this, '_ajax_autocomplete' ) );
18
19 add_filter( 'option_page_capability_' . PostLockdown::KEY, array( $this->postlockdown, 'get_admin_cap' ) );
20
21 add_filter( 'admin_footer_text', array( $this, '_filter_admin_footer_text' ) );
22 }
23
24 /**
25 * Callback for the 'admin_init' hook.
26 *
27 * Registers the plugin's option name so it gets saved.
28 */
29 public function _register_setting() {
30 register_setting( PostLockdown::KEY, PostLockdown::KEY );
31 }
32
33 /**
34 * Callback for the 'admin_menu' hook.
35 *
36 * Adds the plugin's options page.
37 */
38 public function _add_options_page() {
39 $this->page_hook = add_options_page(
40 self::PAGE_TITLE,
41 self::PAGE_TITLE,
42 $this->postlockdown->get_admin_cap(),
43 PostLockdown::KEY,
44 array( $this, '_output_options_page' )
45 );
46 }
47
48 /**
49 * Callback used by add_options_page().
50 *
51 * Outputs the options page HTML.
52 */
53 public function _output_options_page() {
54 $blocks = array();
55
56 $blocks[] = array(
57 'key' => 'locked',
58 'heading' => __( 'Locked Posts', 'postlockdown' ),
59 'input_name' => 'locked_post_ids',
60 'description' => __( 'Locked posts cannot be edited, trashed or deleted by non-admins', 'postlockdown' ),
61 );
62
63 $blocks[] = array(
64 'key' => 'protected',
65 'heading' => __( 'Protected Posts', 'postlockdown' ),
66 'input_name' => 'protected_post_ids',
67 'description' => __( 'Protected posts cannot be trashed or deleted by non-admins', 'postlockdown' ),
68 );
69
70 include_once( $this->postlockdown->plugin_path . 'view/options-page.php' );
71 }
72
73 /**
74 * Callback for the 'pl_autocomplete' AJAX action.
75 *
76 * Responds with a json encoded array of posts matching the query.
77 */
78 public function _ajax_autocomplete() {
79 $posts = $this->get_posts( array(
80 's' => $_REQUEST['term'],
81 'offset' => (int) $_REQUEST['offset'],
82 'posts_per_page' => 10,
83 ) );
84
85 wp_send_json_success( $posts );
86 }
87
88 /**
89 * Callback for the 'admin_enqueue_scripts' hook.
90 *
91 * Enqueues the required scripts and styles for the plugin options page.
92 *
93 * @param string $hook The current admin screen.
94 */
95 public function _enqueue_scripts( $hook ) {
96 // If it's not the plugin options page get out of here.
97 if ( $hook !== $this->page_hook ) {
98 return;
99 }
100
101 $assets_path = $this->postlockdown->plugin_url . 'view/assets/';
102 $extension = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
103
104 wp_enqueue_style( PostLockdown::KEY, $assets_path . 'css/postlockdown' . $extension . '.css', null, null );
105 wp_enqueue_script( PostLockdown::KEY, $assets_path . 'js/postlockdown' . $extension . '.js', array( 'jquery-ui-autocomplete' ), null, true );
106
107 $posts = $this->get_posts( array(
108 'nopaging' => true,
109 'post__in' => array_merge(
110 $this->postlockdown->get_locked_post_ids( true ),
111 $this->postlockdown->get_protected_post_ids( true )
112 ),
113 ) );
114
115 $data = array();
116
117 foreach ( $posts as $post ) {
118 if ( $this->postlockdown->is_post_locked( $post->ID ) ) {
119 $data['locked'][] = $post;
120 }
121
122 if ( $this->postlockdown->is_post_protected( $post->ID ) ) {
123 $data['protected'][] = $post;
124 }
125 }
126
127 wp_localize_script( PostLockdown::KEY, PostLockdown::KEY, $data );
128 }
129
130 /**
131 * Filter for the 'admin_footer_text' hook.
132 * Changes the footer message on the plugin options page.
133 *
134 * @param string $html
135 *
136 * @return string
137 */
138 public function _filter_admin_footer_text( $html ) {
139 $screen = get_current_screen();
140
141 if ( $screen->id !== $this->page_hook ) {
142 return $html;
143 }
144
145 $text = sprintf( __( 'Thank you for using Post Lockdown. If you like it, please consider <a href="%s" target="_blank">leaving a review.</a>' ), __( 'https://wordpress.org/support/view/plugin-reviews/post-lockdown?rate=5#postform' ) );
146
147 $html = '<span id="footer-thankyou">' . $text . '</span>';
148
149 return $html;
150 }
151
152 /**
153 * Convenience wrapper for get_posts().
154 *
155 * @param array $args Array of args to merge with defaults passed to get_posts().
156 *
157 * @return WP_Post[] Array of post objects.
158 */
159 private function get_posts( $args = array() ) {
160 $defaults = array(
161 'post_type' => $this->postlockdown->get_post_types(),
162 'post_status' => array( 'publish', 'pending', 'draft', 'future', 'private', 'inherit' ),
163 );
164
165 $args = wp_parse_args( $args, $defaults );
166
167 $args = apply_filters( 'postlockdown_get_posts', $args );
168
169 $query = new WP_Query( $args );
170
171 return $query->posts;
172 }
173 }
174