コード例 #1
0
/**
 *
 */
function insert_enclosure_image()
{
    $post = fix_post();
    if ($post === false) {
        return false;
    }
    $thid = get_post_thumbnail_id($post->ID);
    if (!$thid) {
        return false;
    }
    debug('insterting featured image to rss', 7);
    if ($cached = wp_cache_get($thid, __NAMESPACE__ . __FUNCTION__)) {
        return $cached;
    }
    $asize = false;
    $meta = wp_get_attachment_metadata($thid);
    // creating sorted candidates array from available sizes,
    // sorted by width*height pixels
    $candidates = array();
    foreach ($meta['sizes'] as $potential => $details) {
        if (isset($details['width']) && isset($details['height'])) {
            $max = (int) $details['width'] * (int) $details['height'];
            $candidates[$potential] = $max;
        }
    }
    arsort($candidates);
    foreach ($candidates as $potential => $maxsize) {
        debug("checking size {$potential}: " . "{$meta['sizes'][$potential]['file']} vs {$meta['file']}", 7);
        if (isset($meta['sizes'][$potential]) && isset($meta['sizes'][$potential]['file']) && $meta['sizes'][$potential]['file'] != $meta['file']) {
            debug("{$meta['sizes'][$potential]['file']} look like a resized file;" . " using it", 7);
            $asize = $potential;
            $img = wp_get_attachment_image_src($thid, $potential);
            break;
        }
    }
    if ($asize == false) {
        return false;
    }
    $upload_dir = wp_upload_dir();
    // check for cached version of the image, in case the plugin is used
    // in tandem with https://github.com/petermolnar/wp-resized2rss
    $cached = WP_CONTENT_DIR . '/cache/' . $meta['sizes'][$asize]['file'];
    $file = $upload_dir['basedir'] . '/' . $meta['sizes'][$asize]['file'];
    if (file_exists($cached)) {
        $fsize = filesize($cached);
    } elseif (file_exists($file)) {
        $fsize = filesize($file);
    } else {
        return false;
    }
    $mime = $meta['sizes'][$asize]['mime-type'];
    $str = sprintf('<enclosure url="%s" type="%s" length="%s" />', \site_url($img[0]), $mime, $fsize);
    wp_cache_set($thid, $str, __NAMESPACE__ . __FUNCTION__, expire);
    echo $str;
}
コード例 #2
0
ファイル: bbcode_convert.php プロジェクト: Turante/boincweb
function fix_fix()
{
    $posts = mysql_query("select * from post where id=99");
    $post = mysql_fetch_object($posts);
    fix_post($post);
}
コード例 #3
0
/**
 * since WordPress has it's built-in rewrite engine, it's eaiser to use
 * that for adding the short urls
 */
function maybe_generate_slug($new_status, $old_status, $post)
{
    $post = fix_post($post);
    if ($post === false) {
        return false;
    }
    // auto-generated slug for empty title; this we should replace
    $pattern = '/^' . $post->ID . '-[0-9]$/';
    if (!preg_match($pattern, $post->post_name) && !empty($post->post_title)) {
        return false;
    } else {
        debug("post {$post->ID} name is {$post->post_name} which matches" . " pattern {$pattern} or the post_title is empty," . " so shortslug is required.", 6);
    }
    // generate new
    $url36 = shortslug($post);
    debug("replacing slug of {$post->ID} with shortslug: {$url36}", 5);
    // save old, just in case
    \add_post_meta($post->ID, '_wp_old_slug', $post->post_name);
    /*
     * this is depricated, but I'll leave it in the code for the future me:
     * in case you use wp_update_post, it will trigger a post transition, so
     * that will trigger _everything_ that hooks to that, which is not what we
     * want. In that case we'll end up with a duplicate event, for both the old
     * and the new slug and, for example, in a webmention hook's case that is
     * far from ideal.
    
    $_post = array(
    	'ID' => $post->ID,
    	'post_name' => $url36,
    );
    
    $wp_error = false;
    wp_update_post( $_post, $wp_error );
    
    if (is_wp_error($wp_error)) {
    	$errors = json_encode($post_id->get_error_messages());
    	debug( $errors );
    }
    */
    global $wpdb;
    $dbname = "{$wpdb->prefix}posts";
    $req = false;
    debug("Updating post slug for #{$post->ID}", 5);
    $q = $wpdb->prepare("UPDATE `{$dbname}` SET `post_name`='%s' WHERE " . "`ID`='{$post->ID}' LIMIT 1", $url36);
    try {
        $req = $wpdb->query($q);
    } catch (Exception $e) {
        debug('Something went wrong: ' . $e->getMessage(), 4);
    }
    $meta = \get_post_meta($post->ID, '_wp_old_slug', false);
    if (in_array($url36, $meta)) {
        debug("removing slug {$url36} from {$post->ID}", 5);
        \delete_post_meta($post->ID, '_wp_old_slug', $url36);
    }
    return true;
}