| | 457 | * Prepares taxonomy data for return in an XML-RPC object. |
| | 458 | * |
| | 459 | * @param array|object $taxonomy The unprepared taxonomy data |
| | 460 | * @return array The prepared taxonomy data |
| | 461 | */ |
| | 462 | function prepare_taxonomy( $taxonomy ) { |
| | 463 | $_taxonomy = (array) $taxonomy; |
| | 464 | |
| | 465 | unset( $_taxonomy['update_count_callback'] ); |
| | 466 | |
| | 467 | return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy ); |
| | 468 | } |
| | 469 | |
| | 470 | /** |
| | 471 | * Prepares term data for return in an XML-RPC object. |
| | 472 | * |
| | 473 | * @param array $term The unprepared term data |
| | 474 | * @return array The prepared term data |
| | 475 | */ |
| | 476 | function prepare_term( $term ) { |
| | 477 | $_term = (array) $term; |
| | 478 | |
| | 479 | return apply_filters( 'xmlrpc_prepare_term', $_term, $term ); |
| | 480 | } |
| | 481 | |
| | 482 | /** |
| | 483 | * Create a new term. |
| | 484 | * |
| | 485 | * @uses wp_insert_term() |
| | 486 | * @param array $args Method parameters. Contains: |
| | 487 | * - int $blog_id |
| | 488 | * - string $username |
| | 489 | * - string $password |
| | 490 | * - array $content_struct |
| | 491 | * The $content_struct must contain: |
| | 492 | * - 'name' |
| | 493 | * - 'taxonomy' |
| | 494 | * Also, it can optionally contain: |
| | 495 | * - 'parent' |
| | 496 | * - 'description' |
| | 497 | * - 'slug' |
| | 498 | * @return int term_id |
| | 499 | */ |
| | 500 | function wp_newTerm( $args ) { |
| | 501 | $this->escape( $args ); |
| | 502 | |
| | 503 | $blog_id = (int) $args[0]; |
| | 504 | $username = $args[1]; |
| | 505 | $password = $args[2]; |
| | 506 | $content_struct = $args[3]; |
| | 507 | |
| | 508 | if ( ! $user = $this->login( $username, $password ) ) |
| | 509 | return $this->error; |
| | 510 | |
| | 511 | do_action( 'xmlrpc_call', 'wp.newTerm' ); |
| | 512 | |
| | 513 | if ( ! taxonomy_exists( $content_struct['taxonomy'] ) ) |
| | 514 | return new IXR_Error( 403, __( 'Invalid taxonomy.' ) ); |
| | 515 | |
| | 516 | $taxonomy = get_taxonomy( $content_struct['taxonomy'] ); |
| | 517 | |
| | 518 | if ( ! current_user_can( $taxonomy->cap->manage_terms ) ) |
| | 519 | return new IXR_Error( 401, __( 'You are not allowed to create terms in this taxonomy.' ) ); |
| | 520 | |
| | 521 | $taxonomy = (array) $taxonomy; |
| | 522 | |
| | 523 | // hold the data of the term |
| | 524 | $term_data = array(); |
| | 525 | |
| | 526 | $term_data['name'] = trim( $content_struct['name'] ); |
| | 527 | if ( empty( $term_data['name'] ) ) |
| | 528 | return new IXR_Error( 403, __( 'The term name cannot be empty.' ) ); |
| | 529 | |
| | 530 | if ( isset( $content_struct['parent'] ) ) { |
| | 531 | if ( ! $taxonomy['hierarchical'] ) |
| | 532 | return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) ); |
| | 533 | |
| | 534 | $parent_term_id = (int) $content_struct['parent']; |
| | 535 | $parent_term = get_term( $parent_term_id , $taxonomy['name'] ); |
| | 536 | |
| | 537 | if ( is_wp_error( $parent_term ) ) |
| | 538 | return new IXR_Error( 500, $parent_term->get_error_message() ); |
| | 539 | |
| | 540 | if ( ! $parent_term ) |
| | 541 | return new IXR_Error( 500, __( 'Parent term does not exist.' ) ); |
| | 542 | |
| | 543 | $term_data['parent'] = $content_struct['parent']; |
| | 544 | } |
| | 545 | |
| | 546 | if ( isset( $content_struct['description'] ) ) |
| | 547 | $term_data['description'] = $content_struct['description']; |
| | 548 | |
| | 549 | if ( isset( $content_struct['slug'] ) ) |
| | 550 | $term_data['slug'] = $content_struct['slug']; |
| | 551 | |
| | 552 | $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data ); |
| | 553 | |
| | 554 | if ( is_wp_error( $term ) ) |
| | 555 | return new IXR_Error( 500, $term->get_error_message() ); |
| | 556 | |
| | 557 | if ( ! $term ) |
| | 558 | return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) ); |
| | 559 | |
| | 560 | return $term['term_id']; |
| | 561 | } |
| | 562 | |
| | 563 | /** |
| | 564 | * Edit a term. |
| | 565 | * |
| | 566 | * @uses wp_update_term() |
| | 567 | * @param array $args Method parameters. Contains: |
| | 568 | * - int $blog_id |
| | 569 | * - string $username |
| | 570 | * - string $password |
| | 571 | * - int $term_id |
| | 572 | * - array $content_struct |
| | 573 | * The $content_struct must contain: |
| | 574 | * - 'taxonomy' |
| | 575 | * Also, it can optionally contain: |
| | 576 | * - 'name' |
| | 577 | * - 'parent' |
| | 578 | * - 'description' |
| | 579 | * - 'slug' |
| | 580 | * @return bool True, on success. |
| | 581 | */ |
| | 582 | function wp_editTerm( $args ) { |
| | 583 | $this->escape( $args ); |
| | 584 | |
| | 585 | $blog_id = (int) $args[0]; |
| | 586 | $username = $args[1]; |
| | 587 | $password = $args[2]; |
| | 588 | $term_id = (int) $args[3]; |
| | 589 | $content_struct = $args[4]; |
| | 590 | |
| | 591 | if ( ! $user = $this->login( $username, $password ) ) |
| | 592 | return $this->error; |
| | 593 | |
| | 594 | do_action( 'xmlrpc_call', 'wp.editTerm' ); |
| | 595 | |
| | 596 | if ( ! taxonomy_exists( $content_struct['taxonomy'] ) ) |
| | 597 | return new IXR_Error( 403, __( 'Invalid taxonomy.' ) ); |
| | 598 | |
| | 599 | $taxonomy = get_taxonomy( $content_struct['taxonomy'] ); |
| | 600 | |
| | 601 | if ( ! current_user_can( $taxonomy->cap->edit_terms ) ) |
| | 602 | return new IXR_Error( 401, __( 'You are not allowed to edit terms in this taxonomy.' ) ); |
| | 603 | |
| | 604 | $taxonomy = (array) $taxonomy; |
| | 605 | |
| | 606 | // hold the data of the term |
| | 607 | $term_data = array(); |
| | 608 | |
| | 609 | $term = get_term( $term_id , $content_struct['taxonomy'] ); |
| | 610 | |
| | 611 | if ( is_wp_error( $term ) ) |
| | 612 | return new IXR_Error( 500, $term->get_error_message() ); |
| | 613 | |
| | 614 | if ( ! $term ) |
| | 615 | return new IXR_Error( 404, __( 'Invalid term ID.' ) ); |
| | 616 | |
| | 617 | if ( isset( $content_struct['name'] ) ) { |
| | 618 | $term_data['name'] = trim( $content_struct['name'] ); |
| | 619 | |
| | 620 | if ( empty( $term_data['name'] ) ) |
| | 621 | return new IXR_Error( 403, __( 'The term name cannot be empty.' ) ); |
| | 622 | } |
| | 623 | |
| | 624 | if ( isset( $content_struct['parent'] ) ) { |
| | 625 | if ( ! $taxonomy['hierarchical'] ) |
| | 626 | return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) ); |
| | 627 | |
| | 628 | $parent_term_id = (int) $content_struct['parent']; |
| | 629 | $parent_term = get_term( $parent_term_id , $taxonomy['name'] ); |
| | 630 | |
| | 631 | if ( is_wp_error( $parent_term ) ) |
| | 632 | return new IXR_Error( 500, $term->get_error_message() ); |
| | 633 | |
| | 634 | if ( ! $parent_term ) |
| | 635 | return new IXR_Error( 403, __( 'Invalid parent term ID.' ) ); |
| | 636 | |
| | 637 | $term_data['parent'] = $content_struct['parent']; |
| | 638 | } |
| | 639 | |
| | 640 | if ( isset( $content_struct['description'] ) ) |
| | 641 | $term_data['description'] = $content_struct['description']; |
| | 642 | |
| | 643 | if ( isset( $content_struct['slug'] ) ) |
| | 644 | $term_data['slug'] = $content_struct['slug']; |
| | 645 | |
| | 646 | $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data ); |
| | 647 | |
| | 648 | if ( is_wp_error( $term ) ) |
| | 649 | return new IXR_Error( 500, $term->get_error_message() ); |
| | 650 | |
| | 651 | if ( ! $term ) |
| | 652 | return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) ); |
| | 653 | |
| | 654 | return true; |
| | 655 | } |
| | 656 | |
| | 657 | /** |
| | 658 | * Delete a term. |
| | 659 | * |
| | 660 | * @uses wp_delete_term() |
| | 661 | * @param array $args Method parameters. Contains: |
| | 662 | * - int $blog_id |
| | 663 | * - string $username |
| | 664 | * - string $password |
| | 665 | * - string $taxnomy_name |
| | 666 | * - int $term_id |
| | 667 | * @return boolean true |
| | 668 | */ |
| | 669 | function wp_deleteTerm( $args ) { |
| | 670 | $this->escape( $args ); |
| | 671 | |
| | 672 | $blog_id = (int) $args[0]; |
| | 673 | $username = $args[1]; |
| | 674 | $password = $args[2]; |
| | 675 | $taxonomy_name = $args[3]; |
| | 676 | $term_id = (int) $args[4]; |
| | 677 | |
| | 678 | if ( ! $user = $this->login( $username, $password ) ) |
| | 679 | return $this->error; |
| | 680 | |
| | 681 | do_action( 'xmlrpc_call', 'wp.editTerm' ); |
| | 682 | |
| | 683 | if ( ! taxonomy_exists( $taxonomy_name ) ) |
| | 684 | return new IXR_Error( 403, __( 'Invalid taxonomy.' ) ); |
| | 685 | |
| | 686 | $taxonomy = get_taxonomy( $taxonomy_name ); |
| | 687 | |
| | 688 | if ( ! current_user_can( $taxonomy->cap->delete_terms ) ) |
| | 689 | return new IXR_Error( 401, __( 'You are not allowed to delete terms in this taxonomy.' ) ); |
| | 690 | |
| | 691 | $term = get_term( $term_id, $taxonomy_name ); |
| | 692 | |
| | 693 | if ( is_wp_error( $term ) ) |
| | 694 | return new IXR_Error( 500, $term->get_error_message() ); |
| | 695 | |
| | 696 | if ( ! $term ) |
| | 697 | return new IXR_Error( 404, __( 'Invalid term ID.' ) ); |
| | 698 | |
| | 699 | $result = wp_delete_term( $term_id, $taxonomy_name ); |
| | 700 | |
| | 701 | if ( is_wp_error( $result ) ) |
| | 702 | return new IXR_Error( 500, $term->get_error_message() ); |
| | 703 | |
| | 704 | if ( ! $result ) |
| | 705 | return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) ); |
| | 706 | |
| | 707 | return $result; |
| | 708 | } |
| | 709 | |
| | 710 | /** |
| | 711 | * Retrieve a term. |
| | 712 | * |
| | 713 | * @uses get_term() |
| | 714 | * @param array $args Method parameters. Contains: |
| | 715 | * - int $blog_id |
| | 716 | * - string $username |
| | 717 | * - string $password |
| | 718 | * - string $taxonomy_name |
| | 719 | * - int $term_id |
| | 720 | * @return array contains: |
| | 721 | * - 'term_id' |
| | 722 | * - 'name' |
| | 723 | * - 'slug' |
| | 724 | * - 'term_group' |
| | 725 | * - 'term_taxonomy_id' |
| | 726 | * - 'taxonomy' |
| | 727 | * - 'description' |
| | 728 | * - 'parent' |
| | 729 | * - 'count' |
| | 730 | */ |
| | 731 | function wp_getTerm( $args ) { |
| | 732 | $this->escape( $args ); |
| | 733 | |
| | 734 | $blog_id = (int) $args[0]; |
| | 735 | $username = $args[1]; |
| | 736 | $password = $args[2]; |
| | 737 | $taxonomy_name = $args[3]; |
| | 738 | $term_id = (int) $args[4]; |
| | 739 | |
| | 740 | if ( ! $user = $this->login( $username, $password ) ) |
| | 741 | return $this->error; |
| | 742 | |
| | 743 | do_action( 'xmlrpc_call', 'wp.getTerm' ); |
| | 744 | |
| | 745 | if ( ! taxonomy_exists( $taxonomy_name ) ) |
| | 746 | return new IXR_Error( 403, __( 'Invalid taxonomy name.' ) ); |
| | 747 | |
| | 748 | $taxonomy = get_taxonomy( $taxonomy_name ); |
| | 749 | |
| | 750 | if ( ! current_user_can( $taxonomy->cap->assign_terms ) ) |
| | 751 | return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) ); |
| | 752 | |
| | 753 | $term = get_term( $term_id , $taxonomy_name ); |
| | 754 | |
| | 755 | if ( is_wp_error( $term ) ) |
| | 756 | return new IXR_Error( 500, $term->get_error_message() ); |
| | 757 | |
| | 758 | if ( ! $term ) |
| | 759 | return new IXR_Error( 404, __( 'Invalid term ID.' ) ); |
| | 760 | |
| | 761 | return $this->prepare_term( $term ); |
| | 762 | } |
| | 763 | |
| | 764 | /** |
| | 765 | * Retrieve all terms for a taxonomy. |
| | 766 | * |
| | 767 | * @uses get_terms() |
| | 768 | * @param array $args Method parameters. Contains: |
| | 769 | * - int $blog_id |
| | 770 | * - string $username |
| | 771 | * - string $password |
| | 772 | * - string $taxonomy_name |
| | 773 | * @return array terms |
| | 774 | */ |
| | 775 | function wp_getTerms( $args ) { |
| | 776 | $this->escape( $args ); |
| | 777 | |
| | 778 | $blog_id = (int) $args[0]; |
| | 779 | $username = $args[1]; |
| | 780 | $password = $args[2]; |
| | 781 | $taxonomy_name = $args[3]; |
| | 782 | |
| | 783 | if ( ! $user = $this->login( $username, $password ) ) |
| | 784 | return $this->error; |
| | 785 | |
| | 786 | do_action( 'xmlrpc_call', 'wp.getTerms' ); |
| | 787 | |
| | 788 | if ( ! taxonomy_exists( $taxonomy_name ) ) |
| | 789 | return new IXR_Error( 403, __( 'Invalid taxonomy name.' ) ); |
| | 790 | |
| | 791 | $taxonomy = get_taxonomy( $taxonomy_name ); |
| | 792 | |
| | 793 | if ( ! current_user_can( $taxonomy->cap->assign_terms ) ) |
| | 794 | return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) ); |
| | 795 | |
| | 796 | $terms = get_terms( $taxonomy_name , array( 'get' => 'all' ) ); |
| | 797 | |
| | 798 | if ( is_wp_error( $terms ) ) |
| | 799 | return new IXR_Error( 500, $terms->get_error_message() ); |
| | 800 | |
| | 801 | $struct = array(); |
| | 802 | |
| | 803 | foreach ( $terms as $term ) { |
| | 804 | $struct[] = $this->prepare_term( $term ); |
| | 805 | } |
| | 806 | |
| | 807 | return $struct; |
| | 808 | } |
| | 809 | |
| | 810 | /** |
| | 811 | * Retrieve a taxonomy. |
| | 812 | * |
| | 813 | * @uses get_taxonomy() |
| | 814 | * @param array $args Method parameters. Contains: |
| | 815 | * - int $blog_id |
| | 816 | * - string $username |
| | 817 | * - string $password |
| | 818 | * - string $taxonomy_name |
| | 819 | * @return array (@see get_taxonomy()) |
| | 820 | */ |
| | 821 | function wp_getTaxonomy( $args ) { |
| | 822 | $this->escape( $args ); |
| | 823 | |
| | 824 | $blog_id = (int) $args[0]; |
| | 825 | $username = $args[1]; |
| | 826 | $password = $args[2]; |
| | 827 | $taxonomy_name = $args[3]; |
| | 828 | |
| | 829 | if ( ! $user = $this->login( $username, $password ) ) |
| | 830 | return $this->error; |
| | 831 | |
| | 832 | do_action( 'xmlrpc_call', 'wp.getTaxonomy' ); |
| | 833 | |
| | 834 | if ( ! taxonomy_exists( $taxonomy_name ) ) |
| | 835 | return new IXR_Error( 403, __( 'The taxonomy type specified is not valid' ) ); |
| | 836 | |
| | 837 | $taxonomy = get_taxonomy( $taxonomy_name ); |
| | 838 | |
| | 839 | if ( ! current_user_can( $taxonomy->cap->edit_terms ) ) |
| | 840 | return new IXR_Error( 401, __( 'Sorry, You are not allowed to edit this post type' ) ); |
| | 841 | |
| | 842 | return $this->prepare_taxonomy( $taxonomy ); |
| | 843 | } |
| | 844 | |
| | 845 | /** |
| | 846 | * Retrieve all taxonomies. |
| | 847 | * |
| | 848 | * @uses get_taxonomies() |
| | 849 | * @param array $args Method parameters. Contains: |
| | 850 | * - int $blog_id |
| | 851 | * - string $username |
| | 852 | * - string $password |
| | 853 | * @return array taxonomies |
| | 854 | */ |
| | 855 | function wp_getTaxonomies( $args ) { |
| | 856 | $this->escape( $args ); |
| | 857 | |
| | 858 | $blog_id = (int) $args[0]; |
| | 859 | $username = $args[1]; |
| | 860 | $password = $args[2]; |
| | 861 | |
| | 862 | if ( ! $user = $this->login( $username, $password ) ) |
| | 863 | return $this->error; |
| | 864 | |
| | 865 | do_action( 'xmlrpc_call', 'wp.getTaxonomies' ); |
| | 866 | |
| | 867 | $taxonomies = get_taxonomies( '', 'objects' ); |
| | 868 | |
| | 869 | // holds all the taxonomy data |
| | 870 | $struct = array(); |
| | 871 | |
| | 872 | foreach ( $taxonomies as $taxonomy ) { |
| | 873 | // capability check for post_types |
| | 874 | if ( ! current_user_can( $taxonomy->cap->edit_terms ) ) |
| | 875 | continue; |
| | 876 | |
| | 877 | $struct[] = $this->prepare_taxonomy( $taxonomy ); |
| | 878 | } |
| | 879 | |
| | 880 | return $struct; |
| | 881 | } |
| | 882 | |
| | 883 | /** |