/** * Default catch-all publishing procedure. */ function publish_post_on_facebook($id, $new = false, $old = false) { if (!$id) { return false; } $post_id = $id; if ($rev = wp_is_post_revision($post_id)) { $post_id = $rev; } // Should we even try? if (!$this->data->get_option('wdfb_autopost', 'allow_autopost') && !@$_POST['wdfb_metabox_publishing_publish']) { $stored_publish_test = get_post_meta($post_id, 'wdfb_scheduled_publish', true); // Allow scheduled semi-auto publishing if (empty($stored_publish_test['wdfb_metabox_publishing_publish'])) { return false; } // Okay, so this has a stored value } $post = get_post($post_id); if ('publish' != $post->post_status) { if ('future' == $post->post_status && (!empty($_POST['wdfb_metabox_publishing_publish']) || !empty($_POST['wdfb_metabox_publishing_title']) || !empty($_POST['wdfb_metabox_publishing_account']))) { update_post_meta($post_id, 'wdfb_scheduled_publish', array('wdfb_metabox_publishing_publish' => $_POST['wdfb_metabox_publishing_publish'], 'wdfb_metabox_publishing_title' => $_POST['wdfb_metabox_publishing_title'], 'wdfb_metabox_publishing_account' => $_POST['wdfb_metabox_publishing_account'])); } return false; // Draft, auto-save or something else we don't want } $_POST = wp_parse_args(@$_POST, get_post_meta($post_id, 'wdfb_scheduled_publish', true)); $is_published = get_post_meta($post_id, 'wdfb_published_on_fb', true); if ($is_published && !@$_POST['wdfb_metabox_publishing_publish']) { return true; } // Already posted and no manual override, nothing to do if ($old && 'publish' == $old->post_status && !@$_POST['wdfb_metabox_publishing_publish']) { return false; } // Previously published, we don't want to override $post_type = $post->post_type; $post_title = @$_POST['wdfb_metabox_publishing_title'] ? stripslashes($_POST['wdfb_metabox_publishing_title']) : $post->post_title; // If publishing semi-auto, always use wall $post_as = @$_POST['wdfb_metabox_publishing_publish'] ? 'feed' : $this->data->get_option('wdfb_autopost', "type_{$post_type}_fb_type"); $post_to = @$_POST['wdfb_metabox_publishing_account'] ? $_POST['wdfb_metabox_publishing_account'] : $this->data->get_option('wdfb_autopost', "type_{$post_type}_fb_user"); if (!$post_to) { return false; } // Don't know where to post, bail $as_page = false; if ($post_to != $this->model->get_current_user_fb_id()) { $as_page = isset($_POST['wdfb_post_as_page']) ? $_POST['wdfb_post_as_page'] : $this->data->get_option('wdfb_autopost', 'post_as_page'); } if (!$post_as) { return true; } // Skip this type $post_content = wdfb_get_excerpt($post); switch ($post_as) { case "notes": $send = array('subject' => $post_title, 'message' => $post_content); break; case "events": add_action('wp_insert_post', array($this, 'publish_event_on_facebook'), 999, 2); // Delay long enough for Events+ to kick in return false; case "feed": default: $use_shortlink = $this->data->get_option('wdfb_autopost', "type_{$post_type}_use_shortlink"); $permalink = $use_shortlink ? wp_get_shortlink($post_id) : get_permalink($post_id); $permalink = $permalink ? $permalink : get_permalink($post_id); $picture = wdfb_get_og_image($post_id); $description = get_option('blogdescription'); $description = $description ? $description : get_bloginfo('name'); $send = array('caption' => preg_replace('/(.{0,950}).*/um', '$1', preg_replace('/\\r|\\n/', ' ', $post_content)), 'message' => $post_title, 'link' => $permalink, 'name' => $post->post_title, 'description' => $description, 'actions' => array('name' => __('Share', 'wdfb'), 'link' => 'http://www.facebook.com/sharer.php?u=' . rawurlencode($permalink))); if ($picture) { $send['picture'] = $picture; } break; } $send = apply_filters('wdfb-autopost-post_update', $send, $post_id); $send = apply_filters('wdfb-autopost-send', $send, $post_as, $post_to); $res = $this->model->post_on_facebook($post_as, $post_to, $send, $as_page); if ($res) { update_post_meta($post_id, 'wdfb_published_on_fb', 1); update_post_meta($post_id, 'wdfb_scheduled_publish', array()); do_action('wdfb-autopost-posting_successful', $post_id); } add_filter('redirect_post_location', create_function('$loc', 'return add_query_arg("wdfb_published", ' . (int) $res . ', $loc);')); do_action('wdfb-autopost-posting_complete', $res); }
function publish_post_on_facebook($id) { if (!$id) { return false; } $post_id = $id; if ($rev = wp_is_post_revision($post_id)) { $post_id = $rev; } // Should we even try? if (!$this->data->get_option('wdfb_autopost', 'allow_autopost')) { return false; } if (!$this->data->get_option('wdfb_autopost', 'allow_frontend_autopost')) { return false; } $post = get_post($post_id); if ('publish' != $post->post_status) { return false; } // Draft, auto-save or something else we don't want $is_published = get_post_meta($post_id, 'wdfb_published_on_fb', true); if ($is_published) { return true; } // Already posted and no manual override, nothing to do $post_type = $post->post_type; $post_title = $post->post_title; $post_content = wp_strip_all_tags(strip_shortcodes($post->post_content)); $post_as = $this->data->get_option('wdfb_autopost', "type_{$post_type}_fb_type"); $post_to = $this->data->get_option('wdfb_autopost', "type_{$post_type}_fb_user"); if (!$post_to) { return false; } // Don't know where to post, bail $as_page = false; if ($post_to != $this->model->get_current_user_fb_id()) { $as_page = isset($_POST['wdfb_post_as_page']) ? $_POST['wdfb_post_as_page'] : $this->data->get_option('wdfb_autopost', 'post_as_page'); } if (!$post_as) { return true; } // Skip this type switch ($post_as) { case "notes": $send = array('subject' => $post_title, 'message' => $post_content); break; case "events": $time = time(); $start_timestamp = apply_filters('wdfb-autopost-events-start_time', $time, $post); $end_timestamp = apply_filters('wdfb-autopost-events-end_time', $time + 86400, $post); $location = apply_filters('wdfb-autopost-events-location', false, $post); $start_time = date('Y-m-d\\TH:i:s', $start_timestamp); $end_time = date('Y-m-d\\TH:i:s', $end_timestamp); $send = array('name' => $post_title, 'description' => $post_content, 'start_time' => $start_time, 'end_time' => $end_time); if ($location) { $send['location'] = $location; } break; case "feed": default: $use_shortlink = $this->data->get_option('wdfb_autopost', "type_{$post_type}_use_shortlink"); $permalink = $use_shortlink ? wp_get_shortlink($post_id) : get_permalink($post_id); $permalink = $permalink ? $permalink : get_permalink($post_id); $picture = wdfb_get_og_image($post_id); $send = array('caption' => substr($post_content, 0, 999), 'message' => $post_title, 'link' => $permalink, 'name' => $post->post_title, 'description' => get_option('blogdescription'), 'actions' => array('name' => __('Share', 'wdfb'), 'link' => 'http://www.facebook.com/sharer.php?u=' . rawurlencode($permalink))); if ($picture) { $send['picture'] = $picture; } break; } $send = apply_filters('wdfb-autopost-post_update', $send, $post_id); $send = apply_filters('wdfb-autopost-send', $send, $post_as, $post_to); $res = $this->model->post_on_facebook($post_as, $post_to, $send, $as_page); if ($res) { update_post_meta($post_id, 'wdfb_published_on_fb', 1); do_action('wdfb-autopost-posting_successful', $post_id); } do_action('wdfb-autopost-posting_complete', $res); }