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