broken-link-checker
Last commit date
images
16 years ago
JSON.php
16 years ago
broken-link-checker.php
16 years ago
instance-classes.php
16 years ago
link-classes.php
16 years ago
readme.txt
16 years ago
uninstall.php
16 years ago
utility-class.php
16 years ago
wsblc_ajax.php
16 years ago
instance-classes.php
465 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @author W-Shadow |
| 5 | * @copyright 2009 |
| 6 | */ |
| 7 | |
| 8 | if (!class_exists('blcLinkInstance')) { |
| 9 | class blcLinkInstance { |
| 10 | |
| 11 | //Object state |
| 12 | var $is_new = false; |
| 13 | |
| 14 | //DB fields |
| 15 | var $instance_id = 0; |
| 16 | var $link_id = 0; |
| 17 | var $source_id = 0; |
| 18 | var $source_type = ''; |
| 19 | var $link_text = ''; |
| 20 | var $instance_type = ''; |
| 21 | |
| 22 | //These are used to pass info to callbacks when editing an instance |
| 23 | var $old_url = null; |
| 24 | var $new_url = null; |
| 25 | |
| 26 | /** |
| 27 | * blcLinkInstance::__construct() |
| 28 | * Class constructor |
| 29 | * |
| 30 | * @param mixed $arg XXXXX look up how to do a multiline doc here (phpdoc) |
| 31 | * @return void |
| 32 | */ |
| 33 | function __construct($arg = null){ |
| 34 | |
| 35 | if (is_int($arg)){ |
| 36 | //Load an instance with ID = $arg from the DB. |
| 37 | $q = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}blc_instances WHERE instance_id=%d LIMIT 1", $arg); |
| 38 | $arr = $wpdb->get_row( $q, ARRAY_A ); |
| 39 | |
| 40 | if ( is_array($arr) ){ //Loaded successfully |
| 41 | $this->set_values($arr); |
| 42 | } else { |
| 43 | //Link instance not found. The object is invalid. |
| 44 | } |
| 45 | |
| 46 | } else if (is_array($arg)){ |
| 47 | $this->set_values($arg); |
| 48 | |
| 49 | //Is this a new instance? |
| 50 | $this->is_new = empty($this->instance_id); |
| 51 | |
| 52 | } else { |
| 53 | $this->is_new = true; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * blcLinkInstance::blcLinkInstance() |
| 59 | * Old-style constructor for PHP 4. Do not use. |
| 60 | * |
| 61 | * @param mixed $arg |
| 62 | * @return void |
| 63 | */ |
| 64 | function blcLinkInstance($arg = null){ |
| 65 | $this->__construct($arg); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * blcLinkInstance::valid() |
| 70 | * Verifies whether the object represents a valid link instance |
| 71 | * |
| 72 | * @return bool |
| 73 | */ |
| 74 | function valid(){ |
| 75 | //Some basic validation to ensure the required properties are set. |
| 76 | return !empty($this->link_id) && !empty($this->instance_type) && !empty($this->source_id) |
| 77 | && !empty($this->source_type) && (!empty($this->instance_id) || $this->is_new); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * blcLinkInstance::set_values() |
| 82 | * Set property values to the ones provided in an array (doesn't sanitize). |
| 83 | * |
| 84 | * @param array $arr An associative array |
| 85 | * @return void |
| 86 | */ |
| 87 | function set_values($arr){ |
| 88 | foreach( $arr as $key => $value ){ |
| 89 | $this->$key = $value; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * blcLinkInstance::edit() |
| 95 | * Replace this instance's URL with a new one. |
| 96 | * Warning : this shouldn't be called directly. Use blcLink->edit() instead. |
| 97 | * |
| 98 | * @param string $new_url |
| 99 | * @return bool |
| 100 | */ |
| 101 | function edit($old_url, $new_url){ |
| 102 | echo "Error : The stub function blcLinkInstance->edit() was executed!\r\n"; |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * blcLinkInstance::unlink() |
| 108 | * Remove this instance from the post/blogroll/etc. Also deletes the appropriate DB record(s). |
| 109 | * |
| 110 | * @return bool |
| 111 | */ |
| 112 | function unlink( $url = null ) { |
| 113 | //FB::warn("The stub function blcLinkInstance->unlink() was executed!"); |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * blcLinkInstance::forget() |
| 119 | * Remove the link instance record from database. Doesn't affect the post/link/whatever. |
| 120 | * |
| 121 | * @return mixed 1 on success, 0 if the instance wasn't found, false on error |
| 122 | */ |
| 123 | function forget(){ |
| 124 | global $wpdb; |
| 125 | |
| 126 | if ( !$this->valid() ) return false; |
| 127 | |
| 128 | if ( !empty($this->link_id) ) { |
| 129 | $rez = $wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->prefix}blc_instances WHERE instance_id=%d", $this->instance_id) ); |
| 130 | $this->link_id = 0; |
| 131 | return $rez; |
| 132 | } else { |
| 133 | return false; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * blcLinkInstance::save() |
| 139 | * Store the instance in the database. |
| 140 | * |
| 141 | * @return bool TRUE on success, FALSE on failure |
| 142 | */ |
| 143 | function save(){ |
| 144 | global $wpdb; |
| 145 | |
| 146 | if ( !$this->valid() ) return false; |
| 147 | |
| 148 | if ( $this->is_new ){ |
| 149 | |
| 150 | //Insert a new row |
| 151 | $q = " |
| 152 | INSERT INTO {$wpdb->prefix}blc_instances |
| 153 | ( link_id, source_id, source_type, link_text, instance_type ) |
| 154 | VALUES( %d, %d, %s, %s, %s )"; |
| 155 | |
| 156 | $q = $wpdb->prepare($q, $this->link_id, $this->source_id, $this->source_type, |
| 157 | $this->link_text, $this->instance_type ); |
| 158 | |
| 159 | $rez = $wpdb->query($q); |
| 160 | $rez = $rez !== false; |
| 161 | |
| 162 | if ($rez){ |
| 163 | $this->instance_id = $wpdb->insert_id; |
| 164 | //If the instance was successfully saved then it's no longer "new". |
| 165 | $this->is_new = !$rez; |
| 166 | } |
| 167 | |
| 168 | return $rez; |
| 169 | |
| 170 | } else { |
| 171 | |
| 172 | //Create a new DB record |
| 173 | $q = "UPDATE {$wpdb->prefix}blc_instances |
| 174 | SET link_id = %d, source_id = %d, source_type = %s, link_text = %s, instance_type = %s |
| 175 | WHERE instance_id = %d"; |
| 176 | |
| 177 | $q = $wpdb->prepare($q, $this->link_id, $this->source_id, $this->source_type, |
| 178 | $this->link_text, $this->instance_type, $this->instance_id ); |
| 179 | |
| 180 | $rez = $wpdb->query($q) !== false; |
| 181 | |
| 182 | if ($rez){ |
| 183 | //FB::info($this, "Instance updated"); |
| 184 | } else { |
| 185 | //FB::error("DB error while updating instance {$this->instance_id} : {$wpdb->last_error}"); |
| 186 | } |
| 187 | |
| 188 | return $rez; |
| 189 | |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * blcLinkInstance::get_url() |
| 195 | * Get the URL associated with this instance |
| 196 | * |
| 197 | * @return string |
| 198 | */ |
| 199 | function get_url(){ |
| 200 | if ( !$this->valid() ){ |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | //If the URL isn't specified get it from the link record |
| 205 | $link = new blcLink( intval($this->link_id) ); |
| 206 | return $link->url; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | class blcLinkInstance_post_link extends blcLinkInstance { |
| 211 | |
| 212 | function edit($old_url, $new_url){ |
| 213 | global $wpdb; |
| 214 | |
| 215 | if ( !$this->valid() ){ |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | //If the old URL isn't specified get it from the link record |
| 220 | if ( empty($old_url) ){ |
| 221 | $old_url = $this->get_url(); |
| 222 | } |
| 223 | |
| 224 | //Load the post |
| 225 | $post = get_post($this->source_id, ARRAY_A); |
| 226 | if (!$post){ |
| 227 | //FB::error('Can\'t load post ' . $this->source_id); |
| 228 | return false; |
| 229 | } |
| 230 | //FB::info('Post ' . $this->source_id . ' loaded successfully'); |
| 231 | |
| 232 | $this->old_url = $old_url; |
| 233 | $this->new_url = $new_url; |
| 234 | |
| 235 | //Find all links and replace those that match $old_url. |
| 236 | $content = preg_replace_callback(blcUtility::link_pattern(), array(&$this, 'edit_callback'), $post['post_content']); |
| 237 | $q = $wpdb->prepare("UPDATE $wpdb->posts SET post_content = %s WHERE id = %d", $content, $this->source_id); |
| 238 | |
| 239 | return $wpdb->query($q) !== false; |
| 240 | } |
| 241 | |
| 242 | function edit_callback($matches){ |
| 243 | $url = blcUtility::normalize_url($matches[3]); |
| 244 | |
| 245 | if ($url == $this->old_url){ |
| 246 | return $matches[1].$matches[2].$this->new_url.$matches[2].$matches[4].$matches[5].$matches[6]; |
| 247 | } else { |
| 248 | return $matches[0]; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | function unlink( $url = null ){ |
| 253 | global $wpdb; |
| 254 | |
| 255 | if ( !$this->valid() ){ |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | //If the URL isn't specified get it from the link record |
| 260 | if ( empty($url) ){ |
| 261 | $url = $this->get_url(); |
| 262 | } |
| 263 | |
| 264 | //Load the post |
| 265 | $post = get_post($this->source_id, ARRAY_A); |
| 266 | if (!$post){ |
| 267 | //FB::error('Can\'t load post ' . $this->source_id); |
| 268 | return false; |
| 269 | } |
| 270 | //FB::info('Post ' . $this->source_id . ' loaded successfully'); |
| 271 | |
| 272 | //Find all links and remove those that match $url. |
| 273 | $this->old_url = $url; //used by the callback |
| 274 | $content = preg_replace_callback(blcUtility::link_pattern(), array(&$this, 'unlink_callback'), $post['post_content']); |
| 275 | $q = $wpdb->prepare("UPDATE $wpdb->posts SET post_content = %s WHERE id = %d", $content, $this->source_id); |
| 276 | |
| 277 | if ( $wpdb->query($q) !== false ){ |
| 278 | //Delete the instance record |
| 279 | //FB::info("Post updated, deleting instance from DB"); |
| 280 | return $this->forget() !== false; |
| 281 | } else { |
| 282 | //FB::error("Failed to update the post"); |
| 283 | return false; |
| 284 | }; |
| 285 | } |
| 286 | |
| 287 | function unlink_callback($matches){ |
| 288 | $url = blcUtility::normalize_url($matches[3]); |
| 289 | |
| 290 | if ($url == $this->old_url){ |
| 291 | return $matches[5]; //just the anchor text |
| 292 | } else { |
| 293 | return $matches[0]; //return the link unchanged |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | } |
| 298 | |
| 299 | class blcLinkInstance_post_image extends blcLinkInstance { |
| 300 | |
| 301 | function edit($old_url, $new_url){ |
| 302 | global $wpdb; |
| 303 | |
| 304 | if ( !$this->valid() ){ |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | //If the URL isn't specified get it from the link record |
| 309 | if ( empty($old_url) ){ |
| 310 | $old_url = $this->get_url(); |
| 311 | } |
| 312 | |
| 313 | //Load the post |
| 314 | $post = get_post($this->source_id, ARRAY_A); |
| 315 | if (!$post){ |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | $this->old_url = $old_url; |
| 320 | $this->new_url = $new_url; |
| 321 | |
| 322 | //Find all images and change the URL of those that match $old_url. |
| 323 | //Note : this might be inefficient if there's more than one instance of the sme link |
| 324 | //in one post, as each instances would be called when editing the link. |
| 325 | //Either way, I thing the overhead is small enough to ignore for now. |
| 326 | $content = preg_replace_callback(blcUtility::img_pattern(), array(&$this, 'edit_callback'), $post['post_content']); |
| 327 | $q = $wpdb->prepare("UPDATE $wpdb->posts SET post_content = %s WHERE id = %d", $content, $this->source_id); |
| 328 | |
| 329 | return $wpdb->query($q) !== false; |
| 330 | } |
| 331 | |
| 332 | function edit_callback($matches){ |
| 333 | $url = blcUtility::normalize_url($matches[3]); |
| 334 | |
| 335 | if ($url == $this->old_url){ |
| 336 | return $matches[1].$matches[2].$this->new_url.$matches[2].$matches[4].$matches[5]; |
| 337 | } else { |
| 338 | return $matches[0]; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | function unlink( $url = null ){ |
| 343 | global $wpdb; |
| 344 | |
| 345 | if ( !$this->valid() ){ |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | //If the URL isn't specified get it from the link record |
| 350 | if ( empty($url) ){ |
| 351 | $url = $this->get_url(); |
| 352 | } |
| 353 | |
| 354 | //Load the post |
| 355 | $post = get_post($this->source_id, ARRAY_A); |
| 356 | if (!$post){ |
| 357 | //FB::error('Can\'t load post ' . $this->source_id); |
| 358 | return false; |
| 359 | } |
| 360 | //FB::info('Post ' . $this->source_id . ' loaded successfully'); |
| 361 | |
| 362 | //Find all links and remove those that match $url. |
| 363 | $this->old_url = $url; //used by the callback |
| 364 | $content = preg_replace_callback(blcUtility::img_pattern(), array(&$this, 'unlink_callback'), $post['post_content']); |
| 365 | $q = $wpdb->prepare("UPDATE $wpdb->posts SET post_content = %s WHERE id = %d", $content, $this->source_id); |
| 366 | |
| 367 | if ( $wpdb->query($q) !== false ){ |
| 368 | //Delete the instance record |
| 369 | //FB::info("Post updated, deleting instance from DB"); |
| 370 | return $this->forget() !== false; |
| 371 | } else { |
| 372 | //FB::error("Failed to update the post"); |
| 373 | return false; |
| 374 | }; |
| 375 | } |
| 376 | |
| 377 | function unlink_callback($matches){ |
| 378 | $url = blcUtility::normalize_url($matches[3]); |
| 379 | |
| 380 | if ($url == $this->old_url){ |
| 381 | return ''; //remove the image completely |
| 382 | } else { |
| 383 | return $matches[0]; //return the image unchanged |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | } |
| 388 | |
| 389 | class blcLinkInstance_custom_field_link extends blcLinkInstance { |
| 390 | |
| 391 | function edit($old_url, $new_url){ |
| 392 | if ( !$this->valid() ){ |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | //If the URL isn't specified get it from the link record |
| 397 | if ( empty($old_url) ){ |
| 398 | $old_url = $this->old_url; |
| 399 | } |
| 400 | |
| 401 | //FB::log("Changing [{$this->link_text}] to '$new_url' on post {$this->source_id}"); |
| 402 | //Change the meta value |
| 403 | return update_post_meta( $this->source_id, $this->link_text, $new_url, $old_url ); |
| 404 | } |
| 405 | |
| 406 | function unlink( $url = null ){ |
| 407 | //Get the URL from the link record if it wasn't specified |
| 408 | if ( empty($url) ){ |
| 409 | $url = $this->get_url(); |
| 410 | } |
| 411 | |
| 412 | //FB::log("Removing [{$this->link_text}] from post {$this->source_id} where value is '$url'"); |
| 413 | delete_post_meta( $this->source_id, $this->link_text, $url ); |
| 414 | |
| 415 | return $this->forget() !== false; |
| 416 | } |
| 417 | |
| 418 | } |
| 419 | |
| 420 | class blcLinkInstance_blogroll_link extends blcLinkInstance { |
| 421 | |
| 422 | function edit($old_url, $new_url){ |
| 423 | if ( !$this->valid() ){ |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | //FB::log("Changing the bookmark [{$this->link_text}] to '$new_url'"); |
| 428 | //Update the bookmark. Note : wp_update_link calls the edit_link hook, which is also |
| 429 | //hooked by the plugin for maintaining bookmark->instance integrity... Conclusion : |
| 430 | //don't ever call $instance->edit() in that hook! |
| 431 | return wp_update_link( array( |
| 432 | 'link_id' => $this->source_id, |
| 433 | 'link_url' => $new_url |
| 434 | ) ); |
| 435 | } |
| 436 | |
| 437 | function unlink( $url = null ){ |
| 438 | if ( !$this->valid() ){ |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | //Get the URL from the link record if it wasn't specified |
| 443 | if ( empty($url) ){ |
| 444 | $url = $this->get_url(); |
| 445 | } |
| 446 | |
| 447 | //FB::log("Removing bookmark [{$this->link_text}] ( ID : {$this->source_id} )"); |
| 448 | //Note : wp_delete_link calls the delete_link hook, which is also used by the plugin |
| 449 | //for removing instances associated with links deleted through the WP link manager. |
| 450 | //This means that when you delete a bookmark via the plugin's interface, the plugin will |
| 451 | //attempt to delete it twice. Anybody have a better idea? |
| 452 | if ( wp_delete_link( $this->source_id ) ){ |
| 453 | return $this->forget() !== false; |
| 454 | } else { |
| 455 | //FB::error("Failed to delete the bookmark."); |
| 456 | return false; |
| 457 | }; |
| 458 | |
| 459 | } |
| 460 | |
| 461 | } |
| 462 | |
| 463 | }//class_exists |
| 464 | |
| 465 | ?> |