public
Last active

Plugin T5 All URIs

  • Download Gist
all-uris.php
PHP
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: T5 All URIs
* Description: Creates a plaintext list of all URIs on your blog.
* Version: 2012.03.08
* Required: 3.3
* Author: Thomas Scholz <info@toscho.de>
* Author URI: http://toscho.de
* License: MIT
* License URI: http://www.opensource.org/licenses/mit-license.php
*
* Copyright (c) 2012 Thomas Scholz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
 
// Not a WordPress context? Stop.
! defined( 'ABSPATH' ) and exit;
 
// Wait until all needed functions are loaded.
add_action( 'init', array ( 'T5_All_Uris', 'init' ) );
add_action( 'init', array ( 'T5_All_Uris', 'set_rewrite_rule' ) );
 
register_activation_hook( __FILE__, array ( 'T5_All_Uris', 'set_rewrite_rule' ) );
 
class T5_All_Uris
{
/**
* Internal handler for WordPress
*
* @type string
*/
protected static $query_var = 'alluris';
 
/**
* Creates a new instance.
*
* @wp-hook init
* @see __construct()
* @return void
*/
public static function init()
{
new self;
}
 
/**
* Set actions, filters and basic variables, load language.
*/
public function __construct()
{
add_filter( 'query_vars', array ( $this, 'add_query_var' ) );
// Hook in late to allow other plugins to operate earlier.
add_action( 'template_redirect', array ( $this, 'render' ), 100 );
 
$this->base_name = plugin_basename( __FILE__ );
add_filter( 'plugin_row_meta', array( $this, 'add_plugin_row_link' ), 10, 2 );
}
 
/**
* Adds a link to the settings to plugin list.
*
* @wp-hook plugin_row_meta
* @param array $links Already existing links.
* @return string
*/
public function add_plugin_row_link( $links, $file )
{
if ( $this->base_name != $file )
{
return $links;
}
$url = user_trailingslashit( home_url( '/' . self::$query_var ) );
return array_merge( $links, array ( "<a href='$url'>View output</a>" ) );
}
 
/**
* Register an URI.
*
* @wp-hook activation, init
* @return void
*/
public static function set_rewrite_rule()
{
add_rewrite_rule(
'^' . self::$query_var . '$',
'index.php?' . self::$query_var . '=1',
'top'
);
'init' != current_filter() and flush_rewrite_rules();
}
 
/**
* Register our query var.
*
* @wp-hook query_vars
* @param array $vars Existing query vars
* @return array
*/
public function add_query_var( $vars )
{
$vars[] = self::$query_var;
return $vars;
}
 
/**
* Print all URIs as plain text.
*
* @wp-hook template_redirect
* @return void
*/
public function render()
{
if ( ! get_query_var( self::$query_var ) )
{
return;
}
 
header('Content-Type: text/plain;charset=utf-8');
$this->print_post_uris();
$this->print_term_uris();
 
exit;
}
 
/**
* All permalinks.
*
* @return void
*/
protected function print_post_uris()
{
$posts = $this->get_all_post_objects();
if ( empty ( $posts ) )
{
return;
}
 
$out = array ();
foreach ( $posts as $post )
{
$out[] = get_permalink( $post->ID );
}
 
! empty ( $out ) and print join( "\n", $out );
}
 
/**
* Two calls to get all post objects.
*
* @return array
*/
protected function get_all_post_objects()
{
$posts = get_posts(
array (
'numberposts' => -1,
'post_type' => 'any'
)
);
! is_array ( $posts ) and $posts = array ();
 
// Hm, these should be queryable by the previous call. Didn’t work.
$attachments = get_posts(
array (
'numberposts' => -1,
'post_type' => 'attachment'
)
);
! is_array ( $attachments ) and $posts = array ();
 
$out = array_merge( $posts, $attachments );
 
return $out;
}
 
/**
* Print permalinks for all terms.
*
* @return void
*/
protected function print_term_uris()
{
$terms = get_terms(
get_taxonomies(),
array (
'hide_empty' => FALSE,
'get' => 'all'
)
);
 
if ( ! is_array( $terms ) )
{
return;
}
 
foreach ( $terms as $term )
{
print "\n" . get_term_link( $term );
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.