PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/all-in-one-event-calendar/lib/robots/helper.php

https://github.com/dedavidd/piratenpartij.nl
PHP | 134 lines | 81 code | 19 blank | 34 comment | 12 complexity | 699945006140e51f7f36c79f1ee0644d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * File robots.txt helper.
  4. *
  5. * @author Time.ly Network Inc.
  6. * @since 2.0
  7. *
  8. * @package AI1EC
  9. * @subpackage AI1EC.query
  10. */
  11. class Ai1ec_Robots_Helper extends Ai1ec_Base {
  12. /**
  13. * Activation status.
  14. *
  15. * @return bool Whereas activation must be triggered.
  16. */
  17. public function pre_check() {
  18. if ( defined( 'FS_METHOD' ) && 'direct' === FS_METHOD ) {
  19. return true;
  20. }
  21. return false; // disable until FS is properly resolved
  22. }
  23. /**
  24. * Install robotx.txt into current Wordpress instance
  25. *
  26. * @return void
  27. */
  28. public function install() {
  29. $option = $this->_registry->get( 'model.option' );
  30. $settings = $this->_registry->get( 'model.settings' );
  31. $robots = $option->get( 'ai1ec_robots_txt' );
  32. if ( isset( $robots['page_id'] ) &&
  33. ! empty( $robots['is_installed'] ) &&
  34. $robots['page_id'] == $settings->get( 'calendar_page_id' ) ) {
  35. return;
  36. }
  37. $robots_file = ABSPATH . 'robots.txt';
  38. $robots_txt = array();
  39. $is_installed = false;
  40. $current_rules = null;
  41. $custom_rules = $this->rules( null, null );
  42. $url = wp_nonce_url(
  43. 'edit.php?post_type=ai1ec_event&page=all-in-one-event-calendar-settings',
  44. 'ai1ec-nonce'
  45. );
  46. if ( ! function_exists( 'request_filesystem_credentials' ) ) {
  47. return;
  48. }
  49. $creds = request_filesystem_credentials( $url, '', false, false, null );
  50. if ( ! WP_Filesystem( $creds ) ) {
  51. request_filesystem_credentials( $url, '', true, false, null );
  52. }
  53. global $wp_filesystem;
  54. if ( $wp_filesystem->exists( $robots_file )
  55. && $wp_filesystem->is_readable( $robots_file )
  56. && $wp_filesystem->is_writable( $robots_file ) ) {
  57. // Get current robots txt content
  58. $current_rules = $wp_filesystem->get_contents( $robots_file );
  59. // Update robots.txt
  60. $custom_rules = $this->rules( $current_rules, null );
  61. $is_installed = $wp_filesystem->put_contents(
  62. $robots_file,
  63. $custom_rules,
  64. FS_CHMOD_FILE
  65. );
  66. if ( $is_installed ) {
  67. $robots_txt['is_installed'] = true;
  68. }
  69. } else {
  70. $robots_txt['is_installed'] = false;
  71. }
  72. // Set Page ID
  73. $robots_txt['page_id'] = $settings->get( 'calendar_page_id' );
  74. // Update Robots Txt
  75. $option->set( 'ai1ec_robots_txt', $robots_txt );
  76. // Update settings textarea
  77. $settings->set( 'edit_robots_txt', $custom_rules );
  78. }
  79. /**
  80. * Get default robots rules for the calendar
  81. *
  82. * @param string $output Current robots rules
  83. * @param string $public Public flag
  84. * @return array
  85. */
  86. public function rules( $output, $public ) {
  87. // Current rules
  88. $current_rules = array_map(
  89. 'trim',
  90. explode( PHP_EOL, $output )
  91. );
  92. // Get calendar page URI
  93. $calendar_page_id = $this->_registry->get( 'model.settings' )
  94. ->get( 'calendar_page_id' );
  95. $page_base = get_page_uri( $calendar_page_id );
  96. // Custom rules
  97. $custom_rules = array();
  98. if ( $page_base ) {
  99. $custom_rules += array(
  100. "User-agent: *",
  101. "Disallow: /$page_base/action~posterboard/",
  102. "Disallow: /$page_base/action~agenda/",
  103. "Disallow: /$page_base/action~oneday/",
  104. "Disallow: /$page_base/action~month/",
  105. "Disallow: /$page_base/action~week/",
  106. "Disallow: /$page_base/action~stream/",
  107. );
  108. }
  109. $robots = array_merge( $current_rules, $custom_rules );
  110. return implode(
  111. PHP_EOL,
  112. array_filter( array_unique( $robots ) )
  113. );
  114. }
  115. }