| 1 | <?php
|
|---|
| 2 | /*
|
|---|
| 3 | Plugin Name: SimplePieCacheTester
|
|---|
| 4 | Description: To test simplepipe cache
|
|---|
| 5 | Author: arena
|
|---|
| 6 | Version: 0
|
|---|
| 7 | */
|
|---|
| 8 | class SimplePieCacheTester
|
|---|
| 9 | {
|
|---|
| 10 | const feed_0 = 'http://wordpress.org/development/feed/';
|
|---|
| 11 | const feed_1 = 'https://www.theverge.com/rss/index.xml';
|
|---|
| 12 |
|
|---|
| 13 | function __construct()
|
|---|
| 14 | {
|
|---|
| 15 | add_action( 'tool_box', array( $this, 'tool_box' ) );
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | public function tool_box()
|
|---|
| 19 | {
|
|---|
| 20 | $this->out = array();
|
|---|
| 21 |
|
|---|
| 22 | // tests cannot be chained due to mysql ...
|
|---|
| 23 |
|
|---|
| 24 | // $this->test01();
|
|---|
| 25 | // $this->test02();
|
|---|
| 26 | // $this->test03();
|
|---|
| 27 | $this->test04();
|
|---|
| 28 |
|
|---|
| 29 | echo '<pre>' . implode('<br />', $this->out ) . '</pre>';
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | public function test01()
|
|---|
| 33 | {
|
|---|
| 34 | $this->out[] = '';
|
|---|
| 35 | $this->out[] = '**************** TEST 01 ***********************';
|
|---|
| 36 | $this->test = ' * 01 * ';
|
|---|
| 37 |
|
|---|
| 38 | $this->feed = self::feed_0;
|
|---|
| 39 |
|
|---|
| 40 | $this->out[] = $this->test . ' ** using fetch_feed with no filter and one feed as string : ' . $this->feed;
|
|---|
| 41 |
|
|---|
| 42 | $this->init_test();
|
|---|
| 43 |
|
|---|
| 44 | $feed = fetch_feed( $this->feed );
|
|---|
| 45 |
|
|---|
| 46 | $this->end_test( $feed );
|
|---|
| 47 |
|
|---|
| 48 | $this->out[] = '**************** END TEST 01 ***********************';
|
|---|
| 49 | $this->out[] = '';
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | public function test02()
|
|---|
| 53 | {
|
|---|
| 54 | $this->out[] = '';
|
|---|
| 55 | $this->out[] = '**************** TEST 02 ***********************';
|
|---|
| 56 | $this->test = ' * 02 * ';
|
|---|
| 57 |
|
|---|
| 58 | $this->feed = self::feed_0;
|
|---|
| 59 | $this->lifetime = $this->get_lifetime();
|
|---|
| 60 | $this->calls = 0;
|
|---|
| 61 | add_filter( 'wp_feed_cache_transient_lifetime', array( $this, 'wp_feed_cache_transient_lifetime' ), 10, 2 );
|
|---|
| 62 |
|
|---|
| 63 | $this->out[] = $this->test . ' ** using fetch_feed with filter (lifetime = ' . $this->lifetime . ') and one feed as string : ' . $this->feed;
|
|---|
| 64 |
|
|---|
| 65 | $this->init_test();
|
|---|
| 66 |
|
|---|
| 67 | $feed = fetch_feed( $this->feed );
|
|---|
| 68 |
|
|---|
| 69 | $this->end_test( $feed );
|
|---|
| 70 |
|
|---|
| 71 | remove_filter( 'wp_feed_cache_transient_lifetime', array( $this, 'wp_feed_cache_transient_lifetime' ) );
|
|---|
| 72 |
|
|---|
| 73 | $this->out[] = '**************** END TEST 02 ***********************';
|
|---|
| 74 | $this->out[] = '';
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | public function test03()
|
|---|
| 78 | {
|
|---|
| 79 | $this->out[] = '';
|
|---|
| 80 | $this->out[] = '**************** TEST 03 ***********************';
|
|---|
| 81 | $this->test = ' * 03 * ';
|
|---|
| 82 |
|
|---|
| 83 | $this->feed = array( self::feed_0 );
|
|---|
| 84 | $this->lifetime = $this->get_lifetime();
|
|---|
| 85 | $this->calls = 0;
|
|---|
| 86 | add_filter( 'wp_feed_cache_transient_lifetime', array( $this, 'wp_feed_cache_transient_lifetime' ), 10, 2 );
|
|---|
| 87 |
|
|---|
| 88 | $this->out[] = $this->test . ' ** using fetch_feed with filter (lifetime = ' . $this->lifetime . ') and one feed as array : ' . json_encode( $this->feed );
|
|---|
| 89 |
|
|---|
| 90 | $this->init_test();
|
|---|
| 91 |
|
|---|
| 92 | $feed = fetch_feed( $this->feed );
|
|---|
| 93 |
|
|---|
| 94 | $this->end_test( $feed );
|
|---|
| 95 |
|
|---|
| 96 | remove_filter( 'wp_feed_cache_transient_lifetime', array( $this, 'wp_feed_cache_transient_lifetime' ) );
|
|---|
| 97 |
|
|---|
| 98 | $this->out[] = '**************** END TEST 03 ***********************';
|
|---|
| 99 | $this->out[] = '';
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | public function test04()
|
|---|
| 103 | {
|
|---|
| 104 | $this->out[] = '';
|
|---|
| 105 | $this->out[] = '**************** TEST 04 ***********************';
|
|---|
| 106 | $this->test = ' * 04 * ';
|
|---|
| 107 |
|
|---|
| 108 | $this->feed = array( self::feed_0, self::feed_1 );
|
|---|
| 109 | $this->lifetime = $this->get_lifetime();
|
|---|
| 110 | $this->calls = 0;
|
|---|
| 111 | add_filter( 'wp_feed_cache_transient_lifetime', array( $this, 'wp_feed_cache_transient_lifetime' ), 10, 2 );
|
|---|
| 112 |
|
|---|
| 113 | $this->out[] = $this->test . ' ** using fetch_feed with filter (lifetime = ' . $this->lifetime . ') and two feeds as array : ' . json_encode( $this->feed );
|
|---|
| 114 |
|
|---|
| 115 | $this->init_test();
|
|---|
| 116 |
|
|---|
| 117 | $feed = fetch_feed( $this->feed );
|
|---|
| 118 |
|
|---|
| 119 | $this->end_test( $feed );
|
|---|
| 120 |
|
|---|
| 121 | remove_filter( 'wp_feed_cache_transient_lifetime', array( $this, 'wp_feed_cache_transient_lifetime' ) );
|
|---|
| 122 |
|
|---|
| 123 | $this->out[] = '**************** END TEST 04 ***********************';
|
|---|
| 124 | $this->out[] = '';
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | public function wp_feed_cache_transient_lifetime( $lifetime, $url )
|
|---|
| 128 | {
|
|---|
| 129 | $this->calls++;
|
|---|
| 130 | $test = $this->test . '+++++ filter(' . $this->calls . ')';
|
|---|
| 131 | $this->out[] = $test;
|
|---|
| 132 | $u = ( is_array( $url ) ) ? json_encode( $url ) : $url;
|
|---|
| 133 | $this->out[] = $test . ' beg >> lifetime >> ' . $lifetime;
|
|---|
| 134 | $this->out[] = $test . ' beg >> 2nd arg >> ' . $u;
|
|---|
| 135 |
|
|---|
| 136 | if ( $url == $this->feed )
|
|---|
| 137 | {
|
|---|
| 138 | $this->out[] = $test . ' ++ filtered : should be 1st call (2nd func arg string or array)';
|
|---|
| 139 | $lifetime = $this->lifetime;
|
|---|
| 140 | }
|
|---|
| 141 | elseif ( is_array( $this->feed ) )
|
|---|
| 142 | {
|
|---|
| 143 | $feedmd5 = array_map( 'md5', $this->feed );
|
|---|
| 144 | if ( in_array( $url, $feedmd5 ) )
|
|---|
| 145 | {
|
|---|
| 146 | $lifetime = $this->lifetime;
|
|---|
| 147 | if ( count( $this->feed ) == 1 )
|
|---|
| 148 | $this->out[] = $test . ' ++ filtered : should be 2nd call and 2nd arg is one of array of one';
|
|---|
| 149 | else
|
|---|
| 150 | $this->out[] = $test . ' ++ filtered : should be 2nd call and 2nd arg is one of array of many';
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 | elseif ( $url == md5( $this->feed ) )
|
|---|
| 154 | {
|
|---|
| 155 | $this->out[] = $test . ' ++ filtered : should be 2nd call and one feed as string';
|
|---|
| 156 | $lifetime = $this->lifetime;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | $this->out[] = $test . ' end >> lifetime >> ' . $lifetime;
|
|---|
| 160 | return $lifetime;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | public function init_test()
|
|---|
| 164 | {
|
|---|
| 165 | global $wpdb;
|
|---|
| 166 | $wpdb->query( "DELETE FROM $wpdb->options where option_name like '_transient%feed%'" );
|
|---|
| 167 | $this->out[] = $this->test . ' rows in wp_options where option_name like "_transient%feed%" : ' . $wpdb->get_var( "SELECT COUNT(*) as count FROM $wpdb->options where option_name like '_transient%feed%'" );
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | public function end_test( $feed )
|
|---|
| 171 | {
|
|---|
| 172 | global $wpdb;
|
|---|
| 173 | if ( is_wp_error( $feed ) )
|
|---|
| 174 | {
|
|---|
| 175 | $this->out[] = $this->test . $feed->get_error_message();
|
|---|
| 176 | }
|
|---|
| 177 | else
|
|---|
| 178 | {
|
|---|
| 179 | $i = 0;
|
|---|
| 180 | do
|
|---|
| 181 | {
|
|---|
| 182 | $link = $feed->get_link( $i );
|
|---|
| 183 | if ( $link ) $this->out[] = $this->test . ' >>> feed->get_link >>> ' . $link;
|
|---|
| 184 | $i++;
|
|---|
| 185 | } while( $link );
|
|---|
| 186 | }
|
|---|
| 187 | $rows = $wpdb->get_results( "SELECT * FROM $wpdb->options where option_name like '_transient%feed%'" );
|
|---|
| 188 | $this->out[] = $this->test . ' rows in wp_options where option_name like "_transient%feed%" : ' . count($rows);
|
|---|
| 189 |
|
|---|
| 190 | if ( $rows )
|
|---|
| 191 | {
|
|---|
| 192 | $this->out[] .= $this->get_list( $rows );
|
|---|
| 193 | }
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | public function get_list( $rows )
|
|---|
| 197 | {
|
|---|
| 198 | $t = '<table>';
|
|---|
| 199 | $cols = array( 'option_id', 'option_name', 'option_value' );
|
|---|
| 200 | foreach( $rows as $row )
|
|---|
| 201 | {
|
|---|
| 202 | $t .= '<tr>';
|
|---|
| 203 | foreach( $cols as $col )
|
|---|
| 204 | {
|
|---|
| 205 | $v = $row->$col;
|
|---|
| 206 | if ( strlen( $v ) > 100 ) $v = substr( $v, 0, 99 );
|
|---|
| 207 | $t .= "<td>$v</td>";
|
|---|
| 208 | }
|
|---|
| 209 | $t .= '</tr>';
|
|---|
| 210 | }
|
|---|
| 211 | $t .= '</table>';
|
|---|
| 212 | return $this->test . '<br />' . $t;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | public function get_lifetime()
|
|---|
| 216 | {
|
|---|
| 217 | return 2000000000 - time();
|
|---|
| 218 | }
|
|---|
| 219 | }
|
|---|
| 220 | new SimplePieCacheTester();
|
|---|