| 1 | package WP::API; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 4 | $WP::API::VERSION = '0.01'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | 4 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | $WP::API::AUTHORITY = 'cpan:DROLSKY'; | |||||||||||||||||||||||||||
| 7 | } | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Carp qw( confess ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use XMLRPC::Lite; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use WP::API::Media; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use WP::API::Post; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use WP::API::Types qw( ClassName NonEmptyStr PositiveInt Uri ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use WP::API::WrappedClass; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use MooseX::StrictConstructor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | has [qw( username password )] => ( | |||||||||||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||||||||||
| 25 | isa => NonEmptyStr, | ||||||||||||||||||||||||||||
| 26 | required => 1, | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 4 | has blog_id => ( | |||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||
| 31 | isa => PositiveInt, | ||||||||||||||||||||||||||||
| 32 | lazy => 1, | ||||||||||||||||||||||||||||
| 33 | builder => '_build_blog_id', | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 4 | has proxy => ( | |||||||||||||||||||||||||||
| 37 | is => 'ro', | ||||||||||||||||||||||||||||
| 38 | isa => Uri, | ||||||||||||||||||||||||||||
| 39 | coerce => 1, | ||||||||||||||||||||||||||||
| 40 | required => 1, | ||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 4 | has server_time_zone => ( | |||||||||||||||||||||||||||
| 44 | is => 'ro', | ||||||||||||||||||||||||||||
| 45 | isa => NonEmptyStr, | ||||||||||||||||||||||||||||
| 46 | required => 1, | ||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # This exists to make it possible to mock out the XMLRPC::Lite->call method in | ||||||||||||||||||||||||||||
| 50 | # tests. | ||||||||||||||||||||||||||||
| 51 | 4 | has _xmlrpc_class => ( | |||||||||||||||||||||||||||
| 52 | is => 'ro', | ||||||||||||||||||||||||||||
| 53 | isa => ClassName, | ||||||||||||||||||||||||||||
| 54 | default => 'XMLRPC::Lite', | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 4 | has _xmlrpc => ( | |||||||||||||||||||||||||||
| 58 | is => 'ro', | ||||||||||||||||||||||||||||
| 59 | isa => 'XMLRPC::Lite', | ||||||||||||||||||||||||||||
| 60 | init_arg => undef, | ||||||||||||||||||||||||||||
| 61 | lazy => 1, | ||||||||||||||||||||||||||||
| 62 | builder => '_build_xmlrpc', | ||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 5 | ▶ | sub _build_xmlrpc { | ||||||||||||||||||||||||||
|
Subroutine
_build_xmlrpc: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 5 | return $self->_xmlrpc_class()->proxy( $self->proxy() ); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2 | ▶ | sub _build_blog_id { | ||||||||||||||||||||||||||
|
Subroutine
_build_blog_id: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 72 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 2 | my $blogs = $self->call('wp.getUsersBlogs'); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 2 | ▶ | if ( @{$blogs} > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 1 | confess | |||||||||||||||||||||||||||
| 78 | 'This user belongs to more than one blog. Please supply a blog_id to the WP::API constructor'; | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | return $blogs->[0]{blogid}; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 4 | for my $type (qw( media page post user )) { | |||||||||||||||||||||||||||
| 85 | 0 | ▶ | my $sub = sub { | ||||||||||||||||||||||||||
|
Subroutine
media: not called CC=1 SCAR=1.2
Subroutine
user: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 86 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 87 | 0 | return $self->_wrapped_class( 'WP::API::' . ucfirst $type ); | |||||||||||||||||||||||||||
| 88 | 16 | }; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 16 | __PACKAGE__->meta()->add_method( $type => $sub ); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 4 | ▶ | sub _wrapped_class { | ||||||||||||||||||||||||||
|
Subroutine
_wrapped_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 94 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 4 | return WP::API::WrappedClass->wrap( | |||||||||||||||||||||||||||
| 97 | class => shift, | ||||||||||||||||||||||||||||
| 98 | api => $self, | ||||||||||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 8 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 104 | 8 | my $method = shift; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 8 | ▶ | my $call = $self->_xmlrpc()->call( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | $method, | ||||||||||||||||||||||||||||
| 108 | ( $method eq 'wp.getUsersBlogs' ? () : $self->blog_id() ), | ||||||||||||||||||||||||||||
| 109 | $self->username(), | ||||||||||||||||||||||||||||
| 110 | $self->password(), | ||||||||||||||||||||||||||||
| 111 | @_, | ||||||||||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 8 | $self->_check_for_error( $call, $method ); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 7 | return $call->result() | |||||||||||||||||||||||||||
| 117 | or confess | ||||||||||||||||||||||||||||
| 118 | "No result from call to $method XML-RPC method and no error!"; | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 8 | ▶ | sub _check_for_error { | ||||||||||||||||||||||||||
|
Subroutine
_check_for_error: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 122 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 123 | 8 | my $call = shift; | |||||||||||||||||||||||||||
| 124 | 8 | my $method = shift; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 8 | ▶ | my $fault = $call->fault() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | or return; | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 1 | my @pieces; | |||||||||||||||||||||||||||
| 130 | 1 | for my $key (qw( Code String Detail )) { | |||||||||||||||||||||||||||
| 131 | 3 | my $value = $fault->{'fault'.$key}; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 3 | ▶ | next unless defined $value && length $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $value and length $value
MC/DC50%1/2
defined $value and length $value
defined $value
length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 2 | push @pieces, "$key = $value"; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 1 | my $error = "Error calling $method XML-RPC method: "; | |||||||||||||||||||||||||||
| 139 | 1 | $error .= join ' - ', @pieces; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 1 | local $Carp::CarpLevel = $Carp::CarpLevel + 2; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 1 | confess $error; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 4 | __PACKAGE__->meta()->make_immutable(); | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 4 | 1; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # ABSTRACT: An interface to the WordPress XML-RPC API | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | __END__ | ||||||||||||||||||||||||||||