Completed
Push — update/videopress-use-block-pr... ( 3d1273...0b40f6 )
by
unknown
21:10 queued 10:43
created

Posts_List_Page_Notification::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Posts_List_Page_Notification file.
4
 * Disable edit_post and delete_post capabilities for Posts Pages in WP-Admin and display a notification icon.
5
 *
6
 * @package Jetpack
7
 */
8
9
namespace Automattic\Jetpack\Dashboard_Customizations;
10
11
/**
12
 * Class Posts_List_Page_Notification
13
 *
14
 * @package Automattic\Jetpack\Dashboard_Customizations
15
 */
16
class Posts_List_Page_Notification {
17
18
	/**
19
	 * Site's Posts page id
20
	 *
21
	 * @var int|null
22
	 */
23
	private $posts_page_id;
24
25
	/**
26
	 * If the Post_list contains the site's Posts Page
27
	 *
28
	 * @var bool
29
	 */
30
	private $is_page_in_list = false;
31
32
	/**
33
	 * Class instance.
34
	 *
35
	 * @var Posts_List_Page_Notification|null
36
	 */
37
	private static $instance = null;
38
39
	/**
40
	 * Posts_List_Page_Notification constructor.
41
	 *
42
	 * @param string $posts_page_id The Posts page configured in WordPress.
43
	 */
44
	public function __construct( $posts_page_id ) {
45
		add_action( 'init', array( $this, 'init_actions' ) );
46
47
		$this->posts_page_id = '' === $posts_page_id ? null : (int) $posts_page_id;
48
	}
49
50
	/**
51
	 * Add in all hooks.
52
	 */
53
	public function init_actions() {
54
		\add_filter( 'map_meta_cap', array( $this, 'disable_posts_page' ), 10, 4 );
55
		\add_filter( 'post_class', array( $this, 'add_posts_page_css_class' ), 10, 3 );
56
		\add_action( 'admin_print_footer_scripts-edit.php', array( $this, 'add_notification_icon' ) );
57
		\add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_css' ) );
58
	}
59
60
	/**
61
	 * Creates instance.
62
	 *
63
	 * @return Posts_List_Page_Notification
64
	 */
65
	public static function init() {
66
		if ( is_null( self::$instance ) ) {
67
			self::$instance = new self( \get_option( 'page_for_posts' ) );
68
		}
69
70
		return self::$instance;
71
	}
72
73
	/**
74
	 * Disable editing and deleting for the page that is configured as a Posts Page.
75
	 *
76
	 * @param array  $caps Array of capabilities.
77
	 * @param string $cap The current capability.
78
	 * @param string $user_id The user id.
79
	 * @param array  $args Argument array.
80
	 * @return array
81
	 */
82
	public function disable_posts_page( $caps, $cap, $user_id, $args ) {
83
		if ( 'edit_post' !== $cap && 'delete_post' !== $cap ) {
84
			return $caps;
85
		}
86
87
		if ( isset( $args[0] ) && $this->posts_page_id === $args[0] ) {
88
			$caps[] = 'do_not_allow';
89
		}
90
91
		return $caps;
92
	}
93
94
	/**
95
	 * Load the CSS for the WP Posts List
96
	 *
97
	 * We would probably need to move this elsewhere when new features are introduced to wp-posts-list.
98
	 */
99
	public function enqueue_css() {
100
		\wp_enqueue_style( 'wp-posts-list', plugins_url( 'wp-posts-list.css', __FILE__ ), array(), JETPACK__VERSION );
101
	}
102
103
	/**
104
	 * Adds a CSS class on the page configured as a Posts Page.
105
	 *
106
	 * @param array  $classes A list of CSS classes.
107
	 * @param string $class A CSS class.
108
	 * @param string $post_id The current post id.
109
	 * @return array
110
	 */
111
	public function add_posts_page_css_class( $classes, $class, $post_id ) {
112
		if ( $this->posts_page_id !== $post_id ) {
113
			return $classes;
114
		}
115
116
		$this->is_page_in_list = true;
117
118
		$classes[] = 'posts-page';
119
120
		return $classes;
121
	}
122
123
	/**
124
	 * Add a info icon on the Posts Page letting the user know why they cannot delete and remove the page.
125
	 */
126
	public function add_notification_icon() {
127
		// No need to add the JS since the site is not configured with a Posts Page or the current listview doesn't contain the page.
128
		if ( null === $this->posts_page_id || ! $this->is_page_in_list ) {
129
			return;
130
		}
131
132
		$text_notice = __( 'The content of your latest posts page is automatically generated and cannot be edited.', 'jetpack' );
133
		?>
134
		<script>
135
			document.querySelector(".posts-page .check-column").innerHTML = '' +
136
					'<div class="info"><span class="icon dashicons dashicons-info-outline"></span><span class="message"><?php echo esc_html( $text_notice ); ?></span></div>';
137
		</script>
138
		<?php
139
	}
140
}
141