| @@ -2,21 +2,74 @@ | ||
| 2 | 2 | ################################################################################ |
| 3 | 3 | ## LEGACY API: Replicate or mock up functions for legacy support purposes ###### |
| 4 | 4 | ################################################################################ |
| 5 | 5 | |
| 6 | -// version testing | |
| 7 | -function fwp_test_wp_version ($floor, $ceiling = NULL) { | |
| 8 | - global $wp_db_version; | |
| 6 | +class FeedWordPressCompatibility { | |
| 7 | + | |
| 8 | + /** | |
| 9 | + * FeedWordPressCompatibility::test_version: test version of WordPress | |
| 10 | + * based on the database schema version. | |
| 11 | + * | |
| 12 | + * @param int $floor The minimum version necessary | |
| 13 | + * @param mixed $ceiling The first version that is too high. If omitted | |
| 14 | + * or NULL, no version is too high. | |
| 15 | + * @return bool TRUE if within the range of versions, FALSE if too low | |
| 16 | + * or too high. | |
| 17 | + */ | |
| 18 | + /*static*/ function test_version ($floor, $ceiling = null) { | |
| 19 | + global $wp_db_version; | |
| 20 | + | |
| 21 | + $ver = (isset($wp_db_version) ? $wp_db_version : 0); | |
| 22 | + $good = ($ver >= $floor); | |
| 23 | + if (!is_null($ceiling)) : | |
| 24 | + $good = ($good and ($ver < $ceiling)); | |
| 25 | + endif; | |
| 26 | + return $good; | |
| 27 | + } /* FeedWordPressCompatibility::test_version() */ | |
| 28 | + | |
| 29 | + /*static*/ function insert_link_category ($name) { | |
| 30 | + global $wpdb; | |
| 31 | + | |
| 32 | + // WordPress 2.3+ term/taxonomy API | |
| 33 | + $term = wp_insert_term($name, 'link_category'); | |
| 34 | + | |
| 35 | + // OK: returned array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id) | |
| 36 | + if (!is_wp_error($term)) : | |
| 37 | + $cat_id = $term['term_id']; | |
| 38 | + | |
| 39 | + // Error: term with this name already exists. Well, let's use that then. | |
| 40 | + elseif ($term->get_error_code() == 'term_exists') : | |
| 41 | + // Already-existing term ID is returned in data field | |
| 42 | + $cat_id = $term->get_error_data('term_exists'); | |
| 43 | + | |
| 44 | + // Error: another kind of error, harder to recover from. Return WP_Error. | |
| 45 | + else : | |
| 46 | + $cat_id = $term; | |
| 47 | + endif; | |
| 9 | 48 | |
| 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 () */ | |
| 49 | + // Return newly-created category ID | |
| 50 | + return $cat_id; | |
| 51 | + } /* FeedWordPressCompatibility::insert_link_category () */ | |
| 17 | 52 | |
| 18 | -class FeedWordPressCompatibility { | |
| 53 | + /*static*/ function link_category_id ($value, $key = 'cat_name') { | |
| 54 | + global $wpdb; | |
| 55 | + | |
| 56 | + $cat_id = NULL; | |
| 57 | + | |
| 58 | + $the_term = term_exists($value, 'link_category'); | |
| 59 | + | |
| 60 | + // Sometimes, in some versions, we get a row | |
| 61 | + if (is_array($the_term)) : | |
| 62 | + $cat_id = $the_term['term_id']; | |
| 63 | + | |
| 64 | + // other times we get an integer result | |
| 65 | + else : | |
| 66 | + $cat_id = $the_term; | |
| 67 | + endif; | |
| 68 | + | |
| 69 | + return $cat_id; | |
| 70 | + } /* FeedWordPressCompatibility::link_category_id () */ | |
| 71 | + | |
| 19 | 72 | /*static*/ function validate_http_request ($action = -1, $capability = null) { |
| 20 | 73 | // Only worry about this if we're using a method with significant side-effects |
| 21 | 74 | if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') : |
| 22 | 75 | // Limit post by user capabilities |
| @@ -40,109 +93,71 @@ | ||
| 40 | 93 | if (function_exists('wp_nonce_field')) : |
| 41 | 94 | wp_nonce_field($action); |
| 42 | 95 | endif; |
| 43 | 96 | } /* FeedWordPressCompatibility::stamp_nonce() */ |
| 97 | + | |
| 98 | + /*static*/ function bottom_script_hook ($filename) { | |
| 99 | + global $fwp_path; | |
| 100 | + | |
| 101 | + $hook = 'admin_footer-'.$fwp_path.'/'.basename($filename); | |
| 102 | + return $hook; | |
| 103 | + } /* FeedWordPressCompatibility::bottom_script_hook() */ | |
| 44 | 104 | } /* class FeedWordPressCompatibility */ |
| 45 | 105 | |
| 46 | -if (!function_exists('stripslashes_deep')) { | |
| 47 | - function stripslashes_deep($value) { | |
| 48 | - $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); | |
| 49 | - return $value; | |
| 50 | - } | |
| 51 | -} | |
| 106 | +// Compat | |
| 52 | 107 | |
| 53 | -if (!function_exists('get_option')) { | |
| 54 | - function get_option ($option) { | |
| 55 | - return get_settings($option); | |
| 56 | - } | |
| 57 | -} | |
| 58 | -if (!function_exists('current_user_can')) { | |
| 59 | - $fwp_capability['manage_options'] = 6; | |
| 60 | - $fwp_capability['manage_links'] = 5; | |
| 61 | - function current_user_can ($task) { | |
| 62 | - global $user_level; | |
| 108 | +if (!function_exists('set_post_field')) { | |
| 63 | 109 | |
| 64 | - $can = false; | |
| 110 | + /** | |
| 111 | + * Update data from a post field based on Post ID | |
| 112 | + * | |
| 113 | + * Examples of the post field will be, 'post_type', 'post_status', 'post_content', etc. | |
| 114 | + * | |
| 115 | + * The context values are based off of the taxonomy filter functions and | |
| 116 | + * supported values are found within those functions. | |
| 117 | + * | |
| 118 | + * @uses sanitize_post_field() | |
| 119 | + * | |
| 120 | + * @param string $field Post field name | |
| 121 | + * @param mixed $value New value for post field | |
| 122 | + * @param id $post Post ID | |
| 123 | + * @return bool Result of UPDATE query | |
| 124 | + * | |
| 125 | + * Included under terms of GPL from WordPress Ticket #10946 <http://core.trac.wordpress.org/attachment/ticket/10946/post.php.diff> | |
| 126 | + */ | |
| 127 | + function set_post_field ($field, $value, $post_id) { | |
| 128 | + global $wpdb; | |
| 65 | 129 | |
| 66 | - // This is **not** a full replacement for current_user_can. It | |
| 67 | - // is only for checking the capabilities we care about via the | |
| 68 | - // WordPress 1.5 user levels. | |
| 69 | - switch ($task) : | |
| 70 | - case 'manage_options': | |
| 71 | - $can = ($user_level >= 6); | |
| 72 | - break; | |
| 73 | - case 'manage_links': | |
| 74 | - $can = ($user_level >= 5); | |
| 75 | - break; | |
| 76 | - case 'edit_files': | |
| 77 | - $can = ($user_level >= 9); | |
| 78 | - break; | |
| 79 | - endswitch; | |
| 80 | - return $can; | |
| 81 | - } | |
| 82 | -} else { | |
| 83 | - $fwp_capability['manage_options'] = 'manage_options'; | |
| 84 | - $fwp_capability['manage_links'] = 'manage_links'; | |
| 85 | -} | |
| 86 | -if (!function_exists('sanitize_user')) { | |
| 87 | - function sanitize_user ($text, $strict = false) { | |
| 88 | - return $text; // Don't munge it if it wasn't munged going in... | |
| 89 | - } | |
| 90 | -} | |
| 91 | -if (!function_exists('wp_insert_user')) { | |
| 92 | - function wp_insert_user ($userdata) { | |
| 93 | - global $wpdb; | |
| 130 | + $post_id = absint($post_id); | |
| 131 | + // sigh ... when FWP is active, I need to avoid avoid_kses_munge | |
| 132 | + // $value = sanitize_post_field($field, $value, $post_id, 'db'); | |
| 133 | + return $wpdb->update($wpdb->posts, array($field => $value), array('ID' => $post_id)); | |
| 134 | + } /* function set_post_field () */ | |
| 94 | 135 | |
| 95 | - #-- Help WordPress 1.5.x quack like a duck | |
| 96 | - $login = $userdata['user_login']; | |
| 97 | - $author = $userdata['display_name']; | |
| 98 | - $nice_author = $userdata['user_nicename']; | |
| 99 | - $email = $userdata['user_email']; | |
| 100 | - $url = $userdata['user_url']; | |
| 101 | - | |
| 102 | - $wpdb->query ( | |
| 103 | - "INSERT INTO $wpdb->users | |
| 104 | - SET | |
| 105 | - ID='0', | |
| 106 | - user_login='$login', | |
| 107 | - user_firstname='$author', | |
| 108 | - user_nickname='$author', | |
| 109 | - user_nicename='$nice_author', | |
| 110 | - user_description='$author', | |
| 111 | - user_email='$email', | |
| 112 | - user_url='$url'"); | |
| 113 | - $id = $wpdb->insert_id; | |
| 114 | - | |
| 115 | - return $id; | |
| 116 | - } | |
| 117 | -} /* if (!function_exists('wp_insert_user')) */ | |
| 118 | - | |
| 119 | -if (!function_exists('wp_die')) { | |
| 120 | - function wp_die ( $message, $title = '', $args = array() ) { | |
| 121 | - die($message); | |
| 122 | - } /* wp_die() */ | |
| 123 | 136 | } /* if */ |
| 124 | 137 | |
| 125 | -function fwp_category_checklist ($post_id = 0, $descendents_and_self = 0, $selected_cats = false) { | |
| 126 | - if (function_exists('wp_category_checklist')) : | |
| 127 | - wp_category_checklist($post_id, $descendents_and_self, $selected_cats); | |
| 128 | - else : | |
| 129 | - // selected_cats is an array of integer cat_IDs / term_ids for | |
| 130 | - // the categories that should be checked | |
| 131 | - global $post_ID; | |
| 138 | +require_once(dirname(__FILE__).'/feedwordpress-walker-category-checklist.class.php'); | |
| 132 | 139 | |
| 133 | - $cats = get_nested_categories(); | |
| 134 | - | |
| 135 | - // Undo damage from usort() in WP 2.0 | |
| 136 | - $dogs = array(); | |
| 137 | - foreach ($cats as $cat) : | |
| 138 | - $dogs[$cat['cat_ID']] = $cat; | |
| 139 | - endforeach; | |
| 140 | - foreach ($selected_cats as $cat_id) : | |
| 141 | - $dogs[$cat_id]['checked'] = true; | |
| 142 | - endforeach; | |
| 143 | - write_nested_categories($dogs); | |
| 140 | +function fwp_category_checklist ($post_id = 0, $descendents_and_self = 0, $selected_cats = false, $params = array()) { | |
| 141 | + if (is_string($params)) : | |
| 142 | + $prefix = $params; | |
| 143 | + $taxonomy = 'category'; | |
| 144 | + elseif (is_array($params)) : | |
| 145 | + $prefix = (isset($params['prefix']) ? $params['prefix'] : ''); | |
| 146 | + $taxonomy = (isset($params['taxonomy']) ? $params['taxonomy'] : 'category'); | |
| 144 | 147 | endif; |
| 148 | + | |
| 149 | + $walker = new FeedWordPress_Walker_Category_Checklist($params); | |
| 150 | + $walker->set_prefix($prefix); | |
| 151 | + $walker->set_taxonomy($taxonomy); | |
| 152 | + wp_terms_checklist(/*post_id=*/ $post_id, array( | |
| 153 | + 'taxonomy' => $taxonomy, | |
| 154 | + 'descendents_and_self' => $descendents_and_self, | |
| 155 | + 'selected_cats' => $selected_cats, | |
| 156 | + 'popular_cats' => false, | |
| 157 | + 'walker' => $walker, | |
| 158 | + 'checked_ontop' => true, | |
| 159 | + )); | |
| 145 | 160 | } |
| 146 | 161 | |
| 147 | 162 | function fwp_time_elapsed ($ts) { |
| 148 | 163 | if (function_exists('human_time_diff')) : |
| @@ -167,9 +182,9 @@ | ||
| 167 | 182 | if (get_option('feedwordpress_version') != FEEDWORDPRESS_VERSION) : |
| 168 | 183 | echo "<div class=\"wrap\">\n"; |
| 169 | 184 | echo "<h2>Upgrading FeedWordPress...</h2>"; |
| 170 | 185 | |
| 171 | - $feedwordpress =& new FeedWordPress; | |
| 186 | + $feedwordpress = new FeedWordPress; | |
| 172 | 187 | $feedwordpress->upgrade_database($ver); |
| 173 | 188 | echo "<p><strong>Done!</strong> Upgraded database to version ".FEEDWORDPRESS_VERSION.".</p>\n"; |
| 174 | 189 | echo "<form action=\"\" method=\"get\">\n"; |
| 175 | 190 | echo "<div class=\"submit\"><input type=\"hidden\" name=\"page\" value=\"syndication.php\" />"; |
| @@ -205,5 +220,9 @@ | ||
| 205 | 220 | </form> |
| 206 | 221 | </div> |
| 207 | 222 | <?php |
| 208 | 223 | } // function fwp_upgrade_page () |
| 224 | + | |
| 225 | +function remove_dummy_zero ($var) { | |
| 226 | + return !(is_numeric($var) and ((int) $var == 0)); | |
| 227 | +} | |
| 209 | 228 | |