Example #1
0
 public static function write_post($message, $method)
 {
     global $post;
     switch ($method) {
         case 'delete':
             if (!current_user_can('delete_post', $message->postID)) {
                 self::die_failure('cannot_trash_post', __('You are not allowed to trash this post', 'o2'));
             }
             apply_filters('o2_trash_post', $post, $message, $method);
             wp_trash_post($message->postID);
             do_action('o2_writeapi_post_trashed', $message->postID);
             die('1');
         case 'update':
             if (!current_user_can('edit_post', $message->postID)) {
                 self::die_failure('cannot_edit_post', __('You are not allowed to edit this post', 'o2'));
             }
             // Load existing post
             $post = get_post($message->postID);
             if (!$post) {
                 self::die_failure('post_not_found', __('Post not found.', 'o2'));
             }
             // Allow plugins to hook in
             apply_filters('o2_update_post', $post, $message, $method);
             // Merge data that the user can modify on the front end
             $post->post_content = $message->contentRaw;
             // Don't addslashes() here
             $post->post_title = $message->titleRaw;
             if (empty($post->post_title)) {
                 $post->post_title = wp_trim_words($message->contentRaw, 5);
             }
             // Save it
             $id = wp_update_post($post);
             // Set post format
             $postFormat = $message->postFormat;
             if ('standard' == $postFormat) {
                 $postFormat = '';
                 // a "standard" post actually takes no format at all
             }
             set_post_format($id, $postFormat);
             // We must store this in postmeta, because WP doesn't allow us to manually
             // control wp_posts.post_modified[_gmt].
             if (!empty($message->unixtimeModified)) {
                 update_post_meta($id, 'client-modified', $message->unixtimeModified);
             }
             // Reload full object from WP
             $post = get_post($id);
             setup_postdata($post);
             o2_Write_API::update_orphan_attachments($post);
             do_action('o2_writeapi_post_updated', $id);
             // Send back updated Fragment
             self::die_success(o2_Fragment::get_fragment($post));
         case 'patch':
             if (!current_user_can('edit_post', $message->postID)) {
                 self::die_failure('cannot_edit_post', __('You are not allowed to edit this post', 'o2'));
             }
             // We must store this in postmeta, because WP doesn't allow us to manually
             // control wp_posts.post_modified[_gmt].
             if (!empty($message->unixtimeModified)) {
                 update_post_meta($message->postID, 'client-modified', $message->unixtimeModified);
             }
             do_action('o2_callback_' . $message->pluginData->callback, $message->pluginData->data);
             self::die_failure('no_callback_action_taken', __('No callback action taken.', 'o2'));
         case 'create':
             if (!current_user_can('publish_posts')) {
                 self::die_failure('cannot_publish_posts', __('You are not allowed to publish new posts', 'o2'));
             }
             if ('standard' !== $message->postFormat || empty($message->titleRaw)) {
                 $message->titleRaw = wp_trim_words($message->contentRaw, 5, '...');
             }
             if ('aside' === $message->postFormat && !$message->disableAutoTitle) {
                 $message = o2_Write_API::generate_title($message);
             }
             // Set up a basic Post object
             $post = get_default_post_to_edit('post', true);
             $post->post_author = get_current_user_id();
             $post->post_content = addslashes($message->contentRaw);
             $post->post_title = $message->titleRaw;
             $post->post_status = 'publish';
             $post = apply_filters('o2_create_post', $post, $message, $method);
             // if syntaxhighlighter is being used...
             // because we use get_default_post_to_edit above,
             // syntax highlighter thinks it has already applied its encoding magic
             // to tags inside code blocks
             // so we need to tell it to do it again when we hit content_save_pre
             // during wp_insert_post below in order for tags inside code blocks
             // to survive the trip
             global $SyntaxHighlighter;
             if (is_a($SyntaxHighlighter, 'SyntaxHighlighter')) {
                 $SyntaxHighlighter->content_save_pre_ran = false;
             }
             // Save it
             $id = wp_insert_post($post);
             // Set post format
             if ('standard' == $message->postFormat) {
                 $message->postFormat = '';
                 // a "standard" post actually takes no format at all
             }
             set_post_format($id, $message->postFormat);
             // We must store this in postmeta, because WP doesn't allow us to manually
             // control wp_posts.post_modified[_gmt].
             if (!empty($message->unixtimeModified)) {
                 update_post_meta($id, 'client-modified', $message->unixtimeModified);
             }
             // Reload full object from WP
             $post = get_post($id);
             setup_postdata($post);
             o2_Write_API::update_orphan_attachments($post);
             do_action('o2_writeapi_post_created', $id);
             // Send back updated Message in standard form
             self::die_success(o2_Fragment::get_fragment($post));
     }
 }
Example #2
0
 /**
  * Generates a preview version of a post or comment being created/edited
  */
 public static function preview()
 {
     $response = '<p>' . __('Nothing to preview.', 'o2') . '</p>';
     if (!empty($_REQUEST['data'])) {
         switch ($_REQUEST['type']) {
             case 'comment':
                 $response = apply_filters('o2_preview_comment', wp_unslash($_REQUEST['data']));
                 $response = trim(apply_filters('comment_text', $response));
                 break;
             case 'post':
                 $message = new stdClass();
                 $message->titleRaw = '';
                 $message->contentRaw = wp_unslash($_REQUEST['data']);
                 $message = o2_Write_API::generate_title($message);
                 add_filter('o2_should_process_terms', '__return_false');
                 add_filter('o2_process_the_content', '__return_false');
                 $message->contentRaw = apply_filters('o2_preview_post', $message->contentRaw);
                 $response = trim(apply_filters('the_content', $message->contentRaw));
                 if (!empty($message->titleRaw)) {
                     $response = "<h1>{$message->titleRaw}</h1>" . $response;
                 }
                 break;
             default:
                 // This page left intentionally blank
         }
     }
     self::die_success($response);
 }
Example #3
0
File: read-api.php Project: Tug/o2
 /**
  * Generates a preview version of a post or comment being created/edited
  */
 public static function preview()
 {
     $response = array('data' => '<p>' . __('Nothing to preview.', 'o2') . '</p>');
     $content = '';
     switch ($_REQUEST['type']) {
         case 'comment':
             $content = apply_filters('o2_preview_comment', wp_unslash($_REQUEST['data']));
             $content = trim(apply_filters('comment_text', $content));
             break;
         case 'post':
             $message = new stdClass();
             $message->titleRaw = '';
             $message->contentRaw = wp_unslash($_REQUEST['data']);
             $message = o2_Write_API::generate_title($message);
             $message->contentRaw = apply_filters('o2_preview_post', $message->contentRaw);
             if (!empty($message->titleRaw)) {
                 $content = "<h1>{$message->titleRaw}</h1>";
             }
             $content .= trim(apply_filters('the_content', $message->contentRaw));
             break;
         default:
             // This page left intentionally blank
     }
     if (!empty($content)) {
         $response['data'] = $content;
     }
     die(json_encode($response));
 }