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