home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / class-wp-xmlrpc-server.php < prev    next >
Encoding:
PHP Script  |  2017-10-23  |  196.7 KB  |  6,574 lines

  1. <?php
  2. /**
  3.  * XML-RPC protocol support for WordPress
  4.  *
  5.  * @package WordPress
  6.  * @subpackage Publishing
  7.  */
  8.  
  9. /**
  10.  * WordPress XMLRPC server implementation.
  11.  *
  12.  * Implements compatibility for Blogger API, MetaWeblog API, MovableType, and
  13.  * pingback. Additional WordPress API for managing comments, pages, posts,
  14.  * options, etc.
  15.  *
  16.  * As of WordPress 3.5.0, XML-RPC is enabled by default. It can be disabled
  17.  * via the {@see 'xmlrpc_enabled'} filter found in wp_xmlrpc_server::login().
  18.  *
  19.  * @since 1.5.0
  20.  *
  21.  * @see IXR_Server
  22.  */
  23. class wp_xmlrpc_server extends IXR_Server {
  24.     /**
  25.      * Methods.
  26.      *
  27.      * @var array
  28.      */
  29.     public $methods;
  30.  
  31.     /**
  32.      * Blog options.
  33.      *
  34.      * @var array
  35.      */
  36.     public $blog_options;
  37.  
  38.     /**
  39.      * IXR_Error instance.
  40.      *
  41.      * @var IXR_Error
  42.      */
  43.     public $error;
  44.  
  45.     /**
  46.      * Flags that the user authentication has failed in this instance of wp_xmlrpc_server.
  47.      *
  48.      * @var bool
  49.      */
  50.     protected $auth_failed = false;
  51.  
  52.     /**
  53.      * Registers all of the XMLRPC methods that XMLRPC server understands.
  54.      *
  55.      * Sets up server and method property. Passes XMLRPC
  56.      * methods through the {@see 'xmlrpc_methods'} filter to allow plugins to extend
  57.      * or replace XML-RPC methods.
  58.      *
  59.      * @since 1.5.0
  60.      */
  61.     public function __construct() {
  62.         $this->methods = array(
  63.             // WordPress API
  64.             'wp.getUsersBlogs'        => 'this:wp_getUsersBlogs',
  65.             'wp.newPost'            => 'this:wp_newPost',
  66.             'wp.editPost'            => 'this:wp_editPost',
  67.             'wp.deletePost'            => 'this:wp_deletePost',
  68.             'wp.getPost'            => 'this:wp_getPost',
  69.             'wp.getPosts'            => 'this:wp_getPosts',
  70.             'wp.newTerm'            => 'this:wp_newTerm',
  71.             'wp.editTerm'            => 'this:wp_editTerm',
  72.             'wp.deleteTerm'            => 'this:wp_deleteTerm',
  73.             'wp.getTerm'            => 'this:wp_getTerm',
  74.             'wp.getTerms'            => 'this:wp_getTerms',
  75.             'wp.getTaxonomy'        => 'this:wp_getTaxonomy',
  76.             'wp.getTaxonomies'        => 'this:wp_getTaxonomies',
  77.             'wp.getUser'            => 'this:wp_getUser',
  78.             'wp.getUsers'            => 'this:wp_getUsers',
  79.             'wp.getProfile'            => 'this:wp_getProfile',
  80.             'wp.editProfile'        => 'this:wp_editProfile',
  81.             'wp.getPage'            => 'this:wp_getPage',
  82.             'wp.getPages'            => 'this:wp_getPages',
  83.             'wp.newPage'            => 'this:wp_newPage',
  84.             'wp.deletePage'            => 'this:wp_deletePage',
  85.             'wp.editPage'            => 'this:wp_editPage',
  86.             'wp.getPageList'        => 'this:wp_getPageList',
  87.             'wp.getAuthors'            => 'this:wp_getAuthors',
  88.             'wp.getCategories'        => 'this:mw_getCategories',        // Alias
  89.             'wp.getTags'            => 'this:wp_getTags',
  90.             'wp.newCategory'        => 'this:wp_newCategory',
  91.             'wp.deleteCategory'        => 'this:wp_deleteCategory',
  92.             'wp.suggestCategories'    => 'this:wp_suggestCategories',
  93.             'wp.uploadFile'            => 'this:mw_newMediaObject',    // Alias
  94.             'wp.deleteFile'            => 'this:wp_deletePost',        // Alias
  95.             'wp.getCommentCount'    => 'this:wp_getCommentCount',
  96.             'wp.getPostStatusList'    => 'this:wp_getPostStatusList',
  97.             'wp.getPageStatusList'    => 'this:wp_getPageStatusList',
  98.             'wp.getPageTemplates'    => 'this:wp_getPageTemplates',
  99.             'wp.getOptions'            => 'this:wp_getOptions',
  100.             'wp.setOptions'            => 'this:wp_setOptions',
  101.             'wp.getComment'            => 'this:wp_getComment',
  102.             'wp.getComments'        => 'this:wp_getComments',
  103.             'wp.deleteComment'        => 'this:wp_deleteComment',
  104.             'wp.editComment'        => 'this:wp_editComment',
  105.             'wp.newComment'            => 'this:wp_newComment',
  106.             'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
  107.             'wp.getMediaItem'        => 'this:wp_getMediaItem',
  108.             'wp.getMediaLibrary'    => 'this:wp_getMediaLibrary',
  109.             'wp.getPostFormats'     => 'this:wp_getPostFormats',
  110.             'wp.getPostType'        => 'this:wp_getPostType',
  111.             'wp.getPostTypes'        => 'this:wp_getPostTypes',
  112.             'wp.getRevisions'        => 'this:wp_getRevisions',
  113.             'wp.restoreRevision'    => 'this:wp_restoreRevision',
  114.  
  115.             // Blogger API
  116.             'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
  117.             'blogger.getUserInfo' => 'this:blogger_getUserInfo',
  118.             'blogger.getPost' => 'this:blogger_getPost',
  119.             'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
  120.             'blogger.newPost' => 'this:blogger_newPost',
  121.             'blogger.editPost' => 'this:blogger_editPost',
  122.             'blogger.deletePost' => 'this:blogger_deletePost',
  123.  
  124.             // MetaWeblog API (with MT extensions to structs)
  125.             'metaWeblog.newPost' => 'this:mw_newPost',
  126.             'metaWeblog.editPost' => 'this:mw_editPost',
  127.             'metaWeblog.getPost' => 'this:mw_getPost',
  128.             'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
  129.             'metaWeblog.getCategories' => 'this:mw_getCategories',
  130.             'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
  131.  
  132.             // MetaWeblog API aliases for Blogger API
  133.             // see http://www.xmlrpc.com/stories/storyReader$2460
  134.             'metaWeblog.deletePost' => 'this:blogger_deletePost',
  135.             'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
  136.  
  137.             // MovableType API
  138.             'mt.getCategoryList' => 'this:mt_getCategoryList',
  139.             'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
  140.             'mt.getPostCategories' => 'this:mt_getPostCategories',
  141.             'mt.setPostCategories' => 'this:mt_setPostCategories',
  142.             'mt.supportedMethods' => 'this:mt_supportedMethods',
  143.             'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
  144.             'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
  145.             'mt.publishPost' => 'this:mt_publishPost',
  146.  
  147.             // PingBack
  148.             'pingback.ping' => 'this:pingback_ping',
  149.             'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
  150.  
  151.             'demo.sayHello' => 'this:sayHello',
  152.             'demo.addTwoNumbers' => 'this:addTwoNumbers'
  153.         );
  154.  
  155.         $this->initialise_blog_option_info();
  156.  
  157.         /**
  158.          * Filters the methods exposed by the XML-RPC server.
  159.          *
  160.          * This filter can be used to add new methods, and remove built-in methods.
  161.          *
  162.          * @since 1.5.0
  163.          *
  164.          * @param array $methods An array of XML-RPC methods.
  165.          */
  166.         $this->methods = apply_filters( 'xmlrpc_methods', $this->methods );
  167.     }
  168.  
  169.     /**
  170.      * Make private/protected methods readable for backward compatibility.
  171.      *
  172.      * @since 4.0.0
  173.      *
  174.      * @param callable $name      Method to call.
  175.      * @param array    $arguments Arguments to pass when calling.
  176.      * @return array|IXR_Error|false Return value of the callback, false otherwise.
  177.      */
  178.     public function __call( $name, $arguments ) {
  179.         if ( '_multisite_getUsersBlogs' === $name ) {
  180.             return call_user_func_array( array( $this, $name ), $arguments );
  181.         }
  182.         return false;
  183.     }
  184.  
  185.     /**
  186.      * Serves the XML-RPC request.
  187.      *
  188.      * @since 2.9.0
  189.      */
  190.     public function serve_request() {
  191.         $this->IXR_Server($this->methods);
  192.     }
  193.  
  194.     /**
  195.      * Test XMLRPC API by saying, "Hello!" to client.
  196.      *
  197.      * @since 1.5.0
  198.      *
  199.      * @return string Hello string response.
  200.      */
  201.     public function sayHello() {
  202.         return 'Hello!';
  203.     }
  204.  
  205.     /**
  206.      * Test XMLRPC API by adding two numbers for client.
  207.      *
  208.      * @since 1.5.0
  209.      *
  210.      * @param array  $args {
  211.      *     Method arguments. Note: arguments must be ordered as documented.
  212.      *
  213.      *     @type int $number1 A number to add.
  214.      *     @type int $number2 A second number to add.
  215.      * }
  216.      * @return int Sum of the two given numbers.
  217.      */
  218.     public function addTwoNumbers( $args ) {
  219.         $number1 = $args[0];
  220.         $number2 = $args[1];
  221.         return $number1 + $number2;
  222.     }
  223.  
  224.     /**
  225.      * Log user in.
  226.      *
  227.      * @since 2.8.0
  228.      *
  229.      * @param string $username User's username.
  230.      * @param string $password User's password.
  231.      * @return WP_User|bool WP_User object if authentication passed, false otherwise
  232.      */
  233.     public function login( $username, $password ) {
  234.         /*
  235.          * Respect old get_option() filters left for back-compat when the 'enable_xmlrpc'
  236.          * option was deprecated in 3.5.0. Use the 'xmlrpc_enabled' hook instead.
  237.          */
  238.         $enabled = apply_filters( 'pre_option_enable_xmlrpc', false );
  239.         if ( false === $enabled ) {
  240.             $enabled = apply_filters( 'option_enable_xmlrpc', true );
  241.         }
  242.  
  243.         /**
  244.          * Filters whether XML-RPC methods requiring authentication are enabled.
  245.          *
  246.          * Contrary to the way it's named, this filter does not control whether XML-RPC is *fully*
  247.          * enabled, rather, it only controls whether XML-RPC methods requiring authentication - such
  248.          * as for publishing purposes - are enabled.
  249.          *
  250.          * Further, the filter does not control whether pingbacks or other custom endpoints that don't
  251.          * require authentication are enabled. This behavior is expected, and due to how parity was matched
  252.          * with the `enable_xmlrpc` UI option the filter replaced when it was introduced in 3.5.
  253.          *
  254.          * To disable XML-RPC methods that require authentication, use:
  255.          *
  256.          *     add_filter( 'xmlrpc_enabled', '__return_false' );
  257.          *
  258.          * For more granular control over all XML-RPC methods and requests, see the {@see 'xmlrpc_methods'}
  259.          * and {@see 'xmlrpc_element_limit'} hooks.
  260.          *
  261.          * @since 3.5.0
  262.          *
  263.          * @param bool $enabled Whether XML-RPC is enabled. Default true.
  264.          */
  265.         $enabled = apply_filters( 'xmlrpc_enabled', $enabled );
  266.  
  267.         if ( ! $enabled ) {
  268.             $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site.' ) ) );
  269.             return false;
  270.         }
  271.  
  272.         if ( $this->auth_failed ) {
  273.             $user = new WP_Error( 'login_prevented' );
  274.         } else {
  275.             $user = wp_authenticate( $username, $password );
  276.         }
  277.  
  278.         if ( is_wp_error( $user ) ) {
  279.             $this->error = new IXR_Error( 403, __( 'Incorrect username or password.' ) );
  280.  
  281.             // Flag that authentication has failed once on this wp_xmlrpc_server instance
  282.             $this->auth_failed = true;
  283.  
  284.             /**
  285.              * Filters the XML-RPC user login error message.
  286.              *
  287.              * @since 3.5.0
  288.              *
  289.              * @param string  $error The XML-RPC error message.
  290.              * @param WP_User $user  WP_User object.
  291.              */
  292.             $this->error = apply_filters( 'xmlrpc_login_error', $this->error, $user );
  293.             return false;
  294.         }
  295.  
  296.         wp_set_current_user( $user->ID );
  297.         return $user;
  298.     }
  299.  
  300.     /**
  301.      * Check user's credentials. Deprecated.
  302.      *
  303.      * @since 1.5.0
  304.      * @deprecated 2.8.0 Use wp_xmlrpc_server::login()
  305.      * @see wp_xmlrpc_server::login()
  306.      *
  307.      * @param string $username User's username.
  308.      * @param string $password User's password.
  309.      * @return bool Whether authentication passed.
  310.      */
  311.     public function login_pass_ok( $username, $password ) {
  312.         return (bool) $this->login( $username, $password );
  313.     }
  314.  
  315.     /**
  316.      * Escape string or array of strings for database.
  317.      *
  318.      * @since 1.5.2
  319.      *
  320.      * @param string|array $data Escape single string or array of strings.
  321.      * @return string|void Returns with string is passed, alters by-reference
  322.      *                     when array is passed.
  323.      */
  324.     public function escape( &$data ) {
  325.         if ( ! is_array( $data ) )
  326.             return wp_slash( $data );
  327.  
  328.         foreach ( $data as &$v ) {
  329.             if ( is_array( $v ) )
  330.                 $this->escape( $v );
  331.             elseif ( ! is_object( $v ) )
  332.                 $v = wp_slash( $v );
  333.         }
  334.     }
  335.  
  336.     /**
  337.      * Retrieve custom fields for post.
  338.      *
  339.      * @since 2.5.0
  340.      *
  341.      * @param int $post_id Post ID.
  342.      * @return array Custom fields, if exist.
  343.      */
  344.     public function get_custom_fields($post_id) {
  345.         $post_id = (int) $post_id;
  346.  
  347.         $custom_fields = array();
  348.  
  349.         foreach ( (array) has_meta($post_id) as $meta ) {
  350.             // Don't expose protected fields.
  351.             if ( ! current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) )
  352.                 continue;
  353.  
  354.             $custom_fields[] = array(
  355.                 "id"    => $meta['meta_id'],
  356.                 "key"   => $meta['meta_key'],
  357.                 "value" => $meta['meta_value']
  358.             );
  359.         }
  360.  
  361.         return $custom_fields;
  362.     }
  363.  
  364.     /**
  365.      * Set custom fields for post.
  366.      *
  367.      * @since 2.5.0
  368.      *
  369.      * @param int $post_id Post ID.
  370.      * @param array $fields Custom fields.
  371.      */
  372.     public function set_custom_fields($post_id, $fields) {
  373.         $post_id = (int) $post_id;
  374.  
  375.         foreach ( (array) $fields as $meta ) {
  376.             if ( isset($meta['id']) ) {
  377.                 $meta['id'] = (int) $meta['id'];
  378.                 $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
  379.  
  380.                 if ( ! $pmeta || $pmeta->post_id != $post_id ) {
  381.                     continue;
  382.                 }
  383.  
  384.                 if ( isset($meta['key']) ) {
  385.                     $meta['key'] = wp_unslash( $meta['key'] );
  386.                     if ( $meta['key'] !== $pmeta->meta_key )
  387.                         continue;
  388.                     $meta['value'] = wp_unslash( $meta['value'] );
  389.                     if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) )
  390.                         update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
  391.                 } elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
  392.                     delete_metadata_by_mid( 'post', $meta['id'] );
  393.                 }
  394.             } elseif ( current_user_can( 'add_post_meta', $post_id, wp_unslash( $meta['key'] ) ) ) {
  395.                 add_post_meta( $post_id, $meta['key'], $meta['value'] );
  396.             }
  397.         }
  398.     }
  399.  
  400.     /**
  401.      * Retrieve custom fields for a term.
  402.      *
  403.      * @since 4.9.0
  404.      *
  405.      * @param int $term_id Term ID.
  406.      * @return array Array of custom fields, if they exist.
  407.      */
  408.     public function get_term_custom_fields( $term_id ) {
  409.         $term_id = (int) $term_id;
  410.  
  411.         $custom_fields = array();
  412.  
  413.         foreach ( (array) has_term_meta( $term_id ) as $meta ) {
  414.  
  415.             if ( ! current_user_can( 'edit_term_meta', $term_id ) ) {
  416.                 continue;
  417.             }
  418.  
  419.             $custom_fields[] = array(
  420.                 'id'    => $meta['meta_id'],
  421.                 'key'   => $meta['meta_key'],
  422.                 'value' => $meta['meta_value'],
  423.             );
  424.         }
  425.  
  426.         return $custom_fields;
  427.     }
  428.  
  429.     /**
  430.      * Set custom fields for a term.
  431.      *
  432.      * @since 4.9.0
  433.      *
  434.      * @param int $term_id Term ID.
  435.      * @param array $fields Custom fields.
  436.      */
  437.     public function set_term_custom_fields( $term_id, $fields ) {
  438.         $term_id = (int) $term_id;
  439.  
  440.         foreach ( (array) $fields as $meta ) {
  441.             if ( isset( $meta['id'] ) ) {
  442.                 $meta['id'] = (int) $meta['id'];
  443.                 $pmeta = get_metadata_by_mid( 'term', $meta['id'] );
  444.                 if ( isset( $meta['key'] ) ) {
  445.                     $meta['key'] = wp_unslash( $meta['key'] );
  446.                     if ( $meta['key'] !== $pmeta->meta_key ) {
  447.                         continue;
  448.                     }
  449.                     $meta['value'] = wp_unslash( $meta['value'] );
  450.                     if ( current_user_can( 'edit_term_meta', $term_id ) ) {
  451.                         update_metadata_by_mid( 'term', $meta['id'], $meta['value'] );
  452.                     }
  453.                 } elseif ( current_user_can( 'delete_term_meta', $term_id ) ) {
  454.                     delete_metadata_by_mid( 'term', $meta['id'] );
  455.                 }
  456.             } elseif ( current_user_can( 'add_term_meta', $term_id ) ) {
  457.                 add_term_meta( $term_id, $meta['key'], $meta['value'] );
  458.             }
  459.         }
  460.     }
  461.  
  462.     /**
  463.      * Set up blog options property.
  464.      *
  465.      * Passes property through {@see 'xmlrpc_blog_options'} filter.
  466.      *
  467.      * @since 2.6.0
  468.      */
  469.     public function initialise_blog_option_info() {
  470.         $this->blog_options = array(
  471.             // Read only options
  472.             'software_name'     => array(
  473.                 'desc'          => __( 'Software Name' ),
  474.                 'readonly'      => true,
  475.                 'value'         => 'WordPress'
  476.             ),
  477.             'software_version'  => array(
  478.                 'desc'          => __( 'Software Version' ),
  479.                 'readonly'      => true,
  480.                 'value'         => get_bloginfo( 'version' )
  481.             ),
  482.             'blog_url'          => array(
  483.                 'desc'          => __( 'WordPress Address (URL)' ),
  484.                 'readonly'      => true,
  485.                 'option'        => 'siteurl'
  486.             ),
  487.             'home_url'          => array(
  488.                 'desc'          => __( 'Site Address (URL)' ),
  489.                 'readonly'      => true,
  490.                 'option'        => 'home'
  491.             ),
  492.             'login_url'          => array(
  493.                 'desc'          => __( 'Login Address (URL)' ),
  494.                 'readonly'      => true,
  495.                 'value'         => wp_login_url( )
  496.             ),
  497.             'admin_url'          => array(
  498.                 'desc'          => __( 'The URL to the admin area' ),
  499.                 'readonly'      => true,
  500.                 'value'         => get_admin_url( )
  501.             ),
  502.             'image_default_link_type' => array(
  503.                 'desc'          => __( 'Image default link type' ),
  504.                 'readonly'      => true,
  505.                 'option'        => 'image_default_link_type'
  506.             ),
  507.             'image_default_size' => array(
  508.                 'desc'          => __( 'Image default size' ),
  509.                 'readonly'      => true,
  510.                 'option'        => 'image_default_size'
  511.             ),
  512.             'image_default_align' => array(
  513.                 'desc'          => __( 'Image default align' ),
  514.                 'readonly'      => true,
  515.                 'option'        => 'image_default_align'
  516.             ),
  517.             'template'          => array(
  518.                 'desc'          => __( 'Template' ),
  519.                 'readonly'      => true,
  520.                 'option'        => 'template'
  521.             ),
  522.             'stylesheet'        => array(
  523.                 'desc'          => __( 'Stylesheet' ),
  524.                 'readonly'      => true,
  525.                 'option'        => 'stylesheet'
  526.             ),
  527.             'post_thumbnail'    => array(
  528.                 'desc'          => __('Post Thumbnail'),
  529.                 'readonly'      => true,
  530.                 'value'         => current_theme_supports( 'post-thumbnails' )
  531.             ),
  532.  
  533.             // Updatable options
  534.             'time_zone'         => array(
  535.                 'desc'          => __( 'Time Zone' ),
  536.                 'readonly'      => false,
  537.                 'option'        => 'gmt_offset'
  538.             ),
  539.             'blog_title'        => array(
  540.                 'desc'          => __( 'Site Title' ),
  541.                 'readonly'      => false,
  542.                 'option'        => 'blogname'
  543.             ),
  544.             'blog_tagline'      => array(
  545.                 'desc'          => __( 'Site Tagline' ),
  546.                 'readonly'      => false,
  547.                 'option'        => 'blogdescription'
  548.             ),
  549.             'date_format'       => array(
  550.                 'desc'          => __( 'Date Format' ),
  551.                 'readonly'      => false,
  552.                 'option'        => 'date_format'
  553.             ),
  554.             'time_format'       => array(
  555.                 'desc'          => __( 'Time Format' ),
  556.                 'readonly'      => false,
  557.                 'option'        => 'time_format'
  558.             ),
  559.             'users_can_register' => array(
  560.                 'desc'          => __( 'Allow new users to sign up' ),
  561.                 'readonly'      => false,
  562.                 'option'        => 'users_can_register'
  563.             ),
  564.             'thumbnail_size_w'  => array(
  565.                 'desc'          => __( 'Thumbnail Width' ),
  566.                 'readonly'      => false,
  567.                 'option'        => 'thumbnail_size_w'
  568.             ),
  569.             'thumbnail_size_h'  => array(
  570.                 'desc'          => __( 'Thumbnail Height' ),
  571.                 'readonly'      => false,
  572.                 'option'        => 'thumbnail_size_h'
  573.             ),
  574.             'thumbnail_crop'    => array(
  575.                 'desc'          => __( 'Crop thumbnail to exact dimensions' ),
  576.                 'readonly'      => false,
  577.                 'option'        => 'thumbnail_crop'
  578.             ),
  579.             'medium_size_w'     => array(
  580.                 'desc'          => __( 'Medium size image width' ),
  581.                 'readonly'      => false,
  582.                 'option'        => 'medium_size_w'
  583.             ),
  584.             'medium_size_h'     => array(
  585.                 'desc'          => __( 'Medium size image height' ),
  586.                 'readonly'      => false,
  587.                 'option'        => 'medium_size_h'
  588.             ),
  589.             'medium_large_size_w'   => array(
  590.                 'desc'          => __( 'Medium-Large size image width' ),
  591.                 'readonly'      => false,
  592.                 'option'        => 'medium_large_size_w'
  593.             ),
  594.             'medium_large_size_h'   => array(
  595.                 'desc'          => __( 'Medium-Large size image height' ),
  596.                 'readonly'      => false,
  597.                 'option'        => 'medium_large_size_h'
  598.             ),
  599.             'large_size_w'      => array(
  600.                 'desc'          => __( 'Large size image width' ),
  601.                 'readonly'      => false,
  602.                 'option'        => 'large_size_w'
  603.             ),
  604.             'large_size_h'      => array(
  605.                 'desc'          => __( 'Large size image height' ),
  606.                 'readonly'      => false,
  607.                 'option'        => 'large_size_h'
  608.             ),
  609.             'default_comment_status' => array(
  610.                 'desc'          => __( 'Allow people to post comments on new articles' ),
  611.                 'readonly'      => false,
  612.                 'option'        => 'default_comment_status'
  613.             ),
  614.             'default_ping_status' => array(
  615.                 'desc'          => __( 'Allow link notifications from other blogs (pingbacks and trackbacks) on new articles' ),
  616.                 'readonly'      => false,
  617.                 'option'        => 'default_ping_status'
  618.             )
  619.         );
  620.  
  621.         /**
  622.          * Filters the XML-RPC blog options property.
  623.          *
  624.          * @since 2.6.0
  625.          *
  626.          * @param array $blog_options An array of XML-RPC blog options.
  627.          */
  628.         $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
  629.     }
  630.  
  631.     /**
  632.      * Retrieve the blogs of the user.
  633.      *
  634.      * @since 2.6.0
  635.      *
  636.      * @param array $args {
  637.      *     Method arguments. Note: arguments must be ordered as documented.
  638.      *
  639.      *     @type string $username Username.
  640.      *     @type string $password Password.
  641.      * }
  642.      * @return array|IXR_Error Array contains:
  643.      *  - 'isAdmin'
  644.      *  - 'isPrimary' - whether the blog is the user's primary blog
  645.      *  - 'url'
  646.      *  - 'blogid'
  647.      *  - 'blogName'
  648.      *  - 'xmlrpc' - url of xmlrpc endpoint
  649.      */
  650.     public function wp_getUsersBlogs( $args ) {
  651.         if ( ! $this->minimum_args( $args, 2 ) ) {
  652.             return $this->error;
  653.         }
  654.  
  655.         // If this isn't on WPMU then just use blogger_getUsersBlogs
  656.         if ( !is_multisite() ) {
  657.             array_unshift( $args, 1 );
  658.             return $this->blogger_getUsersBlogs( $args );
  659.         }
  660.  
  661.         $this->escape( $args );
  662.  
  663.         $username = $args[0];
  664.         $password = $args[1];
  665.  
  666.         if ( !$user = $this->login($username, $password) )
  667.             return $this->error;
  668.  
  669.         /**
  670.          * Fires after the XML-RPC user has been authenticated but before the rest of
  671.          * the method logic begins.
  672.          *
  673.          * All built-in XML-RPC methods use the action xmlrpc_call, with a parameter
  674.          * equal to the method's name, e.g., wp.getUsersBlogs, wp.newPost, etc.
  675.          *
  676.          * @since 2.5.0
  677.          *
  678.          * @param string $name The method name.
  679.          */
  680.         do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
  681.  
  682.         $blogs = (array) get_blogs_of_user( $user->ID );
  683.         $struct = array();
  684.         $primary_blog_id = 0;
  685.         $active_blog = get_active_blog_for_user( $user->ID );
  686.         if ( $active_blog ) {
  687.             $primary_blog_id = (int) $active_blog->blog_id;
  688.         }
  689.  
  690.         foreach ( $blogs as $blog ) {
  691.             // Don't include blogs that aren't hosted at this site.
  692.             if ( $blog->site_id != get_current_network_id() )
  693.                 continue;
  694.  
  695.             $blog_id = $blog->userblog_id;
  696.  
  697.             switch_to_blog( $blog_id );
  698.  
  699.             $is_admin = current_user_can( 'manage_options' );
  700.             $is_primary = ( (int) $blog_id === $primary_blog_id );
  701.  
  702.             $struct[] = array(
  703.                 'isAdmin'   => $is_admin,
  704.                 'isPrimary' => $is_primary,
  705.                 'url'       => home_url( '/' ),
  706.                 'blogid'    => (string) $blog_id,
  707.                 'blogName'  => get_option( 'blogname' ),
  708.                 'xmlrpc'    => site_url( 'xmlrpc.php', 'rpc' ),
  709.             );
  710.  
  711.             restore_current_blog();
  712.         }
  713.  
  714.         return $struct;
  715.     }
  716.  
  717.     /**
  718.      * Checks if the method received at least the minimum number of arguments.
  719.      *
  720.      * @since 3.4.0
  721.      *
  722.      * @param string|array $args Sanitize single string or array of strings.
  723.      * @param int $count         Minimum number of arguments.
  724.      * @return bool if `$args` contains at least $count arguments.
  725.      */
  726.     protected function minimum_args( $args, $count ) {
  727.         if ( count( $args ) < $count ) {
  728.             $this->error = new IXR_Error( 400, __( 'Insufficient arguments passed to this XML-RPC method.' ) );
  729.             return false;
  730.         }
  731.  
  732.         return true;
  733.     }
  734.  
  735.     /**
  736.      * Prepares taxonomy data for return in an XML-RPC object.
  737.      *
  738.      *
  739.      * @param object $taxonomy The unprepared taxonomy data.
  740.      * @param array $fields    The subset of taxonomy fields to return.
  741.      * @return array The prepared taxonomy data.
  742.      */
  743.     protected function _prepare_taxonomy( $taxonomy, $fields ) {
  744.         $_taxonomy = array(
  745.             'name' => $taxonomy->name,
  746.             'label' => $taxonomy->label,
  747.             'hierarchical' => (bool) $taxonomy->hierarchical,
  748.             'public' => (bool) $taxonomy->public,
  749.             'show_ui' => (bool) $taxonomy->show_ui,
  750.             '_builtin' => (bool) $taxonomy->_builtin,
  751.         );
  752.  
  753.         if ( in_array( 'labels', $fields ) )
  754.             $_taxonomy['labels'] = (array) $taxonomy->labels;
  755.  
  756.         if ( in_array( 'cap', $fields ) )
  757.             $_taxonomy['cap'] = (array) $taxonomy->cap;
  758.  
  759.         if ( in_array( 'menu', $fields ) )
  760.             $_taxonomy['show_in_menu'] = (bool) $_taxonomy->show_in_menu;
  761.  
  762.         if ( in_array( 'object_type', $fields ) )
  763.             $_taxonomy['object_type'] = array_unique( (array) $taxonomy->object_type );
  764.  
  765.         /**
  766.          * Filters XML-RPC-prepared data for the given taxonomy.
  767.          *
  768.          * @since 3.4.0
  769.          *
  770.          * @param array       $_taxonomy An array of taxonomy data.
  771.          * @param WP_Taxonomy $taxonomy  Taxonomy object.
  772.          * @param array       $fields    The subset of taxonomy fields to return.
  773.          */
  774.         return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
  775.     }
  776.  
  777.     /**
  778.      * Prepares term data for return in an XML-RPC object.
  779.      *
  780.      *
  781.      * @param array|object $term The unprepared term data.
  782.      * @return array The prepared term data.
  783.      */
  784.     protected function _prepare_term( $term ) {
  785.         $_term = $term;
  786.         if ( ! is_array( $_term ) )
  787.             $_term = get_object_vars( $_term );
  788.  
  789.         // For integers which may be larger than XML-RPC supports ensure we return strings.
  790.         $_term['term_id'] = strval( $_term['term_id'] );
  791.         $_term['term_group'] = strval( $_term['term_group'] );
  792.         $_term['term_taxonomy_id'] = strval( $_term['term_taxonomy_id'] );
  793.         $_term['parent'] = strval( $_term['parent'] );
  794.  
  795.         // Count we are happy to return as an integer because people really shouldn't use terms that much.
  796.         $_term['count'] = intval( $_term['count'] );
  797.  
  798.         // Get term meta.
  799.         $_term['custom_fields'] = $this->get_term_custom_fields( $_term['term_id'] );
  800.  
  801.         /**
  802.          * Filters XML-RPC-prepared data for the given term.
  803.          *
  804.          * @since 3.4.0
  805.          *
  806.          * @param array        $_term An array of term data.
  807.          * @param array|object $term  Term object or array.
  808.          */
  809.         return apply_filters( 'xmlrpc_prepare_term', $_term, $term );
  810.     }
  811.  
  812.     /**
  813.      * Convert a WordPress date string to an IXR_Date object.
  814.      *
  815.      *
  816.      * @param string $date Date string to convert.
  817.      * @return IXR_Date IXR_Date object.
  818.      */
  819.     protected function _convert_date( $date ) {
  820.         if ( $date === '0000-00-00 00:00:00' ) {
  821.             return new IXR_Date( '00000000T00:00:00Z' );
  822.         }
  823.         return new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date, false ) );
  824.     }
  825.  
  826.     /**
  827.      * Convert a WordPress GMT date string to an IXR_Date object.
  828.      *
  829.      *
  830.      * @param string $date_gmt WordPress GMT date string.
  831.      * @param string $date     Date string.
  832.      * @return IXR_Date IXR_Date object.
  833.      */
  834.     protected function _convert_date_gmt( $date_gmt, $date ) {
  835.         if ( $date !== '0000-00-00 00:00:00' && $date_gmt === '0000-00-00 00:00:00' ) {
  836.             return new IXR_Date( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $date, false ), 'Ymd\TH:i:s' ) );
  837.         }
  838.         return $this->_convert_date( $date_gmt );
  839.     }
  840.  
  841.     /**
  842.      * Prepares post data for return in an XML-RPC object.
  843.      *
  844.      *
  845.      * @param array $post   The unprepared post data.
  846.      * @param array $fields The subset of post type fields to return.
  847.      * @return array The prepared post data.
  848.      */
  849.     protected function _prepare_post( $post, $fields ) {
  850.         // Holds the data for this post. built up based on $fields.
  851.         $_post = array( 'post_id' => strval( $post['ID'] ) );
  852.  
  853.         // Prepare common post fields.
  854.         $post_fields = array(
  855.             'post_title'        => $post['post_title'],
  856.             'post_date'         => $this->_convert_date( $post['post_date'] ),
  857.             'post_date_gmt'     => $this->_convert_date_gmt( $post['post_date_gmt'], $post['post_date'] ),
  858.             'post_modified'     => $this->_convert_date( $post['post_modified'] ),
  859.             'post_modified_gmt' => $this->_convert_date_gmt( $post['post_modified_gmt'], $post['post_modified'] ),
  860.             'post_status'       => $post['post_status'],
  861.             'post_type'         => $post['post_type'],
  862.             'post_name'         => $post['post_name'],
  863.             'post_author'       => $post['post_author'],
  864.             'post_password'     => $post['post_password'],
  865.             'post_excerpt'      => $post['post_excerpt'],
  866.             'post_content'      => $post['post_content'],
  867.             'post_parent'       => strval( $post['post_parent'] ),
  868.             'post_mime_type'    => $post['post_mime_type'],
  869.             'link'              => get_permalink( $post['ID'] ),
  870.             'guid'              => $post['guid'],
  871.             'menu_order'        => intval( $post['menu_order'] ),
  872.             'comment_status'    => $post['comment_status'],
  873.             'ping_status'       => $post['ping_status'],
  874.             'sticky'            => ( $post['post_type'] === 'post' && is_sticky( $post['ID'] ) ),
  875.         );
  876.  
  877.         // Thumbnail.
  878.         $post_fields['post_thumbnail'] = array();
  879.         $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
  880.         if ( $thumbnail_id ) {
  881.             $thumbnail_size = current_theme_supports('post-thumbnail') ? 'post-thumbnail' : 'thumbnail';
  882.             $post_fields['post_thumbnail'] = $this->_prepare_media_item( get_post( $thumbnail_id ), $thumbnail_size );
  883.         }
  884.  
  885.         // Consider future posts as published.
  886.         if ( $post_fields['post_status'] === 'future' )
  887.             $post_fields['post_status'] = 'publish';
  888.  
  889.         // Fill in blank post format.
  890.         $post_fields['post_format'] = get_post_format( $post['ID'] );
  891.         if ( empty( $post_fields['post_format'] ) )
  892.             $post_fields['post_format'] = 'standard';
  893.  
  894.         // Merge requested $post_fields fields into $_post.
  895.         if ( in_array( 'post', $fields ) ) {
  896.             $_post = array_merge( $_post, $post_fields );
  897.         } else {
  898.             $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
  899.             $_post = array_merge( $_post, $requested_fields );
  900.         }
  901.  
  902.         $all_taxonomy_fields = in_array( 'taxonomies', $fields );
  903.  
  904.         if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
  905.             $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
  906.             $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
  907.             $_post['terms'] = array();
  908.             foreach ( $terms as $term ) {
  909.                 $_post['terms'][] = $this->_prepare_term( $term );
  910.             }
  911.         }
  912.  
  913.         if ( in_array( 'custom_fields', $fields ) )
  914.             $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
  915.  
  916.         if ( in_array( 'enclosure', $fields ) ) {
  917.             $_post['enclosure'] = array();
  918.             $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
  919.             if ( ! empty( $enclosures ) ) {
  920.                 $encdata = explode( "\n", $enclosures[0] );
  921.                 $_post['enclosure']['url'] = trim( htmlspecialchars( $encdata[0] ) );
  922.                 $_post['enclosure']['length'] = (int) trim( $encdata[1] );
  923.                 $_post['enclosure']['type'] = trim( $encdata[2] );
  924.             }
  925.         }
  926.  
  927.         /**
  928.          * Filters XML-RPC-prepared date for the given post.
  929.          *
  930.          * @since 3.4.0
  931.          *
  932.          * @param array $_post  An array of modified post data.
  933.          * @param array $post   An array of post data.
  934.          * @param array $fields An array of post fields.
  935.          */
  936.         return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
  937.     }
  938.  
  939.     /**
  940.      * Prepares post data for return in an XML-RPC object.
  941.      *
  942.      * @since 3.4.0
  943.      * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object.
  944.      *
  945.      * @param WP_Post_Type $post_type Post type object.
  946.      * @param array        $fields    The subset of post fields to return.
  947.      * @return array The prepared post type data.
  948.      */
  949.     protected function _prepare_post_type( $post_type, $fields ) {
  950.         $_post_type = array(
  951.             'name' => $post_type->name,
  952.             'label' => $post_type->label,
  953.             'hierarchical' => (bool) $post_type->hierarchical,
  954.             'public' => (bool) $post_type->public,
  955.             'show_ui' => (bool) $post_type->show_ui,
  956.             '_builtin' => (bool) $post_type->_builtin,
  957.             'has_archive' => (bool) $post_type->has_archive,
  958.             'supports' => get_all_post_type_supports( $post_type->name ),
  959.         );
  960.  
  961.         if ( in_array( 'labels', $fields ) ) {
  962.             $_post_type['labels'] = (array) $post_type->labels;
  963.         }
  964.  
  965.         if ( in_array( 'cap', $fields ) ) {
  966.             $_post_type['cap'] = (array) $post_type->cap;
  967.             $_post_type['map_meta_cap'] = (bool) $post_type->map_meta_cap;
  968.         }
  969.  
  970.         if ( in_array( 'menu', $fields ) ) {
  971.             $_post_type['menu_position'] = (int) $post_type->menu_position;
  972.             $_post_type['menu_icon'] = $post_type->menu_icon;
  973.             $_post_type['show_in_menu'] = (bool) $post_type->show_in_menu;
  974.         }
  975.  
  976.         if ( in_array( 'taxonomies', $fields ) )
  977.             $_post_type['taxonomies'] = get_object_taxonomies( $post_type->name, 'names' );
  978.  
  979.         /**
  980.          * Filters XML-RPC-prepared date for the given post type.
  981.          *
  982.          * @since 3.4.0
  983.          * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object.
  984.          *
  985.          * @param array        $_post_type An array of post type data.
  986.          * @param WP_Post_Type $post_type  Post type object.
  987.          */
  988.         return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
  989.     }
  990.  
  991.     /**
  992.      * Prepares media item data for return in an XML-RPC object.
  993.      *
  994.      *
  995.      * @param object $media_item     The unprepared media item data.
  996.      * @param string $thumbnail_size The image size to use for the thumbnail URL.
  997.      * @return array The prepared media item data.
  998.      */
  999.     protected function _prepare_media_item( $media_item, $thumbnail_size = 'thumbnail' ) {
  1000.         $_media_item = array(
  1001.             'attachment_id'    => strval( $media_item->ID ),
  1002.             'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
  1003.             'parent'           => $media_item->post_parent,
  1004.             'link'             => wp_get_attachment_url( $media_item->ID ),
  1005.             'title'            => $media_item->post_title,
  1006.             'caption'          => $media_item->post_excerpt,
  1007.             'description'      => $media_item->post_content,
  1008.             'metadata'         => wp_get_attachment_metadata( $media_item->ID ),
  1009.             'type'             => $media_item->post_mime_type
  1010.         );
  1011.  
  1012.         $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
  1013.         if ( $thumbnail_src )
  1014.             $_media_item['thumbnail'] = $thumbnail_src[0];
  1015.         else
  1016.             $_media_item['thumbnail'] = $_media_item['link'];
  1017.  
  1018.         /**
  1019.          * Filters XML-RPC-prepared data for the given media item.
  1020.          *
  1021.          * @since 3.4.0
  1022.          *
  1023.          * @param array  $_media_item    An array of media item data.
  1024.          * @param object $media_item     Media item object.
  1025.          * @param string $thumbnail_size Image size.
  1026.          */
  1027.         return apply_filters( 'xmlrpc_prepare_media_item', $_media_item, $media_item, $thumbnail_size );
  1028.     }
  1029.  
  1030.     /**
  1031.      * Prepares page data for return in an XML-RPC object.
  1032.      *
  1033.      *
  1034.      * @param object $page The unprepared page data.
  1035.      * @return array The prepared page data.
  1036.      */
  1037.     protected function _prepare_page( $page ) {
  1038.         // Get all of the page content and link.
  1039.         $full_page = get_extended( $page->post_content );
  1040.         $link = get_permalink( $page->ID );
  1041.  
  1042.         // Get info the page parent if there is one.
  1043.         $parent_title = "";
  1044.         if ( ! empty( $page->post_parent ) ) {
  1045.             $parent = get_post( $page->post_parent );
  1046.             $parent_title = $parent->post_title;
  1047.         }
  1048.  
  1049.         // Determine comment and ping settings.
  1050.         $allow_comments = comments_open( $page->ID ) ? 1 : 0;
  1051.         $allow_pings = pings_open( $page->ID ) ? 1 : 0;
  1052.  
  1053.         // Format page date.
  1054.         $page_date = $this->_convert_date( $page->post_date );
  1055.         $page_date_gmt = $this->_convert_date_gmt( $page->post_date_gmt, $page->post_date );
  1056.  
  1057.         // Pull the categories info together.
  1058.         $categories = array();
  1059.         if ( is_object_in_taxonomy( 'page', 'category' ) ) {
  1060.             foreach ( wp_get_post_categories( $page->ID ) as $cat_id ) {
  1061.                 $categories[] = get_cat_name( $cat_id );
  1062.             }
  1063.         }
  1064.  
  1065.         // Get the author info.
  1066.         $author = get_userdata( $page->post_author );
  1067.  
  1068.         $page_template = get_page_template_slug( $page->ID );
  1069.         if ( empty( $page_template ) )
  1070.             $page_template = 'default';
  1071.  
  1072.         $_page = array(
  1073.             'dateCreated'            => $page_date,
  1074.             'userid'                 => $page->post_author,
  1075.             'page_id'                => $page->ID,
  1076.             'page_status'            => $page->post_status,
  1077.             'description'            => $full_page['main'],
  1078.             'title'                  => $page->post_title,
  1079.             'link'                   => $link,
  1080.             'permaLink'              => $link,
  1081.             'categories'             => $categories,
  1082.             'excerpt'                => $page->post_excerpt,
  1083.             'text_more'              => $full_page['extended'],
  1084.             'mt_allow_comments'      => $allow_comments,
  1085.             'mt_allow_pings'         => $allow_pings,
  1086.             'wp_slug'                => $page->post_name,
  1087.             'wp_password'            => $page->post_password,
  1088.             'wp_author'              => $author->display_name,
  1089.             'wp_page_parent_id'      => $page->post_parent,
  1090.             'wp_page_parent_title'   => $parent_title,
  1091.             'wp_page_order'          => $page->menu_order,
  1092.             'wp_author_id'           => (string) $author->ID,
  1093.             'wp_author_display_name' => $author->display_name,
  1094.             'date_created_gmt'       => $page_date_gmt,
  1095.             'custom_fields'          => $this->get_custom_fields( $page->ID ),
  1096.             'wp_page_template'       => $page_template
  1097.         );
  1098.  
  1099.         /**
  1100.          * Filters XML-RPC-prepared data for the given page.
  1101.          *
  1102.          * @since 3.4.0
  1103.          *
  1104.          * @param array   $_page An array of page data.
  1105.          * @param WP_Post $page  Page object.
  1106.          */
  1107.         return apply_filters( 'xmlrpc_prepare_page', $_page, $page );
  1108.     }
  1109.  
  1110.     /**
  1111.      * Prepares comment data for return in an XML-RPC object.
  1112.      *
  1113.      *
  1114.      * @param object $comment The unprepared comment data.
  1115.      * @return array The prepared comment data.
  1116.      */
  1117.     protected function _prepare_comment( $comment ) {
  1118.         // Format page date.
  1119.         $comment_date_gmt = $this->_convert_date_gmt( $comment->comment_date_gmt, $comment->comment_date );
  1120.  
  1121.         if ( '0' == $comment->comment_approved ) {
  1122.             $comment_status = 'hold';
  1123.         } elseif ( 'spam' == $comment->comment_approved ) {
  1124.             $comment_status = 'spam';
  1125.         } elseif ( '1' == $comment->comment_approved ) {
  1126.             $comment_status = 'approve';
  1127.         } else {
  1128.             $comment_status = $comment->comment_approved;
  1129.         }
  1130.         $_comment = array(
  1131.             'date_created_gmt' => $comment_date_gmt,
  1132.             'user_id'          => $comment->user_id,
  1133.             'comment_id'       => $comment->comment_ID,
  1134.             'parent'           => $comment->comment_parent,
  1135.             'status'           => $comment_status,
  1136.             'content'          => $comment->comment_content,
  1137.             'link'             => get_comment_link($comment),
  1138.             'post_id'          => $comment->comment_post_ID,
  1139.             'post_title'       => get_the_title($comment->comment_post_ID),
  1140.             'author'           => $comment->comment_author,
  1141.             'author_url'       => $comment->comment_author_url,
  1142.             'author_email'     => $comment->comment_author_email,
  1143.             'author_ip'        => $comment->comment_author_IP,
  1144.             'type'             => $comment->comment_type,
  1145.         );
  1146.  
  1147.         /**
  1148.          * Filters XML-RPC-prepared data for the given comment.
  1149.          *
  1150.          * @since 3.4.0
  1151.          *
  1152.          * @param array      $_comment An array of prepared comment data.
  1153.          * @param WP_Comment $comment  Comment object.
  1154.          */
  1155.         return apply_filters( 'xmlrpc_prepare_comment', $_comment, $comment );
  1156.     }
  1157.  
  1158.     /**
  1159.      * Prepares user data for return in an XML-RPC object.
  1160.      *
  1161.      *
  1162.      * @param WP_User $user   The unprepared user object.
  1163.      * @param array   $fields The subset of user fields to return.
  1164.      * @return array The prepared user data.
  1165.      */
  1166.     protected function _prepare_user( $user, $fields ) {
  1167.         $_user = array( 'user_id' => strval( $user->ID ) );
  1168.  
  1169.         $user_fields = array(
  1170.             'username'          => $user->user_login,
  1171.             'first_name'        => $user->user_firstname,
  1172.             'last_name'         => $user->user_lastname,
  1173.             'registered'        => $this->_convert_date( $user->user_registered ),
  1174.             'bio'               => $user->user_description,
  1175.             'email'             => $user->user_email,
  1176.             'nickname'          => $user->nickname,
  1177.             'nicename'          => $user->user_nicename,
  1178.             'url'               => $user->user_url,
  1179.             'display_name'      => $user->display_name,
  1180.             'roles'             => $user->roles,
  1181.         );
  1182.  
  1183.         if ( in_array( 'all', $fields ) ) {
  1184.             $_user = array_merge( $_user, $user_fields );
  1185.         } else {
  1186.             if ( in_array( 'basic', $fields ) ) {
  1187.                 $basic_fields = array( 'username', 'email', 'registered', 'display_name', 'nicename' );
  1188.                 $fields = array_merge( $fields, $basic_fields );
  1189.             }
  1190.             $requested_fields = array_intersect_key( $user_fields, array_flip( $fields ) );
  1191.             $_user = array_merge( $_user, $requested_fields );
  1192.         }
  1193.  
  1194.         /**
  1195.          * Filters XML-RPC-prepared data for the given user.
  1196.          *
  1197.          * @since 3.5.0
  1198.          *
  1199.          * @param array   $_user  An array of user data.
  1200.          * @param WP_User $user   User object.
  1201.          * @param array   $fields An array of user fields.
  1202.          */
  1203.         return apply_filters( 'xmlrpc_prepare_user', $_user, $user, $fields );
  1204.     }
  1205.  
  1206.     /**
  1207.      * Create a new post for any registered post type.
  1208.      *
  1209.      * @since 3.4.0
  1210.      *
  1211.      * @link https://en.wikipedia.org/wiki/RSS_enclosure for information on RSS enclosures.
  1212.      *
  1213.      * @param array  $args {
  1214.      *     Method arguments. Note: top-level arguments must be ordered as documented.
  1215.      *
  1216.      *     @type int    $blog_id        Blog ID (unused).
  1217.      *     @type string $username       Username.
  1218.      *     @type string $password       Password.
  1219.      *     @type array  $content_struct {
  1220.      *         Content struct for adding a new post. See wp_insert_post() for information on
  1221.      *         additional post fields
  1222.      *
  1223.      *         @type string $post_type      Post type. Default 'post'.
  1224.      *         @type string $post_status    Post status. Default 'draft'
  1225.      *         @type string $post_title     Post title.
  1226.      *         @type int    $post_author    Post author ID.
  1227.      *         @type string $post_excerpt   Post excerpt.
  1228.      *         @type string $post_content   Post content.
  1229.      *         @type string $post_date_gmt  Post date in GMT.
  1230.      *         @type string $post_date      Post date.
  1231.      *         @type string $post_password  Post password (20-character limit).
  1232.      *         @type string $comment_status Post comment enabled status. Accepts 'open' or 'closed'.
  1233.      *         @type string $ping_status    Post ping status. Accepts 'open' or 'closed'.
  1234.      *         @type bool   $sticky         Whether the post should be sticky. Automatically false if
  1235.      *                                      `$post_status` is 'private'.
  1236.      *         @type int    $post_thumbnail ID of an image to use as the post thumbnail/featured image.
  1237.      *         @type array  $custom_fields  Array of meta key/value pairs to add to the post.
  1238.      *         @type array  $terms          Associative array with taxonomy names as keys and arrays
  1239.      *                                      of term IDs as values.
  1240.      *         @type array  $terms_names    Associative array with taxonomy names as keys and arrays
  1241.      *                                      of term names as values.
  1242.      *         @type array  $enclosure      {
  1243.      *             Array of feed enclosure data to add to post meta.
  1244.      *
  1245.      *             @type string $url    URL for the feed enclosure.
  1246.      *             @type int    $length Size in bytes of the enclosure.
  1247.      *             @type string $type   Mime-type for the enclosure.
  1248.      *         }
  1249.      *     }
  1250.      * }
  1251.      * @return int|IXR_Error Post ID on success, IXR_Error instance otherwise.
  1252.      */
  1253.     public function wp_newPost( $args ) {
  1254.         if ( ! $this->minimum_args( $args, 4 ) )
  1255.             return $this->error;
  1256.  
  1257.         $this->escape( $args );
  1258.  
  1259.         $username       = $args[1];
  1260.         $password       = $args[2];
  1261.         $content_struct = $args[3];
  1262.  
  1263.         if ( ! $user = $this->login( $username, $password ) )
  1264.             return $this->error;
  1265.  
  1266.         // convert the date field back to IXR form
  1267.         if ( isset( $content_struct['post_date'] ) && ! ( $content_struct['post_date'] instanceof IXR_Date ) ) {
  1268.             $content_struct['post_date'] = $this->_convert_date( $content_struct['post_date'] );
  1269.         }
  1270.  
  1271.         // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
  1272.         // since _insert_post will ignore the non-GMT date if the GMT date is set
  1273.         if ( isset( $content_struct['post_date_gmt'] ) && ! ( $content_struct['post_date_gmt'] instanceof IXR_Date ) ) {
  1274.             if ( $content_struct['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) ) {
  1275.                 unset( $content_struct['post_date_gmt'] );
  1276.             } else {
  1277.                 $content_struct['post_date_gmt'] = $this->_convert_date( $content_struct['post_date_gmt'] );
  1278.             }
  1279.         }
  1280.  
  1281.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  1282.         do_action( 'xmlrpc_call', 'wp.newPost' );
  1283.  
  1284.         unset( $content_struct['ID'] );
  1285.  
  1286.         return $this->_insert_post( $user, $content_struct );
  1287.     }
  1288.  
  1289.     /**
  1290.      * Helper method for filtering out elements from an array.
  1291.      *
  1292.      * @since 3.4.0
  1293.      *
  1294.      * @param int $count Number to compare to one.
  1295.      */
  1296.     private function _is_greater_than_one( $count ) {
  1297.         return $count > 1;
  1298.     }
  1299.  
  1300.     /**
  1301.      * Encapsulate the logic for sticking a post
  1302.      * and determining if the user has permission to do so
  1303.      *
  1304.      * @since 4.3.0
  1305.      *
  1306.      * @param array $post_data
  1307.      * @param bool  $update
  1308.      * @return void|IXR_Error
  1309.      */
  1310.     private function _toggle_sticky( $post_data, $update = false ) {
  1311.         $post_type = get_post_type_object( $post_data['post_type'] );
  1312.  
  1313.         // Private and password-protected posts cannot be stickied.
  1314.         if ( 'private' === $post_data['post_status'] || ! empty( $post_data['post_password'] ) ) {
  1315.             // Error if the client tried to stick the post, otherwise, silently unstick.
  1316.             if ( ! empty( $post_data['sticky'] ) ) {
  1317.                 return new IXR_Error( 401, __( 'Sorry, you cannot stick a private post.' ) );
  1318.             }
  1319.  
  1320.             if ( $update ) {
  1321.                 unstick_post( $post_data['ID'] );
  1322.             }
  1323.         } elseif ( isset( $post_data['sticky'] ) )  {
  1324.             if ( ! current_user_can( $post_type->cap->edit_others_posts ) ) {
  1325.                 return new IXR_Error( 401, __( 'Sorry, you are not allowed to make posts sticky.' ) );
  1326.             }
  1327.  
  1328.             $sticky = wp_validate_boolean( $post_data['sticky'] );
  1329.             if ( $sticky ) {
  1330.                 stick_post( $post_data['ID'] );
  1331.             } else {
  1332.                 unstick_post( $post_data['ID'] );
  1333.             }
  1334.         }
  1335.     }
  1336.  
  1337.     /**
  1338.      * Helper method for wp_newPost() and wp_editPost(), containing shared logic.
  1339.      *
  1340.      * @since 3.4.0
  1341.      *
  1342.      * @see wp_insert_post()
  1343.      *
  1344.      * @param WP_User         $user           The post author if post_author isn't set in $content_struct.
  1345.      * @param array|IXR_Error $content_struct Post data to insert.
  1346.      * @return IXR_Error|string
  1347.      */
  1348.     protected function _insert_post( $user, $content_struct ) {
  1349.         $defaults = array(
  1350.             'post_status'    => 'draft',
  1351.             'post_type'      => 'post',
  1352.             'post_author'    => null,
  1353.             'post_password'  => null,
  1354.             'post_excerpt'   => null,
  1355.             'post_content'   => null,
  1356.             'post_title'     => null,
  1357.             'post_date'      => null,
  1358.             'post_date_gmt'  => null,
  1359.             'post_format'    => null,
  1360.             'post_name'      => null,
  1361.             'post_thumbnail' => null,
  1362.             'post_parent'    => null,
  1363.             'ping_status'    => null,
  1364.             'comment_status' => null,
  1365.             'custom_fields'  => null,
  1366.             'terms_names'    => null,
  1367.             'terms'          => null,
  1368.             'sticky'         => null,
  1369.             'enclosure'      => null,
  1370.             'ID'             => null,
  1371.         );
  1372.  
  1373.         $post_data = wp_parse_args( array_intersect_key( $content_struct, $defaults ), $defaults );
  1374.  
  1375.         $post_type = get_post_type_object( $post_data['post_type'] );
  1376.         if ( ! $post_type )
  1377.             return new IXR_Error( 403, __( 'Invalid post type.' ) );
  1378.  
  1379.         $update = ! empty( $post_data['ID'] );
  1380.  
  1381.         if ( $update ) {
  1382.             if ( ! get_post( $post_data['ID'] ) )
  1383.                 return new IXR_Error( 401, __( 'Invalid post ID.' ) );
  1384.             if ( ! current_user_can( 'edit_post', $post_data['ID'] ) )
  1385.                 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  1386.             if ( $post_data['post_type'] != get_post_type( $post_data['ID'] ) )
  1387.                 return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
  1388.         } else {
  1389.             if ( ! current_user_can( $post_type->cap->create_posts ) || ! current_user_can( $post_type->cap->edit_posts ) )
  1390.                 return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
  1391.         }
  1392.  
  1393.         switch ( $post_data['post_status'] ) {
  1394.             case 'draft':
  1395.             case 'pending':
  1396.                 break;
  1397.             case 'private':
  1398.                 if ( ! current_user_can( $post_type->cap->publish_posts ) )
  1399.                     return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type.' ) );
  1400.                 break;
  1401.             case 'publish':
  1402.             case 'future':
  1403.                 if ( ! current_user_can( $post_type->cap->publish_posts ) )
  1404.                     return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type.' ) );
  1405.                 break;
  1406.             default:
  1407.                 if ( ! get_post_status_object( $post_data['post_status'] ) )
  1408.                     $post_data['post_status'] = 'draft';
  1409.             break;
  1410.         }
  1411.  
  1412.         if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) )
  1413.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type.' ) );
  1414.  
  1415.         $post_data['post_author'] = absint( $post_data['post_author'] );
  1416.         if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
  1417.             if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
  1418.                 return new IXR_Error( 401, __( 'Sorry, you are not allowed to create posts as this user.' ) );
  1419.  
  1420.             $author = get_userdata( $post_data['post_author'] );
  1421.  
  1422.             if ( ! $author )
  1423.                 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
  1424.         } else {
  1425.             $post_data['post_author'] = $user->ID;
  1426.         }
  1427.  
  1428.         if ( isset( $post_data['comment_status'] ) && $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' )
  1429.             unset( $post_data['comment_status'] );
  1430.  
  1431.         if ( isset( $post_data['ping_status'] ) && $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' )
  1432.             unset( $post_data['ping_status'] );
  1433.  
  1434.         // Do some timestamp voodoo.
  1435.         if ( ! empty( $post_data['post_date_gmt'] ) ) {
  1436.             // We know this is supposed to be GMT, so we're going to slap that Z on there by force.
  1437.             $dateCreated = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z';
  1438.         } elseif ( ! empty( $post_data['post_date'] ) ) {
  1439.             $dateCreated = $post_data['post_date']->getIso();
  1440.         }
  1441.  
  1442.         // Default to not flagging the post date to be edited unless it's intentional.
  1443.         $post_data['edit_date'] = false;
  1444.  
  1445.         if ( ! empty( $dateCreated ) ) {
  1446.             $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
  1447.             $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
  1448.  
  1449.             // Flag the post date to be edited.
  1450.             $post_data['edit_date'] = true;
  1451.         }
  1452.  
  1453.         if ( ! isset( $post_data['ID'] ) )
  1454.             $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
  1455.         $post_ID = $post_data['ID'];
  1456.  
  1457.         if ( $post_data['post_type'] == 'post' ) {
  1458.             $error = $this->_toggle_sticky( $post_data, $update );
  1459.             if ( $error ) {
  1460.                 return $error;
  1461.             }
  1462.         }
  1463.  
  1464.         if ( isset( $post_data['post_thumbnail'] ) ) {
  1465.             // empty value deletes, non-empty value adds/updates.
  1466.             if ( ! $post_data['post_thumbnail'] )
  1467.                 delete_post_thumbnail( $post_ID );
  1468.             elseif ( ! get_post( absint( $post_data['post_thumbnail'] ) ) )
  1469.                 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
  1470.             set_post_thumbnail( $post_ID, $post_data['post_thumbnail'] );
  1471.             unset( $content_struct['post_thumbnail'] );
  1472.         }
  1473.  
  1474.         if ( isset( $post_data['custom_fields'] ) )
  1475.             $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
  1476.  
  1477.         if ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
  1478.             $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
  1479.  
  1480.             // Accumulate term IDs from terms and terms_names.
  1481.             $terms = array();
  1482.  
  1483.             // First validate the terms specified by ID.
  1484.             if ( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
  1485.                 $taxonomies = array_keys( $post_data['terms'] );
  1486.  
  1487.                 // Validating term ids.
  1488.                 foreach ( $taxonomies as $taxonomy ) {
  1489.                     if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
  1490.                         return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
  1491.  
  1492.                     if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
  1493.                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
  1494.  
  1495.                     $term_ids = $post_data['terms'][$taxonomy];
  1496.                     $terms[ $taxonomy ] = array();
  1497.                     foreach ( $term_ids as $term_id ) {
  1498.                         $term = get_term_by( 'id', $term_id, $taxonomy );
  1499.  
  1500.                         if ( ! $term )
  1501.                             return new IXR_Error( 403, __( 'Invalid term ID.' ) );
  1502.  
  1503.                         $terms[$taxonomy][] = (int) $term_id;
  1504.                     }
  1505.                 }
  1506.             }
  1507.  
  1508.             // Now validate terms specified by name.
  1509.             if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
  1510.                 $taxonomies = array_keys( $post_data['terms_names'] );
  1511.  
  1512.                 foreach ( $taxonomies as $taxonomy ) {
  1513.                     if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
  1514.                         return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
  1515.  
  1516.                     if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
  1517.                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
  1518.  
  1519.                     /*
  1520.                      * For hierarchical taxonomies, we can't assign a term when multiple terms
  1521.                      * in the hierarchy share the same name.
  1522.                      */
  1523.                     $ambiguous_terms = array();
  1524.                     if ( is_taxonomy_hierarchical( $taxonomy ) ) {
  1525.                         $tax_term_names = get_terms( $taxonomy, array( 'fields' => 'names', 'hide_empty' => false ) );
  1526.  
  1527.                         // Count the number of terms with the same name.
  1528.                         $tax_term_names_count = array_count_values( $tax_term_names );
  1529.  
  1530.                         // Filter out non-ambiguous term names.
  1531.                         $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one') );
  1532.  
  1533.                         $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
  1534.                     }
  1535.  
  1536.                     $term_names = $post_data['terms_names'][$taxonomy];
  1537.                     foreach ( $term_names as $term_name ) {
  1538.                         if ( in_array( $term_name, $ambiguous_terms ) )
  1539.                             return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
  1540.  
  1541.                         $term = get_term_by( 'name', $term_name, $taxonomy );
  1542.  
  1543.                         if ( ! $term ) {
  1544.                             // Term doesn't exist, so check that the user is allowed to create new terms.
  1545.                             if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->edit_terms ) )
  1546.                                 return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
  1547.  
  1548.                             // Create the new term.
  1549.                             $term_info = wp_insert_term( $term_name, $taxonomy );
  1550.                             if ( is_wp_error( $term_info ) )
  1551.                                 return new IXR_Error( 500, $term_info->get_error_message() );
  1552.  
  1553.                             $terms[$taxonomy][] = (int) $term_info['term_id'];
  1554.                         } else {
  1555.                             $terms[$taxonomy][] = (int) $term->term_id;
  1556.                         }
  1557.                     }
  1558.                 }
  1559.             }
  1560.  
  1561.             $post_data['tax_input'] = $terms;
  1562.             unset( $post_data['terms'], $post_data['terms_names'] );
  1563.         }
  1564.  
  1565.         if ( isset( $post_data['post_format'] ) ) {
  1566.             $format = set_post_format( $post_ID, $post_data['post_format'] );
  1567.  
  1568.             if ( is_wp_error( $format ) )
  1569.                 return new IXR_Error( 500, $format->get_error_message() );
  1570.  
  1571.             unset( $post_data['post_format'] );
  1572.         }
  1573.  
  1574.         // Handle enclosures.
  1575.         $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
  1576.         $this->add_enclosure_if_new( $post_ID, $enclosure );
  1577.  
  1578.         $this->attach_uploads( $post_ID, $post_data['post_content'] );
  1579.  
  1580.         /**
  1581.          * Filters post data array to be inserted via XML-RPC.
  1582.          *
  1583.          * @since 3.4.0
  1584.          *
  1585.          * @param array $post_data      Parsed array of post data.
  1586.          * @param array $content_struct Post data array.
  1587.          */
  1588.         $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
  1589.  
  1590.         $post_ID = $update ? wp_update_post( $post_data, true ) : wp_insert_post( $post_data, true );
  1591.         if ( is_wp_error( $post_ID ) )
  1592.             return new IXR_Error( 500, $post_ID->get_error_message() );
  1593.  
  1594.         if ( ! $post_ID )
  1595.             return new IXR_Error( 401, __( 'Sorry, your entry could not be posted.' ) );
  1596.  
  1597.         return strval( $post_ID );
  1598.     }
  1599.  
  1600.     /**
  1601.      * Edit a post for any registered post type.
  1602.      *
  1603.      * The $content_struct parameter only needs to contain fields that
  1604.      * should be changed. All other fields will retain their existing values.
  1605.      *
  1606.      * @since 3.4.0
  1607.      *
  1608.      * @param array  $args {
  1609.      *     Method arguments. Note: arguments must be ordered as documented.
  1610.      *
  1611.      *     @type int    $blog_id        Blog ID (unused).
  1612.      *     @type string $username       Username.
  1613.      *     @type string $password       Password.
  1614.      *     @type int    $post_id        Post ID.
  1615.      *     @type array  $content_struct Extra content arguments.
  1616.      * }
  1617.      * @return true|IXR_Error True on success, IXR_Error on failure.
  1618.      */
  1619.     public function wp_editPost( $args ) {
  1620.         if ( ! $this->minimum_args( $args, 5 ) )
  1621.             return $this->error;
  1622.  
  1623.         $this->escape( $args );
  1624.  
  1625.         $username       = $args[1];
  1626.         $password       = $args[2];
  1627.         $post_id        = (int) $args[3];
  1628.         $content_struct = $args[4];
  1629.  
  1630.         if ( ! $user = $this->login( $username, $password ) )
  1631.             return $this->error;
  1632.  
  1633.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  1634.         do_action( 'xmlrpc_call', 'wp.editPost' );
  1635.  
  1636.         $post = get_post( $post_id, ARRAY_A );
  1637.  
  1638.         if ( empty( $post['ID'] ) )
  1639.             return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  1640.  
  1641.         if ( isset( $content_struct['if_not_modified_since'] ) ) {
  1642.             // If the post has been modified since the date provided, return an error.
  1643.             if ( mysql2date( 'U', $post['post_modified_gmt'] ) > $content_struct['if_not_modified_since']->getTimestamp() ) {
  1644.                 return new IXR_Error( 409, __( 'There is a revision of this post that is more recent.' ) );
  1645.             }
  1646.         }
  1647.  
  1648.         // Convert the date field back to IXR form.
  1649.         $post['post_date'] = $this->_convert_date( $post['post_date'] );
  1650.  
  1651.         /*
  1652.          * Ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
  1653.          * since _insert_post() will ignore the non-GMT date if the GMT date is set.
  1654.          */
  1655.         if ( $post['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) )
  1656.             unset( $post['post_date_gmt'] );
  1657.         else
  1658.             $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] );
  1659.  
  1660.         $this->escape( $post );
  1661.         $merged_content_struct = array_merge( $post, $content_struct );
  1662.  
  1663.         $retval = $this->_insert_post( $user, $merged_content_struct );
  1664.         if ( $retval instanceof IXR_Error )
  1665.             return $retval;
  1666.  
  1667.         return true;
  1668.     }
  1669.  
  1670.     /**
  1671.      * Delete a post for any registered post type.
  1672.      *
  1673.      * @since 3.4.0
  1674.      *
  1675.      * @see wp_delete_post()
  1676.      *
  1677.      * @param array  $args {
  1678.      *     Method arguments. Note: arguments must be ordered as documented.
  1679.      *
  1680.      *     @type int    $blog_id  Blog ID (unused).
  1681.      *     @type string $username Username.
  1682.      *     @type string $password Password.
  1683.      *     @type int    $post_id  Post ID.
  1684.      * }
  1685.      * @return true|IXR_Error True on success, IXR_Error instance on failure.
  1686.      */
  1687.     public function wp_deletePost( $args ) {
  1688.         if ( ! $this->minimum_args( $args, 4 ) )
  1689.             return $this->error;
  1690.  
  1691.         $this->escape( $args );
  1692.  
  1693.         $username   = $args[1];
  1694.         $password   = $args[2];
  1695.         $post_id    = (int) $args[3];
  1696.  
  1697.         if ( ! $user = $this->login( $username, $password ) )
  1698.             return $this->error;
  1699.  
  1700.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  1701.         do_action( 'xmlrpc_call', 'wp.deletePost' );
  1702.  
  1703.         $post = get_post( $post_id, ARRAY_A );
  1704.         if ( empty( $post['ID'] ) ) {
  1705.             return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  1706.         }
  1707.  
  1708.         if ( ! current_user_can( 'delete_post', $post_id ) ) {
  1709.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
  1710.         }
  1711.  
  1712.         $result = wp_delete_post( $post_id );
  1713.  
  1714.         if ( ! $result ) {
  1715.             return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
  1716.         }
  1717.  
  1718.         return true;
  1719.     }
  1720.  
  1721.     /**
  1722.      * Retrieve a post.
  1723.      *
  1724.      * @since 3.4.0
  1725.      *
  1726.      * The optional $fields parameter specifies what fields will be included
  1727.      * in the response array. This should be a list of field names. 'post_id' will
  1728.      * always be included in the response regardless of the value of $fields.
  1729.      *
  1730.      * Instead of, or in addition to, individual field names, conceptual group
  1731.      * names can be used to specify multiple fields. The available conceptual
  1732.      * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
  1733.      * and 'enclosure'.
  1734.      *
  1735.      * @see get_post()
  1736.      *
  1737.      * @param array $args {
  1738.      *     Method arguments. Note: arguments must be ordered as documented.
  1739.      *
  1740.      *     @type int    $blog_id  Blog ID (unused).
  1741.      *     @type string $username Username.
  1742.      *     @type string $password Password.
  1743.      *     @type int    $post_id  Post ID.
  1744.      *     @type array  $fields   The subset of post type fields to return.
  1745.      * }
  1746.      * @return array|IXR_Error Array contains (based on $fields parameter):
  1747.      *  - 'post_id'
  1748.      *  - 'post_title'
  1749.      *  - 'post_date'
  1750.      *  - 'post_date_gmt'
  1751.      *  - 'post_modified'
  1752.      *  - 'post_modified_gmt'
  1753.      *  - 'post_status'
  1754.      *  - 'post_type'
  1755.      *  - 'post_name'
  1756.      *  - 'post_author'
  1757.      *  - 'post_password'
  1758.      *  - 'post_excerpt'
  1759.      *  - 'post_content'
  1760.      *  - 'link'
  1761.      *  - 'comment_status'
  1762.      *  - 'ping_status'
  1763.      *  - 'sticky'
  1764.      *  - 'custom_fields'
  1765.      *  - 'terms'
  1766.      *  - 'categories'
  1767.      *  - 'tags'
  1768.      *  - 'enclosure'
  1769.      */
  1770.     public function wp_getPost( $args ) {
  1771.         if ( ! $this->minimum_args( $args, 4 ) )
  1772.             return $this->error;
  1773.  
  1774.         $this->escape( $args );
  1775.  
  1776.         $username = $args[1];
  1777.         $password = $args[2];
  1778.         $post_id  = (int) $args[3];
  1779.  
  1780.         if ( isset( $args[4] ) ) {
  1781.             $fields = $args[4];
  1782.         } else {
  1783.             /**
  1784.              * Filters the list of post query fields used by the given XML-RPC method.
  1785.              *
  1786.              * @since 3.4.0
  1787.              *
  1788.              * @param array  $fields Array of post fields. Default array contains 'post', 'terms', and 'custom_fields'.
  1789.              * @param string $method Method name.
  1790.              */
  1791.             $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
  1792.         }
  1793.  
  1794.         if ( ! $user = $this->login( $username, $password ) )
  1795.             return $this->error;
  1796.  
  1797.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  1798.         do_action( 'xmlrpc_call', 'wp.getPost' );
  1799.  
  1800.         $post = get_post( $post_id, ARRAY_A );
  1801.  
  1802.         if ( empty( $post['ID'] ) )
  1803.             return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  1804.  
  1805.         if ( ! current_user_can( 'edit_post', $post_id ) )
  1806.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  1807.  
  1808.         return $this->_prepare_post( $post, $fields );
  1809.     }
  1810.  
  1811.     /**
  1812.      * Retrieve posts.
  1813.      *
  1814.      * @since 3.4.0
  1815.      *
  1816.      * @see wp_get_recent_posts()
  1817.      * @see wp_getPost() for more on `$fields`
  1818.      * @see get_posts() for more on `$filter` values
  1819.      *
  1820.      * @param array $args {
  1821.      *     Method arguments. Note: arguments must be ordered as documented.
  1822.      *
  1823.      *     @type int    $blog_id  Blog ID (unused).
  1824.      *     @type string $username Username.
  1825.      *     @type string $password Password.
  1826.      *     @type array  $filter   Optional. Modifies the query used to retrieve posts. Accepts 'post_type',
  1827.      *                            'post_status', 'number', 'offset', 'orderby', 's', and 'order'.
  1828.      *                            Default empty array.
  1829.      *     @type array  $fields   Optional. The subset of post type fields to return in the response array.
  1830.      * }
  1831.      * @return array|IXR_Error Array contains a collection of posts.
  1832.      */
  1833.     public function wp_getPosts( $args ) {
  1834.         if ( ! $this->minimum_args( $args, 3 ) )
  1835.             return $this->error;
  1836.  
  1837.         $this->escape( $args );
  1838.  
  1839.         $username = $args[1];
  1840.         $password = $args[2];
  1841.         $filter   = isset( $args[3] ) ? $args[3] : array();
  1842.  
  1843.         if ( isset( $args[4] ) ) {
  1844.             $fields = $args[4];
  1845.         } else {
  1846.             /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  1847.             $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
  1848.         }
  1849.  
  1850.         if ( ! $user = $this->login( $username, $password ) )
  1851.             return $this->error;
  1852.  
  1853.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  1854.         do_action( 'xmlrpc_call', 'wp.getPosts' );
  1855.  
  1856.         $query = array();
  1857.  
  1858.         if ( isset( $filter['post_type'] ) ) {
  1859.             $post_type = get_post_type_object( $filter['post_type'] );
  1860.             if ( ! ( (bool) $post_type ) )
  1861.                 return new IXR_Error( 403, __( 'Invalid post type.' ) );
  1862.         } else {
  1863.             $post_type = get_post_type_object( 'post' );
  1864.         }
  1865.  
  1866.         if ( ! current_user_can( $post_type->cap->edit_posts ) )
  1867.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type.' ) );
  1868.  
  1869.         $query['post_type'] = $post_type->name;
  1870.  
  1871.         if ( isset( $filter['post_status'] ) )
  1872.             $query['post_status'] = $filter['post_status'];
  1873.  
  1874.         if ( isset( $filter['number'] ) )
  1875.             $query['numberposts'] = absint( $filter['number'] );
  1876.  
  1877.         if ( isset( $filter['offset'] ) )
  1878.             $query['offset'] = absint( $filter['offset'] );
  1879.  
  1880.         if ( isset( $filter['orderby'] ) ) {
  1881.             $query['orderby'] = $filter['orderby'];
  1882.  
  1883.             if ( isset( $filter['order'] ) )
  1884.                 $query['order'] = $filter['order'];
  1885.         }
  1886.  
  1887.         if ( isset( $filter['s'] ) ) {
  1888.             $query['s'] = $filter['s'];
  1889.         }
  1890.  
  1891.         $posts_list = wp_get_recent_posts( $query );
  1892.  
  1893.         if ( ! $posts_list )
  1894.             return array();
  1895.  
  1896.         // Holds all the posts data.
  1897.         $struct = array();
  1898.  
  1899.         foreach ( $posts_list as $post ) {
  1900.             if ( ! current_user_can( 'edit_post', $post['ID'] ) )
  1901.                 continue;
  1902.  
  1903.             $struct[] = $this->_prepare_post( $post, $fields );
  1904.         }
  1905.  
  1906.         return $struct;
  1907.     }
  1908.  
  1909.     /**
  1910.      * Create a new term.
  1911.      *
  1912.      * @since 3.4.0
  1913.      *
  1914.      * @see wp_insert_term()
  1915.      *
  1916.      * @param array $args {
  1917.      *     Method arguments. Note: arguments must be ordered as documented.
  1918.      *
  1919.      *     @type int    $blog_id        Blog ID (unused).
  1920.      *     @type string $username       Username.
  1921.      *     @type string $password       Password.
  1922.      *     @type array  $content_struct Content struct for adding a new term. The struct must contain
  1923.      *                                  the term 'name' and 'taxonomy'. Optional accepted values include
  1924.      *                                  'parent', 'description', and 'slug'.
  1925.      * }
  1926.      * @return int|IXR_Error The term ID on success, or an IXR_Error object on failure.
  1927.      */
  1928.     public function wp_newTerm( $args ) {
  1929.         if ( ! $this->minimum_args( $args, 4 ) )
  1930.             return $this->error;
  1931.  
  1932.         $this->escape( $args );
  1933.  
  1934.         $username       = $args[1];
  1935.         $password       = $args[2];
  1936.         $content_struct = $args[3];
  1937.  
  1938.         if ( ! $user = $this->login( $username, $password ) )
  1939.             return $this->error;
  1940.  
  1941.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  1942.         do_action( 'xmlrpc_call', 'wp.newTerm' );
  1943.  
  1944.         if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
  1945.             return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
  1946.  
  1947.         $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
  1948.  
  1949.         if ( ! current_user_can( $taxonomy->cap->edit_terms ) ) {
  1950.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to create terms in this taxonomy.' ) );
  1951.         }
  1952.  
  1953.         $taxonomy = (array) $taxonomy;
  1954.  
  1955.         // hold the data of the term
  1956.         $term_data = array();
  1957.  
  1958.         $term_data['name'] = trim( $content_struct['name'] );
  1959.         if ( empty( $term_data['name'] ) )
  1960.             return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
  1961.  
  1962.         if ( isset( $content_struct['parent'] ) ) {
  1963.             if ( ! $taxonomy['hierarchical'] )
  1964.                 return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) );
  1965.  
  1966.             $parent_term_id = (int) $content_struct['parent'];
  1967.             $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
  1968.  
  1969.             if ( is_wp_error( $parent_term ) )
  1970.                 return new IXR_Error( 500, $parent_term->get_error_message() );
  1971.  
  1972.             if ( ! $parent_term )
  1973.                 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
  1974.  
  1975.             $term_data['parent'] = $content_struct['parent'];
  1976.         }
  1977.  
  1978.         if ( isset( $content_struct['description'] ) )
  1979.             $term_data['description'] = $content_struct['description'];
  1980.  
  1981.         if ( isset( $content_struct['slug'] ) )
  1982.             $term_data['slug'] = $content_struct['slug'];
  1983.  
  1984.         $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data );
  1985.  
  1986.         if ( is_wp_error( $term ) )
  1987.             return new IXR_Error( 500, $term->get_error_message() );
  1988.  
  1989.         if ( ! $term )
  1990.             return new IXR_Error( 500, __( 'Sorry, your term could not be created.' ) );
  1991.  
  1992.         // Add term meta.
  1993.         if ( isset( $content_struct['custom_fields'] ) ) {
  1994.             $this->set_term_custom_fields( $term['term_id'], $content_struct['custom_fields'] );
  1995.         }
  1996.  
  1997.         return strval( $term['term_id'] );
  1998.     }
  1999.  
  2000.     /**
  2001.      * Edit a term.
  2002.      *
  2003.      * @since 3.4.0
  2004.      *
  2005.      * @see wp_update_term()
  2006.      *
  2007.      * @param array $args {
  2008.      *     Method arguments. Note: arguments must be ordered as documented.
  2009.      *
  2010.      *     @type int    $blog_id        Blog ID (unused).
  2011.      *     @type string $username       Username.
  2012.      *     @type string $password       Password.
  2013.      *     @type int    $term_id        Term ID.
  2014.      *     @type array  $content_struct Content struct for editing a term. The struct must contain the
  2015.      *                                  term ''taxonomy'. Optional accepted values include 'name', 'parent',
  2016.      *                                  'description', and 'slug'.
  2017.      * }
  2018.      * @return true|IXR_Error True on success, IXR_Error instance on failure.
  2019.      */
  2020.     public function wp_editTerm( $args ) {
  2021.         if ( ! $this->minimum_args( $args, 5 ) )
  2022.             return $this->error;
  2023.  
  2024.         $this->escape( $args );
  2025.  
  2026.         $username       = $args[1];
  2027.         $password       = $args[2];
  2028.         $term_id        = (int) $args[3];
  2029.         $content_struct = $args[4];
  2030.  
  2031.         if ( ! $user = $this->login( $username, $password ) )
  2032.             return $this->error;
  2033.  
  2034.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2035.         do_action( 'xmlrpc_call', 'wp.editTerm' );
  2036.  
  2037.         if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
  2038.             return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
  2039.  
  2040.         $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
  2041.  
  2042.         $taxonomy = (array) $taxonomy;
  2043.  
  2044.         // hold the data of the term
  2045.         $term_data = array();
  2046.  
  2047.         $term = get_term( $term_id , $content_struct['taxonomy'] );
  2048.  
  2049.         if ( is_wp_error( $term ) )
  2050.             return new IXR_Error( 500, $term->get_error_message() );
  2051.  
  2052.         if ( ! $term )
  2053.             return new IXR_Error( 404, __( 'Invalid term ID.' ) );
  2054.  
  2055.         if ( ! current_user_can( 'edit_term', $term_id ) ) {
  2056.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this term.' ) );
  2057.         }
  2058.  
  2059.         if ( isset( $content_struct['name'] ) ) {
  2060.             $term_data['name'] = trim( $content_struct['name'] );
  2061.  
  2062.             if ( empty( $term_data['name'] ) )
  2063.                 return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
  2064.         }
  2065.  
  2066.         if ( ! empty( $content_struct['parent'] ) ) {
  2067.             if ( ! $taxonomy['hierarchical'] )
  2068.                 return new IXR_Error( 403, __( 'Cannot set parent term, taxonomy is not hierarchical.' ) );
  2069.  
  2070.             $parent_term_id = (int) $content_struct['parent'];
  2071.             $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
  2072.  
  2073.             if ( is_wp_error( $parent_term ) )
  2074.                 return new IXR_Error( 500, $parent_term->get_error_message() );
  2075.  
  2076.             if ( ! $parent_term )
  2077.                 return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
  2078.  
  2079.             $term_data['parent'] = $content_struct['parent'];
  2080.         }
  2081.  
  2082.         if ( isset( $content_struct['description'] ) )
  2083.             $term_data['description'] = $content_struct['description'];
  2084.  
  2085.         if ( isset( $content_struct['slug'] ) )
  2086.             $term_data['slug'] = $content_struct['slug'];
  2087.  
  2088.         $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data );
  2089.  
  2090.         if ( is_wp_error( $term ) )
  2091.             return new IXR_Error( 500, $term->get_error_message() );
  2092.  
  2093.         if ( ! $term )
  2094.             return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
  2095.  
  2096.         // Update term meta.
  2097.         if ( isset( $content_struct['custom_fields'] ) ) {
  2098.             $this->set_term_custom_fields( $term_id, $content_struct['custom_fields'] );
  2099.         }
  2100.  
  2101.         return true;
  2102.     }
  2103.  
  2104.     /**
  2105.      * Delete a term.
  2106.      *
  2107.      * @since 3.4.0
  2108.      *
  2109.      * @see wp_delete_term()
  2110.      *
  2111.      * @param array  $args {
  2112.      *     Method arguments. Note: arguments must be ordered as documented.
  2113.      *
  2114.      *     @type int    $blog_id      Blog ID (unused).
  2115.      *     @type string $username     Username.
  2116.      *     @type string $password     Password.
  2117.      *     @type string $taxnomy_name Taxonomy name.
  2118.      *     @type int    $term_id      Term ID.
  2119.      * }
  2120.      * @return bool|IXR_Error True on success, IXR_Error instance on failure.
  2121.      */
  2122.     public function wp_deleteTerm( $args ) {
  2123.         if ( ! $this->minimum_args( $args, 5 ) )
  2124.             return $this->error;
  2125.  
  2126.         $this->escape( $args );
  2127.  
  2128.         $username           = $args[1];
  2129.         $password           = $args[2];
  2130.         $taxonomy           = $args[3];
  2131.         $term_id            = (int) $args[4];
  2132.  
  2133.         if ( ! $user = $this->login( $username, $password ) )
  2134.             return $this->error;
  2135.  
  2136.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2137.         do_action( 'xmlrpc_call', 'wp.deleteTerm' );
  2138.  
  2139.         if ( ! taxonomy_exists( $taxonomy ) )
  2140.             return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
  2141.  
  2142.         $taxonomy = get_taxonomy( $taxonomy );
  2143.         $term = get_term( $term_id, $taxonomy->name );
  2144.  
  2145.         if ( is_wp_error( $term ) )
  2146.             return new IXR_Error( 500, $term->get_error_message() );
  2147.  
  2148.         if ( ! $term )
  2149.             return new IXR_Error( 404, __( 'Invalid term ID.' ) );
  2150.  
  2151.         if ( ! current_user_can( 'delete_term', $term_id ) ) {
  2152.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this term.' ) );
  2153.         }
  2154.  
  2155.         $result = wp_delete_term( $term_id, $taxonomy->name );
  2156.  
  2157.         if ( is_wp_error( $result ) )
  2158.             return new IXR_Error( 500, $term->get_error_message() );
  2159.  
  2160.         if ( ! $result )
  2161.             return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) );
  2162.  
  2163.         return $result;
  2164.     }
  2165.  
  2166.     /**
  2167.      * Retrieve a term.
  2168.      *
  2169.      * @since 3.4.0
  2170.      *
  2171.      * @see get_term()
  2172.      *
  2173.      * @param array  $args {
  2174.      *     Method arguments. Note: arguments must be ordered as documented.
  2175.      *
  2176.      *     @type int    $blog_id  Blog ID (unused).
  2177.      *     @type string $username Username.
  2178.      *     @type string $password Password.
  2179.      *     @type string $taxnomy  Taxonomy name.
  2180.      *     @type string $term_id  Term ID.
  2181.      * }
  2182.      * @return array|IXR_Error IXR_Error on failure, array on success, containing:
  2183.      *  - 'term_id'
  2184.      *  - 'name'
  2185.      *  - 'slug'
  2186.      *  - 'term_group'
  2187.      *  - 'term_taxonomy_id'
  2188.      *  - 'taxonomy'
  2189.      *  - 'description'
  2190.      *  - 'parent'
  2191.      *  - 'count'
  2192.      */
  2193.     public function wp_getTerm( $args ) {
  2194.         if ( ! $this->minimum_args( $args, 5 ) )
  2195.             return $this->error;
  2196.  
  2197.         $this->escape( $args );
  2198.  
  2199.         $username           = $args[1];
  2200.         $password           = $args[2];
  2201.         $taxonomy           = $args[3];
  2202.         $term_id            = (int) $args[4];
  2203.  
  2204.         if ( ! $user = $this->login( $username, $password ) )
  2205.             return $this->error;
  2206.  
  2207.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2208.         do_action( 'xmlrpc_call', 'wp.getTerm' );
  2209.  
  2210.         if ( ! taxonomy_exists( $taxonomy ) )
  2211.             return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
  2212.  
  2213.         $taxonomy = get_taxonomy( $taxonomy );
  2214.  
  2215.         $term = get_term( $term_id , $taxonomy->name, ARRAY_A );
  2216.  
  2217.         if ( is_wp_error( $term ) )
  2218.             return new IXR_Error( 500, $term->get_error_message() );
  2219.  
  2220.         if ( ! $term )
  2221.             return new IXR_Error( 404, __( 'Invalid term ID.' ) );
  2222.  
  2223.         if ( ! current_user_can( 'assign_term', $term_id ) ) {
  2224.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign this term.' ) );
  2225.         }
  2226.  
  2227.         return $this->_prepare_term( $term );
  2228.     }
  2229.  
  2230.     /**
  2231.      * Retrieve all terms for a taxonomy.
  2232.      *
  2233.      * @since 3.4.0
  2234.      *
  2235.      * The optional $filter parameter modifies the query used to retrieve terms.
  2236.      * Accepted keys are 'number', 'offset', 'orderby', 'order', 'hide_empty', and 'search'.
  2237.      *
  2238.      * @see get_terms()
  2239.      *
  2240.      * @param array  $args {
  2241.      *     Method arguments. Note: arguments must be ordered as documented.
  2242.      *
  2243.      *     @type int    $blog_id  Blog ID (unused).
  2244.      *     @type string $username Username.
  2245.      *     @type string $password Password.
  2246.      *     @type string $taxnomy  Taxonomy name.
  2247.      *     @type array  $filter   Optional. Modifies the query used to retrieve posts. Accepts 'number',
  2248.      *                            'offset', 'orderby', 'order', 'hide_empty', and 'search'. Default empty array.
  2249.      * }
  2250.      * @return array|IXR_Error An associative array of terms data on success, IXR_Error instance otherwise.
  2251.      */
  2252.     public function wp_getTerms( $args ) {
  2253.         if ( ! $this->minimum_args( $args, 4 ) )
  2254.             return $this->error;
  2255.  
  2256.         $this->escape( $args );
  2257.  
  2258.         $username       = $args[1];
  2259.         $password       = $args[2];
  2260.         $taxonomy       = $args[3];
  2261.         $filter         = isset( $args[4] ) ? $args[4] : array();
  2262.  
  2263.         if ( ! $user = $this->login( $username, $password ) )
  2264.             return $this->error;
  2265.  
  2266.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2267.         do_action( 'xmlrpc_call', 'wp.getTerms' );
  2268.  
  2269.         if ( ! taxonomy_exists( $taxonomy ) )
  2270.             return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
  2271.  
  2272.         $taxonomy = get_taxonomy( $taxonomy );
  2273.  
  2274.         if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
  2275.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign terms in this taxonomy.' ) );
  2276.  
  2277.         $query = array();
  2278.  
  2279.         if ( isset( $filter['number'] ) )
  2280.             $query['number'] = absint( $filter['number'] );
  2281.  
  2282.         if ( isset( $filter['offset'] ) )
  2283.             $query['offset'] = absint( $filter['offset'] );
  2284.  
  2285.         if ( isset( $filter['orderby'] ) ) {
  2286.             $query['orderby'] = $filter['orderby'];
  2287.  
  2288.             if ( isset( $filter['order'] ) )
  2289.                 $query['order'] = $filter['order'];
  2290.         }
  2291.  
  2292.         if ( isset( $filter['hide_empty'] ) )
  2293.             $query['hide_empty'] = $filter['hide_empty'];
  2294.         else
  2295.             $query['get'] = 'all';
  2296.  
  2297.         if ( isset( $filter['search'] ) )
  2298.             $query['search'] = $filter['search'];
  2299.  
  2300.         $terms = get_terms( $taxonomy->name, $query );
  2301.  
  2302.         if ( is_wp_error( $terms ) )
  2303.             return new IXR_Error( 500, $terms->get_error_message() );
  2304.  
  2305.         $struct = array();
  2306.  
  2307.         foreach ( $terms as $term ) {
  2308.             $struct[] = $this->_prepare_term( $term );
  2309.         }
  2310.  
  2311.         return $struct;
  2312.     }
  2313.  
  2314.     /**
  2315.      * Retrieve a taxonomy.
  2316.      *
  2317.      * @since 3.4.0
  2318.      *
  2319.      * @see get_taxonomy()
  2320.      *
  2321.      * @param array  $args {
  2322.      *     Method arguments. Note: arguments must be ordered as documented.
  2323.      *
  2324.      *     @type int    $blog_id  Blog ID (unused).
  2325.      *     @type string $username Username.
  2326.      *     @type string $password Password.
  2327.      *     @type string $taxnomy  Taxonomy name.
  2328.      *     @type array  $fields   Optional. Array of taxonomy fields to limit to in the return.
  2329.      *                            Accepts 'labels', 'cap', 'menu', and 'object_type'.
  2330.      *                            Default empty array.
  2331.      * }
  2332.      * @return array|IXR_Error An array of taxonomy data on success, IXR_Error instance otherwise.
  2333.      */
  2334.     public function wp_getTaxonomy( $args ) {
  2335.         if ( ! $this->minimum_args( $args, 4 ) )
  2336.             return $this->error;
  2337.  
  2338.         $this->escape( $args );
  2339.  
  2340.         $username = $args[1];
  2341.         $password = $args[2];
  2342.         $taxonomy = $args[3];
  2343.  
  2344.         if ( isset( $args[4] ) ) {
  2345.             $fields = $args[4];
  2346.         } else {
  2347.             /**
  2348.              * Filters the taxonomy query fields used by the given XML-RPC method.
  2349.              *
  2350.              * @since 3.4.0
  2351.              *
  2352.              * @param array  $fields An array of taxonomy fields to retrieve.
  2353.              * @param string $method The method name.
  2354.              */
  2355.             $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomy' );
  2356.         }
  2357.  
  2358.         if ( ! $user = $this->login( $username, $password ) )
  2359.             return $this->error;
  2360.  
  2361.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2362.         do_action( 'xmlrpc_call', 'wp.getTaxonomy' );
  2363.  
  2364.         if ( ! taxonomy_exists( $taxonomy ) )
  2365.             return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
  2366.  
  2367.         $taxonomy = get_taxonomy( $taxonomy );
  2368.  
  2369.         if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
  2370.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign terms in this taxonomy.' ) );
  2371.  
  2372.         return $this->_prepare_taxonomy( $taxonomy, $fields );
  2373.     }
  2374.  
  2375.     /**
  2376.      * Retrieve all taxonomies.
  2377.      *
  2378.      * @since 3.4.0
  2379.      *
  2380.      * @see get_taxonomies()
  2381.      *
  2382.      * @param array  $args {
  2383.      *     Method arguments. Note: arguments must be ordered as documented.
  2384.      *
  2385.      *     @type int    $blog_id  Blog ID (unused).
  2386.      *     @type string $username Username.
  2387.      *     @type string $password Password.
  2388.      *     @type array  $filter   Optional. An array of arguments for retrieving taxonomies.
  2389.      *     @type array  $fields   Optional. The subset of taxonomy fields to return.
  2390.      * }
  2391.      * @return array|IXR_Error An associative array of taxonomy data with returned fields determined
  2392.      *                         by `$fields`, or an IXR_Error instance on failure.
  2393.      */
  2394.     public function wp_getTaxonomies( $args ) {
  2395.         if ( ! $this->minimum_args( $args, 3 ) )
  2396.             return $this->error;
  2397.  
  2398.         $this->escape( $args );
  2399.  
  2400.         $username = $args[1];
  2401.         $password = $args[2];
  2402.         $filter   = isset( $args[3] ) ? $args[3] : array( 'public' => true );
  2403.  
  2404.         if ( isset( $args[4] ) ) {
  2405.             $fields = $args[4];
  2406.         } else {
  2407.             /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2408.             $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomies' );
  2409.         }
  2410.  
  2411.         if ( ! $user = $this->login( $username, $password ) )
  2412.             return $this->error;
  2413.  
  2414.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2415.         do_action( 'xmlrpc_call', 'wp.getTaxonomies' );
  2416.  
  2417.         $taxonomies = get_taxonomies( $filter, 'objects' );
  2418.  
  2419.         // holds all the taxonomy data
  2420.         $struct = array();
  2421.  
  2422.         foreach ( $taxonomies as $taxonomy ) {
  2423.             // capability check for post_types
  2424.             if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
  2425.                 continue;
  2426.  
  2427.             $struct[] = $this->_prepare_taxonomy( $taxonomy, $fields );
  2428.         }
  2429.  
  2430.         return $struct;
  2431.     }
  2432.  
  2433.     /**
  2434.      * Retrieve a user.
  2435.      *
  2436.      * The optional $fields parameter specifies what fields will be included
  2437.      * in the response array. This should be a list of field names. 'user_id' will
  2438.      * always be included in the response regardless of the value of $fields.
  2439.      *
  2440.      * Instead of, or in addition to, individual field names, conceptual group
  2441.      * names can be used to specify multiple fields. The available conceptual
  2442.      * groups are 'basic' and 'all'.
  2443.      *
  2444.      * @uses get_userdata()
  2445.      *
  2446.      * @param array  $args {
  2447.      *     Method arguments. Note: arguments must be ordered as documented.
  2448.      *
  2449.      *     @type int    $blog_id (unused)
  2450.      *     @type string $username
  2451.      *     @type string $password
  2452.      *     @type int    $user_id
  2453.      *     @type array  $fields (optional)
  2454.      * }
  2455.      * @return array|IXR_Error Array contains (based on $fields parameter):
  2456.      *  - 'user_id'
  2457.      *  - 'username'
  2458.      *  - 'first_name'
  2459.      *  - 'last_name'
  2460.      *  - 'registered'
  2461.      *  - 'bio'
  2462.      *  - 'email'
  2463.      *  - 'nickname'
  2464.      *  - 'nicename'
  2465.      *  - 'url'
  2466.      *  - 'display_name'
  2467.      *  - 'roles'
  2468.      */
  2469.     public function wp_getUser( $args ) {
  2470.         if ( ! $this->minimum_args( $args, 4 ) )
  2471.             return $this->error;
  2472.  
  2473.         $this->escape( $args );
  2474.  
  2475.         $username = $args[1];
  2476.         $password = $args[2];
  2477.         $user_id  = (int) $args[3];
  2478.  
  2479.         if ( isset( $args[4] ) ) {
  2480.             $fields = $args[4];
  2481.         } else {
  2482.             /**
  2483.              * Filters the default user query fields used by the given XML-RPC method.
  2484.              *
  2485.              * @since 3.5.0
  2486.              *
  2487.              * @param array  $fields User query fields for given method. Default 'all'.
  2488.              * @param string $method The method name.
  2489.              */
  2490.             $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUser' );
  2491.         }
  2492.  
  2493.         if ( ! $user = $this->login( $username, $password ) )
  2494.             return $this->error;
  2495.  
  2496.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2497.         do_action( 'xmlrpc_call', 'wp.getUser' );
  2498.  
  2499.         if ( ! current_user_can( 'edit_user', $user_id ) )
  2500.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this user.' ) );
  2501.  
  2502.         $user_data = get_userdata( $user_id );
  2503.  
  2504.         if ( ! $user_data )
  2505.             return new IXR_Error( 404, __( 'Invalid user ID.' ) );
  2506.  
  2507.         return $this->_prepare_user( $user_data, $fields );
  2508.     }
  2509.  
  2510.     /**
  2511.      * Retrieve users.
  2512.      *
  2513.      * The optional $filter parameter modifies the query used to retrieve users.
  2514.      * Accepted keys are 'number' (default: 50), 'offset' (default: 0), 'role',
  2515.      * 'who', 'orderby', and 'order'.
  2516.      *
  2517.      * The optional $fields parameter specifies what fields will be included
  2518.      * in the response array.
  2519.      *
  2520.      * @uses get_users()
  2521.      * @see wp_getUser() for more on $fields and return values
  2522.      *
  2523.      * @param array  $args {
  2524.      *     Method arguments. Note: arguments must be ordered as documented.
  2525.      *
  2526.      *     @type int    $blog_id (unused)
  2527.      *     @type string $username
  2528.      *     @type string $password
  2529.      *     @type array  $filter (optional)
  2530.      *     @type array  $fields (optional)
  2531.      * }
  2532.      * @return array|IXR_Error users data
  2533.      */
  2534.     public function wp_getUsers( $args ) {
  2535.         if ( ! $this->minimum_args( $args, 3 ) )
  2536.             return $this->error;
  2537.  
  2538.         $this->escape( $args );
  2539.  
  2540.         $username = $args[1];
  2541.         $password = $args[2];
  2542.         $filter   = isset( $args[3] ) ? $args[3] : array();
  2543.  
  2544.         if ( isset( $args[4] ) ) {
  2545.             $fields = $args[4];
  2546.         } else {
  2547.             /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2548.             $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUsers' );
  2549.         }
  2550.  
  2551.         if ( ! $user = $this->login( $username, $password ) )
  2552.             return $this->error;
  2553.  
  2554.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2555.         do_action( 'xmlrpc_call', 'wp.getUsers' );
  2556.  
  2557.         if ( ! current_user_can( 'list_users' ) )
  2558.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to list users.' ) );
  2559.  
  2560.         $query = array( 'fields' => 'all_with_meta' );
  2561.  
  2562.         $query['number'] = ( isset( $filter['number'] ) ) ? absint( $filter['number'] ) : 50;
  2563.         $query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0;
  2564.  
  2565.         if ( isset( $filter['orderby'] ) ) {
  2566.             $query['orderby'] = $filter['orderby'];
  2567.  
  2568.             if ( isset( $filter['order'] ) )
  2569.                 $query['order'] = $filter['order'];
  2570.         }
  2571.  
  2572.         if ( isset( $filter['role'] ) ) {
  2573.             if ( get_role( $filter['role'] ) === null )
  2574.                 return new IXR_Error( 403, __( 'Invalid role.' ) );
  2575.  
  2576.             $query['role'] = $filter['role'];
  2577.         }
  2578.  
  2579.         if ( isset( $filter['who'] ) ) {
  2580.             $query['who'] = $filter['who'];
  2581.         }
  2582.  
  2583.         $users = get_users( $query );
  2584.  
  2585.         $_users = array();
  2586.         foreach ( $users as $user_data ) {
  2587.             if ( current_user_can( 'edit_user', $user_data->ID ) )
  2588.                 $_users[] = $this->_prepare_user( $user_data, $fields );
  2589.         }
  2590.         return $_users;
  2591.     }
  2592.  
  2593.     /**
  2594.      * Retrieve information about the requesting user.
  2595.      *
  2596.      * @uses get_userdata()
  2597.      *
  2598.      * @param array  $args {
  2599.      *     Method arguments. Note: arguments must be ordered as documented.
  2600.      *
  2601.      *     @type int    $blog_id (unused)
  2602.      *     @type string $username
  2603.      *     @type string $password
  2604.      *     @type array  $fields (optional)
  2605.      * }
  2606.      * @return array|IXR_Error (@see wp_getUser)
  2607.      */
  2608.     public function wp_getProfile( $args ) {
  2609.         if ( ! $this->minimum_args( $args, 3 ) )
  2610.             return $this->error;
  2611.  
  2612.         $this->escape( $args );
  2613.  
  2614.         $username = $args[1];
  2615.         $password = $args[2];
  2616.  
  2617.         if ( isset( $args[3] ) ) {
  2618.             $fields = $args[3];
  2619.         } else {
  2620.             /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2621.             $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getProfile' );
  2622.         }
  2623.  
  2624.         if ( ! $user = $this->login( $username, $password ) )
  2625.             return $this->error;
  2626.  
  2627.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2628.         do_action( 'xmlrpc_call', 'wp.getProfile' );
  2629.  
  2630.         if ( ! current_user_can( 'edit_user', $user->ID ) )
  2631.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit your profile.' ) );
  2632.  
  2633.         $user_data = get_userdata( $user->ID );
  2634.  
  2635.         return $this->_prepare_user( $user_data, $fields );
  2636.     }
  2637.  
  2638.     /**
  2639.      * Edit user's profile.
  2640.      *
  2641.      * @uses wp_update_user()
  2642.      *
  2643.      * @param array  $args {
  2644.      *     Method arguments. Note: arguments must be ordered as documented.
  2645.      *
  2646.      *     @type int    $blog_id (unused)
  2647.      *     @type string $username
  2648.      *     @type string $password
  2649.      *     @type array  $content_struct It can optionally contain:
  2650.      *      - 'first_name'
  2651.      *      - 'last_name'
  2652.      *      - 'website'
  2653.      *      - 'display_name'
  2654.      *      - 'nickname'
  2655.      *      - 'nicename'
  2656.      *      - 'bio'
  2657.      * }
  2658.      * @return true|IXR_Error True, on success.
  2659.      */
  2660.     public function wp_editProfile( $args ) {
  2661.         if ( ! $this->minimum_args( $args, 4 ) )
  2662.             return $this->error;
  2663.  
  2664.         $this->escape( $args );
  2665.  
  2666.         $username       = $args[1];
  2667.         $password       = $args[2];
  2668.         $content_struct = $args[3];
  2669.  
  2670.         if ( ! $user = $this->login( $username, $password ) )
  2671.             return $this->error;
  2672.  
  2673.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2674.         do_action( 'xmlrpc_call', 'wp.editProfile' );
  2675.  
  2676.         if ( ! current_user_can( 'edit_user', $user->ID ) )
  2677.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit your profile.' ) );
  2678.  
  2679.         // holds data of the user
  2680.         $user_data = array();
  2681.         $user_data['ID'] = $user->ID;
  2682.  
  2683.         // only set the user details if it was given
  2684.         if ( isset( $content_struct['first_name'] ) )
  2685.             $user_data['first_name'] = $content_struct['first_name'];
  2686.  
  2687.         if ( isset( $content_struct['last_name'] ) )
  2688.             $user_data['last_name'] = $content_struct['last_name'];
  2689.  
  2690.         if ( isset( $content_struct['url'] ) )
  2691.             $user_data['user_url'] = $content_struct['url'];
  2692.  
  2693.         if ( isset( $content_struct['display_name'] ) )
  2694.             $user_data['display_name'] = $content_struct['display_name'];
  2695.  
  2696.         if ( isset( $content_struct['nickname'] ) )
  2697.             $user_data['nickname'] = $content_struct['nickname'];
  2698.  
  2699.         if ( isset( $content_struct['nicename'] ) )
  2700.             $user_data['user_nicename'] = $content_struct['nicename'];
  2701.  
  2702.         if ( isset( $content_struct['bio'] ) )
  2703.             $user_data['description'] = $content_struct['bio'];
  2704.  
  2705.         $result = wp_update_user( $user_data );
  2706.  
  2707.         if ( is_wp_error( $result ) )
  2708.             return new IXR_Error( 500, $result->get_error_message() );
  2709.  
  2710.         if ( ! $result )
  2711.             return new IXR_Error( 500, __( 'Sorry, the user cannot be updated.' ) );
  2712.  
  2713.         return true;
  2714.     }
  2715.  
  2716.     /**
  2717.      * Retrieve page.
  2718.      *
  2719.      * @since 2.2.0
  2720.      *
  2721.      * @param array  $args {
  2722.      *     Method arguments. Note: arguments must be ordered as documented.
  2723.      *
  2724.      *     @type int    $blog_id (unused)
  2725.      *     @type int    $page_id
  2726.      *     @type string $username
  2727.      *     @type string $password
  2728.      * }
  2729.      * @return array|IXR_Error
  2730.      */
  2731.     public function wp_getPage( $args ) {
  2732.         $this->escape( $args );
  2733.  
  2734.         $page_id  = (int) $args[1];
  2735.         $username = $args[2];
  2736.         $password = $args[3];
  2737.  
  2738.         if ( !$user = $this->login($username, $password) ) {
  2739.             return $this->error;
  2740.         }
  2741.  
  2742.         $page = get_post($page_id);
  2743.         if ( ! $page )
  2744.             return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  2745.  
  2746.         if ( !current_user_can( 'edit_page', $page_id ) )
  2747.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this page.' ) );
  2748.  
  2749.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2750.         do_action( 'xmlrpc_call', 'wp.getPage' );
  2751.  
  2752.         // If we found the page then format the data.
  2753.         if ( $page->ID && ($page->post_type == 'page') ) {
  2754.             return $this->_prepare_page( $page );
  2755.         }
  2756.         // If the page doesn't exist indicate that.
  2757.         else {
  2758.             return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
  2759.         }
  2760.     }
  2761.  
  2762.     /**
  2763.      * Retrieve Pages.
  2764.      *
  2765.      * @since 2.2.0
  2766.      *
  2767.      * @param array  $args {
  2768.      *     Method arguments. Note: arguments must be ordered as documented.
  2769.      *
  2770.      *     @type int    $blog_id (unused)
  2771.      *     @type string $username
  2772.      *     @type string $password
  2773.      *     @type int    $num_pages
  2774.      * }
  2775.      * @return array|IXR_Error
  2776.      */
  2777.     public function wp_getPages( $args ) {
  2778.         $this->escape( $args );
  2779.  
  2780.         $username  = $args[1];
  2781.         $password  = $args[2];
  2782.         $num_pages = isset($args[3]) ? (int) $args[3] : 10;
  2783.  
  2784.         if ( !$user = $this->login($username, $password) )
  2785.             return $this->error;
  2786.  
  2787.         if ( !current_user_can( 'edit_pages' ) )
  2788.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit pages.' ) );
  2789.  
  2790.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2791.         do_action( 'xmlrpc_call', 'wp.getPages' );
  2792.  
  2793.         $pages = get_posts( array('post_type' => 'page', 'post_status' => 'any', 'numberposts' => $num_pages) );
  2794.         $num_pages = count($pages);
  2795.  
  2796.         // If we have pages, put together their info.
  2797.         if ( $num_pages >= 1 ) {
  2798.             $pages_struct = array();
  2799.  
  2800.             foreach ($pages as $page) {
  2801.                 if ( current_user_can( 'edit_page', $page->ID ) )
  2802.                     $pages_struct[] = $this->_prepare_page( $page );
  2803.             }
  2804.  
  2805.             return $pages_struct;
  2806.         }
  2807.  
  2808.         return array();
  2809.     }
  2810.  
  2811.     /**
  2812.      * Create new page.
  2813.      *
  2814.      * @since 2.2.0
  2815.      *
  2816.      * @see wp_xmlrpc_server::mw_newPost()
  2817.      *
  2818.      * @param array  $args {
  2819.      *     Method arguments. Note: arguments must be ordered as documented.
  2820.      *
  2821.      *     @type int    $blog_id (unused)
  2822.      *     @type string $username
  2823.      *     @type string $password
  2824.      *     @type array  $content_struct
  2825.      * }
  2826.      * @return int|IXR_Error
  2827.      */
  2828.     public function wp_newPage( $args ) {
  2829.         // Items not escaped here will be escaped in newPost.
  2830.         $username = $this->escape( $args[1] );
  2831.         $password = $this->escape( $args[2] );
  2832.  
  2833.         if ( !$user = $this->login($username, $password) )
  2834.             return $this->error;
  2835.  
  2836.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2837.         do_action( 'xmlrpc_call', 'wp.newPage' );
  2838.  
  2839.         // Mark this as content for a page.
  2840.         $args[3]["post_type"] = 'page';
  2841.  
  2842.         // Let mw_newPost do all of the heavy lifting.
  2843.         return $this->mw_newPost( $args );
  2844.     }
  2845.  
  2846.     /**
  2847.      * Delete page.
  2848.      *
  2849.      * @since 2.2.0
  2850.      *
  2851.      * @param array  $args {
  2852.      *     Method arguments. Note: arguments must be ordered as documented.
  2853.      *
  2854.      *     @type int    $blog_id (unused)
  2855.      *     @type string $username
  2856.      *     @type string $password
  2857.      *     @type int    $page_id
  2858.      * }
  2859.      * @return true|IXR_Error True, if success.
  2860.      */
  2861.     public function wp_deletePage( $args ) {
  2862.         $this->escape( $args );
  2863.  
  2864.         $username = $args[1];
  2865.         $password = $args[2];
  2866.         $page_id  = (int) $args[3];
  2867.  
  2868.         if ( !$user = $this->login($username, $password) )
  2869.             return $this->error;
  2870.  
  2871.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2872.         do_action( 'xmlrpc_call', 'wp.deletePage' );
  2873.  
  2874.         // Get the current page based on the page_id and
  2875.         // make sure it is a page and not a post.
  2876.         $actual_page = get_post($page_id, ARRAY_A);
  2877.         if ( !$actual_page || ($actual_page['post_type'] != 'page') )
  2878.             return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
  2879.  
  2880.         // Make sure the user can delete pages.
  2881.         if ( !current_user_can('delete_page', $page_id) )
  2882.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this page.' ) );
  2883.  
  2884.         // Attempt to delete the page.
  2885.         $result = wp_delete_post($page_id);
  2886.         if ( !$result )
  2887.             return new IXR_Error( 500, __( 'Failed to delete the page.' ) );
  2888.  
  2889.         /**
  2890.          * Fires after a page has been successfully deleted via XML-RPC.
  2891.          *
  2892.          * @since 3.4.0
  2893.          *
  2894.          * @param int   $page_id ID of the deleted page.
  2895.          * @param array $args    An array of arguments to delete the page.
  2896.          */
  2897.         do_action( 'xmlrpc_call_success_wp_deletePage', $page_id, $args );
  2898.  
  2899.         return true;
  2900.     }
  2901.  
  2902.     /**
  2903.      * Edit page.
  2904.      *
  2905.      * @since 2.2.0
  2906.      *
  2907.      * @param array  $args {
  2908.      *     Method arguments. Note: arguments must be ordered as documented.
  2909.      *
  2910.      *     @type int    $blog_id (unused)
  2911.      *     @type int    $page_id
  2912.      *     @type string $username
  2913.      *     @type string $password
  2914.      *     @type string $content
  2915.      *     @type string $publish
  2916.      * }
  2917.      * @return array|IXR_Error
  2918.      */
  2919.     public function wp_editPage( $args ) {
  2920.         // Items will be escaped in mw_editPost.
  2921.         $page_id  = (int) $args[1];
  2922.         $username = $args[2];
  2923.         $password = $args[3];
  2924.         $content  = $args[4];
  2925.         $publish  = $args[5];
  2926.  
  2927.         $escaped_username = $this->escape( $username );
  2928.         $escaped_password = $this->escape( $password );
  2929.  
  2930.         if ( !$user = $this->login( $escaped_username, $escaped_password ) ) {
  2931.             return $this->error;
  2932.         }
  2933.  
  2934.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2935.         do_action( 'xmlrpc_call', 'wp.editPage' );
  2936.  
  2937.         // Get the page data and make sure it is a page.
  2938.         $actual_page = get_post($page_id, ARRAY_A);
  2939.         if ( !$actual_page || ($actual_page['post_type'] != 'page') )
  2940.             return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
  2941.  
  2942.         // Make sure the user is allowed to edit pages.
  2943.         if ( !current_user_can('edit_page', $page_id) )
  2944.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this page.' ) );
  2945.  
  2946.         // Mark this as content for a page.
  2947.         $content['post_type'] = 'page';
  2948.  
  2949.         // Arrange args in the way mw_editPost understands.
  2950.         $args = array(
  2951.             $page_id,
  2952.             $username,
  2953.             $password,
  2954.             $content,
  2955.             $publish
  2956.         );
  2957.  
  2958.         // Let mw_editPost do all of the heavy lifting.
  2959.         return $this->mw_editPost( $args );
  2960.     }
  2961.  
  2962.     /**
  2963.      * Retrieve page list.
  2964.      *
  2965.      * @since 2.2.0
  2966.      *
  2967.      * @global wpdb $wpdb WordPress database abstraction object.
  2968.      *
  2969.      * @param array  $args {
  2970.      *     Method arguments. Note: arguments must be ordered as documented.
  2971.      *
  2972.      *     @type int    $blog_id (unused)
  2973.      *     @type string $username
  2974.      *     @type string $password
  2975.      * }
  2976.      * @return array|IXR_Error
  2977.      */
  2978.     public function wp_getPageList( $args ) {
  2979.         global $wpdb;
  2980.  
  2981.         $this->escape( $args );
  2982.  
  2983.         $username = $args[1];
  2984.         $password = $args[2];
  2985.  
  2986.         if ( !$user = $this->login($username, $password) )
  2987.             return $this->error;
  2988.  
  2989.         if ( !current_user_can( 'edit_pages' ) )
  2990.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit pages.' ) );
  2991.  
  2992.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2993.         do_action( 'xmlrpc_call', 'wp.getPageList' );
  2994.  
  2995.         // Get list of pages ids and titles
  2996.         $page_list = $wpdb->get_results("
  2997.             SELECT ID page_id,
  2998.                 post_title page_title,
  2999.                 post_parent page_parent_id,
  3000.                 post_date_gmt,
  3001.                 post_date,
  3002.                 post_status
  3003.             FROM {$wpdb->posts}
  3004.             WHERE post_type = 'page'
  3005.             ORDER BY ID
  3006.         ");
  3007.  
  3008.         // The date needs to be formatted properly.
  3009.         $num_pages = count($page_list);
  3010.         for ( $i = 0; $i < $num_pages; $i++ ) {
  3011.             $page_list[$i]->dateCreated = $this->_convert_date(  $page_list[$i]->post_date );
  3012.             $page_list[$i]->date_created_gmt = $this->_convert_date_gmt( $page_list[$i]->post_date_gmt, $page_list[$i]->post_date );
  3013.  
  3014.             unset($page_list[$i]->post_date_gmt);
  3015.             unset($page_list[$i]->post_date);
  3016.             unset($page_list[$i]->post_status);
  3017.         }
  3018.  
  3019.         return $page_list;
  3020.     }
  3021.  
  3022.     /**
  3023.      * Retrieve authors list.
  3024.      *
  3025.      * @since 2.2.0
  3026.      *
  3027.      * @param array  $args {
  3028.      *     Method arguments. Note: arguments must be ordered as documented.
  3029.      *
  3030.      *     @type int    $blog_id (unused)
  3031.      *     @type string $username
  3032.      *     @type string $password
  3033.      * }
  3034.      * @return array|IXR_Error
  3035.      */
  3036.     public function wp_getAuthors( $args ) {
  3037.         $this->escape( $args );
  3038.  
  3039.         $username = $args[1];
  3040.         $password = $args[2];
  3041.  
  3042.         if ( !$user = $this->login($username, $password) )
  3043.             return $this->error;
  3044.  
  3045.         if ( !current_user_can('edit_posts') )
  3046.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts.' ) );
  3047.  
  3048.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3049.         do_action( 'xmlrpc_call', 'wp.getAuthors' );
  3050.  
  3051.         $authors = array();
  3052.         foreach ( get_users( array( 'fields' => array('ID','user_login','display_name') ) ) as $user ) {
  3053.             $authors[] = array(
  3054.                 'user_id'       => $user->ID,
  3055.                 'user_login'    => $user->user_login,
  3056.                 'display_name'  => $user->display_name
  3057.             );
  3058.         }
  3059.  
  3060.         return $authors;
  3061.     }
  3062.  
  3063.     /**
  3064.      * Get list of all tags
  3065.      *
  3066.      * @since 2.7.0
  3067.      *
  3068.      * @param array  $args {
  3069.      *     Method arguments. Note: arguments must be ordered as documented.
  3070.      *
  3071.      *     @type int    $blog_id (unused)
  3072.      *     @type string $username
  3073.      *     @type string $password
  3074.      * }
  3075.      * @return array|IXR_Error
  3076.      */
  3077.     public function wp_getTags( $args ) {
  3078.         $this->escape( $args );
  3079.  
  3080.         $username = $args[1];
  3081.         $password = $args[2];
  3082.  
  3083.         if ( !$user = $this->login($username, $password) )
  3084.             return $this->error;
  3085.  
  3086.         if ( !current_user_can( 'edit_posts' ) )
  3087.             return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
  3088.  
  3089.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3090.         do_action( 'xmlrpc_call', 'wp.getKeywords' );
  3091.  
  3092.         $tags = array();
  3093.  
  3094.         if ( $all_tags = get_tags() ) {
  3095.             foreach ( (array) $all_tags as $tag ) {
  3096.                 $struct = array();
  3097.                 $struct['tag_id']            = $tag->term_id;
  3098.                 $struct['name']                = $tag->name;
  3099.                 $struct['count']            = $tag->count;
  3100.                 $struct['slug']                = $tag->slug;
  3101.                 $struct['html_url']            = esc_html( get_tag_link( $tag->term_id ) );
  3102.                 $struct['rss_url']            = esc_html( get_tag_feed_link( $tag->term_id ) );
  3103.  
  3104.                 $tags[] = $struct;
  3105.             }
  3106.         }
  3107.  
  3108.         return $tags;
  3109.     }
  3110.  
  3111.     /**
  3112.      * Create new category.
  3113.      *
  3114.      * @since 2.2.0
  3115.      *
  3116.      * @param array  $args {
  3117.      *     Method arguments. Note: arguments must be ordered as documented.
  3118.      *
  3119.      *     @type int    $blog_id (unused)
  3120.      *     @type string $username
  3121.      *     @type string $password
  3122.      *     @type array  $category
  3123.      * }
  3124.      * @return int|IXR_Error Category ID.
  3125.      */
  3126.     public function wp_newCategory( $args ) {
  3127.         $this->escape( $args );
  3128.  
  3129.         $username = $args[1];
  3130.         $password = $args[2];
  3131.         $category = $args[3];
  3132.  
  3133.         if ( !$user = $this->login($username, $password) )
  3134.             return $this->error;
  3135.  
  3136.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3137.         do_action( 'xmlrpc_call', 'wp.newCategory' );
  3138.  
  3139.         // Make sure the user is allowed to add a category.
  3140.         if ( ! current_user_can( 'manage_categories' ) ) {
  3141.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a category.' ) );
  3142.         }
  3143.  
  3144.         // If no slug was provided make it empty so that
  3145.         // WordPress will generate one.
  3146.         if ( empty($category['slug']) )
  3147.             $category['slug'] = '';
  3148.  
  3149.         // If no parent_id was provided make it empty
  3150.         // so that it will be a top level page (no parent).
  3151.         if ( !isset($category['parent_id']) )
  3152.             $category['parent_id'] = '';
  3153.  
  3154.         // If no description was provided make it empty.
  3155.         if ( empty($category["description"]) )
  3156.             $category["description"] = "";
  3157.  
  3158.         $new_category = array(
  3159.             'cat_name'                => $category['name'],
  3160.             'category_nicename'        => $category['slug'],
  3161.             'category_parent'        => $category['parent_id'],
  3162.             'category_description'    => $category['description']
  3163.         );
  3164.  
  3165.         $cat_id = wp_insert_category($new_category, true);
  3166.         if ( is_wp_error( $cat_id ) ) {
  3167.             if ( 'term_exists' == $cat_id->get_error_code() )
  3168.                 return (int) $cat_id->get_error_data();
  3169.             else
  3170.                 return new IXR_Error(500, __('Sorry, the new category failed.'));
  3171.         } elseif ( ! $cat_id ) {
  3172.             return new IXR_Error(500, __('Sorry, the new category failed.'));
  3173.         }
  3174.  
  3175.         /**
  3176.          * Fires after a new category has been successfully created via XML-RPC.
  3177.          *
  3178.          * @since 3.4.0
  3179.          *
  3180.          * @param int   $cat_id ID of the new category.
  3181.          * @param array $args   An array of new category arguments.
  3182.          */
  3183.         do_action( 'xmlrpc_call_success_wp_newCategory', $cat_id, $args );
  3184.  
  3185.         return $cat_id;
  3186.     }
  3187.  
  3188.     /**
  3189.      * Remove category.
  3190.      *
  3191.      * @since 2.5.0
  3192.      *
  3193.      * @param array  $args {
  3194.      *     Method arguments. Note: arguments must be ordered as documented.
  3195.      *
  3196.      *     @type int    $blog_id (unused)
  3197.      *     @type string $username
  3198.      *     @type string $password
  3199.      *     @type int    $category_id
  3200.      * }
  3201.      * @return bool|IXR_Error See wp_delete_term() for return info.
  3202.      */
  3203.     public function wp_deleteCategory( $args ) {
  3204.         $this->escape( $args );
  3205.  
  3206.         $username    = $args[1];
  3207.         $password    = $args[2];
  3208.         $category_id = (int) $args[3];
  3209.  
  3210.         if ( !$user = $this->login($username, $password) )
  3211.             return $this->error;
  3212.  
  3213.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3214.         do_action( 'xmlrpc_call', 'wp.deleteCategory' );
  3215.  
  3216.         if ( ! current_user_can( 'delete_term', $category_id ) ) {
  3217.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this category.' ) );
  3218.         }
  3219.  
  3220.         $status = wp_delete_term( $category_id, 'category' );
  3221.  
  3222.         if ( true == $status ) {
  3223.             /**
  3224.              * Fires after a category has been successfully deleted via XML-RPC.
  3225.              *
  3226.              * @since 3.4.0
  3227.              *
  3228.              * @param int   $category_id ID of the deleted category.
  3229.              * @param array $args        An array of arguments to delete the category.
  3230.              */
  3231.             do_action( 'xmlrpc_call_success_wp_deleteCategory', $category_id, $args );
  3232.         }
  3233.  
  3234.         return $status;
  3235.     }
  3236.  
  3237.     /**
  3238.      * Retrieve category list.
  3239.      *
  3240.      * @since 2.2.0
  3241.      *
  3242.      * @param array  $args {
  3243.      *     Method arguments. Note: arguments must be ordered as documented.
  3244.      *
  3245.      *     @type int    $blog_id (unused)
  3246.      *     @type string $username
  3247.      *     @type string $password
  3248.      *     @type array  $category
  3249.      *     @type int    $max_results
  3250.      * }
  3251.      * @return array|IXR_Error
  3252.      */
  3253.     public function wp_suggestCategories( $args ) {
  3254.         $this->escape( $args );
  3255.  
  3256.         $username    = $args[1];
  3257.         $password    = $args[2];
  3258.         $category    = $args[3];
  3259.         $max_results = (int) $args[4];
  3260.  
  3261.         if ( !$user = $this->login($username, $password) )
  3262.             return $this->error;
  3263.  
  3264.         if ( !current_user_can( 'edit_posts' ) )
  3265.             return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
  3266.  
  3267.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3268.         do_action( 'xmlrpc_call', 'wp.suggestCategories' );
  3269.  
  3270.         $category_suggestions = array();
  3271.         $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
  3272.         foreach ( (array) get_categories($args) as $cat ) {
  3273.             $category_suggestions[] = array(
  3274.                 'category_id'    => $cat->term_id,
  3275.                 'category_name'    => $cat->name
  3276.             );
  3277.         }
  3278.  
  3279.         return $category_suggestions;
  3280.     }
  3281.  
  3282.     /**
  3283.      * Retrieve comment.
  3284.      *
  3285.      * @since 2.7.0
  3286.      *
  3287.      * @param array  $args {
  3288.      *     Method arguments. Note: arguments must be ordered as documented.
  3289.      *
  3290.      *     @type int    $blog_id (unused)
  3291.      *     @type string $username
  3292.      *     @type string $password
  3293.      *     @type int    $comment_id
  3294.      * }
  3295.      * @return array|IXR_Error
  3296.      */
  3297.     public function wp_getComment($args) {
  3298.         $this->escape($args);
  3299.  
  3300.         $username    = $args[1];
  3301.         $password    = $args[2];
  3302.         $comment_id    = (int) $args[3];
  3303.  
  3304.         if ( ! $user = $this->login( $username, $password ) ) {
  3305.             return $this->error;
  3306.         }
  3307.  
  3308.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3309.         do_action( 'xmlrpc_call', 'wp.getComment' );
  3310.  
  3311.         if ( ! $comment = get_comment( $comment_id ) ) {
  3312.             return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
  3313.         }
  3314.  
  3315.         if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
  3316.             return new IXR_Error( 403, __( 'Sorry, you are not allowed to moderate or edit this comment.' ) );
  3317.         }
  3318.  
  3319.         return $this->_prepare_comment( $comment );
  3320.     }
  3321.  
  3322.     /**
  3323.      * Retrieve comments.
  3324.      *
  3325.      * Besides the common blog_id (unused), username, and password arguments, it takes a filter
  3326.      * array as last argument.
  3327.      *
  3328.      * Accepted 'filter' keys are 'status', 'post_id', 'offset', and 'number'.
  3329.      *
  3330.      * The defaults are as follows:
  3331.      * - 'status' - Default is ''. Filter by status (e.g., 'approve', 'hold')
  3332.      * - 'post_id' - Default is ''. The post where the comment is posted. Empty string shows all comments.
  3333.      * - 'number' - Default is 10. Total number of media items to retrieve.
  3334.      * - 'offset' - Default is 0. See WP_Query::query() for more.
  3335.      *
  3336.      * @since 2.7.0
  3337.      *
  3338.      * @param array  $args {
  3339.      *     Method arguments. Note: arguments must be ordered as documented.
  3340.      *
  3341.      *     @type int    $blog_id (unused)
  3342.      *     @type string $username
  3343.      *     @type string $password
  3344.      *     @type array  $struct
  3345.      * }
  3346.      * @return array|IXR_Error Contains a collection of comments. See wp_xmlrpc_server::wp_getComment() for a description of each item contents
  3347.      */
  3348.     public function wp_getComments( $args ) {
  3349.         $this->escape( $args );
  3350.  
  3351.         $username = $args[1];
  3352.         $password = $args[2];
  3353.         $struct      = isset( $args[3] ) ? $args[3] : array();
  3354.  
  3355.         if ( ! $user = $this->login( $username, $password ) ) {
  3356.             return $this->error;
  3357.         }
  3358.  
  3359.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3360.         do_action( 'xmlrpc_call', 'wp.getComments' );
  3361.  
  3362.         if ( isset( $struct['status'] ) ) {
  3363.             $status = $struct['status'];
  3364.         } else {
  3365.             $status = '';
  3366.         }
  3367.  
  3368.         if ( ! current_user_can( 'moderate_comments' ) && 'approve' !== $status ) {
  3369.             return new IXR_Error( 401, __( 'Invalid comment status.' ) );
  3370.         }
  3371.  
  3372.         $post_id = '';
  3373.         if ( isset( $struct['post_id'] ) ) {
  3374.             $post_id = absint( $struct['post_id'] );
  3375.         }
  3376.  
  3377.         $post_type = '';
  3378.         if ( isset( $struct['post_type'] ) ) {
  3379.             $post_type_object = get_post_type_object( $struct['post_type'] );
  3380.             if ( ! $post_type_object || ! post_type_supports( $post_type_object->name, 'comments' ) ) {
  3381.                 return new IXR_Error( 404, __( 'Invalid post type.' ) );
  3382.             }
  3383.             $post_type = $struct['post_type'];
  3384.         }
  3385.  
  3386.         $offset = 0;
  3387.         if ( isset( $struct['offset'] ) ) {
  3388.             $offset = absint( $struct['offset'] );
  3389.         }
  3390.  
  3391.         $number = 10;
  3392.         if ( isset( $struct['number'] ) ) {
  3393.             $number = absint( $struct['number'] );
  3394.         }
  3395.  
  3396.         $comments = get_comments( array(
  3397.             'status' => $status,
  3398.             'post_id' => $post_id,
  3399.             'offset' => $offset,
  3400.             'number' => $number,
  3401.             'post_type' => $post_type,
  3402.         ) );
  3403.  
  3404.         $comments_struct = array();
  3405.         if ( is_array( $comments ) ) {
  3406.             foreach ( $comments as $comment ) {
  3407.                 $comments_struct[] = $this->_prepare_comment( $comment );
  3408.             }
  3409.         }
  3410.  
  3411.         return $comments_struct;
  3412.     }
  3413.  
  3414.     /**
  3415.      * Delete a comment.
  3416.      *
  3417.      * By default, the comment will be moved to the trash instead of deleted.
  3418.      * See wp_delete_comment() for more information on this behavior.
  3419.      *
  3420.      * @since 2.7.0
  3421.      *
  3422.      * @param array  $args {
  3423.      *     Method arguments. Note: arguments must be ordered as documented.
  3424.      *
  3425.      *     @type int    $blog_id (unused)
  3426.      *     @type string $username
  3427.      *     @type string $password
  3428.      *     @type int    $comment_ID
  3429.      * }
  3430.      * @return bool|IXR_Error See wp_delete_comment().
  3431.      */
  3432.     public function wp_deleteComment( $args ) {
  3433.         $this->escape($args);
  3434.  
  3435.         $username    = $args[1];
  3436.         $password    = $args[2];
  3437.         $comment_ID    = (int) $args[3];
  3438.  
  3439.         if ( ! $user = $this->login( $username, $password ) ) {
  3440.             return $this->error;
  3441.         }
  3442.  
  3443.         if ( ! get_comment( $comment_ID ) ) {
  3444.             return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
  3445.         }
  3446.  
  3447.         if ( ! current_user_can( 'edit_comment', $comment_ID ) ) {
  3448.             return new IXR_Error( 403, __( 'Sorry, you are not allowed to delete this comment.' ) );
  3449.         }
  3450.  
  3451.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3452.         do_action( 'xmlrpc_call', 'wp.deleteComment' );
  3453.  
  3454.         $status = wp_delete_comment( $comment_ID );
  3455.  
  3456.         if ( $status ) {
  3457.             /**
  3458.              * Fires after a comment has been successfully deleted via XML-RPC.
  3459.              *
  3460.              * @since 3.4.0
  3461.              *
  3462.              * @param int   $comment_ID ID of the deleted comment.
  3463.              * @param array $args       An array of arguments to delete the comment.
  3464.              */
  3465.             do_action( 'xmlrpc_call_success_wp_deleteComment', $comment_ID, $args );
  3466.         }
  3467.  
  3468.         return $status;
  3469.     }
  3470.  
  3471.     /**
  3472.      * Edit comment.
  3473.      *
  3474.      * Besides the common blog_id (unused), username, and password arguments, it takes a
  3475.      * comment_id integer and a content_struct array as last argument.
  3476.      *
  3477.      * The allowed keys in the content_struct array are:
  3478.      *  - 'author'
  3479.      *  - 'author_url'
  3480.      *  - 'author_email'
  3481.      *  - 'content'
  3482.      *  - 'date_created_gmt'
  3483.      *  - 'status'. Common statuses are 'approve', 'hold', 'spam'. See get_comment_statuses() for more details
  3484.      *
  3485.      * @since 2.7.0
  3486.      *
  3487.      * @param array  $args {
  3488.      *     Method arguments. Note: arguments must be ordered as documented.
  3489.      *
  3490.      *     @type int    $blog_id (unused)
  3491.      *     @type string $username
  3492.      *     @type string $password
  3493.      *     @type int    $comment_ID
  3494.      *     @type array  $content_struct
  3495.      * }
  3496.      * @return true|IXR_Error True, on success.
  3497.      */
  3498.     public function wp_editComment( $args ) {
  3499.         $this->escape( $args );
  3500.  
  3501.         $username    = $args[1];
  3502.         $password    = $args[2];
  3503.         $comment_ID    = (int) $args[3];
  3504.         $content_struct = $args[4];
  3505.  
  3506.         if ( !$user = $this->login( $username, $password ) ) {
  3507.             return $this->error;
  3508.         }
  3509.  
  3510.         if ( ! get_comment( $comment_ID ) ) {
  3511.             return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
  3512.         }
  3513.  
  3514.         if ( ! current_user_can( 'edit_comment', $comment_ID ) ) {
  3515.             return new IXR_Error( 403, __( 'Sorry, you are not allowed to moderate or edit this comment.' ) );
  3516.         }
  3517.  
  3518.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3519.         do_action( 'xmlrpc_call', 'wp.editComment' );
  3520.  
  3521.         if ( isset($content_struct['status']) ) {
  3522.             $statuses = get_comment_statuses();
  3523.             $statuses = array_keys($statuses);
  3524.  
  3525.             if ( ! in_array($content_struct['status'], $statuses) )
  3526.                 return new IXR_Error( 401, __( 'Invalid comment status.' ) );
  3527.             $comment_approved = $content_struct['status'];
  3528.         }
  3529.  
  3530.         // Do some timestamp voodoo
  3531.         if ( !empty( $content_struct['date_created_gmt'] ) ) {
  3532.             // We know this is supposed to be GMT, so we're going to slap that Z on there by force
  3533.             $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
  3534.             $comment_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
  3535.             $comment_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
  3536.         }
  3537.  
  3538.         if ( isset($content_struct['content']) )
  3539.             $comment_content = $content_struct['content'];
  3540.  
  3541.         if ( isset($content_struct['author']) )
  3542.             $comment_author = $content_struct['author'];
  3543.  
  3544.         if ( isset($content_struct['author_url']) )
  3545.             $comment_author_url = $content_struct['author_url'];
  3546.  
  3547.         if ( isset($content_struct['author_email']) )
  3548.             $comment_author_email = $content_struct['author_email'];
  3549.  
  3550.         // We've got all the data -- post it:
  3551.         $comment = compact('comment_ID', 'comment_content', 'comment_approved', 'comment_date', 'comment_date_gmt', 'comment_author', 'comment_author_email', 'comment_author_url');
  3552.  
  3553.         $result = wp_update_comment($comment);
  3554.         if ( is_wp_error( $result ) )
  3555.             return new IXR_Error(500, $result->get_error_message());
  3556.  
  3557.         if ( !$result )
  3558.             return new IXR_Error(500, __('Sorry, the comment could not be edited.'));
  3559.  
  3560.         /**
  3561.          * Fires after a comment has been successfully updated via XML-RPC.
  3562.          *
  3563.          * @since 3.4.0
  3564.          *
  3565.          * @param int   $comment_ID ID of the updated comment.
  3566.          * @param array $args       An array of arguments to update the comment.
  3567.          */
  3568.         do_action( 'xmlrpc_call_success_wp_editComment', $comment_ID, $args );
  3569.  
  3570.         return true;
  3571.     }
  3572.  
  3573.     /**
  3574.      * Create new comment.
  3575.      *
  3576.      * @since 2.7.0
  3577.      *
  3578.      * @param array  $args {
  3579.      *     Method arguments. Note: arguments must be ordered as documented.
  3580.      *
  3581.      *     @type int        $blog_id (unused)
  3582.      *     @type string     $username
  3583.      *     @type string     $password
  3584.      *     @type string|int $post
  3585.      *     @type array      $content_struct
  3586.      * }
  3587.      * @return int|IXR_Error See wp_new_comment().
  3588.      */
  3589.     public function wp_newComment($args) {
  3590.         $this->escape($args);
  3591.  
  3592.         $username       = $args[1];
  3593.         $password       = $args[2];
  3594.         $post           = $args[3];
  3595.         $content_struct = $args[4];
  3596.  
  3597.         /**
  3598.          * Filters whether to allow anonymous comments over XML-RPC.
  3599.          *
  3600.          * @since 2.7.0
  3601.          *
  3602.          * @param bool $allow Whether to allow anonymous commenting via XML-RPC.
  3603.          *                    Default false.
  3604.          */
  3605.         $allow_anon = apply_filters( 'xmlrpc_allow_anonymous_comments', false );
  3606.  
  3607.         $user = $this->login($username, $password);
  3608.  
  3609.         if ( !$user ) {
  3610.             $logged_in = false;
  3611.             if ( $allow_anon && get_option('comment_registration') ) {
  3612.                 return new IXR_Error( 403, __( 'You must be registered to comment.' ) );
  3613.             } elseif ( ! $allow_anon ) {
  3614.                 return $this->error;
  3615.             }
  3616.         } else {
  3617.             $logged_in = true;
  3618.         }
  3619.  
  3620.         if ( is_numeric($post) )
  3621.             $post_id = absint($post);
  3622.         else
  3623.             $post_id = url_to_postid($post);
  3624.  
  3625.         if ( ! $post_id ) {
  3626.             return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  3627.         }
  3628.  
  3629.         if ( ! get_post( $post_id ) ) {
  3630.             return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  3631.         }
  3632.  
  3633.         if ( ! comments_open( $post_id ) ) {
  3634.             return new IXR_Error( 403, __( 'Sorry, comments are closed for this item.' ) );
  3635.         }
  3636.  
  3637.         if ( empty( $content_struct['content'] ) ) {
  3638.             return new IXR_Error( 403, __( 'Comment is required.' ) );
  3639.         }
  3640.  
  3641.         $comment = array(
  3642.             'comment_post_ID' => $post_id,
  3643.             'comment_content' => $content_struct['content'],
  3644.         );
  3645.  
  3646.         if ( $logged_in ) {
  3647.             $display_name = $user->display_name;
  3648.             $user_email = $user->user_email;
  3649.             $user_url = $user->user_url;
  3650.  
  3651.             $comment['comment_author'] = $this->escape( $display_name );
  3652.             $comment['comment_author_email'] = $this->escape( $user_email );
  3653.             $comment['comment_author_url'] = $this->escape( $user_url );
  3654.             $comment['user_ID'] = $user->ID;
  3655.         } else {
  3656.             $comment['comment_author'] = '';
  3657.             if ( isset($content_struct['author']) )
  3658.                 $comment['comment_author'] = $content_struct['author'];
  3659.  
  3660.             $comment['comment_author_email'] = '';
  3661.             if ( isset($content_struct['author_email']) )
  3662.                 $comment['comment_author_email'] = $content_struct['author_email'];
  3663.  
  3664.             $comment['comment_author_url'] = '';
  3665.             if ( isset($content_struct['author_url']) )
  3666.                 $comment['comment_author_url'] = $content_struct['author_url'];
  3667.  
  3668.             $comment['user_ID'] = 0;
  3669.  
  3670.             if ( get_option('require_name_email') ) {
  3671.                 if ( 6 > strlen($comment['comment_author_email']) || '' == $comment['comment_author'] )
  3672.                     return new IXR_Error( 403, __( 'Comment author name and email are required.' ) );
  3673.                 elseif ( !is_email($comment['comment_author_email']) )
  3674.                     return new IXR_Error( 403, __( 'A valid email address is required.' ) );
  3675.             }
  3676.         }
  3677.  
  3678.         $comment['comment_parent'] = isset($content_struct['comment_parent']) ? absint($content_struct['comment_parent']) : 0;
  3679.  
  3680.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3681.         do_action( 'xmlrpc_call', 'wp.newComment' );
  3682.  
  3683.         $comment_ID = wp_new_comment( $comment, true );
  3684.         if ( is_wp_error( $comment_ID ) ) {
  3685.             return new IXR_Error( 403, $comment_ID->get_error_message() );
  3686.         }
  3687.  
  3688.         if ( ! $comment_ID ) {
  3689.             return new IXR_Error( 403, __( 'An unidentified error has occurred.' ) );
  3690.         }
  3691.  
  3692.         /**
  3693.          * Fires after a new comment has been successfully created via XML-RPC.
  3694.          *
  3695.          * @since 3.4.0
  3696.          *
  3697.          * @param int   $comment_ID ID of the new comment.
  3698.          * @param array $args       An array of new comment arguments.
  3699.          */
  3700.         do_action( 'xmlrpc_call_success_wp_newComment', $comment_ID, $args );
  3701.  
  3702.         return $comment_ID;
  3703.     }
  3704.  
  3705.     /**
  3706.      * Retrieve all of the comment status.
  3707.      *
  3708.      * @since 2.7.0
  3709.      *
  3710.      * @param array  $args {
  3711.      *     Method arguments. Note: arguments must be ordered as documented.
  3712.      *
  3713.      *     @type int    $blog_id (unused)
  3714.      *     @type string $username
  3715.      *     @type string $password
  3716.      * }
  3717.      * @return array|IXR_Error
  3718.      */
  3719.     public function wp_getCommentStatusList( $args ) {
  3720.         $this->escape( $args );
  3721.  
  3722.         $username = $args[1];
  3723.         $password = $args[2];
  3724.  
  3725.         if ( ! $user = $this->login( $username, $password ) ) {
  3726.             return $this->error;
  3727.         }
  3728.  
  3729.         if ( ! current_user_can( 'publish_posts' ) ) {
  3730.             return new IXR_Error( 403, __( 'Sorry, you are not allowed access to details about this site.' ) );
  3731.         }
  3732.  
  3733.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3734.         do_action( 'xmlrpc_call', 'wp.getCommentStatusList' );
  3735.  
  3736.         return get_comment_statuses();
  3737.     }
  3738.  
  3739.     /**
  3740.      * Retrieve comment count.
  3741.      *
  3742.      * @since 2.5.0
  3743.      *
  3744.      * @param array  $args {
  3745.      *     Method arguments. Note: arguments must be ordered as documented.
  3746.      *
  3747.      *     @type int    $blog_id (unused)
  3748.      *     @type string $username
  3749.      *     @type string $password
  3750.      *     @type int    $post_id
  3751.      * }
  3752.      * @return array|IXR_Error
  3753.      */
  3754.     public function wp_getCommentCount( $args ) {
  3755.         $this->escape( $args );
  3756.  
  3757.         $username    = $args[1];
  3758.         $password    = $args[2];
  3759.         $post_id    = (int) $args[3];
  3760.  
  3761.         if ( ! $user = $this->login( $username, $password ) ) {
  3762.             return $this->error;
  3763.         }
  3764.  
  3765.         $post = get_post( $post_id, ARRAY_A );
  3766.         if ( empty( $post['ID'] ) ) {
  3767.             return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  3768.         }
  3769.  
  3770.         if ( ! current_user_can( 'edit_post', $post_id ) ) {
  3771.             return new IXR_Error( 403, __( 'Sorry, you are not allowed access to details of this post.' ) );
  3772.         }
  3773.  
  3774.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3775.         do_action( 'xmlrpc_call', 'wp.getCommentCount' );
  3776.  
  3777.         $count = wp_count_comments( $post_id );
  3778.  
  3779.         return array(
  3780.             'approved' => $count->approved,
  3781.             'awaiting_moderation' => $count->moderated,
  3782.             'spam' => $count->spam,
  3783.             'total_comments' => $count->total_comments
  3784.         );
  3785.     }
  3786.  
  3787.     /**
  3788.      * Retrieve post statuses.
  3789.      *
  3790.      * @since 2.5.0
  3791.      *
  3792.      * @param array  $args {
  3793.      *     Method arguments. Note: arguments must be ordered as documented.
  3794.      *
  3795.      *     @type int    $blog_id (unused)
  3796.      *     @type string $username
  3797.      *     @type string $password
  3798.      * }
  3799.      * @return array|IXR_Error
  3800.      */
  3801.     public function wp_getPostStatusList( $args ) {
  3802.         $this->escape( $args );
  3803.  
  3804.         $username = $args[1];
  3805.         $password = $args[2];
  3806.  
  3807.         if ( !$user = $this->login($username, $password) )
  3808.             return $this->error;
  3809.  
  3810.         if ( !current_user_can( 'edit_posts' ) )
  3811.             return new IXR_Error( 403, __( 'Sorry, you are not allowed access to details about this site.' ) );
  3812.  
  3813.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3814.         do_action( 'xmlrpc_call', 'wp.getPostStatusList' );
  3815.  
  3816.         return get_post_statuses();
  3817.     }
  3818.  
  3819.     /**
  3820.      * Retrieve page statuses.
  3821.      *
  3822.      * @since 2.5.0
  3823.      *
  3824.      * @param array  $args {
  3825.      *     Method arguments. Note: arguments must be ordered as documented.
  3826.      *
  3827.      *     @type int    $blog_id (unused)
  3828.      *     @type string $username
  3829.      *     @type string $password
  3830.      * }
  3831.      * @return array|IXR_Error
  3832.      */
  3833.     public function wp_getPageStatusList( $args ) {
  3834.         $this->escape( $args );
  3835.  
  3836.         $username = $args[1];
  3837.         $password = $args[2];
  3838.  
  3839.         if ( !$user = $this->login($username, $password) )
  3840.             return $this->error;
  3841.  
  3842.         if ( !current_user_can( 'edit_pages' ) )
  3843.             return new IXR_Error( 403, __( 'Sorry, you are not allowed access to details about this site.' ) );
  3844.  
  3845.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3846.         do_action( 'xmlrpc_call', 'wp.getPageStatusList' );
  3847.  
  3848.         return get_page_statuses();
  3849.     }
  3850.  
  3851.     /**
  3852.      * Retrieve page templates.
  3853.      *
  3854.      * @since 2.6.0
  3855.      *
  3856.      * @param array  $args {
  3857.      *     Method arguments. Note: arguments must be ordered as documented.
  3858.      *
  3859.      *     @type int    $blog_id (unused)
  3860.      *     @type string $username
  3861.      *     @type string $password
  3862.      * }
  3863.      * @return array|IXR_Error
  3864.      */
  3865.     public function wp_getPageTemplates( $args ) {
  3866.         $this->escape( $args );
  3867.  
  3868.         $username = $args[1];
  3869.         $password = $args[2];
  3870.  
  3871.         if ( !$user = $this->login($username, $password) )
  3872.             return $this->error;
  3873.  
  3874.         if ( !current_user_can( 'edit_pages' ) )
  3875.             return new IXR_Error( 403, __( 'Sorry, you are not allowed access to details about this site.' ) );
  3876.  
  3877.         $templates = get_page_templates();
  3878.         $templates['Default'] = 'default';
  3879.  
  3880.         return $templates;
  3881.     }
  3882.  
  3883.     /**
  3884.      * Retrieve blog options.
  3885.      *
  3886.      * @since 2.6.0
  3887.      *
  3888.      * @param array  $args {
  3889.      *     Method arguments. Note: arguments must be ordered as documented.
  3890.      *
  3891.      *     @type int    $blog_id (unused)
  3892.      *     @type string $username
  3893.      *     @type string $password
  3894.      *     @type array  $options
  3895.      * }
  3896.      * @return array|IXR_Error
  3897.      */
  3898.     public function wp_getOptions( $args ) {
  3899.         $this->escape( $args );
  3900.  
  3901.         $username    = $args[1];
  3902.         $password    = $args[2];
  3903.         $options    = isset( $args[3] ) ? (array) $args[3] : array();
  3904.  
  3905.         if ( !$user = $this->login($username, $password) )
  3906.             return $this->error;
  3907.  
  3908.         // If no specific options where asked for, return all of them
  3909.         if ( count( $options ) == 0 )
  3910.             $options = array_keys($this->blog_options);
  3911.  
  3912.         return $this->_getOptions($options);
  3913.     }
  3914.  
  3915.     /**
  3916.      * Retrieve blog options value from list.
  3917.      *
  3918.      * @since 2.6.0
  3919.      *
  3920.      * @param array $options Options to retrieve.
  3921.      * @return array
  3922.      */
  3923.     public function _getOptions($options) {
  3924.         $data = array();
  3925.         $can_manage = current_user_can( 'manage_options' );
  3926.         foreach ( $options as $option ) {
  3927.             if ( array_key_exists( $option, $this->blog_options ) ) {
  3928.                 $data[$option] = $this->blog_options[$option];
  3929.                 //Is the value static or dynamic?
  3930.                 if ( isset( $data[$option]['option'] ) ) {
  3931.                     $data[$option]['value'] = get_option( $data[$option]['option'] );
  3932.                     unset($data[$option]['option']);
  3933.                 }
  3934.  
  3935.                 if ( ! $can_manage )
  3936.                     $data[$option]['readonly'] = true;
  3937.             }
  3938.         }
  3939.  
  3940.         return $data;
  3941.     }
  3942.  
  3943.     /**
  3944.      * Update blog options.
  3945.      *
  3946.      * @since 2.6.0
  3947.      *
  3948.      * @param array  $args {
  3949.      *     Method arguments. Note: arguments must be ordered as documented.
  3950.      *
  3951.      *     @type int    $blog_id (unused)
  3952.      *     @type string $username
  3953.      *     @type string $password
  3954.      *     @type array  $options
  3955.      * }
  3956.      * @return array|IXR_Error
  3957.      */
  3958.     public function wp_setOptions( $args ) {
  3959.         $this->escape( $args );
  3960.  
  3961.         $username    = $args[1];
  3962.         $password    = $args[2];
  3963.         $options    = (array) $args[3];
  3964.  
  3965.         if ( !$user = $this->login($username, $password) )
  3966.             return $this->error;
  3967.  
  3968.         if ( !current_user_can( 'manage_options' ) )
  3969.             return new IXR_Error( 403, __( 'Sorry, you are not allowed to update options.' ) );
  3970.  
  3971.         $option_names = array();
  3972.         foreach ( $options as $o_name => $o_value ) {
  3973.             $option_names[] = $o_name;
  3974.             if ( !array_key_exists( $o_name, $this->blog_options ) )
  3975.                 continue;
  3976.  
  3977.             if ( $this->blog_options[$o_name]['readonly'] == true )
  3978.                 continue;
  3979.  
  3980.             update_option( $this->blog_options[$o_name]['option'], wp_unslash( $o_value ) );
  3981.         }
  3982.  
  3983.         //Now return the updated values
  3984.         return $this->_getOptions($option_names);
  3985.     }
  3986.  
  3987.     /**
  3988.      * Retrieve a media item by ID
  3989.      *
  3990.      * @since 3.1.0
  3991.      *
  3992.      * @param array  $args {
  3993.      *     Method arguments. Note: arguments must be ordered as documented.
  3994.      *
  3995.      *     @type int    $blog_id (unused)
  3996.      *     @type string $username
  3997.      *     @type string $password
  3998.      *     @type int    $attachment_id
  3999.      * }
  4000.      * @return array|IXR_Error Associative array contains:
  4001.      *  - 'date_created_gmt'
  4002.      *  - 'parent'
  4003.      *  - 'link'
  4004.      *  - 'thumbnail'
  4005.      *  - 'title'
  4006.      *  - 'caption'
  4007.      *  - 'description'
  4008.      *  - 'metadata'
  4009.      */
  4010.     public function wp_getMediaItem( $args ) {
  4011.         $this->escape( $args );
  4012.  
  4013.         $username        = $args[1];
  4014.         $password        = $args[2];
  4015.         $attachment_id    = (int) $args[3];
  4016.  
  4017.         if ( !$user = $this->login($username, $password) )
  4018.             return $this->error;
  4019.  
  4020.         if ( !current_user_can( 'upload_files' ) )
  4021.             return new IXR_Error( 403, __( 'Sorry, you are not allowed to upload files.' ) );
  4022.  
  4023.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4024.         do_action( 'xmlrpc_call', 'wp.getMediaItem' );
  4025.  
  4026.         if ( ! $attachment = get_post($attachment_id) )
  4027.             return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
  4028.  
  4029.         return $this->_prepare_media_item( $attachment );
  4030.     }
  4031.  
  4032.     /**
  4033.      * Retrieves a collection of media library items (or attachments)
  4034.      *
  4035.      * Besides the common blog_id (unused), username, and password arguments, it takes a filter
  4036.      * array as last argument.
  4037.      *
  4038.      * Accepted 'filter' keys are 'parent_id', 'mime_type', 'offset', and 'number'.
  4039.      *
  4040.      * The defaults are as follows:
  4041.      * - 'number' - Default is 5. Total number of media items to retrieve.
  4042.      * - 'offset' - Default is 0. See WP_Query::query() for more.
  4043.      * - 'parent_id' - Default is ''. The post where the media item is attached. Empty string shows all media items. 0 shows unattached media items.
  4044.      * - 'mime_type' - Default is ''. Filter by mime type (e.g., 'image/jpeg', 'application/pdf')
  4045.      *
  4046.      * @since 3.1.0
  4047.      *
  4048.      * @param array  $args {
  4049.      *     Method arguments. Note: arguments must be ordered as documented.
  4050.      *
  4051.      *     @type int    $blog_id (unused)
  4052.      *     @type string $username
  4053.      *     @type string $password
  4054.      *     @type array  $struct
  4055.      * }
  4056.      * @return array|IXR_Error Contains a collection of media items. See wp_xmlrpc_server::wp_getMediaItem() for a description of each item contents
  4057.      */
  4058.     public function wp_getMediaLibrary($args) {
  4059.         $this->escape($args);
  4060.  
  4061.         $username    = $args[1];
  4062.         $password    = $args[2];
  4063.         $struct        = isset( $args[3] ) ? $args[3] : array() ;
  4064.  
  4065.         if ( !$user = $this->login($username, $password) )
  4066.             return $this->error;
  4067.  
  4068.         if ( !current_user_can( 'upload_files' ) )
  4069.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to upload files.' ) );
  4070.  
  4071.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4072.         do_action( 'xmlrpc_call', 'wp.getMediaLibrary' );
  4073.  
  4074.         $parent_id = ( isset($struct['parent_id']) ) ? absint($struct['parent_id']) : '' ;
  4075.         $mime_type = ( isset($struct['mime_type']) ) ? $struct['mime_type'] : '' ;
  4076.         $offset = ( isset($struct['offset']) ) ? absint($struct['offset']) : 0 ;
  4077.         $number = ( isset($struct['number']) ) ? absint($struct['number']) : -1 ;
  4078.  
  4079.         $attachments = get_posts( array('post_type' => 'attachment', 'post_parent' => $parent_id, 'offset' => $offset, 'numberposts' => $number, 'post_mime_type' => $mime_type ) );
  4080.  
  4081.         $attachments_struct = array();
  4082.  
  4083.         foreach ($attachments as $attachment )
  4084.             $attachments_struct[] = $this->_prepare_media_item( $attachment );
  4085.  
  4086.         return $attachments_struct;
  4087.     }
  4088.  
  4089.     /**
  4090.      * Retrieves a list of post formats used by the site.
  4091.      *
  4092.      * @since 3.1.0
  4093.      *
  4094.      * @param array  $args {
  4095.      *     Method arguments. Note: arguments must be ordered as documented.
  4096.      *
  4097.      *     @type int    $blog_id (unused)
  4098.      *     @type string $username
  4099.      *     @type string $password
  4100.      * }
  4101.      * @return array|IXR_Error List of post formats, otherwise IXR_Error object.
  4102.      */
  4103.     public function wp_getPostFormats( $args ) {
  4104.         $this->escape( $args );
  4105.  
  4106.         $username = $args[1];
  4107.         $password = $args[2];
  4108.  
  4109.         if ( !$user = $this->login( $username, $password ) )
  4110.             return $this->error;
  4111.  
  4112.         if ( !current_user_can( 'edit_posts' ) )
  4113.             return new IXR_Error( 403, __( 'Sorry, you are not allowed access to details about this site.' ) );
  4114.  
  4115.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4116.         do_action( 'xmlrpc_call', 'wp.getPostFormats' );
  4117.  
  4118.         $formats = get_post_format_strings();
  4119.  
  4120.         // find out if they want a list of currently supports formats
  4121.         if ( isset( $args[3] ) && is_array( $args[3] ) ) {
  4122.             if ( $args[3]['show-supported'] ) {
  4123.                 if ( current_theme_supports( 'post-formats' ) ) {
  4124.                     $supported = get_theme_support( 'post-formats' );
  4125.  
  4126.                     $data = array();
  4127.                     $data['all'] = $formats;
  4128.                     $data['supported'] = $supported[0];
  4129.  
  4130.                     $formats = $data;
  4131.                 }
  4132.             }
  4133.         }
  4134.  
  4135.         return $formats;
  4136.     }
  4137.  
  4138.     /**
  4139.      * Retrieves a post type
  4140.      *
  4141.      * @since 3.4.0
  4142.      *
  4143.      * @see get_post_type_object()
  4144.      *
  4145.      * @param array  $args {
  4146.      *     Method arguments. Note: arguments must be ordered as documented.
  4147.      *
  4148.      *     @type int    $blog_id (unused)
  4149.      *     @type string $username
  4150.      *     @type string $password
  4151.      *     @type string $post_type_name
  4152.      *     @type array  $fields (optional)
  4153.      * }
  4154.      * @return array|IXR_Error Array contains:
  4155.      *  - 'labels'
  4156.      *  - 'description'
  4157.      *  - 'capability_type'
  4158.      *  - 'cap'
  4159.      *  - 'map_meta_cap'
  4160.      *  - 'hierarchical'
  4161.      *  - 'menu_position'
  4162.      *  - 'taxonomies'
  4163.      *  - 'supports'
  4164.      */
  4165.     public function wp_getPostType( $args ) {
  4166.         if ( ! $this->minimum_args( $args, 4 ) )
  4167.             return $this->error;
  4168.  
  4169.         $this->escape( $args );
  4170.  
  4171.         $username       = $args[1];
  4172.         $password       = $args[2];
  4173.         $post_type_name = $args[3];
  4174.  
  4175.         if ( isset( $args[4] ) ) {
  4176.             $fields = $args[4];
  4177.         } else {
  4178.             /**
  4179.              * Filters the default query fields used by the given XML-RPC method.
  4180.              *
  4181.              * @since 3.4.0
  4182.              *
  4183.              * @param array  $fields An array of post type query fields for the given method.
  4184.              * @param string $method The method name.
  4185.              */
  4186.             $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostType' );
  4187.         }
  4188.  
  4189.         if ( !$user = $this->login( $username, $password ) )
  4190.             return $this->error;
  4191.  
  4192.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4193.         do_action( 'xmlrpc_call', 'wp.getPostType' );
  4194.  
  4195.         if ( ! post_type_exists( $post_type_name ) )
  4196.             return new IXR_Error( 403, __( 'Invalid post type.' ) );
  4197.  
  4198.         $post_type = get_post_type_object( $post_type_name );
  4199.  
  4200.         if ( ! current_user_can( $post_type->cap->edit_posts ) )
  4201.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type.' ) );
  4202.  
  4203.         return $this->_prepare_post_type( $post_type, $fields );
  4204.     }
  4205.  
  4206.     /**
  4207.      * Retrieves a post types
  4208.      *
  4209.      * @since 3.4.0
  4210.      *
  4211.      * @see get_post_types()
  4212.      *
  4213.      * @param array  $args {
  4214.      *     Method arguments. Note: arguments must be ordered as documented.
  4215.      *
  4216.      *     @type int    $blog_id (unused)
  4217.      *     @type string $username
  4218.      *     @type string $password
  4219.      *     @type array  $filter (optional)
  4220.      *     @type array  $fields (optional)
  4221.      * }
  4222.      * @return array|IXR_Error
  4223.      */
  4224.     public function wp_getPostTypes( $args ) {
  4225.         if ( ! $this->minimum_args( $args, 3 ) )
  4226.             return $this->error;
  4227.  
  4228.         $this->escape( $args );
  4229.  
  4230.         $username = $args[1];
  4231.         $password = $args[2];
  4232.         $filter   = isset( $args[3] ) ? $args[3] : array( 'public' => true );
  4233.  
  4234.         if ( isset( $args[4] ) ) {
  4235.             $fields = $args[4];
  4236.         } else {
  4237.             /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4238.             $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostTypes' );
  4239.         }
  4240.  
  4241.         if ( ! $user = $this->login( $username, $password ) )
  4242.             return $this->error;
  4243.  
  4244.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4245.         do_action( 'xmlrpc_call', 'wp.getPostTypes' );
  4246.  
  4247.         $post_types = get_post_types( $filter, 'objects' );
  4248.  
  4249.         $struct = array();
  4250.  
  4251.         foreach ( $post_types as $post_type ) {
  4252.             if ( ! current_user_can( $post_type->cap->edit_posts ) )
  4253.                 continue;
  4254.  
  4255.             $struct[$post_type->name] = $this->_prepare_post_type( $post_type, $fields );
  4256.         }
  4257.  
  4258.         return $struct;
  4259.     }
  4260.  
  4261.     /**
  4262.      * Retrieve revisions for a specific post.
  4263.      *
  4264.      * @since 3.5.0
  4265.      *
  4266.      * The optional $fields parameter specifies what fields will be included
  4267.      * in the response array.
  4268.      *
  4269.      * @uses wp_get_post_revisions()
  4270.      * @see wp_getPost() for more on $fields
  4271.      *
  4272.      * @param array  $args {
  4273.      *     Method arguments. Note: arguments must be ordered as documented.
  4274.      *
  4275.      *     @type int    $blog_id (unused)
  4276.      *     @type string $username
  4277.      *     @type string $password
  4278.      *     @type int    $post_id
  4279.      *     @type array  $fields (optional)
  4280.      * }
  4281.      * @return array|IXR_Error contains a collection of posts.
  4282.      */
  4283.     public function wp_getRevisions( $args ) {
  4284.         if ( ! $this->minimum_args( $args, 4 ) )
  4285.             return $this->error;
  4286.  
  4287.         $this->escape( $args );
  4288.  
  4289.         $username = $args[1];
  4290.         $password = $args[2];
  4291.         $post_id  = (int) $args[3];
  4292.  
  4293.         if ( isset( $args[4] ) ) {
  4294.             $fields = $args[4];
  4295.         } else {
  4296.             /**
  4297.              * Filters the default revision query fields used by the given XML-RPC method.
  4298.              *
  4299.              * @since 3.5.0
  4300.              *
  4301.              * @param array  $field  An array of revision query fields.
  4302.              * @param string $method The method name.
  4303.              */
  4304.             $fields = apply_filters( 'xmlrpc_default_revision_fields', array( 'post_date', 'post_date_gmt' ), 'wp.getRevisions' );
  4305.         }
  4306.  
  4307.         if ( ! $user = $this->login( $username, $password ) )
  4308.             return $this->error;
  4309.  
  4310.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4311.         do_action( 'xmlrpc_call', 'wp.getRevisions' );
  4312.  
  4313.         if ( ! $post = get_post( $post_id ) )
  4314.             return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  4315.  
  4316.         if ( ! current_user_can( 'edit_post', $post_id ) )
  4317.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts.' ) );
  4318.  
  4319.         // Check if revisions are enabled.
  4320.         if ( ! wp_revisions_enabled( $post ) )
  4321.             return new IXR_Error( 401, __( 'Sorry, revisions are disabled.' ) );
  4322.  
  4323.         $revisions = wp_get_post_revisions( $post_id );
  4324.  
  4325.         if ( ! $revisions )
  4326.             return array();
  4327.  
  4328.         $struct = array();
  4329.  
  4330.         foreach ( $revisions as $revision ) {
  4331.             if ( ! current_user_can( 'read_post', $revision->ID ) )
  4332.                 continue;
  4333.  
  4334.             // Skip autosaves
  4335.             if ( wp_is_post_autosave( $revision ) )
  4336.                 continue;
  4337.  
  4338.             $struct[] = $this->_prepare_post( get_object_vars( $revision ), $fields );
  4339.         }
  4340.  
  4341.         return $struct;
  4342.     }
  4343.  
  4344.     /**
  4345.      * Restore a post revision
  4346.      *
  4347.      * @since 3.5.0
  4348.      *
  4349.      * @uses wp_restore_post_revision()
  4350.      *
  4351.      * @param array  $args {
  4352.      *     Method arguments. Note: arguments must be ordered as documented.
  4353.      *
  4354.      *     @type int    $blog_id (unused)
  4355.      *     @type string $username
  4356.      *     @type string $password
  4357.      *     @type int    $revision_id
  4358.      * }
  4359.      * @return bool|IXR_Error false if there was an error restoring, true if success.
  4360.      */
  4361.     public function wp_restoreRevision( $args ) {
  4362.         if ( ! $this->minimum_args( $args, 3 ) )
  4363.             return $this->error;
  4364.  
  4365.         $this->escape( $args );
  4366.  
  4367.         $username    = $args[1];
  4368.         $password    = $args[2];
  4369.         $revision_id = (int) $args[3];
  4370.  
  4371.         if ( ! $user = $this->login( $username, $password ) )
  4372.             return $this->error;
  4373.  
  4374.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4375.         do_action( 'xmlrpc_call', 'wp.restoreRevision' );
  4376.  
  4377.         if ( ! $revision = wp_get_post_revision( $revision_id ) )
  4378.             return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  4379.  
  4380.         if ( wp_is_post_autosave( $revision ) )
  4381.             return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  4382.  
  4383.         if ( ! $post = get_post( $revision->post_parent ) )
  4384.             return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  4385.  
  4386.         if ( ! current_user_can( 'edit_post', $revision->post_parent ) )
  4387.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  4388.  
  4389.         // Check if revisions are disabled.
  4390.         if ( ! wp_revisions_enabled( $post ) )
  4391.             return new IXR_Error( 401, __( 'Sorry, revisions are disabled.' ) );
  4392.  
  4393.         $post = wp_restore_post_revision( $revision_id );
  4394.  
  4395.         return (bool) $post;
  4396.     }
  4397.  
  4398.     /* Blogger API functions.
  4399.      * specs on http://plant.blogger.com/api and https://groups.yahoo.com/group/bloggerDev/
  4400.      */
  4401.  
  4402.     /**
  4403.      * Retrieve blogs that user owns.
  4404.      *
  4405.      * Will make more sense once we support multiple blogs.
  4406.      *
  4407.      * @since 1.5.0
  4408.      *
  4409.      * @param array  $args {
  4410.      *     Method arguments. Note: arguments must be ordered as documented.
  4411.      *
  4412.      *     @type int    $blog_id (unused)
  4413.      *     @type string $username
  4414.      *     @type string $password
  4415.      * }
  4416.      * @return array|IXR_Error
  4417.      */
  4418.     public function blogger_getUsersBlogs($args) {
  4419.         if ( ! $this->minimum_args( $args, 3 ) ) {
  4420.             return $this->error;
  4421.         }
  4422.  
  4423.         if ( is_multisite() ) {
  4424.             return $this->_multisite_getUsersBlogs($args);
  4425.         }
  4426.  
  4427.         $this->escape($args);
  4428.  
  4429.         $username = $args[1];
  4430.         $password = $args[2];
  4431.  
  4432.         if ( !$user = $this->login($username, $password) )
  4433.             return $this->error;
  4434.  
  4435.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4436.         do_action( 'xmlrpc_call', 'blogger.getUsersBlogs' );
  4437.  
  4438.         $is_admin = current_user_can('manage_options');
  4439.  
  4440.         $struct = array(
  4441.             'isAdmin'  => $is_admin,
  4442.             'url'      => get_option('home') . '/',
  4443.             'blogid'   => '1',
  4444.             'blogName' => get_option('blogname'),
  4445.             'xmlrpc'   => site_url( 'xmlrpc.php', 'rpc' ),
  4446.         );
  4447.  
  4448.         return array($struct);
  4449.     }
  4450.  
  4451.     /**
  4452.      * Private function for retrieving a users blogs for multisite setups
  4453.      *
  4454.      * @since 3.0.0
  4455.      *
  4456.      * @param array $args {
  4457.      *     Method arguments. Note: arguments must be ordered as documented.
  4458.      *
  4459.      *     @type string $username Username.
  4460.      *     @type string $password Password.
  4461.      * }
  4462.      * @return array|IXR_Error
  4463.      */
  4464.     protected function _multisite_getUsersBlogs( $args ) {
  4465.         $current_blog = get_site();
  4466.  
  4467.         $domain = $current_blog->domain;
  4468.         $path = $current_blog->path . 'xmlrpc.php';
  4469.  
  4470.         $rpc = new IXR_Client( set_url_scheme( "http://{$domain}{$path}" ) );
  4471.         $rpc->query('wp.getUsersBlogs', $args[1], $args[2]);
  4472.         $blogs = $rpc->getResponse();
  4473.  
  4474.         if ( isset($blogs['faultCode']) )
  4475.             return new IXR_Error($blogs['faultCode'], $blogs['faultString']);
  4476.  
  4477.         if ( $_SERVER['HTTP_HOST'] == $domain && $_SERVER['REQUEST_URI'] == $path ) {
  4478.             return $blogs;
  4479.         } else {
  4480.             foreach ( (array) $blogs as $blog ) {
  4481.                 if ( strpos($blog['url'], $_SERVER['HTTP_HOST']) )
  4482.                     return array($blog);
  4483.             }
  4484.             return array();
  4485.         }
  4486.     }
  4487.  
  4488.     /**
  4489.      * Retrieve user's data.
  4490.      *
  4491.      * Gives your client some info about you, so you don't have to.
  4492.      *
  4493.      * @since 1.5.0
  4494.      *
  4495.      * @param array  $args {
  4496.      *     Method arguments. Note: arguments must be ordered as documented.
  4497.      *
  4498.      *     @type int    $blog_id (unused)
  4499.      *     @type string $username
  4500.      *     @type string $password
  4501.      * }
  4502.      * @return array|IXR_Error
  4503.      */
  4504.     public function blogger_getUserInfo( $args ) {
  4505.         $this->escape( $args );
  4506.  
  4507.         $username = $args[1];
  4508.         $password = $args[2];
  4509.  
  4510.         if ( !$user = $this->login($username, $password) )
  4511.             return $this->error;
  4512.  
  4513.         if ( !current_user_can( 'edit_posts' ) )
  4514.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to access user data on this site.' ) );
  4515.  
  4516.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4517.         do_action( 'xmlrpc_call', 'blogger.getUserInfo' );
  4518.  
  4519.         $struct = array(
  4520.             'nickname'  => $user->nickname,
  4521.             'userid'    => $user->ID,
  4522.             'url'       => $user->user_url,
  4523.             'lastname'  => $user->last_name,
  4524.             'firstname' => $user->first_name
  4525.         );
  4526.  
  4527.         return $struct;
  4528.     }
  4529.  
  4530.     /**
  4531.      * Retrieve post.
  4532.      *
  4533.      * @since 1.5.0
  4534.      *
  4535.      * @param array  $args {
  4536.      *     Method arguments. Note: arguments must be ordered as documented.
  4537.      *
  4538.      *     @type int    $blog_id (unused)
  4539.      *     @type int    $post_ID
  4540.      *     @type string $username
  4541.      *     @type string $password
  4542.      * }
  4543.      * @return array|IXR_Error
  4544.      */
  4545.     public function blogger_getPost( $args ) {
  4546.         $this->escape( $args );
  4547.  
  4548.         $post_ID  = (int) $args[1];
  4549.         $username = $args[2];
  4550.         $password = $args[3];
  4551.  
  4552.         if ( !$user = $this->login($username, $password) )
  4553.             return $this->error;
  4554.  
  4555.         $post_data = get_post($post_ID, ARRAY_A);
  4556.         if ( ! $post_data )
  4557.             return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  4558.  
  4559.         if ( !current_user_can( 'edit_post', $post_ID ) )
  4560.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  4561.  
  4562.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4563.         do_action( 'xmlrpc_call', 'blogger.getPost' );
  4564.  
  4565.         $categories = implode(',', wp_get_post_categories($post_ID));
  4566.  
  4567.         $content  = '<title>'.wp_unslash($post_data['post_title']).'</title>';
  4568.         $content .= '<category>'.$categories.'</category>';
  4569.         $content .= wp_unslash($post_data['post_content']);
  4570.  
  4571.         $struct = array(
  4572.             'userid'    => $post_data['post_author'],
  4573.             'dateCreated' => $this->_convert_date( $post_data['post_date'] ),
  4574.             'content'     => $content,
  4575.             'postid'  => (string) $post_data['ID']
  4576.         );
  4577.  
  4578.         return $struct;
  4579.     }
  4580.  
  4581.     /**
  4582.      * Retrieve list of recent posts.
  4583.      *
  4584.      * @since 1.5.0
  4585.      *
  4586.      * @param array  $args {
  4587.      *     Method arguments. Note: arguments must be ordered as documented.
  4588.      *
  4589.      *     @type string $appkey (unused)
  4590.      *     @type int    $blog_id (unused)
  4591.      *     @type string $username
  4592.      *     @type string $password
  4593.      *     @type int    $numberposts (optional)
  4594.      * }
  4595.      * @return array|IXR_Error
  4596.      */
  4597.     public function blogger_getRecentPosts( $args ) {
  4598.  
  4599.         $this->escape($args);
  4600.  
  4601.         // $args[0] = appkey - ignored
  4602.         $username = $args[2];
  4603.         $password = $args[3];
  4604.         if ( isset( $args[4] ) )
  4605.             $query = array( 'numberposts' => absint( $args[4] ) );
  4606.         else
  4607.             $query = array();
  4608.  
  4609.         if ( !$user = $this->login($username, $password) )
  4610.             return $this->error;
  4611.  
  4612.         if ( ! current_user_can( 'edit_posts' ) )
  4613.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts.' ) );
  4614.  
  4615.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4616.         do_action( 'xmlrpc_call', 'blogger.getRecentPosts' );
  4617.  
  4618.         $posts_list = wp_get_recent_posts( $query );
  4619.  
  4620.         if ( !$posts_list ) {
  4621.             $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
  4622.             return $this->error;
  4623.         }
  4624.  
  4625.         $recent_posts = array();
  4626.         foreach ($posts_list as $entry) {
  4627.             if ( !current_user_can( 'edit_post', $entry['ID'] ) )
  4628.                 continue;
  4629.  
  4630.             $post_date  = $this->_convert_date( $entry['post_date'] );
  4631.             $categories = implode(',', wp_get_post_categories($entry['ID']));
  4632.  
  4633.             $content  = '<title>'.wp_unslash($entry['post_title']).'</title>';
  4634.             $content .= '<category>'.$categories.'</category>';
  4635.             $content .= wp_unslash($entry['post_content']);
  4636.  
  4637.             $recent_posts[] = array(
  4638.                 'userid' => $entry['post_author'],
  4639.                 'dateCreated' => $post_date,
  4640.                 'content' => $content,
  4641.                 'postid' => (string) $entry['ID'],
  4642.             );
  4643.         }
  4644.  
  4645.         return $recent_posts;
  4646.     }
  4647.  
  4648.     /**
  4649.      * Deprecated.
  4650.      *
  4651.      * @since 1.5.0
  4652.      * @deprecated 3.5.0
  4653.      *
  4654.      * @param array $args Unused.
  4655.      * @return IXR_Error Error object.
  4656.      */
  4657.     public function blogger_getTemplate($args) {
  4658.         return new IXR_Error( 403, __('Sorry, that file cannot be edited.' ) );
  4659.     }
  4660.  
  4661.     /**
  4662.      * Deprecated.
  4663.      *
  4664.      * @since 1.5.0
  4665.      * @deprecated 3.5.0
  4666.      *
  4667.      * @param array $args Unused.
  4668.      * @return IXR_Error Error object.
  4669.      */
  4670.     public function blogger_setTemplate($args) {
  4671.         return new IXR_Error( 403, __('Sorry, that file cannot be edited.' ) );
  4672.     }
  4673.  
  4674.     /**
  4675.      * Creates new post.
  4676.      *
  4677.      * @since 1.5.0
  4678.      *
  4679.      * @param array $args {
  4680.      *     Method arguments. Note: arguments must be ordered as documented.
  4681.      *
  4682.      *     @type string $appkey (unused)
  4683.      *     @type int    $blog_id (unused)
  4684.      *     @type string $username
  4685.      *     @type string $password
  4686.      *     @type string $content
  4687.      *     @type string $publish
  4688.      * }
  4689.      * @return int|IXR_Error
  4690.      */
  4691.     public function blogger_newPost( $args ) {
  4692.         $this->escape( $args );
  4693.  
  4694.         $username = $args[2];
  4695.         $password = $args[3];
  4696.         $content  = $args[4];
  4697.         $publish  = $args[5];
  4698.  
  4699.         if ( !$user = $this->login($username, $password) )
  4700.             return $this->error;
  4701.  
  4702.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4703.         do_action( 'xmlrpc_call', 'blogger.newPost' );
  4704.  
  4705.         $cap = ($publish) ? 'publish_posts' : 'edit_posts';
  4706.         if ( ! current_user_can( get_post_type_object( 'post' )->cap->create_posts ) || !current_user_can($cap) )
  4707.             return new IXR_Error(401, __('Sorry, you are not allowed to post on this site.'));
  4708.  
  4709.         $post_status = ($publish) ? 'publish' : 'draft';
  4710.  
  4711.         $post_author = $user->ID;
  4712.  
  4713.         $post_title = xmlrpc_getposttitle($content);
  4714.         $post_category = xmlrpc_getpostcategory($content);
  4715.         $post_content = xmlrpc_removepostdata($content);
  4716.  
  4717.         $post_date = current_time('mysql');
  4718.         $post_date_gmt = current_time('mysql', 1);
  4719.  
  4720.         $post_data = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status');
  4721.  
  4722.         $post_ID = wp_insert_post($post_data);
  4723.         if ( is_wp_error( $post_ID ) )
  4724.             return new IXR_Error(500, $post_ID->get_error_message());
  4725.  
  4726.         if ( !$post_ID )
  4727.             return new IXR_Error(500, __('Sorry, your entry could not be posted.'));
  4728.  
  4729.         $this->attach_uploads( $post_ID, $post_content );
  4730.  
  4731.         /**
  4732.          * Fires after a new post has been successfully created via the XML-RPC Blogger API.
  4733.          *
  4734.          * @since 3.4.0
  4735.          *
  4736.          * @param int   $post_ID ID of the new post.
  4737.          * @param array $args    An array of new post arguments.
  4738.          */
  4739.         do_action( 'xmlrpc_call_success_blogger_newPost', $post_ID, $args );
  4740.  
  4741.         return $post_ID;
  4742.     }
  4743.  
  4744.     /**
  4745.      * Edit a post.
  4746.      *
  4747.      * @since 1.5.0
  4748.      *
  4749.      * @param array  $args {
  4750.      *     Method arguments. Note: arguments must be ordered as documented.
  4751.      *
  4752.      *     @type int    $blog_id (unused)
  4753.      *     @type int    $post_ID
  4754.      *     @type string $username
  4755.      *     @type string $password
  4756.      *     @type string $content
  4757.      *     @type bool   $publish
  4758.      * }
  4759.      * @return true|IXR_Error true when done.
  4760.      */
  4761.     public function blogger_editPost( $args ) {
  4762.  
  4763.         $this->escape($args);
  4764.  
  4765.         $post_ID  = (int) $args[1];
  4766.         $username = $args[2];
  4767.         $password = $args[3];
  4768.         $content  = $args[4];
  4769.         $publish  = $args[5];
  4770.  
  4771.         if ( ! $user = $this->login( $username, $password ) ) {
  4772.             return $this->error;
  4773.         }
  4774.  
  4775.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4776.         do_action( 'xmlrpc_call', 'blogger.editPost' );
  4777.  
  4778.         $actual_post = get_post( $post_ID, ARRAY_A );
  4779.  
  4780.         if ( ! $actual_post || $actual_post['post_type'] != 'post' ) {
  4781.             return new IXR_Error( 404, __( 'Sorry, no such post.' ) );
  4782.         }
  4783.  
  4784.         $this->escape($actual_post);
  4785.  
  4786.         if ( ! current_user_can( 'edit_post', $post_ID ) ) {
  4787.             return new IXR_Error(401, __('Sorry, you are not allowed to edit this post.'));
  4788.         }
  4789.         if ( 'publish' == $actual_post['post_status'] && ! current_user_can( 'publish_posts' ) ) {
  4790.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish this post.' ) );
  4791.         }
  4792.  
  4793.         $postdata = array();
  4794.         $postdata['ID'] = $actual_post['ID'];
  4795.         $postdata['post_content'] = xmlrpc_removepostdata( $content );
  4796.         $postdata['post_title'] = xmlrpc_getposttitle( $content );
  4797.         $postdata['post_category'] = xmlrpc_getpostcategory( $content );
  4798.         $postdata['post_status'] = $actual_post['post_status'];
  4799.         $postdata['post_excerpt'] = $actual_post['post_excerpt'];
  4800.         $postdata['post_status'] = $publish ? 'publish' : 'draft';
  4801.  
  4802.         $result = wp_update_post( $postdata );
  4803.  
  4804.         if ( ! $result ) {
  4805.             return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be edited.'));
  4806.         }
  4807.         $this->attach_uploads( $actual_post['ID'], $postdata['post_content'] );
  4808.  
  4809.         /**
  4810.          * Fires after a post has been successfully updated via the XML-RPC Blogger API.
  4811.          *
  4812.          * @since 3.4.0
  4813.          *
  4814.          * @param int   $post_ID ID of the updated post.
  4815.          * @param array $args    An array of arguments for the post to edit.
  4816.          */
  4817.         do_action( 'xmlrpc_call_success_blogger_editPost', $post_ID, $args );
  4818.  
  4819.         return true;
  4820.     }
  4821.  
  4822.     /**
  4823.      * Remove a post.
  4824.      *
  4825.      * @since 1.5.0
  4826.      *
  4827.      * @param array  $args {
  4828.      *     Method arguments. Note: arguments must be ordered as documented.
  4829.      *
  4830.      *     @type int    $blog_id (unused)
  4831.      *     @type int    $post_ID
  4832.      *     @type string $username
  4833.      *     @type string $password
  4834.      * }
  4835.      * @return true|IXR_Error True when post is deleted.
  4836.      */
  4837.     public function blogger_deletePost( $args ) {
  4838.         $this->escape( $args );
  4839.  
  4840.         $post_ID  = (int) $args[1];
  4841.         $username = $args[2];
  4842.         $password = $args[3];
  4843.  
  4844.         if ( !$user = $this->login($username, $password) )
  4845.             return $this->error;
  4846.  
  4847.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4848.         do_action( 'xmlrpc_call', 'blogger.deletePost' );
  4849.  
  4850.         $actual_post = get_post( $post_ID, ARRAY_A );
  4851.  
  4852.         if ( ! $actual_post || $actual_post['post_type'] != 'post' ) {
  4853.             return new IXR_Error( 404, __( 'Sorry, no such post.' ) );
  4854.         }
  4855.  
  4856.         if ( ! current_user_can( 'delete_post', $post_ID ) ) {
  4857.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
  4858.         }
  4859.  
  4860.         $result = wp_delete_post( $post_ID );
  4861.  
  4862.         if ( ! $result ) {
  4863.             return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
  4864.         }
  4865.  
  4866.         /**
  4867.          * Fires after a post has been successfully deleted via the XML-RPC Blogger API.
  4868.          *
  4869.          * @since 3.4.0
  4870.          *
  4871.          * @param int   $post_ID ID of the deleted post.
  4872.          * @param array $args    An array of arguments to delete the post.
  4873.          */
  4874.         do_action( 'xmlrpc_call_success_blogger_deletePost', $post_ID, $args );
  4875.  
  4876.         return true;
  4877.     }
  4878.  
  4879.     /* MetaWeblog API functions
  4880.      * specs on wherever Dave Winer wants them to be
  4881.      */
  4882.  
  4883.     /**
  4884.      * Create a new post.
  4885.      *
  4886.      * The 'content_struct' argument must contain:
  4887.      *  - title
  4888.      *  - description
  4889.      *  - mt_excerpt
  4890.      *  - mt_text_more
  4891.      *  - mt_keywords
  4892.      *  - mt_tb_ping_urls
  4893.      *  - categories
  4894.      *
  4895.      * Also, it can optionally contain:
  4896.      *  - wp_slug
  4897.      *  - wp_password
  4898.      *  - wp_page_parent_id
  4899.      *  - wp_page_order
  4900.      *  - wp_author_id
  4901.      *  - post_status | page_status - can be 'draft', 'private', 'publish', or 'pending'
  4902.      *  - mt_allow_comments - can be 'open' or 'closed'
  4903.      *  - mt_allow_pings - can be 'open' or 'closed'
  4904.      *  - date_created_gmt
  4905.      *  - dateCreated
  4906.      *  - wp_post_thumbnail
  4907.      *
  4908.      * @since 1.5.0
  4909.      *
  4910.      * @param array  $args {
  4911.      *     Method arguments. Note: arguments must be ordered as documented.
  4912.      *
  4913.      *     @type int    $blog_id (unused)
  4914.      *     @type string $username
  4915.      *     @type string $password
  4916.      *     @type array  $content_struct
  4917.      *     @type int    $publish
  4918.      * }
  4919.      * @return int|IXR_Error
  4920.      */
  4921.     public function mw_newPost($args) {
  4922.         $this->escape($args);
  4923.  
  4924.         $username       = $args[1];
  4925.         $password       = $args[2];
  4926.         $content_struct = $args[3];
  4927.         $publish        = isset( $args[4] ) ? $args[4] : 0;
  4928.  
  4929.         if ( !$user = $this->login($username, $password) )
  4930.             return $this->error;
  4931.  
  4932.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4933.         do_action( 'xmlrpc_call', 'metaWeblog.newPost' );
  4934.  
  4935.         $page_template = '';
  4936.         if ( !empty( $content_struct['post_type'] ) ) {
  4937.             if ( $content_struct['post_type'] == 'page' ) {
  4938.                 if ( $publish )
  4939.                     $cap  = 'publish_pages';
  4940.                 elseif ( isset( $content_struct['page_status'] ) && 'publish' == $content_struct['page_status'] )
  4941.                     $cap  = 'publish_pages';
  4942.                 else
  4943.                     $cap = 'edit_pages';
  4944.                 $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
  4945.                 $post_type = 'page';
  4946.                 if ( !empty( $content_struct['wp_page_template'] ) )
  4947.                     $page_template = $content_struct['wp_page_template'];
  4948.             } elseif ( $content_struct['post_type'] == 'post' ) {
  4949.                 if ( $publish )
  4950.                     $cap  = 'publish_posts';
  4951.                 elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'] )
  4952.                     $cap  = 'publish_posts';
  4953.                 else
  4954.                     $cap = 'edit_posts';
  4955.                 $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
  4956.                 $post_type = 'post';
  4957.             } else {
  4958.                 // No other post_type values are allowed here
  4959.                 return new IXR_Error( 401, __( 'Invalid post type.' ) );
  4960.             }
  4961.         } else {
  4962.             if ( $publish )
  4963.                 $cap  = 'publish_posts';
  4964.             elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'])
  4965.                 $cap  = 'publish_posts';
  4966.             else
  4967.                 $cap = 'edit_posts';
  4968.             $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
  4969.             $post_type = 'post';
  4970.         }
  4971.  
  4972.         if ( ! current_user_can( get_post_type_object( $post_type )->cap->create_posts ) )
  4973.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts on this site.' ) );
  4974.         if ( !current_user_can( $cap ) )
  4975.             return new IXR_Error( 401, $error_message );
  4976.  
  4977.         // Check for a valid post format if one was given
  4978.         if ( isset( $content_struct['wp_post_format'] ) ) {
  4979.             $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
  4980.             if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
  4981.                 return new IXR_Error( 404, __( 'Invalid post format.' ) );
  4982.             }
  4983.         }
  4984.  
  4985.         // Let WordPress generate the post_name (slug) unless
  4986.         // one has been provided.
  4987.         $post_name = "";
  4988.         if ( isset($content_struct['wp_slug']) )
  4989.             $post_name = $content_struct['wp_slug'];
  4990.  
  4991.         // Only use a password if one was given.
  4992.         if ( isset($content_struct['wp_password']) )
  4993.             $post_password = $content_struct['wp_password'];
  4994.  
  4995.         // Only set a post parent if one was provided.
  4996.         if ( isset($content_struct['wp_page_parent_id']) )
  4997.             $post_parent = $content_struct['wp_page_parent_id'];
  4998.  
  4999.         // Only set the menu_order if it was provided.
  5000.         if ( isset($content_struct['wp_page_order']) )
  5001.             $menu_order = $content_struct['wp_page_order'];
  5002.  
  5003.         $post_author = $user->ID;
  5004.  
  5005.         // If an author id was provided then use it instead.
  5006.         if ( isset( $content_struct['wp_author_id'] ) && ( $user->ID != $content_struct['wp_author_id'] ) ) {
  5007.             switch ( $post_type ) {
  5008.                 case "post":
  5009.                     if ( !current_user_can( 'edit_others_posts' ) )
  5010.                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to create posts as this user.' ) );
  5011.                     break;
  5012.                 case "page":
  5013.                     if ( !current_user_can( 'edit_others_pages' ) )
  5014.                         return new IXR_Error( 401, __( 'Sorry, you are not allowed to create pages as this user.' ) );
  5015.                     break;
  5016.                 default:
  5017.                     return new IXR_Error( 401, __( 'Invalid post type.' ) );
  5018.             }
  5019.             $author = get_userdata( $content_struct['wp_author_id'] );
  5020.             if ( ! $author )
  5021.                 return new IXR_Error( 404, __( 'Invalid author ID.' ) );
  5022.             $post_author = $content_struct['wp_author_id'];
  5023.         }
  5024.  
  5025.         $post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : null;
  5026.         $post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : null;
  5027.  
  5028.         $post_status = $publish ? 'publish' : 'draft';
  5029.  
  5030.         if ( isset( $content_struct["{$post_type}_status"] ) ) {
  5031.             switch ( $content_struct["{$post_type}_status"] ) {
  5032.                 case 'draft':
  5033.                 case 'pending':
  5034.                 case 'private':
  5035.                 case 'publish':
  5036.                     $post_status = $content_struct["{$post_type}_status"];
  5037.                     break;
  5038.                 default:
  5039.                     $post_status = $publish ? 'publish' : 'draft';
  5040.                     break;
  5041.             }
  5042.         }
  5043.  
  5044.         $post_excerpt = isset($content_struct['mt_excerpt']) ? $content_struct['mt_excerpt'] : null;
  5045.         $post_more = isset($content_struct['mt_text_more']) ? $content_struct['mt_text_more'] : null;
  5046.  
  5047.         $tags_input = isset($content_struct['mt_keywords']) ? $content_struct['mt_keywords'] : null;
  5048.  
  5049.         if ( isset($content_struct['mt_allow_comments']) ) {
  5050.             if ( !is_numeric($content_struct['mt_allow_comments']) ) {
  5051.                 switch ( $content_struct['mt_allow_comments'] ) {
  5052.                     case 'closed':
  5053.                         $comment_status = 'closed';
  5054.                         break;
  5055.                     case 'open':
  5056.                         $comment_status = 'open';
  5057.                         break;
  5058.                     default:
  5059.                         $comment_status = get_default_comment_status( $post_type );
  5060.                         break;
  5061.                 }
  5062.             } else {
  5063.                 switch ( (int) $content_struct['mt_allow_comments'] ) {
  5064.                     case 0:
  5065.                     case 2:
  5066.                         $comment_status = 'closed';
  5067.                         break;
  5068.                     case 1:
  5069.                         $comment_status = 'open';
  5070.                         break;
  5071.                     default:
  5072.                         $comment_status = get_default_comment_status( $post_type );
  5073.                         break;
  5074.                 }
  5075.             }
  5076.         } else {
  5077.             $comment_status = get_default_comment_status( $post_type );
  5078.         }
  5079.  
  5080.         if ( isset($content_struct['mt_allow_pings']) ) {
  5081.             if ( !is_numeric($content_struct['mt_allow_pings']) ) {
  5082.                 switch ( $content_struct['mt_allow_pings'] ) {
  5083.                     case 'closed':
  5084.                         $ping_status = 'closed';
  5085.                         break;
  5086.                     case 'open':
  5087.                         $ping_status = 'open';
  5088.                         break;
  5089.                     default:
  5090.                         $ping_status = get_default_comment_status( $post_type, 'pingback' );
  5091.                         break;
  5092.                 }
  5093.             } else {
  5094.                 switch ( (int) $content_struct['mt_allow_pings'] ) {
  5095.                     case 0:
  5096.                         $ping_status = 'closed';
  5097.                         break;
  5098.                     case 1:
  5099.                         $ping_status = 'open';
  5100.                         break;
  5101.                     default:
  5102.                         $ping_status = get_default_comment_status( $post_type, 'pingback' );
  5103.                         break;
  5104.                 }
  5105.             }
  5106.         } else {
  5107.             $ping_status = get_default_comment_status( $post_type, 'pingback' );
  5108.         }
  5109.  
  5110.         if ( $post_more )
  5111.             $post_content = $post_content . '<!--more-->' . $post_more;
  5112.  
  5113.         $to_ping = null;
  5114.         if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
  5115.             $to_ping = $content_struct['mt_tb_ping_urls'];
  5116.             if ( is_array($to_ping) )
  5117.                 $to_ping = implode(' ', $to_ping);
  5118.         }
  5119.  
  5120.         // Do some timestamp voodoo
  5121.         if ( !empty( $content_struct['date_created_gmt'] ) )
  5122.             // We know this is supposed to be GMT, so we're going to slap that Z on there by force
  5123.             $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
  5124.         elseif ( !empty( $content_struct['dateCreated']) )
  5125.             $dateCreated = $content_struct['dateCreated']->getIso();
  5126.  
  5127.         if ( !empty( $dateCreated ) ) {
  5128.             $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
  5129.             $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
  5130.         } else {
  5131.             $post_date = '';
  5132.             $post_date_gmt = '';
  5133.         }
  5134.  
  5135.         $post_category = array();
  5136.         if ( isset( $content_struct['categories'] ) ) {
  5137.             $catnames = $content_struct['categories'];
  5138.  
  5139.             if ( is_array($catnames) ) {
  5140.                 foreach ($catnames as $cat) {
  5141.                     $post_category[] = get_cat_ID($cat);
  5142.                 }
  5143.             }
  5144.         }
  5145.  
  5146.         $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'to_ping', 'post_type', 'post_name', 'post_password', 'post_parent', 'menu_order', 'tags_input', 'page_template');
  5147.  
  5148.         $post_ID = $postdata['ID'] = get_default_post_to_edit( $post_type, true )->ID;
  5149.  
  5150.         // Only posts can be sticky
  5151.         if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
  5152.             $data = $postdata;
  5153.             $data['sticky'] = $content_struct['sticky'];
  5154.             $error = $this->_toggle_sticky( $data );
  5155.             if ( $error ) {
  5156.                 return $error;
  5157.             }
  5158.         }
  5159.  
  5160.         if ( isset($content_struct['custom_fields']) )
  5161.             $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
  5162.  
  5163.         if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
  5164.             if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
  5165.                 return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
  5166.  
  5167.             unset( $content_struct['wp_post_thumbnail'] );
  5168.         }
  5169.  
  5170.         // Handle enclosures
  5171.         $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
  5172.         $this->add_enclosure_if_new($post_ID, $thisEnclosure);
  5173.  
  5174.         $this->attach_uploads( $post_ID, $post_content );
  5175.  
  5176.         // Handle post formats if assigned, value is validated earlier
  5177.         // in this function
  5178.         if ( isset( $content_struct['wp_post_format'] ) )
  5179.             set_post_format( $post_ID, $content_struct['wp_post_format'] );
  5180.  
  5181.         $post_ID = wp_insert_post( $postdata, true );
  5182.         if ( is_wp_error( $post_ID ) )
  5183.             return new IXR_Error(500, $post_ID->get_error_message());
  5184.  
  5185.         if ( !$post_ID )
  5186.             return new IXR_Error(500, __('Sorry, your entry could not be posted.'));
  5187.  
  5188.         /**
  5189.          * Fires after a new post has been successfully created via the XML-RPC MovableType API.
  5190.          *
  5191.          * @since 3.4.0
  5192.          *
  5193.          * @param int   $post_ID ID of the new post.
  5194.          * @param array $args    An array of arguments to create the new post.
  5195.          */
  5196.         do_action( 'xmlrpc_call_success_mw_newPost', $post_ID, $args );
  5197.  
  5198.         return strval($post_ID);
  5199.     }
  5200.  
  5201.     /**
  5202.      * Adds an enclosure to a post if it's new.
  5203.      *
  5204.      * @since 2.8.0
  5205.      *
  5206.      * @param integer $post_ID   Post ID.
  5207.      * @param array   $enclosure Enclosure data.
  5208.      */
  5209.     public function add_enclosure_if_new( $post_ID, $enclosure ) {
  5210.         if ( is_array( $enclosure ) && isset( $enclosure['url'] ) && isset( $enclosure['length'] ) && isset( $enclosure['type'] ) ) {
  5211.             $encstring = $enclosure['url'] . "\n" . $enclosure['length'] . "\n" . $enclosure['type'] . "\n";
  5212.             $found = false;
  5213.             if ( $enclosures = get_post_meta( $post_ID, 'enclosure' ) ) {
  5214.                 foreach ( $enclosures as $enc ) {
  5215.                     // This method used to omit the trailing new line. #23219
  5216.                     if ( rtrim( $enc, "\n" ) == rtrim( $encstring, "\n" ) ) {
  5217.                         $found = true;
  5218.                         break;
  5219.                     }
  5220.                 }
  5221.             }
  5222.             if ( ! $found )
  5223.                 add_post_meta( $post_ID, 'enclosure', $encstring );
  5224.         }
  5225.     }
  5226.  
  5227.     /**
  5228.      * Attach upload to a post.
  5229.      *
  5230.      * @since 2.1.0
  5231.      *
  5232.      * @global wpdb $wpdb WordPress database abstraction object.
  5233.      *
  5234.      * @param int $post_ID Post ID.
  5235.      * @param string $post_content Post Content for attachment.
  5236.      */
  5237.     public function attach_uploads( $post_ID, $post_content ) {
  5238.         global $wpdb;
  5239.  
  5240.         // find any unattached files
  5241.         $attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
  5242.         if ( is_array( $attachments ) ) {
  5243.             foreach ( $attachments as $file ) {
  5244.                 if ( ! empty( $file->guid ) && strpos( $post_content, $file->guid ) !== false )
  5245.                     $wpdb->update($wpdb->posts, array('post_parent' => $post_ID), array('ID' => $file->ID) );
  5246.             }
  5247.         }
  5248.     }
  5249.  
  5250.     /**
  5251.      * Edit a post.
  5252.      *
  5253.      * @since 1.5.0
  5254.      *
  5255.      * @param array  $args {
  5256.      *     Method arguments. Note: arguments must be ordered as documented.
  5257.      *
  5258.      *     @type int    $blog_id (unused)
  5259.      *     @type string $username
  5260.      *     @type string $password
  5261.      *     @type array  $content_struct
  5262.      *     @type int    $publish
  5263.      * }
  5264.      * @return bool|IXR_Error True on success.
  5265.      */
  5266.     public function mw_editPost( $args ) {
  5267.         $this->escape( $args );
  5268.  
  5269.         $post_ID        = (int) $args[0];
  5270.         $username       = $args[1];
  5271.         $password       = $args[2];
  5272.         $content_struct = $args[3];
  5273.         $publish        = isset( $args[4] ) ? $args[4] : 0;
  5274.  
  5275.         if ( ! $user = $this->login($username, $password) )
  5276.             return $this->error;
  5277.  
  5278.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5279.         do_action( 'xmlrpc_call', 'metaWeblog.editPost' );
  5280.  
  5281.         $postdata = get_post( $post_ID, ARRAY_A );
  5282.  
  5283.         /*
  5284.          * If there is no post data for the give post id, stop now and return an error.
  5285.          * Otherwise a new post will be created (which was the old behavior).
  5286.          */
  5287.         if ( ! $postdata || empty( $postdata[ 'ID' ] ) )
  5288.             return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  5289.  
  5290.         if ( ! current_user_can( 'edit_post', $post_ID ) )
  5291.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  5292.  
  5293.         // Use wp.editPost to edit post types other than post and page.
  5294.         if ( ! in_array( $postdata[ 'post_type' ], array( 'post', 'page' ) ) )
  5295.             return new IXR_Error( 401, __( 'Invalid post type.' ) );
  5296.  
  5297.         // Thwart attempt to change the post type.
  5298.         if ( ! empty( $content_struct[ 'post_type' ] ) && ( $content_struct['post_type'] != $postdata[ 'post_type' ] ) )
  5299.             return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
  5300.  
  5301.         // Check for a valid post format if one was given
  5302.         if ( isset( $content_struct['wp_post_format'] ) ) {
  5303.             $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
  5304.             if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
  5305.                 return new IXR_Error( 404, __( 'Invalid post format.' ) );
  5306.             }
  5307.         }
  5308.  
  5309.         $this->escape($postdata);
  5310.  
  5311.         $ID = $postdata['ID'];
  5312.         $post_content = $postdata['post_content'];
  5313.         $post_title = $postdata['post_title'];
  5314.         $post_excerpt = $postdata['post_excerpt'];
  5315.         $post_password = $postdata['post_password'];
  5316.         $post_parent = $postdata['post_parent'];
  5317.         $post_type = $postdata['post_type'];
  5318.         $menu_order = $postdata['menu_order'];
  5319.  
  5320.         // Let WordPress manage slug if none was provided.
  5321.         $post_name = $postdata['post_name'];
  5322.         if ( isset($content_struct['wp_slug']) )
  5323.             $post_name = $content_struct['wp_slug'];
  5324.  
  5325.         // Only use a password if one was given.
  5326.         if ( isset($content_struct['wp_password']) )
  5327.             $post_password = $content_struct['wp_password'];
  5328.  
  5329.         // Only set a post parent if one was given.
  5330.         if ( isset($content_struct['wp_page_parent_id']) )
  5331.             $post_parent = $content_struct['wp_page_parent_id'];
  5332.  
  5333.         // Only set the menu_order if it was given.
  5334.         if ( isset($content_struct['wp_page_order']) )
  5335.             $menu_order = $content_struct['wp_page_order'];
  5336.  
  5337.         $page_template = null;
  5338.         if ( ! empty( $content_struct['wp_page_template'] ) && 'page' == $post_type )
  5339.             $page_template = $content_struct['wp_page_template'];
  5340.  
  5341.         $post_author = $postdata['post_author'];
  5342.  
  5343.         // Only set the post_author if one is set.
  5344.         if ( isset( $content_struct['wp_author_id'] ) ) {
  5345.             // Check permissions if attempting to switch author to or from another user.
  5346.             if ( $user->ID != $content_struct['wp_author_id'] || $user->ID != $post_author ) {
  5347.                 switch ( $post_type ) {
  5348.                     case 'post':
  5349.                         if ( ! current_user_can( 'edit_others_posts' ) ) {
  5350.                             return new IXR_Error( 401, __( 'Sorry, you are not allowed to change the post author as this user.' ) );
  5351.                         }
  5352.                         break;
  5353.                     case 'page':
  5354.                         if ( ! current_user_can( 'edit_others_pages' ) ) {
  5355.                             return new IXR_Error( 401, __( 'Sorry, you are not allowed to change the page author as this user.' ) );
  5356.                         }
  5357.                         break;
  5358.                     default:
  5359.                         return new IXR_Error( 401, __( 'Invalid post type.' ) );
  5360.                 }
  5361.                 $post_author = $content_struct['wp_author_id'];
  5362.             }
  5363.         }
  5364.  
  5365.         if ( isset($content_struct['mt_allow_comments']) ) {
  5366.             if ( !is_numeric($content_struct['mt_allow_comments']) ) {
  5367.                 switch ( $content_struct['mt_allow_comments'] ) {
  5368.                     case 'closed':
  5369.                         $comment_status = 'closed';
  5370.                         break;
  5371.                     case 'open':
  5372.                         $comment_status = 'open';
  5373.                         break;
  5374.                     default:
  5375.                         $comment_status = get_default_comment_status( $post_type );
  5376.                         break;
  5377.                 }
  5378.             } else {
  5379.                 switch ( (int) $content_struct['mt_allow_comments'] ) {
  5380.                     case 0:
  5381.                     case 2:
  5382.                         $comment_status = 'closed';
  5383.                         break;
  5384.                     case 1:
  5385.                         $comment_status = 'open';
  5386.                         break;
  5387.                     default:
  5388.                         $comment_status = get_default_comment_status( $post_type );
  5389.                         break;
  5390.                 }
  5391.             }
  5392.         }
  5393.  
  5394.         if ( isset($content_struct['mt_allow_pings']) ) {
  5395.             if ( !is_numeric($content_struct['mt_allow_pings']) ) {
  5396.                 switch ( $content_struct['mt_allow_pings'] ) {
  5397.                     case 'closed':
  5398.                         $ping_status = 'closed';
  5399.                         break;
  5400.                     case 'open':
  5401.                         $ping_status = 'open';
  5402.                         break;
  5403.                     default:
  5404.                         $ping_status = get_default_comment_status( $post_type, 'pingback' );
  5405.                         break;
  5406.                 }
  5407.             } else {
  5408.                 switch ( (int) $content_struct["mt_allow_pings"] ) {
  5409.                     case 0:
  5410.                         $ping_status = 'closed';
  5411.                         break;
  5412.                     case 1:
  5413.                         $ping_status = 'open';
  5414.                         break;
  5415.                     default:
  5416.                         $ping_status = get_default_comment_status( $post_type, 'pingback' );
  5417.                         break;
  5418.                 }
  5419.             }
  5420.         }
  5421.  
  5422.         if ( isset( $content_struct['title'] ) )
  5423.             $post_title =  $content_struct['title'];
  5424.  
  5425.         if ( isset( $content_struct['description'] ) )
  5426.             $post_content = $content_struct['description'];
  5427.  
  5428.         $post_category = array();
  5429.         if ( isset( $content_struct['categories'] ) ) {
  5430.             $catnames = $content_struct['categories'];
  5431.             if ( is_array($catnames) ) {
  5432.                 foreach ($catnames as $cat) {
  5433.                     $post_category[] = get_cat_ID($cat);
  5434.                 }
  5435.             }
  5436.         }
  5437.  
  5438.         if ( isset( $content_struct['mt_excerpt'] ) )
  5439.             $post_excerpt =  $content_struct['mt_excerpt'];
  5440.  
  5441.         $post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : null;
  5442.  
  5443.         $post_status = $publish ? 'publish' : 'draft';
  5444.         if ( isset( $content_struct["{$post_type}_status"] ) ) {
  5445.             switch( $content_struct["{$post_type}_status"] ) {
  5446.                 case 'draft':
  5447.                 case 'pending':
  5448.                 case 'private':
  5449.                 case 'publish':
  5450.                     $post_status = $content_struct["{$post_type}_status"];
  5451.                     break;
  5452.                 default:
  5453.                     $post_status = $publish ? 'publish' : 'draft';
  5454.                     break;
  5455.             }
  5456.         }
  5457.  
  5458.         $tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : null;
  5459.  
  5460.         if ( 'publish' == $post_status || 'private' == $post_status ) {
  5461.             if ( 'page' == $post_type && ! current_user_can( 'publish_pages' ) ) {
  5462.                 return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish this page.' ) );
  5463.             } elseif ( ! current_user_can( 'publish_posts' ) ) {
  5464.                 return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish this post.' ) );
  5465.             }
  5466.         }
  5467.  
  5468.         if ( $post_more )
  5469.             $post_content = $post_content . "<!--more-->" . $post_more;
  5470.  
  5471.         $to_ping = null;
  5472.         if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
  5473.             $to_ping = $content_struct['mt_tb_ping_urls'];
  5474.             if ( is_array($to_ping) )
  5475.                 $to_ping = implode(' ', $to_ping);
  5476.         }
  5477.  
  5478.         // Do some timestamp voodoo.
  5479.         if ( !empty( $content_struct['date_created_gmt'] ) )
  5480.             // We know this is supposed to be GMT, so we're going to slap that Z on there by force.
  5481.             $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
  5482.         elseif ( !empty( $content_struct['dateCreated']) )
  5483.             $dateCreated = $content_struct['dateCreated']->getIso();
  5484.  
  5485.         // Default to not flagging the post date to be edited unless it's intentional.
  5486.         $edit_date = false;
  5487.  
  5488.         if ( !empty( $dateCreated ) ) {
  5489.             $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
  5490.             $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
  5491.  
  5492.             // Flag the post date to be edited.
  5493.             $edit_date = true;
  5494.         } else {
  5495.             $post_date     = $postdata['post_date'];
  5496.             $post_date_gmt = $postdata['post_date_gmt'];
  5497.         }
  5498.  
  5499.         // We've got all the data -- post it.
  5500.         $newpost = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'edit_date', 'post_date', 'post_date_gmt', 'to_ping', 'post_name', 'post_password', 'post_parent', 'menu_order', 'post_author', 'tags_input', 'page_template');
  5501.  
  5502.         $result = wp_update_post($newpost, true);
  5503.         if ( is_wp_error( $result ) )
  5504.             return new IXR_Error(500, $result->get_error_message());
  5505.  
  5506.         if ( !$result )
  5507.             return new IXR_Error(500, __('Sorry, your entry could not be edited.'));
  5508.  
  5509.         // Only posts can be sticky
  5510.         if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
  5511.             $data = $newpost;
  5512.             $data['sticky'] = $content_struct['sticky'];
  5513.             $data['post_type'] = 'post';
  5514.             $error = $this->_toggle_sticky( $data, true );
  5515.             if ( $error ) {
  5516.                 return $error;
  5517.             }
  5518.         }
  5519.  
  5520.         if ( isset($content_struct['custom_fields']) )
  5521.             $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
  5522.  
  5523.         if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
  5524.  
  5525.             // Empty value deletes, non-empty value adds/updates.
  5526.             if ( empty( $content_struct['wp_post_thumbnail'] ) ) {
  5527.                 delete_post_thumbnail( $post_ID );
  5528.             } else {
  5529.                 if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
  5530.                     return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
  5531.             }
  5532.             unset( $content_struct['wp_post_thumbnail'] );
  5533.         }
  5534.  
  5535.         // Handle enclosures.
  5536.         $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
  5537.         $this->add_enclosure_if_new($post_ID, $thisEnclosure);
  5538.  
  5539.         $this->attach_uploads( $ID, $post_content );
  5540.  
  5541.         // Handle post formats if assigned, validation is handled earlier in this function.
  5542.         if ( isset( $content_struct['wp_post_format'] ) )
  5543.             set_post_format( $post_ID, $content_struct['wp_post_format'] );
  5544.  
  5545.         /**
  5546.          * Fires after a post has been successfully updated via the XML-RPC MovableType API.
  5547.          *
  5548.          * @since 3.4.0
  5549.          *
  5550.          * @param int   $post_ID ID of the updated post.
  5551.          * @param array $args    An array of arguments to update the post.
  5552.          */
  5553.         do_action( 'xmlrpc_call_success_mw_editPost', $post_ID, $args );
  5554.  
  5555.         return true;
  5556.     }
  5557.  
  5558.     /**
  5559.      * Retrieve post.
  5560.      *
  5561.      * @since 1.5.0
  5562.      *
  5563.      * @param array  $args {
  5564.      *     Method arguments. Note: arguments must be ordered as documented.
  5565.      *
  5566.      *     @type int    $blog_id (unused)
  5567.      *     @type int    $post_ID
  5568.      *     @type string $username
  5569.      *     @type string $password
  5570.      * }
  5571.      * @return array|IXR_Error
  5572.      */
  5573.     public function mw_getPost( $args ) {
  5574.         $this->escape( $args );
  5575.  
  5576.         $post_ID  = (int) $args[0];
  5577.         $username = $args[1];
  5578.         $password = $args[2];
  5579.  
  5580.         if ( !$user = $this->login($username, $password) )
  5581.             return $this->error;
  5582.  
  5583.         $postdata = get_post($post_ID, ARRAY_A);
  5584.         if ( ! $postdata )
  5585.             return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  5586.  
  5587.         if ( !current_user_can( 'edit_post', $post_ID ) )
  5588.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  5589.  
  5590.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5591.         do_action( 'xmlrpc_call', 'metaWeblog.getPost' );
  5592.  
  5593.         if ($postdata['post_date'] != '') {
  5594.             $post_date = $this->_convert_date( $postdata['post_date'] );
  5595.             $post_date_gmt = $this->_convert_date_gmt( $postdata['post_date_gmt'],  $postdata['post_date'] );
  5596.             $post_modified = $this->_convert_date( $postdata['post_modified'] );
  5597.             $post_modified_gmt = $this->_convert_date_gmt( $postdata['post_modified_gmt'], $postdata['post_modified'] );
  5598.  
  5599.             $categories = array();
  5600.             $catids = wp_get_post_categories($post_ID);
  5601.             foreach ($catids as $catid)
  5602.                 $categories[] = get_cat_name($catid);
  5603.  
  5604.             $tagnames = array();
  5605.             $tags = wp_get_post_tags( $post_ID );
  5606.             if ( !empty( $tags ) ) {
  5607.                 foreach ( $tags as $tag )
  5608.                     $tagnames[] = $tag->name;
  5609.                 $tagnames = implode( ', ', $tagnames );
  5610.             } else {
  5611.                 $tagnames = '';
  5612.             }
  5613.  
  5614.             $post = get_extended($postdata['post_content']);
  5615.             $link = get_permalink($postdata['ID']);
  5616.  
  5617.             // Get the author info.
  5618.             $author = get_userdata($postdata['post_author']);
  5619.  
  5620.             $allow_comments = ('open' == $postdata['comment_status']) ? 1 : 0;
  5621.             $allow_pings = ('open' == $postdata['ping_status']) ? 1 : 0;
  5622.  
  5623.             // Consider future posts as published
  5624.             if ( $postdata['post_status'] === 'future' )
  5625.                 $postdata['post_status'] = 'publish';
  5626.  
  5627.             // Get post format
  5628.             $post_format = get_post_format( $post_ID );
  5629.             if ( empty( $post_format ) )
  5630.                 $post_format = 'standard';
  5631.  
  5632.             $sticky = false;
  5633.             if ( is_sticky( $post_ID ) )
  5634.                 $sticky = true;
  5635.  
  5636.             $enclosure = array();
  5637.             foreach ( (array) get_post_custom($post_ID) as $key => $val) {
  5638.                 if ($key == 'enclosure') {
  5639.                     foreach ( (array) $val as $enc ) {
  5640.                         $encdata = explode("\n", $enc);
  5641.                         $enclosure['url'] = trim(htmlspecialchars($encdata[0]));
  5642.                         $enclosure['length'] = (int) trim($encdata[1]);
  5643.                         $enclosure['type'] = trim($encdata[2]);
  5644.                         break 2;
  5645.                     }
  5646.                 }
  5647.             }
  5648.  
  5649.             $resp = array(
  5650.                 'dateCreated' => $post_date,
  5651.                 'userid' => $postdata['post_author'],
  5652.                 'postid' => $postdata['ID'],
  5653.                 'description' => $post['main'],
  5654.                 'title' => $postdata['post_title'],
  5655.                 'link' => $link,
  5656.                 'permaLink' => $link,
  5657.                 // commented out because no other tool seems to use this
  5658.                 //          'content' => $entry['post_content'],
  5659.                 'categories' => $categories,
  5660.                 'mt_excerpt' => $postdata['post_excerpt'],
  5661.                 'mt_text_more' => $post['extended'],
  5662.                 'wp_more_text' => $post['more_text'],
  5663.                 'mt_allow_comments' => $allow_comments,
  5664.                 'mt_allow_pings' => $allow_pings,
  5665.                 'mt_keywords' => $tagnames,
  5666.                 'wp_slug' => $postdata['post_name'],
  5667.                 'wp_password' => $postdata['post_password'],
  5668.                 'wp_author_id' => (string) $author->ID,
  5669.                 'wp_author_display_name' => $author->display_name,
  5670.                 'date_created_gmt' => $post_date_gmt,
  5671.                 'post_status' => $postdata['post_status'],
  5672.                 'custom_fields' => $this->get_custom_fields($post_ID),
  5673.                 'wp_post_format' => $post_format,
  5674.                 'sticky' => $sticky,
  5675.                 'date_modified' => $post_modified,
  5676.                 'date_modified_gmt' => $post_modified_gmt
  5677.             );
  5678.  
  5679.             if ( !empty($enclosure) ) $resp['enclosure'] = $enclosure;
  5680.  
  5681.             $resp['wp_post_thumbnail'] = get_post_thumbnail_id( $postdata['ID'] );
  5682.  
  5683.             return $resp;
  5684.         } else {
  5685.             return new IXR_Error(404, __('Sorry, no such post.'));
  5686.         }
  5687.     }
  5688.  
  5689.     /**
  5690.      * Retrieve list of recent posts.
  5691.      *
  5692.      * @since 1.5.0
  5693.      *
  5694.      * @param array  $args {
  5695.      *     Method arguments. Note: arguments must be ordered as documented.
  5696.      *
  5697.      *     @type int    $blog_id (unused)
  5698.      *     @type string $username
  5699.      *     @type string $password
  5700.      *     @type int    $numberposts
  5701.      * }
  5702.      * @return array|IXR_Error
  5703.      */
  5704.     public function mw_getRecentPosts( $args ) {
  5705.         $this->escape( $args );
  5706.  
  5707.         $username = $args[1];
  5708.         $password = $args[2];
  5709.         if ( isset( $args[3] ) )
  5710.             $query = array( 'numberposts' => absint( $args[3] ) );
  5711.         else
  5712.             $query = array();
  5713.  
  5714.         if ( !$user = $this->login($username, $password) )
  5715.             return $this->error;
  5716.  
  5717.         if ( ! current_user_can( 'edit_posts' ) )
  5718.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts.' ) );
  5719.  
  5720.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5721.         do_action( 'xmlrpc_call', 'metaWeblog.getRecentPosts' );
  5722.  
  5723.         $posts_list = wp_get_recent_posts( $query );
  5724.  
  5725.         if ( !$posts_list )
  5726.             return array();
  5727.  
  5728.         $recent_posts = array();
  5729.         foreach ($posts_list as $entry) {
  5730.             if ( !current_user_can( 'edit_post', $entry['ID'] ) )
  5731.                 continue;
  5732.  
  5733.             $post_date = $this->_convert_date( $entry['post_date'] );
  5734.             $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
  5735.             $post_modified = $this->_convert_date( $entry['post_modified'] );
  5736.             $post_modified_gmt = $this->_convert_date_gmt( $entry['post_modified_gmt'], $entry['post_modified'] );
  5737.  
  5738.             $categories = array();
  5739.             $catids = wp_get_post_categories($entry['ID']);
  5740.             foreach ( $catids as $catid )
  5741.                 $categories[] = get_cat_name($catid);
  5742.  
  5743.             $tagnames = array();
  5744.             $tags = wp_get_post_tags( $entry['ID'] );
  5745.             if ( !empty( $tags ) ) {
  5746.                 foreach ( $tags as $tag ) {
  5747.                     $tagnames[] = $tag->name;
  5748.                 }
  5749.                 $tagnames = implode( ', ', $tagnames );
  5750.             } else {
  5751.                 $tagnames = '';
  5752.             }
  5753.  
  5754.             $post = get_extended($entry['post_content']);
  5755.             $link = get_permalink($entry['ID']);
  5756.  
  5757.             // Get the post author info.
  5758.             $author = get_userdata($entry['post_author']);
  5759.  
  5760.             $allow_comments = ('open' == $entry['comment_status']) ? 1 : 0;
  5761.             $allow_pings = ('open' == $entry['ping_status']) ? 1 : 0;
  5762.  
  5763.             // Consider future posts as published
  5764.             if ( $entry['post_status'] === 'future' )
  5765.                 $entry['post_status'] = 'publish';
  5766.  
  5767.             // Get post format
  5768.             $post_format = get_post_format( $entry['ID'] );
  5769.             if ( empty( $post_format ) )
  5770.                 $post_format = 'standard';
  5771.  
  5772.             $recent_posts[] = array(
  5773.                 'dateCreated' => $post_date,
  5774.                 'userid' => $entry['post_author'],
  5775.                 'postid' => (string) $entry['ID'],
  5776.                 'description' => $post['main'],
  5777.                 'title' => $entry['post_title'],
  5778.                 'link' => $link,
  5779.                 'permaLink' => $link,
  5780.                 // commented out because no other tool seems to use this
  5781.                 // 'content' => $entry['post_content'],
  5782.                 'categories' => $categories,
  5783.                 'mt_excerpt' => $entry['post_excerpt'],
  5784.                 'mt_text_more' => $post['extended'],
  5785.                 'wp_more_text' => $post['more_text'],
  5786.                 'mt_allow_comments' => $allow_comments,
  5787.                 'mt_allow_pings' => $allow_pings,
  5788.                 'mt_keywords' => $tagnames,
  5789.                 'wp_slug' => $entry['post_name'],
  5790.                 'wp_password' => $entry['post_password'],
  5791.                 'wp_author_id' => (string) $author->ID,
  5792.                 'wp_author_display_name' => $author->display_name,
  5793.                 'date_created_gmt' => $post_date_gmt,
  5794.                 'post_status' => $entry['post_status'],
  5795.                 'custom_fields' => $this->get_custom_fields($entry['ID']),
  5796.                 'wp_post_format' => $post_format,
  5797.                 'date_modified' => $post_modified,
  5798.                 'date_modified_gmt' => $post_modified_gmt,
  5799.                 'sticky' => ( $entry['post_type'] === 'post' && is_sticky( $entry['ID'] ) ),
  5800.                 'wp_post_thumbnail' => get_post_thumbnail_id( $entry['ID'] )
  5801.             );
  5802.         }
  5803.  
  5804.         return $recent_posts;
  5805.     }
  5806.  
  5807.     /**
  5808.      * Retrieve the list of categories on a given blog.
  5809.      *
  5810.      * @since 1.5.0
  5811.      *
  5812.      * @param array  $args {
  5813.      *     Method arguments. Note: arguments must be ordered as documented.
  5814.      *
  5815.      *     @type int    $blog_id (unused)
  5816.      *     @type string $username
  5817.      *     @type string $password
  5818.      * }
  5819.      * @return array|IXR_Error
  5820.      */
  5821.     public function mw_getCategories( $args ) {
  5822.         $this->escape( $args );
  5823.  
  5824.         $username = $args[1];
  5825.         $password = $args[2];
  5826.  
  5827.         if ( !$user = $this->login($username, $password) )
  5828.             return $this->error;
  5829.  
  5830.         if ( !current_user_can( 'edit_posts' ) )
  5831.             return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
  5832.  
  5833.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5834.         do_action( 'xmlrpc_call', 'metaWeblog.getCategories' );
  5835.  
  5836.         $categories_struct = array();
  5837.  
  5838.         if ( $cats = get_categories(array('get' => 'all')) ) {
  5839.             foreach ( $cats as $cat ) {
  5840.                 $struct = array();
  5841.                 $struct['categoryId'] = $cat->term_id;
  5842.                 $struct['parentId'] = $cat->parent;
  5843.                 $struct['description'] = $cat->name;
  5844.                 $struct['categoryDescription'] = $cat->description;
  5845.                 $struct['categoryName'] = $cat->name;
  5846.                 $struct['htmlUrl'] = esc_html(get_category_link($cat->term_id));
  5847.                 $struct['rssUrl'] = esc_html(get_category_feed_link($cat->term_id, 'rss2'));
  5848.  
  5849.                 $categories_struct[] = $struct;
  5850.             }
  5851.         }
  5852.  
  5853.         return $categories_struct;
  5854.     }
  5855.  
  5856.     /**
  5857.      * Uploads a file, following your settings.
  5858.      *
  5859.      * Adapted from a patch by Johann Richard.
  5860.      *
  5861.      * @link http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/
  5862.      *
  5863.      * @since 1.5.0
  5864.      *
  5865.      * @global wpdb $wpdb WordPress database abstraction object.
  5866.      *
  5867.      * @param array  $args {
  5868.      *     Method arguments. Note: arguments must be ordered as documented.
  5869.      *
  5870.      *     @type int    $blog_id (unused)
  5871.      *     @type string $username
  5872.      *     @type string $password
  5873.      *     @type array  $data
  5874.      * }
  5875.      * @return array|IXR_Error
  5876.      */
  5877.     public function mw_newMediaObject( $args ) {
  5878.         global $wpdb;
  5879.  
  5880.         $username = $this->escape( $args[1] );
  5881.         $password = $this->escape( $args[2] );
  5882.         $data     = $args[3];
  5883.  
  5884.         $name = sanitize_file_name( $data['name'] );
  5885.         $type = $data['type'];
  5886.         $bits = $data['bits'];
  5887.  
  5888.         if ( !$user = $this->login($username, $password) )
  5889.             return $this->error;
  5890.  
  5891.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5892.         do_action( 'xmlrpc_call', 'metaWeblog.newMediaObject' );
  5893.  
  5894.         if ( !current_user_can('upload_files') ) {
  5895.             $this->error = new IXR_Error( 401, __( 'Sorry, you are not allowed to upload files.' ) );
  5896.             return $this->error;
  5897.         }
  5898.  
  5899.         if ( is_multisite() && upload_is_user_over_quota( false ) ) {
  5900.             $this->error = new IXR_Error( 401, __( 'Sorry, you have used your space allocation.' ) );
  5901.             return $this->error;
  5902.         }
  5903.  
  5904.         /**
  5905.          * Filters whether to preempt the XML-RPC media upload.
  5906.          *
  5907.          * Passing a truthy value will effectively short-circuit the media upload,
  5908.          * returning that value as a 500 error instead.
  5909.          *
  5910.          * @since 2.1.0
  5911.          *
  5912.          * @param bool $error Whether to pre-empt the media upload. Default false.
  5913.          */
  5914.         if ( $upload_err = apply_filters( 'pre_upload_error', false ) ) {
  5915.             return new IXR_Error( 500, $upload_err );
  5916.         }
  5917.  
  5918.         $upload = wp_upload_bits($name, null, $bits);
  5919.         if ( ! empty($upload['error']) ) {
  5920.             /* translators: 1: file name, 2: error message */
  5921.             $errorString = sprintf( __( 'Could not write file %1$s (%2$s).' ), $name, $upload['error'] );
  5922.             return new IXR_Error( 500, $errorString );
  5923.         }
  5924.         // Construct the attachment array
  5925.         $post_id = 0;
  5926.         if ( ! empty( $data['post_id'] ) ) {
  5927.             $post_id = (int) $data['post_id'];
  5928.  
  5929.             if ( ! current_user_can( 'edit_post', $post_id ) )
  5930.                 return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  5931.         }
  5932.         $attachment = array(
  5933.             'post_title' => $name,
  5934.             'post_content' => '',
  5935.             'post_type' => 'attachment',
  5936.             'post_parent' => $post_id,
  5937.             'post_mime_type' => $type,
  5938.             'guid' => $upload[ 'url' ]
  5939.         );
  5940.  
  5941.         // Save the data
  5942.         $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
  5943.         wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
  5944.  
  5945.         /**
  5946.          * Fires after a new attachment has been added via the XML-RPC MovableType API.
  5947.          *
  5948.          * @since 3.4.0
  5949.          *
  5950.          * @param int   $id   ID of the new attachment.
  5951.          * @param array $args An array of arguments to add the attachment.
  5952.          */
  5953.         do_action( 'xmlrpc_call_success_mw_newMediaObject', $id, $args );
  5954.  
  5955.         $struct = $this->_prepare_media_item( get_post( $id ) );
  5956.  
  5957.         // Deprecated values
  5958.         $struct['id']   = $struct['attachment_id'];
  5959.         $struct['file'] = $struct['title'];
  5960.         $struct['url']  = $struct['link'];
  5961.  
  5962.         return $struct;
  5963.     }
  5964.  
  5965.     /* MovableType API functions
  5966.      * specs on http://www.movabletype.org/docs/mtmanual_programmatic.html
  5967.      */
  5968.  
  5969.     /**
  5970.      * Retrieve the post titles of recent posts.
  5971.      *
  5972.      * @since 1.5.0
  5973.      *
  5974.      * @param array  $args {
  5975.      *     Method arguments. Note: arguments must be ordered as documented.
  5976.      *
  5977.      *     @type int    $blog_id (unused)
  5978.      *     @type string $username
  5979.      *     @type string $password
  5980.      *     @type int    $numberposts
  5981.      * }
  5982.      * @return array|IXR_Error
  5983.      */
  5984.     public function mt_getRecentPostTitles( $args ) {
  5985.         $this->escape( $args );
  5986.  
  5987.         $username = $args[1];
  5988.         $password = $args[2];
  5989.         if ( isset( $args[3] ) )
  5990.             $query = array( 'numberposts' => absint( $args[3] ) );
  5991.         else
  5992.             $query = array();
  5993.  
  5994.         if ( !$user = $this->login($username, $password) )
  5995.             return $this->error;
  5996.  
  5997.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5998.         do_action( 'xmlrpc_call', 'mt.getRecentPostTitles' );
  5999.  
  6000.         $posts_list = wp_get_recent_posts( $query );
  6001.  
  6002.         if ( !$posts_list ) {
  6003.             $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
  6004.             return $this->error;
  6005.         }
  6006.  
  6007.         $recent_posts = array();
  6008.  
  6009.         foreach ($posts_list as $entry) {
  6010.             if ( !current_user_can( 'edit_post', $entry['ID'] ) )
  6011.                 continue;
  6012.  
  6013.             $post_date = $this->_convert_date( $entry['post_date'] );
  6014.             $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
  6015.  
  6016.             $recent_posts[] = array(
  6017.                 'dateCreated' => $post_date,
  6018.                 'userid' => $entry['post_author'],
  6019.                 'postid' => (string) $entry['ID'],
  6020.                 'title' => $entry['post_title'],
  6021.                 'post_status' => $entry['post_status'],
  6022.                 'date_created_gmt' => $post_date_gmt
  6023.             );
  6024.         }
  6025.  
  6026.         return $recent_posts;
  6027.     }
  6028.  
  6029.     /**
  6030.      * Retrieve list of all categories on blog.
  6031.      *
  6032.      * @since 1.5.0
  6033.      *
  6034.      * @param array  $args {
  6035.      *     Method arguments. Note: arguments must be ordered as documented.
  6036.      *
  6037.      *     @type int    $blog_id (unused)
  6038.      *     @type string $username
  6039.      *     @type string $password
  6040.      * }
  6041.      * @return array|IXR_Error
  6042.      */
  6043.     public function mt_getCategoryList( $args ) {
  6044.         $this->escape( $args );
  6045.  
  6046.         $username = $args[1];
  6047.         $password = $args[2];
  6048.  
  6049.         if ( !$user = $this->login($username, $password) )
  6050.             return $this->error;
  6051.  
  6052.         if ( !current_user_can( 'edit_posts' ) )
  6053.             return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
  6054.  
  6055.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  6056.         do_action( 'xmlrpc_call', 'mt.getCategoryList' );
  6057.  
  6058.         $categories_struct = array();
  6059.  
  6060.         if ( $cats = get_categories(array('hide_empty' => 0, 'hierarchical' => 0)) ) {
  6061.             foreach ( $cats as $cat ) {
  6062.                 $struct = array();
  6063.                 $struct['categoryId'] = $cat->term_id;
  6064.                 $struct['categoryName'] = $cat->name;
  6065.  
  6066.                 $categories_struct[] = $struct;
  6067.             }
  6068.         }
  6069.  
  6070.         return $categories_struct;
  6071.     }
  6072.  
  6073.     /**
  6074.      * Retrieve post categories.
  6075.      *
  6076.      * @since 1.5.0
  6077.      *
  6078.      * @param array  $args {
  6079.      *     Method arguments. Note: arguments must be ordered as documented.
  6080.      *
  6081.      *     @type int    $post_ID
  6082.      *     @type string $username
  6083.      *     @type string $password
  6084.      * }
  6085.      * @return array|IXR_Error
  6086.      */
  6087.     public function mt_getPostCategories( $args ) {
  6088.         $this->escape( $args );
  6089.  
  6090.         $post_ID  = (int) $args[0];
  6091.         $username = $args[1];
  6092.         $password = $args[2];
  6093.  
  6094.         if ( !$user = $this->login($username, $password) )
  6095.             return $this->error;
  6096.  
  6097.         if ( ! get_post( $post_ID ) )
  6098.             return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  6099.  
  6100.         if ( !current_user_can( 'edit_post', $post_ID ) )
  6101.             return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  6102.  
  6103.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  6104.         do_action( 'xmlrpc_call', 'mt.getPostCategories' );
  6105.  
  6106.         $categories = array();
  6107.         $catids = wp_get_post_categories(intval($post_ID));
  6108.         // first listed category will be the primary category
  6109.         $isPrimary = true;
  6110.         foreach ( $catids as $catid ) {
  6111.             $categories[] = array(
  6112.                 'categoryName' => get_cat_name($catid),
  6113.                 'categoryId' => (string) $catid,
  6114.                 'isPrimary' => $isPrimary
  6115.             );
  6116.             $isPrimary = false;
  6117.         }
  6118.  
  6119.         return $categories;
  6120.     }
  6121.  
  6122.     /**
  6123.      * Sets categories for a post.
  6124.      *
  6125.      * @since 1.5.0
  6126.      *
  6127.      * @param array  $args {
  6128.      *     Method arguments. Note: arguments must be ordered as documented.
  6129.      *
  6130.      *     @type int    $post_ID
  6131.      *     @type string $username
  6132.      *     @type string $password
  6133.      *     @type array  $categories
  6134.      * }
  6135.      * @return true|IXR_Error True on success.
  6136.      */
  6137.     public function mt_setPostCategories( $args ) {
  6138.         $this->escape( $args );
  6139.  
  6140.         $post_ID    = (int) $args[0];
  6141.         $username   = $args[1];
  6142.         $password   = $args[2];
  6143.         $categories = $args[3];
  6144.  
  6145.         if ( !$user = $this->login($username, $password) )
  6146.             return $this->error;
  6147.  
  6148.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  6149.         do_action( 'xmlrpc_call', 'mt.setPostCategories' );
  6150.  
  6151.         if ( ! get_post( $post_ID ) )
  6152.             return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  6153.  
  6154.         if ( !current_user_can('edit_post', $post_ID) )
  6155.             return new IXR_Error(401, __('Sorry, you are not allowed to edit this post.'));
  6156.  
  6157.         $catids = array();
  6158.         foreach ( $categories as $cat ) {
  6159.             $catids[] = $cat['categoryId'];
  6160.         }
  6161.  
  6162.         wp_set_post_categories($post_ID, $catids);
  6163.  
  6164.         return true;
  6165.     }
  6166.  
  6167.     /**
  6168.      * Retrieve an array of methods supported by this server.
  6169.      *
  6170.      * @since 1.5.0
  6171.      *
  6172.      * @return array
  6173.      */
  6174.     public function mt_supportedMethods() {
  6175.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  6176.         do_action( 'xmlrpc_call', 'mt.supportedMethods' );
  6177.  
  6178.         return array_keys( $this->methods );
  6179.     }
  6180.  
  6181.     /**
  6182.      * Retrieve an empty array because we don't support per-post text filters.
  6183.      *
  6184.      * @since 1.5.0
  6185.      */
  6186.     public function mt_supportedTextFilters() {
  6187.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  6188.         do_action( 'xmlrpc_call', 'mt.supportedTextFilters' );
  6189.  
  6190.         /**
  6191.          * Filters the MoveableType text filters list for XML-RPC.
  6192.          *
  6193.          * @since 2.2.0
  6194.          *
  6195.          * @param array $filters An array of text filters.
  6196.          */
  6197.         return apply_filters( 'xmlrpc_text_filters', array() );
  6198.     }
  6199.  
  6200.     /**
  6201.      * Retrieve trackbacks sent to a given post.
  6202.      *
  6203.      * @since 1.5.0
  6204.      *
  6205.      * @global wpdb $wpdb WordPress database abstraction object.
  6206.      *
  6207.      * @param int $post_ID
  6208.      * @return array|IXR_Error
  6209.      */
  6210.     public function mt_getTrackbackPings( $post_ID ) {
  6211.         global $wpdb;
  6212.  
  6213.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  6214.         do_action( 'xmlrpc_call', 'mt.getTrackbackPings' );
  6215.  
  6216.         $actual_post = get_post($post_ID, ARRAY_A);
  6217.  
  6218.         if ( !$actual_post )
  6219.             return new IXR_Error(404, __('Sorry, no such post.'));
  6220.  
  6221.         $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
  6222.  
  6223.         if ( !$comments )
  6224.             return array();
  6225.  
  6226.         $trackback_pings = array();
  6227.         foreach ( $comments as $comment ) {
  6228.             if ( 'trackback' == $comment->comment_type ) {
  6229.                 $content = $comment->comment_content;
  6230.                 $title = substr($content, 8, (strpos($content, '</strong>') - 8));
  6231.                 $trackback_pings[] = array(
  6232.                     'pingTitle' => $title,
  6233.                     'pingURL'   => $comment->comment_author_url,
  6234.                     'pingIP'    => $comment->comment_author_IP
  6235.                 );
  6236.             }
  6237.         }
  6238.  
  6239.         return $trackback_pings;
  6240.     }
  6241.  
  6242.     /**
  6243.      * Sets a post's publish status to 'publish'.
  6244.      *
  6245.      * @since 1.5.0
  6246.      *
  6247.      * @param array  $args {
  6248.      *     Method arguments. Note: arguments must be ordered as documented.
  6249.      *
  6250.      *     @type int    $post_ID
  6251.      *     @type string $username
  6252.      *     @type string $password
  6253.      * }
  6254.      * @return int|IXR_Error
  6255.      */
  6256.     public function mt_publishPost( $args ) {
  6257.         $this->escape( $args );
  6258.  
  6259.         $post_ID  = (int) $args[0];
  6260.         $username = $args[1];
  6261.         $password = $args[2];
  6262.  
  6263.         if ( !$user = $this->login($username, $password) )
  6264.             return $this->error;
  6265.  
  6266.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  6267.         do_action( 'xmlrpc_call', 'mt.publishPost' );
  6268.  
  6269.         $postdata = get_post($post_ID, ARRAY_A);
  6270.         if ( ! $postdata )
  6271.             return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  6272.  
  6273.         if ( !current_user_can('publish_posts') || !current_user_can('edit_post', $post_ID) )
  6274.             return new IXR_Error(401, __('Sorry, you are not allowed to publish this post.'));
  6275.  
  6276.         $postdata['post_status'] = 'publish';
  6277.  
  6278.         // retain old cats
  6279.         $cats = wp_get_post_categories($post_ID);
  6280.         $postdata['post_category'] = $cats;
  6281.         $this->escape($postdata);
  6282.  
  6283.         return wp_update_post( $postdata );
  6284.     }
  6285.  
  6286.     /* PingBack functions
  6287.      * specs on www.hixie.ch/specs/pingback/pingback
  6288.      */
  6289.  
  6290.     /**
  6291.      * Retrieves a pingback and registers it.
  6292.      *
  6293.      * @since 1.5.0
  6294.      *
  6295.      * @param array  $args {
  6296.      *     Method arguments. Note: arguments must be ordered as documented.
  6297.      *
  6298.      *     @type string $pagelinkedfrom
  6299.      *     @type string $pagelinkedto
  6300.      * }
  6301.      * @return string|IXR_Error
  6302.      */
  6303.     public function pingback_ping( $args ) {
  6304.         global $wpdb;
  6305.  
  6306.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  6307.         do_action( 'xmlrpc_call', 'pingback.ping' );
  6308.  
  6309.         $this->escape( $args );
  6310.  
  6311.         $pagelinkedfrom = str_replace( '&', '&', $args[0] );
  6312.         $pagelinkedto = str_replace( '&', '&', $args[1] );
  6313.         $pagelinkedto = str_replace( '&', '&', $pagelinkedto );
  6314.  
  6315.         /**
  6316.          * Filters the pingback source URI.
  6317.          *
  6318.          * @since 3.6.0
  6319.          *
  6320.          * @param string $pagelinkedfrom URI of the page linked from.
  6321.          * @param string $pagelinkedto   URI of the page linked to.
  6322.          */
  6323.         $pagelinkedfrom = apply_filters( 'pingback_ping_source_uri', $pagelinkedfrom, $pagelinkedto );
  6324.  
  6325.         if ( ! $pagelinkedfrom )
  6326.             return $this->pingback_error( 0, __( 'A valid URL was not provided.' ) );
  6327.  
  6328.         // Check if the page linked to is in our site
  6329.         $pos1 = strpos($pagelinkedto, str_replace(array('http://www.','http://','https://www.','https://'), '', get_option('home')));
  6330.         if ( !$pos1 )
  6331.             return $this->pingback_error( 0, __( 'Is there no link to us?' ) );
  6332.  
  6333.         // let's find which post is linked to
  6334.         // FIXME: does url_to_postid() cover all these cases already?
  6335.         //        if so, then let's use it and drop the old code.
  6336.         $urltest = parse_url($pagelinkedto);
  6337.         if ( $post_ID = url_to_postid($pagelinkedto) ) {
  6338.             // $way
  6339.         } elseif ( isset( $urltest['path'] ) && preg_match('#p/[0-9]{1,}#', $urltest['path'], $match) ) {
  6340.             // the path defines the post_ID (archives/p/XXXX)
  6341.             $blah = explode('/', $match[0]);
  6342.             $post_ID = (int) $blah[1];
  6343.         } elseif ( isset( $urltest['query'] ) && preg_match('#p=[0-9]{1,}#', $urltest['query'], $match) ) {
  6344.             // the querystring defines the post_ID (?p=XXXX)
  6345.             $blah = explode('=', $match[0]);
  6346.             $post_ID = (int) $blah[1];
  6347.         } elseif ( isset($urltest['fragment']) ) {
  6348.             // an #anchor is there, it's either...
  6349.             if ( intval($urltest['fragment']) ) {
  6350.                 // ...an integer #XXXX (simplest case)
  6351.                 $post_ID = (int) $urltest['fragment'];
  6352.             } elseif ( preg_match('/post-[0-9]+/',$urltest['fragment']) ) {
  6353.                 // ...a post id in the form 'post-###'
  6354.                 $post_ID = preg_replace('/[^0-9]+/', '', $urltest['fragment']);
  6355.             } elseif ( is_string($urltest['fragment']) ) {
  6356.                 // ...or a string #title, a little more complicated
  6357.                 $title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']);
  6358.                 $sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", $title );
  6359.                 if (! ($post_ID = $wpdb->get_var($sql)) ) {
  6360.                     // returning unknown error '0' is better than die()ing
  6361.                       return $this->pingback_error( 0, '' );
  6362.                 }
  6363.             }
  6364.         } else {
  6365.             // TODO: Attempt to extract a post ID from the given URL
  6366.               return $this->pingback_error( 33, __('The specified target URL cannot be used as a target. It either doesn’t exist, or it is not a pingback-enabled resource.' ) );
  6367.         }
  6368.         $post_ID = (int) $post_ID;
  6369.  
  6370.         $post = get_post($post_ID);
  6371.  
  6372.         if ( !$post ) // Post_ID not found
  6373.               return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn’t exist, or it is not a pingback-enabled resource.' ) );
  6374.  
  6375.         if ( $post_ID == url_to_postid($pagelinkedfrom) )
  6376.             return $this->pingback_error( 0, __( 'The source URL and the target URL cannot both point to the same resource.' ) );
  6377.  
  6378.         // Check if pings are on
  6379.         if ( !pings_open($post) )
  6380.               return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn’t exist, or it is not a pingback-enabled resource.' ) );
  6381.  
  6382.         // Let's check that the remote site didn't already pingback this entry
  6383.         if ( $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ) )
  6384.             return $this->pingback_error( 48, __( 'The pingback has already been registered.' ) );
  6385.  
  6386.         // very stupid, but gives time to the 'from' server to publish !
  6387.         sleep(1);
  6388.  
  6389.         $remote_ip = preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] );
  6390.  
  6391.         /** This filter is documented in wp-includes/class-http.php */
  6392.         $user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ) );
  6393.  
  6394.         // Let's check the remote site
  6395.         $http_api_args = array(
  6396.             'timeout' => 10,
  6397.             'redirection' => 0,
  6398.             'limit_response_size' => 153600, // 150 KB
  6399.             'user-agent' => "$user_agent; verifying pingback from $remote_ip",
  6400.             'headers' => array(
  6401.                 'X-Pingback-Forwarded-For' => $remote_ip,
  6402.             ),
  6403.         );
  6404.  
  6405.         $request = wp_safe_remote_get( $pagelinkedfrom, $http_api_args );
  6406.         $remote_source = $remote_source_original = wp_remote_retrieve_body( $request );
  6407.  
  6408.         if ( ! $remote_source ) {
  6409.             return $this->pingback_error( 16, __( 'The source URL does not exist.' ) );
  6410.         }
  6411.  
  6412.         /**
  6413.          * Filters the pingback remote source.
  6414.          *
  6415.          * @since 2.5.0
  6416.          *
  6417.          * @param string $remote_source Response source for the page linked from.
  6418.          * @param string $pagelinkedto  URL of the page linked to.
  6419.          */
  6420.         $remote_source = apply_filters( 'pre_remote_source', $remote_source, $pagelinkedto );
  6421.  
  6422.         // Work around bug in strip_tags():
  6423.         $remote_source = str_replace( '<!DOC', '<DOC', $remote_source );
  6424.         $remote_source = preg_replace( '/[\r\n\t ]+/', ' ', $remote_source ); // normalize spaces
  6425.         $remote_source = preg_replace( "/<\/*(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/", "\n\n", $remote_source );
  6426.  
  6427.         preg_match( '|<title>([^<]*?)</title>|is', $remote_source, $matchtitle );
  6428.         $title = isset( $matchtitle[1] ) ? $matchtitle[1] : '';
  6429.         if ( empty( $title ) ) {
  6430.             return $this->pingback_error( 32, __( 'We cannot find a title on that page.' ) );
  6431.         }
  6432.  
  6433.         $remote_source = strip_tags( $remote_source, '<a>' ); // just keep the tag we need
  6434.  
  6435.         $p = explode( "\n\n", $remote_source );
  6436.  
  6437.         $preg_target = preg_quote($pagelinkedto, '|');
  6438.  
  6439.         foreach ( $p as $para ) {
  6440.             if ( strpos($para, $pagelinkedto) !== false ) { // it exists, but is it a link?
  6441.                 preg_match("|<a[^>]+?".$preg_target."[^>]*>([^>]+?)</a>|", $para, $context);
  6442.  
  6443.                 // If the URL isn't in a link context, keep looking
  6444.                 if ( empty($context) )
  6445.                     continue;
  6446.  
  6447.                 // We're going to use this fake tag to mark the context in a bit
  6448.                 // the marker is needed in case the link text appears more than once in the paragraph
  6449.                 $excerpt = preg_replace('|\</?wpcontext\>|', '', $para);
  6450.  
  6451.                 // prevent really long link text
  6452.                 if ( strlen($context[1]) > 100 )
  6453.                     $context[1] = substr($context[1], 0, 100) . '…';
  6454.  
  6455.                 $marker = '<wpcontext>'.$context[1].'</wpcontext>';    // set up our marker
  6456.                 $excerpt= str_replace($context[0], $marker, $excerpt); // swap out the link for our marker
  6457.                 $excerpt = strip_tags($excerpt, '<wpcontext>');        // strip all tags but our context marker
  6458.                 $excerpt = trim($excerpt);
  6459.                 $preg_marker = preg_quote($marker, '|');
  6460.                 $excerpt = preg_replace("|.*?\s(.{0,100}$preg_marker.{0,100})\s.*|s", '$1', $excerpt);
  6461.                 $excerpt = strip_tags($excerpt); // YES, again, to remove the marker wrapper
  6462.                 break;
  6463.             }
  6464.         }
  6465.  
  6466.         if ( empty($context) ) // Link to target not found
  6467.             return $this->pingback_error( 17, __( 'The source URL does not contain a link to the target URL, and so cannot be used as a source.' ) );
  6468.  
  6469.         $pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom);
  6470.  
  6471.         $context = '[…] ' . esc_html( $excerpt ) . ' […]';
  6472.         $pagelinkedfrom = $this->escape( $pagelinkedfrom );
  6473.  
  6474.         $comment_post_ID = (int) $post_ID;
  6475.         $comment_author = $title;
  6476.         $comment_author_email = '';
  6477.         $this->escape($comment_author);
  6478.         $comment_author_url = $pagelinkedfrom;
  6479.         $comment_content = $context;
  6480.         $this->escape($comment_content);
  6481.         $comment_type = 'pingback';
  6482.  
  6483.         $commentdata = compact(
  6484.             'comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email',
  6485.             'comment_content', 'comment_type', 'remote_source', 'remote_source_original'
  6486.         );
  6487.  
  6488.         $comment_ID = wp_new_comment($commentdata);
  6489.  
  6490.         if ( is_wp_error( $comment_ID ) ) {
  6491.             return $this->pingback_error( 0, $comment_ID->get_error_message() );
  6492.         }
  6493.  
  6494.         /**
  6495.          * Fires after a post pingback has been sent.
  6496.          *
  6497.          * @since 0.71
  6498.          *
  6499.          * @param int $comment_ID Comment ID.
  6500.          */
  6501.         do_action( 'pingback_post', $comment_ID );
  6502.  
  6503.         /* translators: 1: URL of the page linked from, 2: URL of the page linked to */
  6504.         return sprintf( __( 'Pingback from %1$s to %2$s registered. Keep the web talking! :-)' ), $pagelinkedfrom, $pagelinkedto );
  6505.     }
  6506.  
  6507.     /**
  6508.      * Retrieve array of URLs that pingbacked the given URL.
  6509.      *
  6510.      * Specs on http://www.aquarionics.com/misc/archives/blogite/0198.html
  6511.      *
  6512.      * @since 1.5.0
  6513.      *
  6514.      * @global wpdb $wpdb WordPress database abstraction object.
  6515.      *
  6516.      * @param string $url
  6517.      * @return array|IXR_Error
  6518.      */
  6519.     public function pingback_extensions_getPingbacks( $url ) {
  6520.         global $wpdb;
  6521.  
  6522.         /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  6523.         do_action( 'xmlrpc_call', 'pingback.extensions.getPingbacks' );
  6524.  
  6525.         $url = $this->escape( $url );
  6526.  
  6527.         $post_ID = url_to_postid($url);
  6528.         if ( !$post_ID ) {
  6529.             // We aren't sure that the resource is available and/or pingback enabled
  6530.               return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn’t exist, or it is not a pingback-enabled resource.' ) );
  6531.         }
  6532.  
  6533.         $actual_post = get_post($post_ID, ARRAY_A);
  6534.  
  6535.         if ( !$actual_post ) {
  6536.             // No such post = resource not found
  6537.               return $this->pingback_error( 32, __('The specified target URL does not exist.' ) );
  6538.         }
  6539.  
  6540.         $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
  6541.  
  6542.         if ( !$comments )
  6543.             return array();
  6544.  
  6545.         $pingbacks = array();
  6546.         foreach ( $comments as $comment ) {
  6547.             if ( 'pingback' == $comment->comment_type )
  6548.                 $pingbacks[] = $comment->comment_author_url;
  6549.         }
  6550.  
  6551.         return $pingbacks;
  6552.     }
  6553.  
  6554.     /**
  6555.      * Sends a pingback error based on the given error code and message.
  6556.      *
  6557.      * @since 3.6.0
  6558.      *
  6559.      * @param int    $code    Error code.
  6560.      * @param string $message Error message.
  6561.      * @return IXR_Error Error object.
  6562.      */
  6563.     protected function pingback_error( $code, $message ) {
  6564.         /**
  6565.          * Filters the XML-RPC pingback error return.
  6566.          *
  6567.          * @since 3.5.1
  6568.          *
  6569.          * @param IXR_Error $error An IXR_Error object containing the error code and message.
  6570.          */
  6571.         return apply_filters( 'xmlrpc_pingback_error', new IXR_Error( $code, $message ) );
  6572.     }
  6573. }
  6574.