| @@ -2,101 +2,75 @@ | ||
| 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; | |
| 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 () */ | |
| 6 | +class FeedWordPressCompatibility { | |
| 17 | 7 | |
| 18 | -class FeedWordPressCompatibility { | |
| 19 | - /*static*/ function test_version ($floor, $ceiling = null) { | |
| 20 | - return fwp_test_wp_version($floor, $ceiling); | |
| 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; | |
| 21 | 27 | } /* FeedWordPressCompatibility::test_version() */ |
| 22 | 28 | |
| 23 | - /*static*/ function insert_link_category ($name) { | |
| 29 | + static function insert_link_category ($name) { | |
| 24 | 30 | global $wpdb; |
| 25 | 31 | |
| 26 | - $name = $wpdb->escape($name); | |
| 32 | + // WordPress 2.3+ term/taxonomy API | |
| 33 | + $term = wp_insert_term($name, 'link_category'); | |
| 27 | 34 | |
| 28 | - // WordPress 2.3+ term/taxonomy API | |
| 29 | - if (function_exists('wp_insert_term')) : | |
| 30 | - $term = wp_insert_term($name, 'link_category'); | |
| 35 | + // OK: returned array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id) | |
| 36 | + if (!is_wp_error($term)) : | |
| 31 | 37 | $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. | |
| 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. | |
| 50 | 45 | else : |
| 51 | - FeedWordPress::critical_bug('FeedWordPress::link_category_id::wp_db_version', $wp_db_version, __LINE__); | |
| 46 | + $cat_id = $term; | |
| 52 | 47 | endif; |
| 53 | - | |
| 48 | + | |
| 54 | 49 | // Return newly-created category ID |
| 55 | 50 | return $cat_id; |
| 56 | 51 | } /* FeedWordPressCompatibility::insert_link_category () */ |
| 57 | 52 | |
| 58 | - /*static*/ function link_category_id ($value, $key = 'cat_name') { | |
| 53 | + static function link_category_id ($value, $key = 'cat_name') { | |
| 59 | 54 | global $wpdb; |
| 60 | 55 | |
| 61 | 56 | $cat_id = NULL; |
| 62 | 57 | |
| 63 | - // WordPress 2.3 introduces a new taxonomy/term API | |
| 64 | - if (function_exists('is_term')) : | |
| 65 | - $the_term = is_term($value, 'link_category'); | |
| 58 | + $the_term = term_exists($value, 'link_category'); | |
| 66 | 59 | |
| 67 | - // Sometimes, in some versions, we get a row | |
| 68 | - if (is_array($the_term)) : | |
| 69 | - $cat_id = $the_term['term_id']; | |
| 60 | + // Sometimes, in some versions, we get a row | |
| 61 | + if (is_array($the_term)) : | |
| 62 | + $cat_id = $the_term['term_id']; | |
| 70 | 63 | |
| 71 | - // other times we get an integer result | |
| 72 | - else : | |
| 73 | - $cat_id = $the_term; | |
| 74 | - endif; | |
| 75 | - | |
| 76 | - // WordPress 2.1 and 2.2 use a common table for both link and post categories | |
| 77 | - elseif (fwp_test_wp_version(FWP_SCHEMA_21, FWP_SCHEMA_23)) : | |
| 78 | - $value = $wpdb->escape($value); | |
| 79 | - $cat_id = $wpdb->get_var("SELECT cat_id FROM {$wpdb->categories} WHERE {$key}='$value'"); | |
| 80 | - | |
| 81 | - // WordPress 1.5 and 2.0.x have a separate table for link categories | |
| 82 | - elseif (fwp_test_wp_version(0, FWP_SCHEMA_21)): | |
| 83 | - $value = $wpdb->escape($value); | |
| 84 | - $cat_id = $wpdb->get_var("SELECT cat_id FROM {$wpdb->linkcategories} WHERE {$key}='$value'"); | |
| 85 | - | |
| 86 | - // This should never happen. | |
| 64 | + // other times we get an integer result | |
| 87 | 65 | else : |
| 88 | - FeedWordPress::critical_bug(__CLASS__.'::'.__METHOD__.'::wp_db_version', $wp_db_version, __LINE__); | |
| 66 | + $cat_id = $the_term; | |
| 89 | 67 | endif; |
| 90 | - | |
| 68 | + | |
| 91 | 69 | return $cat_id; |
| 92 | 70 | } /* FeedWordPressCompatibility::link_category_id () */ |
| 93 | 71 | |
| 94 | - /*static*/ function post_tags () { | |
| 95 | - return FeedWordPressCompatibility::test_version(FWP_SCHEMA_23); | |
| 96 | - } /* FeedWordPressCompatibility::post_tags () */ | |
| 97 | - | |
| 98 | - /*static*/ function validate_http_request ($action = -1, $capability = null) { | |
| 72 | + static function validate_http_request ($action = -1, $capability = null) { | |
| 99 | 73 | // Only worry about this if we're using a method with significant side-effects |
| 100 | 74 | if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') : |
| 101 | 75 | // Limit post by user capabilities |
| 102 | 76 | if (!is_null($capability) and !current_user_can($capability)) : |
| @@ -112,152 +86,106 @@ | ||
| 112 | 86 | check_admin_referer(); |
| 113 | 87 | endif; |
| 114 | 88 | endif; |
| 115 | 89 | } /* FeedWordPressCompatibility::validate_http_request() */ |
| 116 | - | |
| 117 | - /*static*/ function stamp_nonce ($action = -1) { | |
| 90 | + | |
| 91 | + static function stamp_nonce ($action = -1) { | |
| 118 | 92 | // stamp form with hidden fields for a nonce in WP 2.0.3 & later |
| 119 | 93 | if (function_exists('wp_nonce_field')) : |
| 120 | 94 | wp_nonce_field($action); |
| 121 | 95 | endif; |
| 122 | 96 | } /* FeedWordPressCompatibility::stamp_nonce() */ |
| 123 | - | |
| 124 | - /*static*/ function bottom_script_hook ($filename) { | |
| 97 | + | |
| 98 | + static function bottom_script_hook ($filename) { | |
| 125 | 99 | global $fwp_path; |
| 126 | 100 | |
| 127 | - $hook = 'admin_footer'; | |
| 128 | - if (FeedWordPressCompatibility::test_version(FWP_SCHEMA_28)) : // WordPress 2.8+ | |
| 129 | - $hook = $hook . '-' . $fwp_path . '/' . basename($filename); | |
| 130 | - endif; | |
| 101 | + $hook = 'admin_footer-'.$fwp_path.'/'.basename($filename); | |
| 131 | 102 | return $hook; |
| 132 | 103 | } /* FeedWordPressCompatibility::bottom_script_hook() */ |
| 133 | 104 | } /* class FeedWordPressCompatibility */ |
| 134 | 105 | |
| 135 | -define('FEEDWORDPRESS_AND_TAGS', (FeedWordPressCompatibility::post_tags() ? ' & Tags' : '')); | |
| 106 | +// Compat | |
| 136 | 107 | |
| 137 | -if (!function_exists('stripslashes_deep')) { | |
| 138 | - function stripslashes_deep($value) { | |
| 139 | - $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); | |
| 140 | - return $value; | |
| 141 | - } | |
| 142 | -} | |
| 108 | +if (!function_exists('set_post_field')) { | |
| 143 | 109 | |
| 144 | -if (!function_exists('get_option')) { | |
| 145 | - function get_option ($option) { | |
| 146 | - return get_settings($option); | |
| 147 | - } | |
| 148 | -} | |
| 149 | -if (!function_exists('current_user_can')) { | |
| 150 | - $fwp_capability['manage_options'] = 6; | |
| 151 | - $fwp_capability['manage_links'] = 5; | |
| 152 | - function current_user_can ($task) { | |
| 153 | - global $user_level; | |
| 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; | |
| 154 | 129 | |
| 155 | - $can = false; | |
| 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 () */ | |
| 156 | 135 | |
| 157 | - // This is **not** a full replacement for current_user_can. It | |
| 158 | - // is only for checking the capabilities we care about via the | |
| 159 | - // WordPress 1.5 user levels. | |
| 160 | - switch ($task) : | |
| 161 | - case 'manage_options': | |
| 162 | - $can = ($user_level >= 6); | |
| 163 | - break; | |
| 164 | - case 'manage_links': | |
| 165 | - $can = ($user_level >= 5); | |
| 166 | - break; | |
| 167 | - case 'edit_files': | |
| 168 | - $can = ($user_level >= 9); | |
| 169 | - break; | |
| 170 | - endswitch; | |
| 171 | - return $can; | |
| 172 | - } | |
| 173 | -} else { | |
| 174 | - $fwp_capability['manage_options'] = 'manage_options'; | |
| 175 | - $fwp_capability['manage_links'] = 'manage_links'; | |
| 176 | -} | |
| 177 | -if (!function_exists('sanitize_user')) { | |
| 178 | - function sanitize_user ($text, $strict = false) { | |
| 179 | - return $text; // Don't munge it if it wasn't munged going in... | |
| 180 | - } | |
| 181 | -} | |
| 182 | -if (!function_exists('wp_insert_user')) { | |
| 183 | - function wp_insert_user ($userdata) { | |
| 184 | - global $wpdb; | |
| 136 | +} /* if */ | |
| 185 | 137 | |
| 186 | - #-- Help WordPress 1.5.x quack like a duck | |
| 187 | - $login = $userdata['user_login']; | |
| 188 | - $author = $userdata['display_name']; | |
| 189 | - $nice_author = $userdata['user_nicename']; | |
| 190 | - $email = $userdata['user_email']; | |
| 191 | - $url = $userdata['user_url']; | |
| 138 | +if (!function_exists('is_countable')) { | |
| 192 | 139 | |
| 193 | - $wpdb->query ( | |
| 194 | - "INSERT INTO $wpdb->users | |
| 195 | - SET | |
| 196 | - ID='0', | |
| 197 | - user_login='$login', | |
| 198 | - user_firstname='$author', | |
| 199 | - user_nickname='$author', | |
| 200 | - user_nicename='$nice_author', | |
| 201 | - user_description='$author', | |
| 202 | - user_email='$email', | |
| 203 | - user_url='$url'"); | |
| 204 | - $id = $wpdb->insert_id; | |
| 205 | - | |
| 206 | - return $id; | |
| 140 | + // Copied from WordPress 5.3.2 wp-includes/compat.php pursuant to terms | |
| 141 | + // of GPL. Provide support for is_countable() for versions of PHP < 7.3 | |
| 142 | + // and for versions of WordPress < 4.9.6. -C.J. 2020/01/24 | |
| 143 | + | |
| 144 | + /** | |
| 145 | + * Polyfill for is_countable() function added in PHP 7.3. | |
| 146 | + * | |
| 147 | + * Verify that the content of a variable is an array or an object | |
| 148 | + * implementing the Countable interface. | |
| 149 | + * | |
| 150 | + * @since 4.9.6 | |
| 151 | + * | |
| 152 | + * @param mixed $var The value to check. | |
| 153 | + * | |
| 154 | + * @return bool True if `$var` is countable, false otherwise. | |
| 155 | + */ | |
| 156 | + function is_countable( $var ) { | |
| 157 | + return ( is_array( $var ) | |
| 158 | + || $var instanceof Countable | |
| 159 | + || $var instanceof SimpleXMLElement | |
| 160 | + || $var instanceof ResourceBundle | |
| 161 | + ); | |
| 207 | 162 | } |
| 208 | -} /* if (!function_exists('wp_insert_user')) */ | |
| 209 | 163 | |
| 210 | -if (!function_exists('wp_die')) { | |
| 211 | - function wp_die ( $message, $title = '', $args = array() ) { | |
| 212 | - die($message); | |
| 213 | - } /* wp_die() */ | |
| 214 | 164 | } /* if */ |
| 215 | 165 | |
| 216 | -if (!function_exists('add_post_meta')) { | |
| 217 | - function add_post_meta ($postId, $key, $value, $unique) { | |
| 218 | - global $wpdb; | |
| 166 | +require_once(dirname(__FILE__).'/feedwordpress-walker-category-checklist.class.php'); | |
| 219 | 167 | |
| 220 | - $postId = (int) $postId; | |
| 221 | - $key = $wpdb->escape($key); | |
| 222 | - $value = $wpdb->escape($value); | |
| 223 | - | |
| 224 | - $result = $wpdb->query(" | |
| 225 | - INSERT INTO $wpdb->postmeta | |
| 226 | - SET | |
| 227 | - post_id='$postId', | |
| 228 | - meta_key='$key', | |
| 229 | - meta_value='$value' | |
| 230 | - "); | |
| 231 | - if (!$result) : | |
| 232 | - $err = mysql_error(); | |
| 233 | - if (FEEDWORDPRESS_DEBUG) : | |
| 234 | - echo "[DEBUG:".date('Y-m-d H:i:S')."][feedwordpress]: post metadata insertion FAILED for field '$key' := '$value': [$err]"; | |
| 235 | - endif; | |
| 236 | - endif; | |
| 237 | - } /* add_post_meta() */ | |
| 238 | -} /* if */ | |
| 168 | +function fwp_category_checklist ($post_id = 0, $descendents_and_self = 0, $selected_cats = false, $params = array()) { | |
| 169 | + if (is_string($params)) : | |
| 170 | + $prefix = $params; | |
| 171 | + $taxonomy = 'category'; | |
| 172 | + elseif (is_array($params)) : | |
| 173 | + $prefix = (isset($params['prefix']) ? $params['prefix'] : ''); | |
| 174 | + $taxonomy = (isset($params['taxonomy']) ? $params['taxonomy'] : 'category'); | |
| 175 | + endif; | |
| 239 | 176 | |
| 240 | -function fwp_category_checklist ($post_id = 0, $descendents_and_self = 0, $selected_cats = false) { | |
| 241 | - if (function_exists('wp_category_checklist')) : | |
| 242 | - wp_category_checklist($post_id, $descendents_and_self, $selected_cats); | |
| 243 | - else : | |
| 244 | - // selected_cats is an array of integer cat_IDs / term_ids for | |
| 245 | - // the categories that should be checked | |
| 246 | - global $post_ID; | |
| 247 | - | |
| 248 | - $cats = get_nested_categories(); | |
| 249 | - | |
| 250 | - // Undo damage from usort() in WP 2.0 | |
| 251 | - $dogs = array(); | |
| 252 | - foreach ($cats as $cat) : | |
| 253 | - $dogs[$cat['cat_ID']] = $cat; | |
| 254 | - endforeach; | |
| 255 | - foreach ($selected_cats as $cat_id) : | |
| 256 | - $dogs[$cat_id]['checked'] = true; | |
| 257 | - endforeach; | |
| 258 | - write_nested_categories($dogs); | |
| 259 | - endif; | |
| 177 | + $walker = new FeedWordPress_Walker_Category_Checklist($params); | |
| 178 | + $walker->set_prefix($prefix); | |
| 179 | + $walker->set_taxonomy($taxonomy); | |
| 180 | + wp_terms_checklist(/*post_id=*/ $post_id, array( | |
| 181 | + 'taxonomy' => $taxonomy, | |
| 182 | + 'descendents_and_self' => $descendents_and_self, | |
| 183 | + 'selected_cats' => $selected_cats, | |
| 184 | + 'popular_cats' => false, | |
| 185 | + 'walker' => $walker, | |
| 186 | + 'checked_ontop' => true, | |
| 187 | + )); | |
| 260 | 188 | } |
| 261 | 189 | |
| 262 | 190 | function fwp_time_elapsed ($ts) { |
| 263 | 191 | if (function_exists('human_time_diff')) : |
| @@ -276,9 +204,9 @@ | ||
| 276 | 204 | ## UPGRADE INTERFACE: Have users upgrade DB from older versions of FWP ######### |
| 277 | 205 | ################################################################################ |
| 278 | 206 | |
| 279 | 207 | function fwp_upgrade_page () { |
| 280 | - if (isset($GLOBALS['fwp_post']['action']) and $GLOBALS['fwp_post']['action']=='Upgrade') : | |
| 208 | + if (MyPHP::post('action')=='Upgrade') : | |
| 281 | 209 | $ver = get_option('feedwordpress_version'); |
| 282 | 210 | if (get_option('feedwordpress_version') != FEEDWORDPRESS_VERSION) : |
| 283 | 211 | echo "<div class=\"wrap\">\n"; |
| 284 | 212 | echo "<h2>Upgrading FeedWordPress...</h2>"; |
| @@ -320,5 +248,9 @@ | ||
| 320 | 248 | </form> |
| 321 | 249 | </div> |
| 322 | 250 | <?php |
| 323 | 251 | } // function fwp_upgrade_page () |
| 252 | + | |
| 253 | +function remove_dummy_zero ($var) { | |
| 254 | + return !(is_numeric($var) and ((int) $var == 0)); | |
| 255 | +} | |
| 324 | 256 | |