raw
mp-wp_genesis           1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * Misc WordPress Administration API.
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Administration
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 /**
mp-wp_genesis 10 * {@internal Missing Short Description}}
mp-wp_genesis 11 *
mp-wp_genesis 12 * @since unknown
mp-wp_genesis 13 *
mp-wp_genesis 14 * @return unknown
mp-wp_genesis 15 */
mp-wp_genesis 16 function got_mod_rewrite() {
mp-wp_genesis 17 $got_rewrite = apache_mod_loaded('mod_rewrite', true);
mp-wp_genesis 18 return apply_filters('got_rewrite', $got_rewrite);
mp-wp_genesis 19 }
mp-wp_genesis 20
mp-wp_genesis 21 /**
mp-wp_genesis 22 * {@internal Missing Short Description}}
mp-wp_genesis 23 *
mp-wp_genesis 24 * @since unknown
mp-wp_genesis 25 *
mp-wp_genesis 26 * @param unknown_type $filename
mp-wp_genesis 27 * @param unknown_type $marker
mp-wp_genesis 28 * @return array An array of strings from a file (.htaccess ) from between BEGIN and END markers.
mp-wp_genesis 29 */
mp-wp_genesis 30 function extract_from_markers( $filename, $marker ) {
mp-wp_genesis 31 $result = array ();
mp-wp_genesis 32
mp-wp_genesis 33 if (!file_exists( $filename ) ) {
mp-wp_genesis 34 return $result;
mp-wp_genesis 35 }
mp-wp_genesis 36
mp-wp_genesis 37 if ( $markerdata = explode( "\n", implode( '', file( $filename ) ) ));
mp-wp_genesis 38 {
mp-wp_genesis 39 $state = false;
mp-wp_genesis 40 foreach ( $markerdata as $markerline ) {
mp-wp_genesis 41 if (strpos($markerline, '# END ' . $marker) !== false)
mp-wp_genesis 42 $state = false;
mp-wp_genesis 43 if ( $state )
mp-wp_genesis 44 $result[] = $markerline;
mp-wp_genesis 45 if (strpos($markerline, '# BEGIN ' . $marker) !== false)
mp-wp_genesis 46 $state = true;
mp-wp_genesis 47 }
mp-wp_genesis 48 }
mp-wp_genesis 49
mp-wp_genesis 50 return $result;
mp-wp_genesis 51 }
mp-wp_genesis 52
mp-wp_genesis 53 /**
mp-wp_genesis 54 * {@internal Missing Short Description}}
mp-wp_genesis 55 *
mp-wp_genesis 56 * Inserts an array of strings into a file (.htaccess ), placing it between
mp-wp_genesis 57 * BEGIN and END markers. Replaces existing marked info. Retains surrounding
mp-wp_genesis 58 * data. Creates file if none exists.
mp-wp_genesis 59 *
mp-wp_genesis 60 * @since unknown
mp-wp_genesis 61 *
mp-wp_genesis 62 * @param unknown_type $filename
mp-wp_genesis 63 * @param unknown_type $marker
mp-wp_genesis 64 * @param unknown_type $insertion
mp-wp_genesis 65 * @return bool True on write success, false on failure.
mp-wp_genesis 66 */
mp-wp_genesis 67 function insert_with_markers( $filename, $marker, $insertion ) {
mp-wp_genesis 68 if (!file_exists( $filename ) || is_writeable( $filename ) ) {
mp-wp_genesis 69 if (!file_exists( $filename ) ) {
mp-wp_genesis 70 $markerdata = '';
mp-wp_genesis 71 } else {
mp-wp_genesis 72 $markerdata = explode( "\n", implode( '', file( $filename ) ) );
mp-wp_genesis 73 }
mp-wp_genesis 74
mp-wp_genesis 75 $f = fopen( $filename, 'w' );
mp-wp_genesis 76 $foundit = false;
mp-wp_genesis 77 if ( $markerdata ) {
mp-wp_genesis 78 $state = true;
mp-wp_genesis 79 foreach ( $markerdata as $n => $markerline ) {
mp-wp_genesis 80 if (strpos($markerline, '# BEGIN ' . $marker) !== false)
mp-wp_genesis 81 $state = false;
mp-wp_genesis 82 if ( $state ) {
mp-wp_genesis 83 if ( $n + 1 < count( $markerdata ) )
mp-wp_genesis 84 fwrite( $f, "{$markerline}\n" );
mp-wp_genesis 85 else
mp-wp_genesis 86 fwrite( $f, "{$markerline}" );
mp-wp_genesis 87 }
mp-wp_genesis 88 if (strpos($markerline, '# END ' . $marker) !== false) {
mp-wp_genesis 89 fwrite( $f, "# BEGIN {$marker}\n" );
mp-wp_genesis 90 if ( is_array( $insertion ))
mp-wp_genesis 91 foreach ( $insertion as $insertline )
mp-wp_genesis 92 fwrite( $f, "{$insertline}\n" );
mp-wp_genesis 93 fwrite( $f, "# END {$marker}\n" );
mp-wp_genesis 94 $state = true;
mp-wp_genesis 95 $foundit = true;
mp-wp_genesis 96 }
mp-wp_genesis 97 }
mp-wp_genesis 98 }
mp-wp_genesis 99 if (!$foundit) {
mp-wp_genesis 100 fwrite( $f, "\n# BEGIN {$marker}\n" );
mp-wp_genesis 101 foreach ( $insertion as $insertline )
mp-wp_genesis 102 fwrite( $f, "{$insertline}\n" );
mp-wp_genesis 103 fwrite( $f, "# END {$marker}\n" );
mp-wp_genesis 104 }
mp-wp_genesis 105 fclose( $f );
mp-wp_genesis 106 return true;
mp-wp_genesis 107 } else {
mp-wp_genesis 108 return false;
mp-wp_genesis 109 }
mp-wp_genesis 110 }
mp-wp_genesis 111
mp-wp_genesis 112 /**
mp-wp_genesis 113 * Updates the htaccess file with the current rules if it is writable.
mp-wp_genesis 114 *
mp-wp_genesis 115 * Always writes to the file if it exists and is writable to ensure that we
mp-wp_genesis 116 * blank out old rules.
mp-wp_genesis 117 *
mp-wp_genesis 118 * @since unknown
mp-wp_genesis 119 */
mp-wp_genesis 120 function save_mod_rewrite_rules() {
mp-wp_genesis 121 global $wp_rewrite;
mp-wp_genesis 122
mp-wp_genesis 123 $home_path = get_home_path();
mp-wp_genesis 124 $htaccess_file = $home_path.'.htaccess';
mp-wp_genesis 125
mp-wp_genesis 126 // If the file doesn't already exists check for write access to the directory and whether of not we have some rules.
mp-wp_genesis 127 // else check for write access to the file.
mp-wp_genesis 128 if ((!file_exists($htaccess_file) && is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks()) || is_writable($htaccess_file)) {
mp-wp_genesis 129 if ( got_mod_rewrite() ) {
mp-wp_genesis 130 $rules = explode( "\n", $wp_rewrite->mod_rewrite_rules() );
mp-wp_genesis 131 return insert_with_markers( $htaccess_file, 'WordPress', $rules );
mp-wp_genesis 132 }
mp-wp_genesis 133 }
mp-wp_genesis 134
mp-wp_genesis 135 return false;
mp-wp_genesis 136 }
mp-wp_genesis 137
mp-wp_genesis 138 /**
mp-wp_genesis 139 * {@internal Missing Short Description}}
mp-wp_genesis 140 *
mp-wp_genesis 141 * @since unknown
mp-wp_genesis 142 *
mp-wp_genesis 143 * @param unknown_type $file
mp-wp_genesis 144 */
mp-wp_genesis 145 function update_recently_edited( $file ) {
mp-wp_genesis 146 $oldfiles = (array ) get_option( 'recently_edited' );
mp-wp_genesis 147 if ( $oldfiles ) {
mp-wp_genesis 148 $oldfiles = array_reverse( $oldfiles );
mp-wp_genesis 149 $oldfiles[] = $file;
mp-wp_genesis 150 $oldfiles = array_reverse( $oldfiles );
mp-wp_genesis 151 $oldfiles = array_unique( $oldfiles );
mp-wp_genesis 152 if ( 5 < count( $oldfiles ))
mp-wp_genesis 153 array_pop( $oldfiles );
mp-wp_genesis 154 } else {
mp-wp_genesis 155 $oldfiles[] = $file;
mp-wp_genesis 156 }
mp-wp_genesis 157 update_option( 'recently_edited', $oldfiles );
mp-wp_genesis 158 }
mp-wp_genesis 159
mp-wp_genesis 160 /**
mp-wp_genesis 161 * If siteurl or home changed, flush rewrite rules.
mp-wp_genesis 162 *
mp-wp_genesis 163 * @since unknown
mp-wp_genesis 164 *
mp-wp_genesis 165 * @param unknown_type $old_value
mp-wp_genesis 166 * @param unknown_type $value
mp-wp_genesis 167 */
mp-wp_genesis 168 function update_home_siteurl( $old_value, $value ) {
mp-wp_genesis 169 global $wp_rewrite;
mp-wp_genesis 170
mp-wp_genesis 171 if ( defined( "WP_INSTALLING" ) )
mp-wp_genesis 172 return;
mp-wp_genesis 173
mp-wp_genesis 174 // If home changed, write rewrite rules to new location.
mp-wp_genesis 175 $wp_rewrite->flush_rules();
mp-wp_genesis 176 }
mp-wp_genesis 177
mp-wp_genesis 178 add_action( 'update_option_home', 'update_home_siteurl', 10, 2 );
mp-wp_genesis 179 add_action( 'update_option_siteurl', 'update_home_siteurl', 10, 2 );
mp-wp_genesis 180
mp-wp_genesis 181 /**
mp-wp_genesis 182 * {@internal Missing Short Description}}
mp-wp_genesis 183 *
mp-wp_genesis 184 * @since unknown
mp-wp_genesis 185 *
mp-wp_genesis 186 * @param unknown_type $url
mp-wp_genesis 187 * @return unknown
mp-wp_genesis 188 */
mp-wp_genesis 189 function url_shorten( $url ) {
mp-wp_genesis 190 $short_url = str_replace( 'http://', '', stripslashes( $url ));
mp-wp_genesis 191 $short_url = str_replace( 'www.', '', $short_url );
mp-wp_genesis 192 if ('/' == substr( $short_url, -1 ))
mp-wp_genesis 193 $short_url = substr( $short_url, 0, -1 );
mp-wp_genesis 194 if ( strlen( $short_url ) > 35 )
mp-wp_genesis 195 $short_url = substr( $short_url, 0, 32 ).'...';
mp-wp_genesis 196 return $short_url;
mp-wp_genesis 197 }
mp-wp_genesis 198
mp-wp_genesis 199 /**
mp-wp_genesis 200 * {@internal Missing Short Description}}
mp-wp_genesis 201 *
mp-wp_genesis 202 * @since unknown
mp-wp_genesis 203 *
mp-wp_genesis 204 * @param unknown_type $vars
mp-wp_genesis 205 */
mp-wp_genesis 206 function wp_reset_vars( $vars ) {
mp-wp_genesis 207 for ( $i=0; $i<count( $vars ); $i += 1 ) {
mp-wp_genesis 208 $var = $vars[$i];
mp-wp_genesis 209 global $$var;
mp-wp_genesis 210
mp-wp_genesis 211 if (!isset( $$var ) ) {
mp-wp_genesis 212 if ( empty( $_POST["$var"] ) ) {
mp-wp_genesis 213 if ( empty( $_GET["$var"] ) )
mp-wp_genesis 214 $$var = '';
mp-wp_genesis 215 else
mp-wp_genesis 216 $$var = $_GET["$var"];
mp-wp_genesis 217 } else {
mp-wp_genesis 218 $$var = $_POST["$var"];
mp-wp_genesis 219 }
mp-wp_genesis 220 }
mp-wp_genesis 221 }
mp-wp_genesis 222 }
mp-wp_genesis 223
mp-wp_genesis 224 /**
mp-wp_genesis 225 * {@internal Missing Short Description}}
mp-wp_genesis 226 *
mp-wp_genesis 227 * @since unknown
mp-wp_genesis 228 *
mp-wp_genesis 229 * @param unknown_type $message
mp-wp_genesis 230 */
mp-wp_genesis 231 function show_message($message) {
mp-wp_genesis 232 if( is_wp_error($message) ){
mp-wp_genesis 233 if( $message->get_error_data() )
mp-wp_genesis 234 $message = $message->get_error_message() . ': ' . $message->get_error_data();
mp-wp_genesis 235 else
mp-wp_genesis 236 $message = $message->get_error_message();
mp-wp_genesis 237 }
mp-wp_genesis 238 echo "<p>$message</p>\n";
mp-wp_genesis 239 }
mp-wp_genesis 240
mp-wp_genesis 241 ?>