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