PluginProbe
Virtual Robots.txt / trunk
Virtual Robots.txt vtrunk
pc-robotstxt / pc-robotstxt.php

pc-robotstxt.php in Virtual Robots.txt trunk, at pc-robotstxt.php

145 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Virtual Robots.txt
4 Version: 1.10
5 Plugin URI: http://infolific.com/technology/software-worth-using/robots-txt-plugin-for-wordpress
6 Description: Automatically creates a virtual robots.txt file for your site.
7 Author: Marios Alexandrou
8 Author URI: http://infolific.com/technology/
9 License: GPLv2 or later
10 Text Domain: pc-robotstxt
11 */
12
13 /*
14 Copyright 2016 Marios Alexandrou
15
16 Originally developed by Peter Coughlin
17
18 This program is free software; you can redistribute it and/or
19 modify it under the terms of the GNU General Public License
20 as published by the Free Software Foundation; either version 2
21 of the License, or (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 */
32
33 class pc_robotstxt {
34
35 function __construct() {
36
37 // make sure we have the right paths...
38 if ( !defined( 'WP_PLUGIN_URL' ) ) {
39 if ( !defined( 'WP_CONTENT_DIR' ) ) define( 'WP_CONTENT_DIR', ABSPATH.'wp-content' );
40 if ( !defined( 'WP_CONTENT_URL' ) ) define( 'WP_CONTENT_URL', get_option( 'siteurl' ).'/wp-content' );
41 if ( !defined( 'WP_PLUGIN_DIR' ) ) define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR.'/plugins' );
42 define( 'WP_PLUGIN_URL', WP_CONTENT_URL.'/plugins' );
43 }// end if
44
45 // stuff to do when the plugin is loaded
46 // i.e. register_activation_hook(__FILE__, array(&$this, 'activate'));
47 // i.e. register_deactivation_hook(__FILE__, array(&$this, 'deactivate'));
48 register_deactivation_hook( __FILE__, array( &$this, 'deactivate' ) );
49
50 // add_filter('hook_name', 'your_filter', [priority], [accepted_args]);
51 // i.e. add_filter('the_content', array(&$this, 'filter'));
52
53 // add_action ('hook_name', 'your_function_name', [priority], [accepted_args]);
54 // i.e. add_action('wp_head', array(&$this, 'action'));
55
56 // only if we're public
57 if ( get_option( 'blog_public' ) ) {
58 remove_action( 'do_robots', 'do_robots' );
59 add_action( 'do_robots', array(&$this, 'do_robots' ) );
60 }// end if
61
62 //add quick links to plugins page
63 $plugin = plugin_basename( __FILE__ );
64 if ( is_admin() )
65 add_filter( "plugin_action_links_$plugin", array( &$this, 'settings_link' ) );
66
67 }// end function
68
69 function activate() {
70 // stuff to do when the plugin is activated
71 }// end function
72
73 function deactivate() {
74 // stuff to do when plugin is deactivated
75 // i.e. delete_option('pc_robotstxt');
76 $options = $this->get_options();
77 if ( $options['remove_settings'] )
78 delete_option( 'pc_robotstxt' );
79 }// end function
80
81 function settings_link($links) {
82 $settings_link = '<a href="options-general.php?page=pc-robotstxt/admin.php">Settings</a>';
83 array_unshift( $links, $settings_link );
84 return $links;
85 }// end function
86
87 function do_robots() {
88
89 if ( is_robots() ) {
90
91 $options = $this->get_options();
92
93 $output = "# This virtual robots.txt file was created by the Virtual Robots.txt WordPress plugin: https://www.wordpress.org/plugins/pc-robotstxt/\n";
94
95 if ( '' != $options['user_agents'] ) {
96 $output .= stripcslashes( $options['user_agents'] );
97 }
98
99 // if there's an existing sitemap file or we're using pc-xml-sitemap plugin add a reference..
100 $protocol = ( ( !empty($_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] != 'off' ) || $_SERVER['SERVER_PORT'] == 443 ) ? "https://" : "http://";
101 if ( file_exists( $_SERVER['DOCUMENT_ROOT'].'/sitemap.xml.gz' ) ) {
102 $output .= "\n\n".'Sitemap: '.$protocol.$_SERVER['HTTP_HOST'].'/sitemap.xml.gz';
103 } elseif ( class_exists('pc_xml_sitemap' ) || file_exists($_SERVER['DOCUMENT_ROOT'].'/sitemap.xml' ) ) {
104 $output .= "\n\n".'Sitemap: '.$protocol.$_SERVER['HTTP_HOST'].'/sitemap.xml';
105 }
106
107 header('Status: 200 OK', true, 200);
108 header('Content-type: text/plain; charset=' . get_bloginfo('charset'));
109 echo $output;
110 exit;
111
112 }// end if
113
114 }// end function
115
116 function get_options() {
117 $options = get_option( 'pc_robotstxt' );
118 if ( !is_array( $options ) )
119 $options = $this->set_defaults();
120 return $options;
121 }// end function
122
123 function set_defaults() {
124 $options = array(
125 'user_agents' => "User-agent: *\n"
126 ."Disallow: /wp-admin/\n"
127 ."Allow: /wp-admin/admin-ajax.php\n"
128 ."Disallow: /wp-includes/\n"
129 ."Allow: /wp-includes/js/\n"
130 ."Allow: /wp-includes/images/\n"
131 ."Disallow: /trackback/\n"
132 ."Disallow: /wp-login.php\n"
133 ."Disallow: /wp-register.php",
134 'remove_settings' => false
135 );
136 update_option( 'pc_robotstxt', $options );
137 return $options;
138 }// end function
139
140 }// end class
141 $pc_robotstxt = new pc_robotstxt;
142
143 if ( is_admin() ) {
144 include_once dirname( __FILE__ ).'/admin.php';
145 }