Make WordPress Core

Changeset 20137


Ignore:
Timestamp:
03/07/2012 05:02:22 PM (15 years ago)
Author:
westi
Message:

XML-RPC: Initial implementation of Taxonomy and Term APIs.

Implements: wp.newTerm, wp.editTerm, wp.deleteTerm, wp.getTerm, wp.getTerms, wp.getTaxonomy and wp.getTaxonomies

See: #18438, #18439, #18440, #18441, #18442, #18443, and #18444 props maxcutler, markoheijnen and nprasath002.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-wp-xmlrpc-server.php

    r20075 r20137  
    4242                        'wp.getPost'                    => 'this:wp_getPost',
    4343                        'wp.getPosts'                   => 'this:wp_getPosts',
     44                        'wp.newTerm'                    => 'this:wp_newTerm',
     45                        'wp.editTerm'                   => 'this:wp_editTerm',
     46                        'wp.deleteTerm'                 => 'this:wp_deleteTerm',
     47                        'wp.getTerm'                    => 'this:wp_getTerm',
     48                        'wp.getTerms'                   => 'this:wp_getTerms',
     49                        'wp.getTaxonomy'                => 'this:wp_getTaxonomy',
     50                        'wp.getTaxonomies'              => 'this:wp_getTaxonomies',
    4451                        'wp.getPage'                    => 'this:wp_getPage',
    4552                        'wp.getPages'                   => 'this:wp_getPages',
     
    478485
    479486        /**
     487         * Prepares taxonomy data for return in an XML-RPC object.
     488         *
     489         * @access protected
     490        .*
     491         * @param array|object $taxonomy The unprepared taxonomy data
     492         * @return array The prepared taxonomy data
     493         */
     494        protected function _prepare_taxonomy( $taxonomy ) {
     495                $_taxonomy = (array) $taxonomy;
     496
     497                unset( $_taxonomy['update_count_callback'] );
     498
     499                return apply_filters( 'xmlrpc__prepare_taxonomy', $_taxonomy, $taxonomy );
     500        }
     501
     502        /**
     503         * Prepares term data for return in an XML-RPC object.
     504         *
     505         * @access protected
     506        .*
     507         * @param array|object $term The unprepared term data
     508         * @return array The prepared term data
     509         */
     510        protected function _prepare_term( $term ) {
     511                $_term = $term;
     512                if ( ! is_array( $_term) )
     513                        $_term = get_object_vars( $_term );
     514
     515                return apply_filters( 'xmlrpc__prepare_term', $_term, $term );
     516        }
     517
     518        /**
    480519         * Prepares post data for return in an XML-RPC object.
    481520         *
    482          * @access private
     521         * @access protected
    483522         *
    484523         * @param array $post The unprepared post data
     
    486525         * @return array The prepared post data
    487526         */
    488         function _prepare_post( $post, $fields ) {
     527        protected function _prepare_post( $post, $fields ) {
    489528                // holds the data for this post. built up based on $fields
    490529                $_post = array( 'post_id' => $post['ID'] );
     
    534573                        $_post['terms'] = array();
    535574                        foreach ( $terms as $term ) {
    536                                 $_post['terms'][] = (array) $term;
     575                                $_post['terms'][] = $this->_prepare_term( $term );
    537576                        }
    538577                }
     
    11071146
    11081147                        $struct[] = $this->_prepare_post( $post, $fields );
     1148                }
     1149
     1150                return $struct;
     1151        }
     1152
     1153        /**
     1154         * Create a new term.
     1155         *
     1156         * @uses wp_insert_term()
     1157         * @param array $args Method parameters. Contains:
     1158         *  - int     $blog_id
     1159         *  - string  $username
     1160         *  - string  $password
     1161         *  - array   $content_struct
     1162         *      The $content_struct must contain:
     1163         *      - 'name'
     1164         *      - 'taxonomy'
     1165         *      Also, it can optionally contain:
     1166         *      - 'parent'
     1167         *      - 'description'
     1168         *      - 'slug'
     1169         * @return string term_id
     1170         */
     1171        function wp_newTerm( $args ) {
     1172                $this->escape( $args );
     1173
     1174                $blog_id            = (int) $args[0];
     1175                $username           = $args[1];
     1176                $password           = $args[2];
     1177                $content_struct     = $args[3];
     1178
     1179                if ( ! $user = $this->login( $username, $password ) )
     1180                        return $this->error;
     1181
     1182                do_action( 'xmlrpc_call', 'wp.newTerm' );
     1183
     1184                if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
     1185                        return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
     1186
     1187                $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
     1188
     1189                if ( ! current_user_can( $taxonomy->cap->manage_terms ) )
     1190                        return new IXR_Error( 401, __( 'You are not allowed to create terms in this taxonomy.' ) );
     1191
     1192                $taxonomy = (array) $taxonomy;
     1193
     1194                // hold the data of the term
     1195                $term_data = array();
     1196
     1197                $term_data['name'] = trim( $content_struct['name'] );
     1198                if ( empty( $term_data['name'] ) )
     1199                        return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
     1200
     1201                if ( isset( $content_struct['parent'] ) ) {
     1202                        if ( ! $taxonomy['hierarchical'] )
     1203                                return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) );
     1204
     1205                        $parent_term_id = (int) $content_struct['parent'];
     1206                        $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
     1207
     1208                        if ( is_wp_error( $parent_term ) )
     1209                                return new IXR_Error( 500, $parent_term->get_error_message() );
     1210
     1211                        if ( ! $parent_term )
     1212                                return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
     1213
     1214                        $term_data['parent'] = $content_struct['parent'];
     1215                }
     1216
     1217                if ( isset( $content_struct['description'] ) )
     1218                        $term_data['description'] = $content_struct['description'];
     1219
     1220                if ( isset( $content_struct['slug'] ) )
     1221                        $term_data['slug'] = $content_struct['slug'];
     1222
     1223                $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data );
     1224
     1225                if ( is_wp_error( $term ) )
     1226                        return new IXR_Error( 500, $term->get_error_message() );
     1227
     1228                if ( ! $term )
     1229                        return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) );
     1230
     1231                return strval( $term['term_id'] );
     1232        }
     1233
     1234        /**
     1235         * Edit a term.
     1236         *
     1237         * @uses wp_update_term()
     1238         * @param array $args Method parameters. Contains:
     1239         *  - int     $blog_id
     1240         *  - string  $username
     1241         *  - string  $password
     1242         *  - string  $term_id
     1243         *  - array   $content_struct
     1244         *      The $content_struct must contain:
     1245         *      - 'taxonomy'
     1246         *      Also, it can optionally contain:
     1247         *      - 'name'
     1248         *      - 'parent'
     1249         *      - 'description'
     1250         *      - 'slug'
     1251         * @return bool True, on success.
     1252         */
     1253        function wp_editTerm( $args ) {
     1254                $this->escape( $args );
     1255
     1256                $blog_id            = (int) $args[0];
     1257                $username           = $args[1];
     1258                $password           = $args[2];
     1259                $term_id            = (int) $args[3];
     1260                $content_struct     = $args[4];
     1261
     1262                if ( ! $user = $this->login( $username, $password ) )
     1263                        return $this->error;
     1264
     1265                do_action( 'xmlrpc_call', 'wp.editTerm' );
     1266
     1267                if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
     1268                        return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
     1269
     1270                $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
     1271
     1272                if ( ! current_user_can( $taxonomy->cap->edit_terms ) )
     1273                        return new IXR_Error( 401, __( 'You are not allowed to edit terms in this taxonomy.' ) );
     1274
     1275                $taxonomy = (array) $taxonomy;
     1276
     1277                // hold the data of the term
     1278                $term_data = array();
     1279
     1280                $term = get_term( $term_id , $content_struct['taxonomy'] );
     1281
     1282                if ( is_wp_error( $term ) )
     1283                        return new IXR_Error( 500, $term->get_error_message() );
     1284
     1285                if ( ! $term )
     1286                        return new IXR_Error( 404, __( 'Invalid term ID.' ) );
     1287
     1288                if ( isset( $content_struct['name'] ) ) {
     1289                        $term_data['name'] = trim( $content_struct['name'] );
     1290
     1291                        if ( empty( $term_data['name'] ) )
     1292                                return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
     1293                }
     1294
     1295                if ( isset( $content_struct['parent'] ) ) {
     1296                        if ( ! $taxonomy['hierarchical'] )
     1297                                return new IXR_Error( 403, __( "This taxonomy is not hierarchical so you can't set a parent." ) );
     1298
     1299                        $parent_term_id = (int) $content_struct['parent'];
     1300                        $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
     1301
     1302                        if ( is_wp_error( $parent_term ) )
     1303                                return new IXR_Error( 500, $parent_term->get_error_message() );
     1304
     1305                        if ( ! $parent_term )
     1306                                return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
     1307
     1308                        $term_data['parent'] = $content_struct['parent'];
     1309                }
     1310
     1311                if ( isset( $content_struct['description'] ) )
     1312                        $term_data['description'] = $content_struct['description'];
     1313
     1314                if ( isset( $content_struct['slug'] ) )
     1315                        $term_data['slug'] = $content_struct['slug'];
     1316
     1317                $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data );
     1318
     1319                if ( is_wp_error( $term ) )
     1320                        return new IXR_Error( 500, $term->get_error_message() );
     1321
     1322                if ( ! $term )
     1323                        return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
     1324
     1325                return true;
     1326        }
     1327
     1328        /**
     1329         * Delete a term.
     1330         *
     1331         * @uses wp_delete_term()
     1332         * @param array $args Method parameters. Contains:
     1333         *  - int     $blog_id
     1334         *  - string  $username
     1335         *  - string  $password
     1336         *  - string  $taxnomy_name
     1337         *  - string     $term_id
     1338         * @return boolean|IXR_Error If it suceeded true else a reason why not
     1339         */
     1340        function wp_deleteTerm( $args ) {
     1341                $this->escape( $args );
     1342
     1343                $blog_id            = (int) $args[0];
     1344                $username           = $args[1];
     1345                $password           = $args[2];
     1346                $taxonomy_name      = $args[3];
     1347                $term_id            = (int) $args[4];
     1348
     1349                if ( ! $user = $this->login( $username, $password ) )
     1350                        return $this->error;
     1351
     1352                do_action( 'xmlrpc_call', 'wp.deleteTerm' );
     1353
     1354                if ( ! taxonomy_exists( $taxonomy_name ) )
     1355                        return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
     1356
     1357                $taxonomy = get_taxonomy( $taxonomy_name );
     1358
     1359                if ( ! current_user_can( $taxonomy->cap->delete_terms ) )
     1360                        return new IXR_Error( 401, __( 'You are not allowed to delete terms in this taxonomy.' ) );
     1361
     1362                $term = get_term( $term_id, $taxonomy_name );
     1363
     1364                if ( is_wp_error( $term ) )
     1365                        return new IXR_Error( 500, $term->get_error_message() );
     1366
     1367                if ( ! $term )
     1368                        return new IXR_Error( 404, __( 'Invalid term ID.' ) );
     1369
     1370                $result = wp_delete_term( $term_id, $taxonomy_name );
     1371
     1372                if ( is_wp_error( $result ) )
     1373                        return new IXR_Error( 500, $term->get_error_message() );
     1374
     1375                if ( ! $result )
     1376                        return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) );
     1377
     1378                return $result;
     1379        }
     1380
     1381        /**
     1382         * Retrieve a term.
     1383         *
     1384         * @uses get_term()
     1385         * @param array $args Method parameters. Contains:
     1386         *  - int     $blog_id
     1387         *  - string  $username
     1388         *  - string  $password
     1389         *  - string  $taxonomy_name
     1390         *  - string  $term_id
     1391         * @return array contains:
     1392         *  - 'term_id'
     1393         *  - 'name'
     1394         *  - 'slug'
     1395         *  - 'term_group'
     1396         *  - 'term_taxonomy_id'
     1397         *  - 'taxonomy'
     1398         *  - 'description'
     1399         *  - 'parent'
     1400         *  - 'count'
     1401         */
     1402        function wp_getTerm( $args ) {
     1403                $this->escape( $args );
     1404
     1405                $blog_id            = (int) $args[0];
     1406                $username           = $args[1];
     1407                $password           = $args[2];
     1408                $taxonomy_name      = $args[3];
     1409                $term_id            = (int) $args[4];
     1410
     1411                if ( ! $user = $this->login( $username, $password ) )
     1412                        return $this->error;
     1413
     1414                do_action( 'xmlrpc_call', 'wp.getTerm' );
     1415
     1416                if ( ! taxonomy_exists( $taxonomy_name ) )
     1417                        return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
     1418
     1419                $taxonomy = get_taxonomy( $taxonomy_name );
     1420
     1421                if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
     1422                        return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
     1423
     1424                $term = get_term( $term_id , $taxonomy_name, ARRAY_A );
     1425
     1426                if ( is_wp_error( $term ) )
     1427                        return new IXR_Error( 500, $term->get_error_message() );
     1428
     1429                if ( ! $term )
     1430                        return new IXR_Error( 404, __( 'Invalid term ID.' ) );
     1431
     1432                return $this->_prepare_term( $term );
     1433        }
     1434
     1435        /**
     1436         * Retrieve all terms for a taxonomy.
     1437         *
     1438         * The optional $filter parameter modifies the query used to retrieve terms.
     1439         * Accepted keys are 'number', 'offset', 'orderby', 'order', 'hide_empty', and 'search'.
     1440         *
     1441         * @uses get_terms()
     1442         * @param array $args Method parameters. Contains:
     1443         *  - int     $blog_id
     1444         *  - string  $username
     1445         *  - string  $password
     1446         *  - string  $taxonomy_name
     1447         *  - array   $filter optional
     1448         * @return array terms
     1449         */
     1450        function wp_getTerms( $args ) {
     1451                $this->escape( $args );
     1452
     1453                $blog_id        = (int) $args[0];
     1454                $username       = $args[1];
     1455                $password       = $args[2];
     1456                $taxonomy_name  = $args[3];
     1457                $filter         = isset( $args[4] ) ? $args[4] : array();
     1458
     1459                if ( ! $user = $this->login( $username, $password ) )
     1460                        return $this->error;
     1461
     1462                do_action( 'xmlrpc_call', 'wp.getTerms' );
     1463
     1464                if ( ! taxonomy_exists( $taxonomy_name ) )
     1465                        return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
     1466
     1467                $taxonomy = get_taxonomy( $taxonomy_name );
     1468
     1469                if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
     1470                        return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
     1471
     1472                $query = array();
     1473
     1474                if ( isset( $filter['number'] ) )
     1475                        $query['number'] = absint( $filter['number'] );
     1476
     1477                if ( isset( $filter['offset'] ) )
     1478                        $query['offset'] = absint( $filter['offset'] );
     1479
     1480                if ( isset( $filter['orderby'] ) ) {
     1481                        $query['orderby'] = $filter['orderby'];
     1482
     1483                        if ( isset( $filter['order'] ) )
     1484                                $query['order'] = $filter['order'];
     1485                }
     1486
     1487                if ( isset( $filter['hide_empty'] ) )
     1488                        $query['hide_empty'] = $filter['hide_empty'];
     1489                else
     1490                        $query['get'] = 'all';
     1491
     1492                if ( isset( $filter['search'] ) )
     1493                        $query['search'] = $filter['search'];
     1494
     1495                $terms = get_terms( $taxonomy_name, $query );
     1496
     1497                if ( is_wp_error( $terms ) )
     1498                        return new IXR_Error( 500, $terms->get_error_message() );
     1499
     1500                $struct = array();
     1501
     1502                foreach ( $terms as $term ) {
     1503                        $struct[] = $this->_prepare_term( $term );
     1504                }
     1505
     1506                return $struct;
     1507        }
     1508
     1509        /**
     1510         * Retrieve a taxonomy.
     1511         *
     1512         * @uses get_taxonomy()
     1513         * @param array $args Method parameters. Contains:
     1514         *  - int     $blog_id
     1515         *  - string  $username
     1516         *  - string  $password
     1517         *  - string  $taxonomy_name
     1518         * @return array (@see get_taxonomy())
     1519         */
     1520        function wp_getTaxonomy( $args ) {
     1521                $this->escape( $args );
     1522
     1523                $blog_id        = (int) $args[0];
     1524                $username       = $args[1];
     1525                $password       = $args[2];
     1526                $taxonomy_name  = $args[3];
     1527
     1528                if ( ! $user = $this->login( $username, $password ) )
     1529                        return $this->error;
     1530
     1531                do_action( 'xmlrpc_call', 'wp.getTaxonomy' );
     1532
     1533                if ( ! taxonomy_exists( $taxonomy_name ) )
     1534                        return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
     1535
     1536                $taxonomy = get_taxonomy( $taxonomy_name );
     1537
     1538                if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
     1539                        return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
     1540
     1541                return $this->_prepare_taxonomy( $taxonomy );
     1542        }
     1543
     1544        /**
     1545         * Retrieve all taxonomies.
     1546         *
     1547         * @uses get_taxonomies()
     1548         * @param array $args Method parameters. Contains:
     1549         *  - int     $blog_id
     1550         *  - string  $username
     1551         *  - string  $password
     1552         * @return array taxonomies
     1553         */
     1554        function wp_getTaxonomies( $args ) {
     1555                $this->escape( $args );
     1556
     1557                $blog_id            = (int) $args[0];
     1558                $username           = $args[1];
     1559                $password           = $args[2];
     1560
     1561                if ( ! $user = $this->login( $username, $password ) )
     1562                        return $this->error;
     1563
     1564                do_action( 'xmlrpc_call', 'wp.getTaxonomies' );
     1565
     1566                $taxonomies = get_taxonomies( array(), 'objects' );
     1567
     1568                // holds all the taxonomy data
     1569                $struct = array();
     1570
     1571                foreach ( $taxonomies as $taxonomy ) {
     1572                        // capability check for post_types
     1573                        if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
     1574                                continue;
     1575
     1576                        $struct[] = $this->_prepare_taxonomy( $taxonomy );
    11091577                }
    11101578
Note: See TracChangeset for help on using the changeset viewer.