function wordbooker_status_update($post_id, $access_token, $post_date, $target_id, $dummy, $target_name)
{
    global $wordbooker_post_options, $user_ID;
    wordbooker_debugger("Setting status_text" . $wordbooker_post_options['wordbooker_status_update_text'], " ", $post_id);
    if (isset($dummy)) {
        wordbooker_debugger("Status update to " . $target_name . " Test Only", 'No Post Made', $post_id, 90);
        return;
    }
    $status_text = parse_wordbooker_attributes(stripslashes($wordbooker_post_options['wordbooker_status_update_text']), $post_id, strtotime($post_date));
    $status_text = wordbooker_post_excerpt($status_text, 420);
    $data = array('access_token' => $access_token, 'message' => $status_text);
    try {
        $result = wordbooker_fb_status_update($data, $target_id);
        wordbooker_store_post_result($post_id, $result->id);
        wordbooker_debugger("Status update  to " . $target_name . " suceeded result : ", $result->id, $post_id, 90);
    } catch (Exception $e) {
        $error_code = $e->getCode();
        $error_msg = $e->getMessage();
        wordbooker_append_to_errorlogs($method, $error_code, $error_msg, $post_id, $user_ID);
        wordbooker_debugger("Status Update  to " . $target_name . " failed : " . $error_msg, $post_id, 99);
    }
}
Esempio n. 2
0
function wordbooker_fbclient_publishaction($wbuser, $post_id)
{
    global $wordbooker_post_options, $wpdb;
    $wordbooker_settings = wordbooker_options();
    $post = get_post($post_id);
    $post_link_share = get_permalink($post_id);
    $post_link = wordbooker_short_url($post_id);
    $post_title = $post->post_title;
    $post_content = $post->post_content;
    # Grab the content of the post once its been filtered for display - this converts app tags into HTML so we can grab gallery images etc.
    $processed_content = "!!!  " . apply_filters('the_content', $post_content) . "    !!!";
    $yturls = array();
    $matches_tn = array();
    # Get the Yapb image for the post
    if (class_exists('YapbImage')) {
        $siteUrl = get_option('siteurl');
        if (substr($siteUrl, -1) != '/') {
            $siteUrl .= '/';
        }
        $uri = substr($url, strpos($siteUrl, '/', strpos($url, '//') + 2));
        $WordbookerYapbImageclass = new YapbImage(null, $post->ID, $uri);
        $WordbookerYapbImage = $WordbookerYapbImageclass->getInstanceFromDb($post_id);
        if (strlen($WordbookerYapbImage->uri) > 6) {
            $yturls[] = get_bloginfo('url') . $WordbookerYapbImage->uri;
        }
    }
    if (function_exists('get_the_post_thumbnail')) {
        wordbooker_debugger("Getting the thumnail image", " ", $post->ID, 80);
        preg_match_all('/<img \\s+ ([^>]*\\s+)? src \\s* = \\s* [\'"](.*?)[\'"]/ix', get_the_post_thumbnail($post_id), $matches_tn);
    }
    $meta_tag_scan = explode(',', $wordbooker_settings['wordbooker_meta_tag_scan']);
    foreach ($meta_tag_scan as $meta_tag) {
        wordbooker_debugger("Getting image from custom meta : ", $meta_tag, $post->ID, 80);
        $matches_ct[] = get_post_meta($post->ID, $meta_tag, TRUE);
    }
    $matches = $matches_ct;
    if (function_exists('get_the_post_thumbnail')) {
        $matches = array_merge($matches_ct, $matches_tn[2]);
    }
    # If the user only wants the thumbnail then we can simply not do the skim over the processed images
    if (!isset($wordbooker_post_options["wordbooker_thumb_only"])) {
        wordbooker_debugger("Getting the rest of the images ", " ", $post->ID);
        preg_match_all('/<img \\s+ ([^>]*\\s+)? src \\s* = \\s* ["](.*?)["]/ix', $processed_content, $matched);
        $x = strip_shortcodes($post_content);
        preg_match_all('#http://(www.youtube|youtube|[A-Za-z]{2}.youtube)\\.com/(watch\\?v=|w/\\?v=|\\?v=|embed/)([\\w-]+)(.*?)#i', $x, $matches3);
        if (is_array($matches3[3])) {
            foreach ($matches3[3] as $key) {
                $yturls[] = 'http://img.youtube.com/vi/' . $key . '/0.jpg';
            }
        }
    }
    if (function_exists('get_the_post_thumbnail')) {
        # If the thumb only is set then pulled images is just matches
        if (!isset($wordbooker_settings["wordbooker_meta_tag_thumb"])) {
            if (!isset($wordbooker_post_options["wordbooker_thumb_only"])) {
                wordbooker_debugger("Setting image array to be both thumb and the post images ", " ", $post->ID, 80);
                $pulled_images = @array_merge($matches[2], $matched[2], $yturls, $matches);
            } else {
                wordbooker_debugger("Setting image array to be just thumb ", " ", $post->ID, 80);
                $pulled_images[] = $matches[2];
            }
        }
    }
    if (isset($wordbooker_settings["wordbooker_meta_tag_thumb"]) && isset($wordbooker_post_options["wordbooker_thumb_only"])) {
        wordbooker_debugger("Setting image array to be just thumb from meta. ", " ", $post->ID, 80);
        $pulled_images[] = $matches_ct[2];
    } else {
        wordbooker_debugger("Setting image array to be post and thumb images. ", " ", $post->ID, 80);
        if (is_array($matched[2])) {
            $pulled_images[] = array_merge($matches, $matched[2]);
        }
        if (is_array($matched[2]) && is_array($yturls)) {
            $pulled_images = array_merge($matches, $matched[2], $yturls);
        }
    }
    $images = array();
    if (is_array($pulled_images)) {
        foreach ($pulled_images as $ii => $imgsrc) {
            if ($imgsrc) {
                if (stristr(substr($imgsrc, 0, 8), '://') === false) {
                    /* Fully-qualify src URL if necessary. */
                    $scheme = $_SERVER['HTTPS'] ? 'https' : 'http';
                    $new_imgsrc = "{$scheme}://" . $_SERVER['SERVER_NAME'];
                    if ($imgsrc[0] == '/') {
                        $new_imgsrc .= $imgsrc;
                    }
                    $imgsrc = $new_imgsrc;
                }
                $images[] = $imgsrc;
            }
        }
    }
    /* Pull out <wpg2> image tags. */
    $wpg2_g2path = get_option('wpg2_g2paths');
    if ($wpg2_g2path) {
        $g2embeduri = $wpg2_g2path['g2_embeduri'];
        if ($g2embeduri) {
            preg_match_all('/<wpg2>(.*?)</ix', $processed_content, $wpg_matches);
            foreach ($wpg_matches[1] as $wpgtag) {
                if ($wpgtag) {
                    $images[] = $g2embeduri . '?g2_view=' . 'core.DownloadItem' . "&g2_itemId={$wpgtag}";
                }
            }
        }
    }
    $wordbooker_settings = wordbooker_options();
    if (count($images) > 0) {
        # Remove duplicates
        $images = array_unique($images);
        # Strip images from various plugins
        $images = wordbooker_strip_images($images);
        # And limit it to 5 pictures to keep Facebook happy.
        $images = array_slice($images, 0, 5);
    } else {
        if (isset($wordbooker_settings['wordbooker_use_this_image'])) {
            $images[] = $wordbooker_settings['wb_wordbooker_default_image'];
            wordbooker_debugger("No Post images found so using open graph default to keep Facebook happy ", '', $post->ID, 90);
        } else {
            $x = get_bloginfo('wpurl') . '/wp-content/plugins/wordbooker/includes/wordbooker_blank.jpg';
            $images[] = $x;
            wordbooker_debugger("No Post images found so loading blank to keep Facebook happy ", '', $post->ID, 90);
        }
    }
    # Then shove the image on again just for good measure.
    $x = get_bloginfo('wpurl') . '/wp-content/plugins/wordbooker/includes/wordbooker_blank.jpg';
    $images[] = $x;
    $images = array_unique($images);
    foreach ($images as $single) {
        $images_array[] = array('type' => 'image', 'src' => $single, 'href' => $post_link_share);
    }
    $images = $images_array;
    foreach ($images as $key) {
        wordbooker_debugger("Post Images : " . $key['src'], '', $post->ID);
    }
    // Set post_meta to be first image
    update_post_meta($post->ID, '_wordbooker_thumb', $images[0]['src']);
    wordbooker_debugger("Getting the Excerpt", " ", $post->ID, 80);
    unset($processed_content);
    if (isset($wordbooker_post_options["wordbooker_use_excerpt"]) && strlen($post->post_excerpt) > 3) {
        $post_content = $post->post_excerpt;
        $post_content = wordbooker_translate($post_content);
    } else {
        $post_content = wordbooker_post_excerpt($post_content, $wordbooker_post_options['wordbooker_extract_length']);
    }
    update_post_meta($post->ID, '_wordbooker_extract', $post_content);
    # this is getting and setting the post attributes
    $post_attribute = parse_wordbooker_attributes(stripslashes($wordbooker_post_options["wordbooker_attribute"]), $post_id, strtotime($post->post_date));
    $post_data = array('media' => $images, 'post_link' => $post_link, 'post_link_share' => $post_link_share, 'post_title' => $post_title, 'post_excerpt' => $post_content, 'post_attribute' => $post_attribute, 'post_id' => $post->ID, 'post_date' => $post->post_date);
    /*
    	# This is the tagging code - 
    	if (strlen($wordbooker_post_options['wordbooker_tag_list']) > 6 ) {
    		$wordbooker_tag_list=str_replace('[','@[',$wordbooker_post_options['wordbooker_tag_list']);
    		$message=$message."   ". __("and tagged : ",'wordbooker').$wordbooker_tag_list;
    	}
    */
    if (function_exists('qtrans_use')) {
        global $q_config;
        $post_data['post_title'] = qtrans_use($q_config['default_language'], $post_data['post_title']);
    }
    $post_id = $post->ID;
    $wordbooker_fb_post = array('name' => $post_data['post_title'], 'link' => $post_data['post_link'], 'message' => $post_data['post_attribute'], 'description' => $post_data['post_excerpt'], 'picture' => $images[0]['src'], 'caption' => get_bloginfo('description'));
    if (isset($wordbooker_settings['wordbooker_use_url_not_slug'])) {
        unset($wordbooker_fb_post['caption']);
    }
    wordbooker_debugger("Post Titled : ", $post_data['post_title'], $post_id, 90);
    wordbooker_debugger("Post URL : ", $post_data['post_link'], $post_id, 90);
    if ($wordbooker_post_options['wordbooker_actionlink'] == 100) {
        // No action link
        wordbooker_debugger("No action link being used", "", $post_id, 80);
    }
    if ($wordbooker_post_options['wordbooker_actionlink'] == 200) {
        // Share This
        wordbooker_debugger("Share Link being used", " ", $post_id, 80);
        $action_links = array('name' => __('Share', 'wordbooker'), 'link' => 'http://www.facebook.com/share.php?u=' . urlencode($post_data['post_link_share']));
        $wordbooker_fb_post['actions'] = json_encode($action_links);
    }
    if ($wordbooker_post_options['wordbooker_actionlink'] == 300) {
        // Read Full
        wordbooker_debugger("Read Full link being used", " ", $post_id, 80);
        $action_links = array('name' => __('Read entire article', 'wordbooker'), 'link' => $post_data['post_link_share']);
        $wordbooker_fb_post['actions'] = json_encode($action_links);
    }
    $posting_array[] = array('target_id' => __("Primary", 'wordbooker'), 'target' => $wordbooker_post_options['wordbooker_primary_target'], 'target_type' => $wordbooker_post_options['wordbooker_primary_type'], 'target_active' => $wordbooker_post_options['wordbooker_primary_active']);
    $posting_array[] = array('target_id' => __("Secondary", 'wordbooker'), 'target' => $wordbooker_post_options['wordbooker_secondary_target'], 'target_type' => $wordbooker_post_options['wordbooker_secondary_type'], 'target_active' => $wordbooker_post_options['wordbooker_secondary_active']);
    $target_types = array('PW' => "", 'FW' => __('Fan Wall', 'wordbooker'), 'GW' => __('Group wall', 'wordbooker'));
    $posting_type = array("1" => "Wall Post", "2" => "Note", "3" => "Status Update");
    foreach ($posting_array as $posting_target) {
        $access_token = 'dummy access token';
        $wbuser->pages[] = array('id' => 'PW:' . $wbuser->facebook_id, 'name' => "Personal Wall", 'access_token' => $wbuser->access_token);
        if (is_array($wbuser->pages)) {
            foreach ($wbuser->pages as $pager) {
                if ($pager['id'] == $posting_target['target']) {
                    $target_name = $pager['name'];
                    $access_token = $pager['access_token'];
                }
            }
        }
        if (isset($posting_target['target_active'])) {
            $target_type = substr($posting_target['target'], 0, 2);
            wordbooker_debugger("Posting to " . $target_types[$target_type] . " " . $target_name . " (" . $posting_target['target_id'] . ") as a " . $posting_type[$posting_target['target_type']], "", $post_id, 90);
            if ($access_token == 'dummy access token') {
                $access_token = $wbuser->access_token;
            }
            $target = substr($posting_target['target'], 3);
            $is_dummy = $wordbooker_settings['wordbooker_fake_publish'];
            switch ($posting_target['target_type']) {
                # Wall Post
                case 1:
                    wordbooker_wall_post($post_id, $access_token, $post_title, $wordbooker_fb_post, $target, $is_dummy, $target_name);
                    break;
                    # Note
                # Note
                case 2:
                    wordbooker_notes_post($post_id, $access_token, $post_title, $target, $is_dummy, $target_name);
                    break;
                    # Status Update
                # Status Update
                case 3:
                    wordbooker_status_update($post_id, $access_token, $post_data['post_date'], $target, $is_dummy, $target_name);
                    break;
                    # Link Post
                # Link Post
                case 4:
                    wordbooker_link_post($post_id, $access_token, $post_title, $wordbooker_fb_post, $target, $is_dummy, $target_name);
                    break;
            }
        } else {
            wordbooker_debugger("Posting to " . $posting_target['target_id'] . " target (" . $target_name . ") not active", "", $post_id, 90);
        }
    }
}