[44] | 1 | <?php
|
---|
| 2 | define( 'JCSP_DB_VERSION', '0.1beta' );
|
---|
| 3 |
|
---|
| 4 | class Jcsp
|
---|
| 5 | {
|
---|
| 6 | public function getPostArchiveTableName()
|
---|
| 7 | {
|
---|
| 8 | global $wpdb;
|
---|
| 9 | return $wpdb->prefix . 'jcsp_post_permalinks';
|
---|
| 10 | }
|
---|
| 11 |
|
---|
| 12 | public function getCategoryArchiveTableName()
|
---|
| 13 | {
|
---|
| 14 | global $wpdb;
|
---|
| 15 | return $wpdb->prefix . 'jcsp_category_permalinks';
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | public function getPostPermalink( $postId )
|
---|
| 19 | {
|
---|
| 20 | $permalink = get_permalink( $postId );
|
---|
| 21 |
|
---|
| 22 | //cut out the http://domain.com/
|
---|
| 23 | $permalink = explode( '/', $permalink );
|
---|
| 24 | array_shift( $permalink );
|
---|
| 25 | array_shift( $permalink );
|
---|
| 26 | array_shift( $permalink );
|
---|
| 27 | $permalink = implode( '/', $permalink );
|
---|
| 28 |
|
---|
| 29 | if( substr( $permalink, -1 ) == '/' )
|
---|
| 30 | {
|
---|
| 31 | $permalink = substr( $permalink, 0, -1 );
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | return $permalink;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | public function getCategoryPermalink( $categoryId )
|
---|
| 38 | {
|
---|
| 39 | global $wpdb;
|
---|
| 40 | $permalink = get_category_link( $categoryId );
|
---|
| 41 |
|
---|
| 42 | //TODO: This is a little hack that gives a permalink as it will be AFTER the new category is saved, due to the fact that wordpress for some reason calls the edit_category action BEFORE the data is saved... If wp ever decides to change field names then this will break... But I doubt that
|
---|
| 43 | if( isset( $_POST[ 'category_nicename' ] ) )
|
---|
| 44 | {
|
---|
| 45 | $category = $wpdb->get_results( 'SELECT category_nicename FROM ' . $wpdb->categories . ' WHERE cat_ID = ' . $categoryId );
|
---|
| 46 | $permalink = str_replace( $category->category_nicename, $_POST[ 'category_nicename' ], $permalink );
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | //cut out the http://domain.com/
|
---|
| 50 | $permalink = explode( '/', $permalink );
|
---|
| 51 | array_shift( $permalink );
|
---|
| 52 | array_shift( $permalink );
|
---|
| 53 | array_shift( $permalink );
|
---|
| 54 | $permalink = implode( '/', $permalink );
|
---|
| 55 |
|
---|
| 56 | if( substr( $permalink, -1 ) == '/' )
|
---|
| 57 | {
|
---|
| 58 | $permalink = substr( $permalink, 0, -1 );
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | return $permalink;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | public function postUpdated( $postId )
|
---|
| 65 | {
|
---|
| 66 | global $wpdb;
|
---|
| 67 | $permalink = $this->getPostPermalink( $postId );
|
---|
| 68 | $exists = $wpdb->get_results( 'SELECT post_id FROM ' . $this->getPostArchiveTableName() . ' WHERE post_permalink = "' . $wpdb->escape( $permalink ) . '"' );
|
---|
| 69 | if( count( $exists ) > 0 )
|
---|
| 70 | {
|
---|
| 71 | $wpdb->query( 'UPDATE ' . $this->getPostArchiveTableName() . ' SET post_id = ' . $postId . ' WHERE post_permalink = "' . $wpdb->escape( $permalink ) . '"' );
|
---|
| 72 | }
|
---|
| 73 | else
|
---|
| 74 | {
|
---|
| 75 | $wpdb->query( 'INSERT INTO ' . $this->getPostArchiveTableName() . ' ( post_id, post_permalink ) VALUES ( ' . $postId . ', "' . $wpdb->escape( $permalink ) . '")' );
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | public function categoryUpdated( $categoryId )
|
---|
| 80 | {
|
---|
| 81 | global $wpdb;
|
---|
| 82 | $posts = $wpdb->get_results( 'SELECT post_id FROM ' . $wpdb->post2cat . ' WHERE category_id = ' . $categoryId );
|
---|
| 83 | foreach( $posts as $post )
|
---|
| 84 | {
|
---|
| 85 | $this->postUpdated( $post->post_id );
|
---|
| 86 | }
|
---|
| 87 | $permalink = $this->getCategoryPermalink( $categoryId );
|
---|
| 88 | $exists = $wpdb->get_results( 'SELECT category_id FROM ' . $this->getCategoryArchiveTableName() . ' WHERE category_permalink = "' . $wpdb->escape( $permalink ) . '"' );
|
---|
| 89 | if( count( $exists ) > 0 )
|
---|
| 90 | {
|
---|
| 91 | $wpdb->query( 'UPDATE ' . $this->getCategoryArchiveTableName() . ' SET category_id = ' . $categoryId . ' WHERE category_permalink = "' . $wpdb->escape( $permalink ) . '"' );
|
---|
| 92 | }
|
---|
| 93 | else
|
---|
| 94 | {
|
---|
| 95 | $wpdb->query( 'INSERT INTO ' . $this->getCategoryArchiveTableName() . ' ( category_id, category_permalink ) VALUES ( ' . $categoryId . ', "' . $wpdb->escape( $permalink ) . '")' );
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | function ensureTables()
|
---|
| 100 | {
|
---|
| 101 | $this->ensurePostArchiveTable();
|
---|
| 102 | $this->ensureCategoryArchiveTable();
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | function ensurePostArchiveTable()
|
---|
| 106 | {
|
---|
| 107 | global $wpdb;
|
---|
| 108 | $table_name = $this->getPostArchiveTableName();
|
---|
| 109 | if( $wpdb->get_var( 'show tables like "' . $table_name . '"' ) != $table_name )
|
---|
| 110 | {
|
---|
| 111 | $sql = 'CREATE TABLE ' . $table_name . ' (
|
---|
| 112 | post_archive_id mediumint(9) NOT NULL AUTO_INCREMENT,
|
---|
| 113 | post_id mediumint(9) NOT NULL,
|
---|
| 114 | post_permalink VARCHAR(55) NOT NULL,
|
---|
| 115 | UNIQUE KEY post_archive_id (post_archive_id)
|
---|
| 116 | );';
|
---|
| 117 | require_once ABSPATH . 'wp-admin/upgrade-functions.php';
|
---|
| 118 | dbDelta($sql);
|
---|
| 119 | $this->initializePostArchiveTable();
|
---|
| 120 | add_option( 'JCSP_DB_VERSION', JCSP_DB_VERSION );
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | function ensureCategoryArchiveTable()
|
---|
| 125 | {
|
---|
| 126 | global $wpdb;
|
---|
| 127 | $table_name = $this->getCategoryArchiveTableName();
|
---|
| 128 | if( $wpdb->get_var( 'show tables like "' . $table_name . '"' ) != $table_name )
|
---|
| 129 | {
|
---|
| 130 | $sql = 'CREATE TABLE ' . $table_name . ' (
|
---|
| 131 | category_archive_id mediumint(9) NOT NULL AUTO_INCREMENT,
|
---|
| 132 | category_id mediumint(9) NOT NULL,
|
---|
| 133 | category_permalink VARCHAR(55) NOT NULL,
|
---|
| 134 | UNIQUE KEY category_archive_id (category_archive_id)
|
---|
| 135 | );';
|
---|
| 136 |
|
---|
| 137 | require_once ABSPATH . 'wp-admin/upgrade-functions.php';
|
---|
| 138 | dbDelta( $sql );
|
---|
| 139 | $this->initializeCategoryArchiveTable();
|
---|
| 140 | add_option( 'JCSP_DB_VERSION', JCSP_DB_VERSION );
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | function initializePostArchiveTable()
|
---|
| 145 | {
|
---|
| 146 | global $wpdb;
|
---|
| 147 | $posts = $wpdb->get_results( 'SELECT ID FROM ' . $wpdb->posts );
|
---|
| 148 | foreach( $posts as $post )
|
---|
| 149 | {
|
---|
| 150 | $permalink = $this->getPostPermalink( $post->ID );
|
---|
| 151 | $sql = 'INSERT INTO ' . $this->getPostArchiveTableName() . '
|
---|
| 152 | ( post_id, post_permalink )
|
---|
| 153 | VALUES ( ' . $post->ID . ', "' . $wpdb->escape( $permalink ) . '" )';
|
---|
| 154 | $wpdb->query( $sql );
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | function initializeCategoryArchiveTable()
|
---|
| 159 | {
|
---|
| 160 | global $wpdb;
|
---|
| 161 | $categories = $wpdb->get_results( 'SELECT cat_ID FROM ' . $wpdb->categories );
|
---|
| 162 | foreach( $categories as $category )
|
---|
| 163 | {
|
---|
| 164 | $permalink = $this->getCategoryPermalink( $category->cat_ID );
|
---|
| 165 | $sql = 'INSERT INTO ' . $this->getCategoryArchiveTableName() . '
|
---|
| 166 | ( category_id, category_permalink )
|
---|
| 167 | VALUES ( ' . $category->cat_ID . ', "' . $wpdb->escape( $permalink ) . '" )';
|
---|
| 168 | $wpdb->query( $sql );
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | function tryRedirect()
|
---|
| 173 | {
|
---|
| 174 | global $wpdb;
|
---|
| 175 |
|
---|
| 176 | $inLink = $_SERVER[ 'REQUEST_URI' ];
|
---|
| 177 | if( substr( $inLink, 0, 1 ) == '/' )
|
---|
| 178 | {
|
---|
| 179 | $inLink = substr( $inLink, 1 );
|
---|
| 180 | }
|
---|
| 181 | if( substr( $inLink, -1 ) == '/' )
|
---|
| 182 | {
|
---|
| 183 | $inLink = substr( $inLink, 0, -1 );
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | //user is accessing the index page, we have to return or this will cause problems
|
---|
| 187 | if( $inLink == '' )
|
---|
| 188 | {
|
---|
| 189 | return;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | //try a post redirect
|
---|
| 193 | $destination = $wpdb->get_results( 'SELECT post_id FROM ' . $this->getPostArchiveTableName() . ' WHERE post_permalink = "' . $wpdb->escape( $inLink ) . '"' );
|
---|
| 194 | if( count( $destination ) > 0 )
|
---|
| 195 | {
|
---|
| 196 | $permalink = $this->getPostPermalink( $destination[ 0 ]->post_id );
|
---|
| 197 | if( $permalink != $inLink )
|
---|
| 198 | {
|
---|
| 199 | wp_redirect( '/' . $permalink );
|
---|
| 200 | exit;
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | //try a category redirect
|
---|
| 205 | $destination = $wpdb->get_results( 'SELECT category_id FROM ' . $this->getCategoryArchiveTableName() . ' WHERE category_permalink = "' . $wpdb->escape( $inLink ) . '"' );
|
---|
| 206 | if( count( $destination ) > 0 )
|
---|
| 207 | {
|
---|
| 208 | $permalink = $this->getCategoryPermalink( $destination[ 0 ]->category_id );
|
---|
| 209 | if( $permalink != $inLink )
|
---|
| 210 | {
|
---|
| 211 | wp_redirect( '/' . $permalink );
|
---|
| 212 | exit;
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 | ?> |
---|