PageRenderTime 26ms CodeModel.GetById 23ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/class-cornerstone.php

https://bitbucket.org/carloskikea/helpet
PHP | 94 lines | 33 code | 14 blank | 47 comment | 2 complexity | c34ea20c54201ad6c3610f6d8ec42cf7 MD5 | raw file
 1<?php
 2/**
 3 * WPSEO plugin file.
 4 *
 5 * @package WPSEO\Admin
 6 */
 7
 8/**
 9 * Represents the yoast cornerstone content.
10 */
11class WPSEO_Cornerstone {
12
13	const META_NAME = 'is_cornerstone';
14
15	const FIELD_NAME = 'yoast_wpseo_is_cornerstone';
16
17	/**
18	 * Registers the hooks.
19	 *
20	 * @return void
21	 */
22	public function register_hooks() {
23		global $pagenow;
24
25		if ( ! $this->page_contains_cornerstone_content_field( $pagenow ) ) {
26			return;
27		}
28
29		add_action( 'save_post', array( $this, 'save_meta_value' ) );
30		add_filter( 'wpseo_cornerstone_post_types', array( 'WPSEO_Post_Type', 'filter_attachment_post_type' ) );
31	}
32
33	/**
34	 * Saves the meta value to the database.
35	 *
36	 * @param int $post_id The post id to save the meta value for.
37	 *
38	 * @return void
39	 */
40	public function save_meta_value( $post_id ) {
41		$is_cornerstone_content = $this->is_cornerstone_content();
42
43		if ( $is_cornerstone_content ) {
44			$this->update_meta( $post_id, $is_cornerstone_content );
45
46			return;
47		}
48
49		$this->delete_meta( $post_id );
50	}
51
52	/**
53	 * Returns the result of the cornerstone content checkbox.
54	 *
55	 * @return bool True when checkbox is checked.
56	 */
57	protected function is_cornerstone_content() {
58		return filter_input( INPUT_POST, self::FIELD_NAME ) === '1';
59	}
60
61	/**
62	 * Checks if the current page matches one of the pages that contains the cornerstone content field.
63	 *
64	 * @param string $page The page to check.
65	 *
66	 * @return bool True when the page contains the cornerstone content field.
67	 */
68	protected function page_contains_cornerstone_content_field( $page ) {
69		return WPSEO_Metabox::is_post_edit( $page );
70	}
71
72	/**
73	 * Updates the cornerstone content post meta with the given cornerstone content value.
74	 *
75	 * @param int  $post_id                The post id to save the meta value for.
76	 * @param bool $is_cornerstone_content Whether or not the post should be considered to be cornerstone content.
77	 *
78	 * @return void
79	 */
80	protected function update_meta( $post_id, $is_cornerstone_content ) {
81		WPSEO_Meta::set_value( self::META_NAME, $is_cornerstone_content, $post_id );
82	}
83
84	/**
85	 * Deletes the cornerstone content post meta for the given post id.
86	 *
87	 * @param int $post_id The post id to delete the cornerstone content meta value for..
88	 *
89	 * @return void
90	 */
91	protected function delete_meta( $post_id ) {
92		WPSEO_Meta::delete( self::META_NAME, $post_id );
93	}
94}