コード例 #1
0
function bdn_poll_doc()
{
    if (empty($_REQUEST['wpid']) || (int) $_REQUEST['wpid'] == 0) {
        wp_send_json_error();
    }
    $post_id = (int) $_REQUEST['wpid'];
    $post = get_post($post_id);
    //See if we're already authenticated into Drive
    global $driveService, $starttime;
    list($usec, $sec) = explode(' ', microtime());
    $drivestart = (double) $usec + (double) $sec;
    $inittime = $drivestart - $starttime;
    //If the $driveService global isn't set up, auth into teh Goog
    if (!is_object($driveService)) {
        $driveService = bdn_is_user_auth();
    }
    //Get the details about the doc attached to this post
    try {
        $file = $driveService->files->get(get_post_meta($post_id, '_gdocID', true));
    } catch (Exception $e) {
        if (strpos($e->getMessage(), 'Error refreshing the OAuth2 token') !== false) {
            delete_user_meta(get_current_user_id(), '_google_access_token');
        }
        wp_send_json_error(array('reauth' => true));
    }
    //Get the text of the doc. If the HTTP response code is good, get the response body
    $request = new Google_HttpRequest($file['exportLinks']['text/plain'], 'GET', null, null);
    $httpRequest = Google_Client::$io->authenticatedRequest($request);
    if ($httpRequest->getResponseHttpCode() == 200) {
        $content = $httpRequest->getResponseBody();
    }
    list($usec, $sec) = explode(' ', microtime());
    $driveend = (double) $usec + (double) $sec;
    $drivetime = $driveend - $drivestart;
    //Set up globals to store the doc modified times that Goog returned
    global $this_post_modified_gmt, $this_post_modified;
    //Goog returns a GMT doc modified time
    $gmt_created_time = strtotime($file['modifiedDate']);
    $this_post_modified_gmt = date_i18n('Y-m-d H:i:s', $gmt_created_time, true);
    //Convert that to EST
    date_default_timezone_set('America/New_York');
    $this_post_modified = date('Y-m-d H:i:s', $gmt_created_time);
    //If nothing has changed, just send a success response (with the wordcount)
    if ($content == $post->post_content && $this_post_modified == $post->post_modified) {
        wp_send_json_success(array('words' => str_word_count($post->post_content)));
    }
    //We're still here, which means something's been modified
    //We can't force post_modified using wp_update_post, so we're going to do an end-run around
    //WordPress and change it right before we perform the $wpdb->update call.
    //So, grab the globals we just set and override the data we're about to send in wp_update_post
    add_filter('wp_insert_post_data', function ($data) {
        global $this_post_modified_gmt, $this_post_modified;
        $data['post_modified'] = $this_post_modified;
        $data['post_modified_gmt'] = $this_post_modified_gmt;
        return $data;
    });
    //Update the post content
    wp_update_post(array('ID' => $post_id, 'post_content' => $content));
    //Save the name of the last person to modify the doc
    update_post_meta($post_id, '_last_modified_name', $file['lastModifyingUserName']);
    $time_spent = get_post_meta($post_id, '_time_spent', true);
    if (empty($time_spent)) {
        $time_spent = array();
    }
    $time_spent[$file['lastModifyingUserName']] += 30;
    update_post_meta($post_id, '_time_spent', $time_spent);
    //If someone's working on this, change the status to 'In Progress'
    if (str_word_count($content) > 50 && (has_term('story-idea-submitted', 'status', $post_id) || has_term('editor-approved', 'status', $post_id))) {
        wp_set_object_terms($post_id, 'in-progress', 'status');
        if (!($the_status_changes = get_post_meta($post_id, '_status_changes', true))) {
            $the_status_changes = array();
        }
        $the_status_changes[] = array('time' => date_i18n('Y-m-d H:i:s'), 'user' => get_current_user_id(), 'status' => 'in-progress');
        update_post_meta($post_id, '_status_changes', $the_status_changes);
    }
    list($usec, $sec) = explode(' ', microtime());
    $endtime = (double) $usec + (double) $sec;
    //Success!
    wp_send_json_success(array('words' => str_word_count($content), 'timeItTook' => array('init' => $inittime, 'drive' => $drivetime, 'processing' => $endtime - $driveend, 'total' => $endtime - $starttime, 'mysql_time' => bdn_total_query_time())));
}
コード例 #2
0
function bdn_send_to_wp()
{
    if (!is_singular('doc') || !current_user_can('edit_others_posts') || empty($_GET['docToWP'])) {
        return;
    }
    global $post, $driveService;
    $gdocID = get_post_meta($post->ID, '_gdocID', true);
    if (empty($gdocID)) {
        return;
    }
    if (!is_object($driveService)) {
        $driveService = bdn_is_user_auth();
    }
    //Get the file info
    $file = $driveService->files->get($gdocID);
    //Get the html
    $request = new Google_HttpRequest($file['exportLinks']['text/html'], 'GET', null, null);
    $httpRequest = Google_Client::$io->authenticatedRequest($request);
    if ($httpRequest->getResponseHttpCode() == 200) {
        $content = get_clean_doc($httpRequest->getResponseBody());
    }
    $sendpost = array('key' => '_gdocID', 'value' => $gdocID, 'post_title' => $file['title'], 'post_content' => $content, 'author' => get_userdata($post->post_author)->user_login, 'categories' => wp_get_object_terms($post->ID, 'folder', array('fields' => 'names')), 'custom_fields' => array());
    $sendpost = bdn_separate_headline($sendpost);
    $client = new IXR_Client(PUBLISH_ENDPOINT);
    $params = array(1, PUBLISH_USER, PUBLISH_PASS, array($sendpost));
    if (!$client->query('bdn.publishOnKey', $params)) {
        die('Something went wrong - ' . $client->getErrorCode() . ' : ' . $client->getErrorMessage());
    }
    $wp_posts = $client->getResponse();
    $first_post = reset($wp_posts);
    if (empty($wp_posts) || empty($first_post['post_id'])) {
        die('Something went wrong');
    }
    $previous_ids = get_post_meta($post->ID, '_wpID', false);
    foreach ($wp_posts as $wp_post) {
        if (!in_array($wp_post['post_id'], $previous_ids)) {
            add_post_meta($post->ID, '_wpID', $wp_post['post_id'], false);
        }
    }
    $status = array('published');
    if (!empty($_GET['readyForPrint'])) {
        $status[] = 'ready-for-print';
    }
    if (has_term('final-published', 'status', $post->ID)) {
        $statuses[] = 'final-published';
    }
    wp_set_object_terms($post->ID, $status, 'status');
    if (!($the_status_changes = get_post_meta($post->ID, '_status_changes', true))) {
        $the_status_changes = array();
    }
    $the_status_changes[] = array('time' => date_i18n('Y-m-d H:i:s'), 'user' => get_current_user_id(), 'status' => 'published');
    update_post_meta($post->ID, '_status_changes', $the_status_changes);
    if (get_post_meta($post->ID, '_the_file_time', true) > date_i18n('Y-m-d H:i', strtotime('+5 hours'))) {
        update_post_meta($post->ID, '_the_file_time', date_i18n('Y-m-d H:i'));
    }
    ?>
	<script type="text/javascript">
		var r = confirm( 'Doc published! Continue to the WordPress admin?' );
		if ( r == true ) {
			var x = LIVE_SITE_URL . 'wp-admin/post.php?post=<?php 
    echo $first_post['post_id'];
    ?>
&action=edit'
		} else {
			var x = '<?php 
    echo home_url('/');
    ?>
';
		}
		window.location = x;
	</script>
	<?php 
    exit;
}