function save_post($post_id) { self::log("save_post({$post_id})"); // don't do anything on autosave events if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { self::log("DOING_AUTOSAVE is true; ignoring save_post({$post_id})"); return false; } $post = get_post($post_id); // make sure we're not working with a revision if ($post->post_status == 'auto-draft' || ($parent_post_id = wp_is_post_revision($post))) { self::log("Post is a revision; ignoring save_post({$post_id})"); return false; } $is_xmlrpc = defined('XMLRPC_REQUEST') && XMLRPC_REQUEST; self::log($is_xmlrpc ? 'In XML-RPC request' : 'Not in XML-RPC request'); $is_cron = defined('DOING_CRON') && DOING_CRON; self::log($is_cron ? 'In CRON job' : 'Not in CRON job'); $is_pressthis = strpos($_POST['_wp_http_referer'], 'press-this.php') !== false; self::log($is_pressthis ? 'In Press This widget request' : 'Not in Press This widget request'); $is_quickpress = 'post-quickpress-publish' == $_POST['action']; self::log($is_quickpress ? 'In QuickPress widget request' : 'Not in QuickPress widget request'); $fixing_missed_schedule = defined('SP_FIXING_MISSED_SCHEDULE') && SP_FIXING_MISSED_SCHEDULE; // verify permissions if (!$is_cron && !apply_filters('sharepress_user_can_edit_post', false, $post) && !current_user_can('edit_post', $post->ID)) { self::log("Current user is not allowed to edit posts; ignoring save_post({$post_id})"); return false; } $already_posted = get_post_meta($post->ID, self::META_POSTED, true); $is_scheduled = get_post_meta($post->ID, self::META_SCHEDULED, true); $ignore_nonce = apply_filters('sharepress_ignore_save_nonce', false); // if the nonce is present, update meta settings for this post from $_POST if ($ignore_nonce || wp_verify_nonce($_POST['sharepress-nonce'], plugin_basename(__FILE__))) { // remove any past failures delete_post_meta($post->ID, self::META_ERROR); // update facebook meta? if (@$_POST[self::META]['publish_again'] || $_POST[self::META]['enabled'] == 'on' && !$already_posted && !$is_scheduled) { // if publish_action was set, make sure enabled = 'on' if ($_POST[self::META]['publish_again']) { $_POST[self::META]['enabled'] = 'on'; } // remove the publish_again flag unset($_POST[self::META]['publish_again']); // clear the published date in meta delete_post_meta($post->ID, self::META_POSTED); // filter the meta if (!$_POST[self::META]) { $meta = get_post_meta($post->ID, self::META, true); } else { $meta = apply_filters('filter_' . self::META, $_POST[self::META], $post); } // save the meta data update_post_meta($post->ID, self::META, $meta); // filter the twitter meta if (!$_POST[self::META_TWITTER]) { $twitter_meta = get_post_meta($post->ID, self::META_TWITTER, true); } else { $twitter_meta = apply_filters('filter_' . self::META_TWITTER, $_POST[self::META_TWITTER], $post); } if (empty($twitter_meta['enabled'])) { $twitter_meta['enabled'] = 'off'; } // save the twitter meta data update_post_meta($post->ID, self::META_TWITTER, $twitter_meta); // if the post is published, then consider posting to facebook immediately if ($post->post_status == 'publish') { // if lite version or if publish time has already past if (!self::$pro || ($time = self::$pro->get_publish_time()) < current_time('timestamp')) { self::log("Posting to Facebook now; save_post({$post_id})"); $this->share($post); // otherwise, if $time specified, schedule future publish } else { if ($time) { self::log("Scheduling future repost at {$time}; save_post({$post_id})"); update_post_meta($post->ID, self::META_SCHEDULED, $time); // otherwise...? } else { self::log("Not time to post or no post schedule time given, so not posting to Facebook; save_post({$post_id})"); } } } else { self::log("Post status is not 'publish'; not posting to Facebook on save_post({$post_id})"); } } else { if (get_post_meta($post->ID, self::META_SCHEDULED, true) && @$_POST[self::META]['cancelled']) { self::log("Scheduled repost canceled by save_post({$post_id})"); delete_post_meta($post->ID, self::META_SCHEDULED); } else { if (isset($_POST[self::META]['enabled']) && $_POST[self::META]['enabled'] == 'off') { self::log("User has indicated they do not wish to Post to Facebook; save_post({$post_id})"); update_post_meta($post->ID, self::META, array('enabled' => 'off')); update_post_meta($post->ID, self::META_TWITTER, array('enabled' => 'off')); } else { self::log("Post is already posted or is not allowed to be posted to facebook; save_post({$post_id})"); } } } # # When save_post is invoked by XML-RPC, a CRON job, the Press This widget, or the Quick press widget # the SharePress nonce won't be available to test. So, we evaluate whether or not to post based on several criteria: # 1. SharePress must be configured to post to Facebook by default # 2. The Post must not already have been posted by SharePress # 3. The Post must not be scheduled for future posting # } else { if (($fixing_missed_schedule || $is_quickpress || $is_pressthis || $is_xmlrpc || $is_cron) && $this->setting('default_behavior') == 'on' && !$already_posted && !$is_scheduled) { // is there already meta data stored? $meta = get_post_meta($post->ID, self::META, true); if ($meta && $meta['enabled'] && $meta['enabled'] != 'on') { self::log("Post is set not to share on Facebook; ignoring save_post({$post_id})"); return; } // remove any past failures delete_post_meta($post->ID, self::META_ERROR); $defaults = array('message' => $post->post_title, 'title_is_message' => true, 'picture' => null, 'let_facebook_pick_pic' => self::setting('let_facebook_pick_pic_default', 0), 'link' => $this->get_permalink($post->ID), 'description' => $this->get_excerpt($post), 'excerpt_is_description' => true, 'targets' => array_keys(($targets = self::targets()) ? $targets : array()), 'enabled' => Sharepress::setting('default_behavior')); if ($fixing_missed_schedule) { $meta = array_merge($defaults, is_array($meta) ? $meta : array()); } else { $meta = $defaults; } $meta = apply_filters('filter_' . self::META, $meta, $post); if (self::setting('append_link', 'on') == 'on') { if ($meta['message']) { $meta['message'] .= ' - '; } $meta['message'] .= $this->get_permalink($post->ID); } update_post_meta($post->ID, self::META, $meta); // === TWITTER ============================= $defaults = array('enabled' => Sharepress::setting('twitter_behavior')); $meta = get_post_meta($post->ID, self::META_TWITTER, true); if ($fixing_missed_schedule) { $meta = array_merge($defaults, is_array($meta) ? $meta : array()); } else { $meta = $defaults; } $meta = apply_filters('filter_' . self::META_TWITTER, $meta); update_post_meta($post->ID, self::META_TWITTER, $meta); if ($post->post_status == 'publish') { self::log("Sharing with SharePress now; save_post({$post_id})"); $this->share($post); } } else { self::log("{$is_scheduled} {$already_posted} SharePress nonce was invalid; ignoring save_post({$post_id})"); } } }