| 1 |
<?php |
| 2 |
################################################################################ |
| 3 |
## LEGACY API: Replicate or mock up functions for legacy support purposes ###### |
| 4 |
################################################################################ |
| 5 |
|
| 6 |
// version testing |
| 7 |
function fwp_test_wp_version ($floor, $ceiling = NULL) { |
| 8 |
global $wp_db_version; |
| 9 |
|
| 10 |
$ver = (isset($wp_db_version) ? $wp_db_version : 0); |
| 11 |
$good = ($ver >= $floor); |
| 12 |
if (!is_null($ceiling)) : |
| 13 |
$good = ($good and ($ver < $ceiling)); |
| 14 |
endif; |
| 15 |
return $good; |
| 16 |
} /* function fwp_test_wp_version () */ |
| 17 |
|
| 18 |
class FeedWordPressCompatibility { |
| 19 |
/*static*/ function test_version ($floor, $ceiling = null) { |
| 20 |
return fwp_test_wp_version($floor, $ceiling); |
| 21 |
} /* FeedWordPressCompatibility::test_version() */ |
| 22 |
|
| 23 |
/*static*/ function insert_link_category ($name) { |
| 24 |
global $wpdb; |
| 25 |
|
| 26 |
$name = $wpdb->escape($name); |
| 27 |
|
| 28 |
// WordPress 2.3+ term/taxonomy API |
| 29 |
if (function_exists('wp_insert_term')) : |
| 30 |
$term = wp_insert_term($name, 'link_category'); |
| 31 |
$cat_id = $term['term_id']; |
| 32 |
// WordPress 2.1, 2.2 category API. By the way, why the fuck is this API function only available in a wp-admin module? |
| 33 |
elseif (function_exists('wp_insert_category') and !fwp_test_wp_version(FWP_SCHEMA_20, FWP_SCHEMA_21)) : |
| 34 |
$cat_id = wp_insert_category(array('cat_name' => $name)); |
| 35 |
// WordPress 1.5 and 2.0.x |
| 36 |
elseif (fwp_test_wp_version(0, FWP_SCHEMA_21)) : |
| 37 |
$result = $wpdb->query(" |
| 38 |
INSERT INTO $wpdb->linkcategories |
| 39 |
SET |
| 40 |
cat_id = 0, |
| 41 |
cat_name='$name', |
| 42 |
show_images='N', |
| 43 |
show_description='N', |
| 44 |
show_rating='N', |
| 45 |
show_updated='N', |
| 46 |
sort_order='name' |
| 47 |
"); |
| 48 |
$cat_id = $wpdb->insert_id; |
| 49 |
// This should never happen. |
| 50 |
else : |
| 51 |
FeedWordPress::critical_bug('FeedWordPress::link_category_id::wp_db_version', $wp_db_version, __LINE__); |
| 52 |
endif; |
| 53 |
|
| 54 |
// Return newly-created category ID |
| 55 |
return $cat_id; |
| 56 |
} /* FeedWordPressCompatibility::insert_link_category () */ |
| 57 |
|
| 58 |
/*static*/ function link_category_id ($value, $key = 'cat_name') { |
| 59 |
global $wpdb; |
| 60 |
|
| 61 |
$cat_id = NULL; |
| 62 |
|
| 63 |
$the_term = term_exists($value, 'link_category'); |
| 64 |
|
| 65 |
// Sometimes, in some versions, we get a row |
| 66 |
if (is_array($the_term)) : |
| 67 |
$cat_id = $the_term['term_id']; |
| 68 |
|
| 69 |
// other times we get an integer result |
| 70 |
else : |
| 71 |
$cat_id = $the_term; |
| 72 |
endif; |
| 73 |
|
| 74 |
return $cat_id; |
| 75 |
} /* FeedWordPressCompatibility::link_category_id () */ |
| 76 |
|
| 77 |
/*static*/ function post_tags () { |
| 78 |
return FeedWordPressCompatibility::test_version(FWP_SCHEMA_23); |
| 79 |
} /* FeedWordPressCompatibility::post_tags () */ |
| 80 |
|
| 81 |
/*static*/ function validate_http_request ($action = -1, $capability = null) { |
| 82 |
// Only worry about this if we're using a method with significant side-effects |
| 83 |
if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') : |
| 84 |
// Limit post by user capabilities |
| 85 |
if (!is_null($capability) and !current_user_can($capability)) : |
| 86 |
wp_die(__('Cheatin’ uh?')); |
| 87 |
endif; |
| 88 |
|
| 89 |
// If check_admin_referer() checks a nonce. |
| 90 |
if (function_exists('wp_verify_nonce')) : |
| 91 |
check_admin_referer($action); |
| 92 |
|
| 93 |
// No nonces means no checking nonces. |
| 94 |
else : |
| 95 |
check_admin_referer(); |
| 96 |
endif; |
| 97 |
endif; |
| 98 |
} /* FeedWordPressCompatibility::validate_http_request() */ |
| 99 |
|
| 100 |
/*static*/ function stamp_nonce ($action = -1) { |
| 101 |
// stamp form with hidden fields for a nonce in WP 2.0.3 & later |
| 102 |
if (function_exists('wp_nonce_field')) : |
| 103 |
wp_nonce_field($action); |
| 104 |
endif; |
| 105 |
} /* FeedWordPressCompatibility::stamp_nonce() */ |
| 106 |
|
| 107 |
/*static*/ function bottom_script_hook ($filename) { |
| 108 |
global $fwp_path; |
| 109 |
|
| 110 |
$hook = 'admin_footer'; |
| 111 |
if (FeedWordPressCompatibility::test_version(FWP_SCHEMA_28)) : // WordPress 2.8+ |
| 112 |
$hook = $hook . '-' . $fwp_path . '/' . basename($filename); |
| 113 |
endif; |
| 114 |
return $hook; |
| 115 |
} /* FeedWordPressCompatibility::bottom_script_hook() */ |
| 116 |
} /* class FeedWordPressCompatibility */ |
| 117 |
|
| 118 |
define('FEEDWORDPRESS_AND_TAGS', (FeedWordPressCompatibility::post_tags() ? ' & Tags' : '')); |
| 119 |
|
| 120 |
if (!function_exists('stripslashes_deep')) { |
| 121 |
function stripslashes_deep($value) { |
| 122 |
$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); |
| 123 |
return $value; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if (!function_exists('get_option')) { |
| 128 |
function get_option ($option) { |
| 129 |
return get_settings($option); |
| 130 |
} |
| 131 |
} |
| 132 |
if (!function_exists('current_user_can')) { |
| 133 |
$fwp_capability['manage_options'] = 6; |
| 134 |
$fwp_capability['manage_links'] = 5; |
| 135 |
function current_user_can ($task) { |
| 136 |
global $user_level; |
| 137 |
|
| 138 |
$can = false; |
| 139 |
|
| 140 |
// This is **not** a full replacement for current_user_can. It |
| 141 |
// is only for checking the capabilities we care about via the |
| 142 |
// WordPress 1.5 user levels. |
| 143 |
switch ($task) : |
| 144 |
case 'manage_options': |
| 145 |
$can = ($user_level >= 6); |
| 146 |
break; |
| 147 |
case 'manage_links': |
| 148 |
$can = ($user_level >= 5); |
| 149 |
break; |
| 150 |
case 'edit_files': |
| 151 |
$can = ($user_level >= 9); |
| 152 |
break; |
| 153 |
endswitch; |
| 154 |
return $can; |
| 155 |
} |
| 156 |
} else { |
| 157 |
$fwp_capability['manage_options'] = 'manage_options'; |
| 158 |
$fwp_capability['manage_links'] = 'manage_links'; |
| 159 |
} |
| 160 |
if (!function_exists('sanitize_user')) { |
| 161 |
function sanitize_user ($text, $strict = false) { |
| 162 |
return $text; // Don't munge it if it wasn't munged going in... |
| 163 |
} |
| 164 |
} |
| 165 |
if (!function_exists('wp_insert_user')) { |
| 166 |
function wp_insert_user ($userdata) { |
| 167 |
global $wpdb; |
| 168 |
|
| 169 |
#-- Help WordPress 1.5.x quack like a duck |
| 170 |
$login = $userdata['user_login']; |
| 171 |
$author = $userdata['display_name']; |
| 172 |
$nice_author = $userdata['user_nicename']; |
| 173 |
$email = $userdata['user_email']; |
| 174 |
$url = $userdata['user_url']; |
| 175 |
|
| 176 |
$wpdb->query ( |
| 177 |
"INSERT INTO $wpdb->users |
| 178 |
SET |
| 179 |
ID='0', |
| 180 |
user_login='$login', |
| 181 |
user_firstname='$author', |
| 182 |
user_nickname='$author', |
| 183 |
user_nicename='$nice_author', |
| 184 |
user_description='$author', |
| 185 |
user_email='$email', |
| 186 |
user_url='$url'"); |
| 187 |
$id = $wpdb->insert_id; |
| 188 |
|
| 189 |
return $id; |
| 190 |
} |
| 191 |
} /* if (!function_exists('wp_insert_user')) */ |
| 192 |
|
| 193 |
if (!function_exists('wp_die')) { |
| 194 |
function wp_die ( $message, $title = '', $args = array() ) { |
| 195 |
die($message); |
| 196 |
} /* wp_die() */ |
| 197 |
} /* if */ |
| 198 |
|
| 199 |
if (!function_exists('add_post_meta')) { |
| 200 |
function add_post_meta ($postId, $key, $value, $unique) { |
| 201 |
global $wpdb; |
| 202 |
|
| 203 |
$postId = (int) $postId; |
| 204 |
$key = $wpdb->escape($key); |
| 205 |
$value = $wpdb->escape($value); |
| 206 |
|
| 207 |
$result = $wpdb->query(" |
| 208 |
INSERT INTO $wpdb->postmeta |
| 209 |
SET |
| 210 |
post_id='$postId', |
| 211 |
meta_key='$key', |
| 212 |
meta_value='$value' |
| 213 |
"); |
| 214 |
if (!$result) : |
| 215 |
$err = mysql_error(); |
| 216 |
if (FEEDWORDPRESS_DEBUG) : |
| 217 |
echo "[DEBUG:".date('Y-m-d H:i:S')."][feedwordpress]: post metadata insertion FAILED for field '$key' := '$value': [$err]"; |
| 218 |
endif; |
| 219 |
endif; |
| 220 |
} /* add_post_meta() */ |
| 221 |
} /* if */ |
| 222 |
|
| 223 |
if (!function_exists('disabled')) { |
| 224 |
/** |
| 225 |
* Outputs the html disabled attribute. |
| 226 |
* |
| 227 |
* Compares the first two arguments and if identical marks as disabled |
| 228 |
* |
| 229 |
* @since 3.0.0 |
| 230 |
* |
| 231 |
* @param mixed $disabled One of the values to compare |
| 232 |
* @param mixed $current (true) The other value to compare if not just true |
| 233 |
* @param bool $echo Whether to echo or just return the string |
| 234 |
* @return string html attribute or empty string |
| 235 |
*/ |
| 236 |
function disabled( $disabled, $current = true, $echo = true ) { |
| 237 |
return __checked_selected_helper( $disabled, $current, $echo, 'disabled' ); |
| 238 |
} |
| 239 |
} /* if */ |
| 240 |
|
| 241 |
if (!function_exists('term_exists')) { |
| 242 |
// Fucking WordPress 3.0 wordsmithing. |
| 243 |
function term_exists ( $term, $taxonomy = '', $parent = 0 ) { |
| 244 |
return is_term($term, $taxonomy, $parent); |
| 245 |
} |
| 246 |
} /* if */ |
| 247 |
require_once(dirname(__FILE__).'/feedwordpress-walker-category-checklist.class.php'); |
| 248 |
|
| 249 |
function fwp_category_checklist ($post_id = 0, $descendents_and_self = 0, $selected_cats = false, $prefix = '') { |
| 250 |
if (function_exists('wp_category_checklist')) : |
| 251 |
$walker = new FeedWordPress_Walker_Category_Checklist; |
| 252 |
$walker->set_prefix($prefix); |
| 253 |
wp_category_checklist( |
| 254 |
/*post_id=*/ $post_id, |
| 255 |
/*descendents_and_self=*/ $descendents_and_self, |
| 256 |
/*selected_cats=*/ $selected_cats, |
| 257 |
/*popular_cats=*/ false, |
| 258 |
/*walker=*/ $walker |
| 259 |
); |
| 260 |
else : |
| 261 |
// selected_cats is an array of integer cat_IDs / term_ids for |
| 262 |
// the categories that should be checked |
| 263 |
global $post_ID; |
| 264 |
|
| 265 |
$cats = get_nested_categories(); |
| 266 |
|
| 267 |
// Undo damage from usort() in WP 2.0 |
| 268 |
$dogs = array(); |
| 269 |
foreach ($cats as $cat) : |
| 270 |
$dogs[$cat['cat_ID']] = $cat; |
| 271 |
endforeach; |
| 272 |
foreach ($selected_cats as $cat_id) : |
| 273 |
$dogs[$cat_id]['checked'] = true; |
| 274 |
endforeach; |
| 275 |
write_nested_categories($dogs); |
| 276 |
endif; |
| 277 |
} |
| 278 |
|
| 279 |
function fwp_time_elapsed ($ts) { |
| 280 |
if (function_exists('human_time_diff')) : |
| 281 |
if ($ts >= time()) : |
| 282 |
$ret = __(human_time_diff($ts)." from now"); |
| 283 |
else : |
| 284 |
$ret = __(human_time_diff($ts)." ago"); |
| 285 |
endif; |
| 286 |
else : |
| 287 |
$ret = strftime('%x %X', $ts); |
| 288 |
endif; |
| 289 |
return $ret; |
| 290 |
} |
| 291 |
|
| 292 |
################################################################################ |
| 293 |
## UPGRADE INTERFACE: Have users upgrade DB from older versions of FWP ######### |
| 294 |
################################################################################ |
| 295 |
|
| 296 |
function fwp_upgrade_page () { |
| 297 |
if (isset($GLOBALS['fwp_post']['action']) and $GLOBALS['fwp_post']['action']=='Upgrade') : |
| 298 |
$ver = get_option('feedwordpress_version'); |
| 299 |
if (get_option('feedwordpress_version') != FEEDWORDPRESS_VERSION) : |
| 300 |
echo "<div class=\"wrap\">\n"; |
| 301 |
echo "<h2>Upgrading FeedWordPress...</h2>"; |
| 302 |
|
| 303 |
$feedwordpress = new FeedWordPress; |
| 304 |
$feedwordpress->upgrade_database($ver); |
| 305 |
echo "<p><strong>Done!</strong> Upgraded database to version ".FEEDWORDPRESS_VERSION.".</p>\n"; |
| 306 |
echo "<form action=\"\" method=\"get\">\n"; |
| 307 |
echo "<div class=\"submit\"><input type=\"hidden\" name=\"page\" value=\"syndication.php\" />"; |
| 308 |
echo "<input type=\"submit\" value=\"Continue »\" /></form></div>\n"; |
| 309 |
echo "</div>\n"; |
| 310 |
return; |
| 311 |
else : |
| 312 |
echo "<div class=\"updated\"><p>Already at version ".FEEDWORDPRESS_VERSION."!</p></div>"; |
| 313 |
endif; |
| 314 |
endif; |
| 315 |
?> |
| 316 |
<div class="wrap"> |
| 317 |
<h2>Upgrade FeedWordPress</h2> |
| 318 |
|
| 319 |
<p>It appears that you have installed FeedWordPress |
| 320 |
<?php echo FEEDWORDPRESS_VERSION; ?> as an upgrade to an existing installation of |
| 321 |
FeedWordPress. That's no problem, but you will need to take a minute out first |
| 322 |
to upgrade your database: some necessary changes in how the software keeps |
| 323 |
track of posts and feeds will cause problems such as duplicate posts and broken |
| 324 |
templates if we were to continue without the upgrade.</p> |
| 325 |
|
| 326 |
<p>Note that most of FeedWordPress's functionality is temporarily disabled |
| 327 |
until we have successfully completed the upgrade. Everything should begin |
| 328 |
working as normal again once the upgrade is complete. There's extraordinarily |
| 329 |
little chance of any damage as the result of the upgrade, but if you're paranoid |
| 330 |
like me you may want to back up your database before you proceed.</p> |
| 331 |
|
| 332 |
<p>This may take several minutes for a large installation.</p> |
| 333 |
|
| 334 |
<form action="" method="post"> |
| 335 |
<?php FeedWordPressCompatibility::stamp_nonce('feedwordpress_upgrade'); ?> |
| 336 |
<div class="submit"><input type="submit" name="action" value="Upgrade" /></div> |
| 337 |
</form> |
| 338 |
</div> |
| 339 |
<?php |
| 340 |
} // function fwp_upgrade_page () |
| 341 |
|
| 342 |
|