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

Language PHP Lines 135
MD5 Hash 699945006140e51f7f36c79f1ee0644d
Repository https://github.com/dedavidd/piratenpartij.nl.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php

/**
 * File robots.txt helper.
 *
 * @author     Time.ly Network Inc.
 * @since      2.0
 *
 * @package    AI1EC
 * @subpackage AI1EC.query
 */
class Ai1ec_Robots_Helper extends Ai1ec_Base {

	/**
	 * Activation status.
	 *
	 * @return bool Whereas activation must be triggered.
	 */
	public function pre_check() {
		if ( defined( 'FS_METHOD' ) && 'direct' === FS_METHOD ) {
			return true;
		}
		return false; // disable until FS is properly resolved
	}

	/**
	 * Install robotx.txt into current Wordpress instance
	 *
	 * @return void
	 */
	public function install() {
		$option   = $this->_registry->get( 'model.option' );
		$settings = $this->_registry->get( 'model.settings' );
		$robots   = $option->get( 'ai1ec_robots_txt' );

		if ( isset( $robots['page_id'] ) &&
				! empty( $robots['is_installed'] ) &&
					$robots['page_id'] == $settings->get( 'calendar_page_id' ) ) {
			return;
		}

		$robots_file   = ABSPATH . 'robots.txt';
		$robots_txt    = array();
		$is_installed  = false;
		$current_rules = null;
		$custom_rules  = $this->rules( null, null );

		$url = wp_nonce_url(
			'edit.php?post_type=ai1ec_event&page=all-in-one-event-calendar-settings',
			'ai1ec-nonce'
		);

		if ( ! function_exists( 'request_filesystem_credentials' )  ) {
			return;
		}

		$creds = request_filesystem_credentials( $url, '', false, false, null );
		if ( ! WP_Filesystem( $creds ) ) {
			request_filesystem_credentials( $url, '', true, false, null );
		}

		global $wp_filesystem;
		if ( $wp_filesystem->exists( $robots_file )
				&& $wp_filesystem->is_readable( $robots_file )
					&& $wp_filesystem->is_writable( $robots_file ) ) {
			// Get current robots txt content
			$current_rules = $wp_filesystem->get_contents( $robots_file );

			// Update robots.txt
			$custom_rules = $this->rules( $current_rules, null );
			$is_installed = $wp_filesystem->put_contents(
				$robots_file,
				$custom_rules,
				FS_CHMOD_FILE
			);

			if ( $is_installed ) {
				$robots_txt['is_installed'] = true;
			}
		} else {
			$robots_txt['is_installed'] = false;
		}

		// Set Page ID
		$robots_txt['page_id'] = $settings->get( 'calendar_page_id' );

		// Update Robots Txt
		$option->set( 'ai1ec_robots_txt', $robots_txt );

		// Update settings textarea
		$settings->set( 'edit_robots_txt', $custom_rules );
	}

	/**
	 * Get default robots rules for the calendar
	 *
	 * @param  string $output Current robots rules
	 * @param  string $public Public flag
	 * @return array
	 */
	public function rules( $output, $public ) {
		// Current rules
		$current_rules = array_map(
			'trim',
			explode( PHP_EOL, $output )
		);

		// Get calendar page URI
		$calendar_page_id = $this->_registry->get( 'model.settings' )
											->get( 'calendar_page_id' );
		$page_base = get_page_uri( $calendar_page_id );

		// Custom rules
		$custom_rules = array();
		if ( $page_base ) {
			$custom_rules += array(
				"User-agent: *",
				"Disallow: /$page_base/action~posterboard/",
				"Disallow: /$page_base/action~agenda/",
				"Disallow: /$page_base/action~oneday/",
				"Disallow: /$page_base/action~month/",
				"Disallow: /$page_base/action~week/",
				"Disallow: /$page_base/action~stream/",
			);
		}

		$robots = array_merge( $current_rules, $custom_rules );

		return implode(
			PHP_EOL,
			array_filter( array_unique( $robots ) )
		);
	}
}
Back to Top