wp-pay-gateways /
mollie
| 1 | <?php |
||||||
| 2 | /** |
||||||
| 3 | * CLI |
||||||
| 4 | * |
||||||
| 5 | * @author Pronamic <[email protected]> |
||||||
| 6 | * @copyright 2005-2020 Pronamic |
||||||
| 7 | * @license GPL-3.0-or-later |
||||||
| 8 | * @package Pronamic\WordPress\Pay\Mollie |
||||||
| 9 | */ |
||||||
| 10 | |||||||
| 11 | namespace Pronamic\WordPress\Pay\Gateways\Mollie; |
||||||
| 12 | |||||||
| 13 | /** |
||||||
| 14 | * Title: CLI |
||||||
| 15 | * Description: |
||||||
| 16 | * Copyright: 2005-2020 Pronamic |
||||||
| 17 | * Company: Pronamic |
||||||
| 18 | * |
||||||
| 19 | * @author Remco Tolsma |
||||||
| 20 | * @version 3.0.0 |
||||||
| 21 | * @since 3.0.0 |
||||||
| 22 | * @link https://github.com/woocommerce/woocommerce/blob/3.9.0/includes/class-wc-cli.php |
||||||
| 23 | */ |
||||||
| 24 | class CLI { |
||||||
| 25 | /** |
||||||
| 26 | * Construct CLI. |
||||||
| 27 | */ |
||||||
| 28 | public function __construct() { |
||||||
| 29 | \WP_CLI::add_command( |
||||||
|
0 ignored issues
–
show
|
|||||||
| 30 | 'pronamic-pay mollie organizations synchronize', |
||||||
| 31 | function( $args, $assoc_args ) { |
||||||
| 32 | $this->wp_cli_organizations_synchronize( $args, $assoc_args ); |
||||||
| 33 | } |
||||||
| 34 | ); |
||||||
| 35 | |||||||
| 36 | \WP_CLI::add_command( |
||||||
| 37 | 'pronamic-pay mollie customers synchronize', |
||||||
| 38 | function( $args, $assoc_args ) { |
||||||
| 39 | $this->wp_cli_customers_synchronize( $args, $assoc_args ); |
||||||
| 40 | } |
||||||
| 41 | ); |
||||||
| 42 | |||||||
| 43 | \WP_CLI::add_command( |
||||||
| 44 | 'pronamic-pay mollie customers connect-wp-users', |
||||||
| 45 | function( $args, $assoc_args ) { |
||||||
| 46 | $this->wp_cli_customers_connect_wp_users( $args, $assoc_args ); |
||||||
| 47 | } |
||||||
| 48 | ); |
||||||
| 49 | |||||||
| 50 | // Data Stores. |
||||||
| 51 | $this->profile_data_store = new ProfileDataStore(); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 52 | $this->customer_data_store = new CustomerDataStore(); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 53 | } |
||||||
| 54 | |||||||
| 55 | /** |
||||||
| 56 | * CLI organizations synchronize. |
||||||
| 57 | * |
||||||
| 58 | * @link https://docs.mollie.com/reference/v2/organizations-api/current-organization |
||||||
| 59 | * @param array $args Arguments. |
||||||
| 60 | * @param array $assoc_args Associative arguments. |
||||||
| 61 | */ |
||||||
| 62 | public function wp_cli_organizations_synchronize( $args, $assoc_args ) { |
||||||
| 63 | \WP_CLI::error( 'Command not implemented yet.' ); |
||||||
| 64 | } |
||||||
| 65 | |||||||
| 66 | /** |
||||||
| 67 | * CLI customers synchronize. |
||||||
| 68 | * |
||||||
| 69 | * @link https://docs.mollie.com/reference/v2/customers-api/list-customers |
||||||
| 70 | * @param array $args Arguments. |
||||||
| 71 | * @param array $assoc_args Associative arguments. |
||||||
| 72 | */ |
||||||
| 73 | public function wp_cli_customers_synchronize( $args, $assoc_args ) { |
||||||
| 74 | global $post; |
||||||
| 75 | global $wpdb; |
||||||
| 76 | |||||||
| 77 | $query = new \WP_Query( |
||||||
| 78 | array( |
||||||
| 79 | 'post_type' => 'pronamic_gateway', |
||||||
| 80 | 'post_status' => 'publish', |
||||||
| 81 | 'nopaging' => true, |
||||||
| 82 | 'meta_query' => array( |
||||||
| 83 | array( |
||||||
| 84 | 'key' => '_pronamic_gateway_id', |
||||||
| 85 | 'value' => 'mollie', |
||||||
| 86 | ), |
||||||
| 87 | array( |
||||||
| 88 | 'key' => '_pronamic_gateway_mollie_api_key', |
||||||
| 89 | 'compare' => 'EXISTS', |
||||||
| 90 | ), |
||||||
| 91 | ), |
||||||
| 92 | ) |
||||||
| 93 | ); |
||||||
| 94 | |||||||
| 95 | if ( $query->have_posts() ) { |
||||||
| 96 | while ( $query->have_posts() ) { |
||||||
| 97 | $query->the_post(); |
||||||
| 98 | |||||||
| 99 | $api_key = get_post_meta( $post->ID, '_pronamic_gateway_mollie_api_key', true ); |
||||||
| 100 | |||||||
| 101 | \WP_CLI::log( $post->post_title ); |
||||||
| 102 | \WP_CLI::log( $api_key ); |
||||||
| 103 | \WP_CLI::log( '' ); |
||||||
| 104 | |||||||
| 105 | $client = new Client( $api_key ); |
||||||
|
0 ignored issues
–
show
It seems like
$api_key can also be of type false; however, parameter $api_key of Pronamic\WordPress\Pay\G...e\Client::__construct() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 106 | |||||||
| 107 | $urls = array( |
||||||
| 108 | 'https://api.mollie.com/v2/customers?limit=250', |
||||||
| 109 | ); |
||||||
| 110 | |||||||
| 111 | $profile = Profile::from_object( $client->get_current_profile() ); |
||||||
| 112 | |||||||
| 113 | $profile_id = $this->profile_data_store->save_profile( |
||||||
| 114 | $profile, |
||||||
| 115 | array( |
||||||
| 116 | 'api_key_live' => ( 'live_' === substr( $api_key, 0, 5 ) ) ? $api_key : null, |
||||||
|
0 ignored issues
–
show
It seems like
$api_key can also be of type false; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 117 | 'api_key_test' => ( 'test_' === substr( $api_key, 0, 5 ) ) ? $api_key : null, |
||||||
| 118 | ), |
||||||
| 119 | array( |
||||||
| 120 | 'api_key_live' => '%s', |
||||||
| 121 | 'api_key_test' => '%s', |
||||||
| 122 | ) |
||||||
| 123 | ); |
||||||
| 124 | |||||||
| 125 | while ( ! empty( $urls ) ) { |
||||||
| 126 | $url = array_shift( $urls ); |
||||||
| 127 | |||||||
| 128 | \WP_CLI::log( $url ); |
||||||
| 129 | |||||||
| 130 | $response = $client->send_request( $url ); |
||||||
| 131 | |||||||
| 132 | if ( isset( $response->count ) ) { |
||||||
| 133 | \WP_CLI::log( |
||||||
| 134 | \sprintf( |
||||||
| 135 | 'Found %d customer(s).', |
||||||
| 136 | $response->count |
||||||
| 137 | ) |
||||||
| 138 | ); |
||||||
| 139 | } |
||||||
| 140 | |||||||
| 141 | if ( isset( $response->_embedded->customers ) ) { |
||||||
| 142 | \WP_CLI\Utils\format_items( |
||||||
|
0 ignored issues
–
show
The function
format_items was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 143 | 'table', |
||||||
| 144 | $response->_embedded->customers, |
||||||
| 145 | array( |
||||||
| 146 | 'id', |
||||||
| 147 | 'mode', |
||||||
| 148 | 'name', |
||||||
| 149 | 'email', |
||||||
| 150 | 'locale', |
||||||
| 151 | ) |
||||||
| 152 | ); |
||||||
| 153 | |||||||
| 154 | foreach ( $response->_embedded->customers as $object ) { |
||||||
| 155 | $customer = Customer::from_object( $object ); |
||||||
| 156 | |||||||
| 157 | $customer_id = $this->customer_data_store->save_customer( |
||||||
| 158 | $customer, |
||||||
| 159 | array( |
||||||
| 160 | 'profile_id' => $profile_id, |
||||||
| 161 | ), |
||||||
| 162 | array( |
||||||
| 163 | 'profile_id' => '%d', |
||||||
| 164 | ) |
||||||
| 165 | ); |
||||||
| 166 | } |
||||||
| 167 | } |
||||||
| 168 | |||||||
| 169 | if ( isset( $response->_links->next->href ) ) { |
||||||
| 170 | $urls[] = $response->_links->next->href; |
||||||
| 171 | } |
||||||
| 172 | } |
||||||
| 173 | } |
||||||
| 174 | |||||||
| 175 | \wp_reset_postdata(); |
||||||
| 176 | } |
||||||
| 177 | } |
||||||
| 178 | |||||||
| 179 | /** |
||||||
| 180 | * CLI connect Mollie customers to WordPress users. |
||||||
| 181 | * |
||||||
| 182 | * @link https://docs.mollie.com/reference/v2/customers-api/list-customers |
||||||
| 183 | * @link https://make.wordpress.org/cli/handbook/internal-api/wp-cli-add-command/ |
||||||
| 184 | * @link https://developer.wordpress.org/reference/classes/wpdb/query/ |
||||||
| 185 | * @param array $args Arguments. |
||||||
| 186 | * @param array $assoc_args Associative arguments. |
||||||
| 187 | */ |
||||||
| 188 | public function wp_cli_customers_connect_wp_users( $args, $assoc_args ) { |
||||||
| 189 | global $wpdb; |
||||||
| 190 | |||||||
| 191 | $query = " |
||||||
| 192 | INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users ( |
||||||
| 193 | customer_id, |
||||||
| 194 | user_id |
||||||
| 195 | ) |
||||||
| 196 | SELECT |
||||||
| 197 | mollie_customer.id AS mollie_customer_id, |
||||||
| 198 | wp_user.ID AS wp_user_id |
||||||
| 199 | FROM |
||||||
| 200 | $wpdb->pronamic_pay_mollie_customers AS mollie_customer |
||||||
| 201 | INNER JOIN |
||||||
| 202 | $wpdb->users AS wp_user |
||||||
| 203 | ON mollie_customer.email = wp_user.user_email |
||||||
| 204 | ; |
||||||
| 205 | "; |
||||||
| 206 | |||||||
| 207 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Prepare is OK. |
||||||
| 208 | $result = $wpdb->query( $query ); |
||||||
| 209 | |||||||
| 210 | if ( false === $result ) { |
||||||
| 211 | \WP_CLI::error( |
||||||
| 212 | sprintf( |
||||||
| 213 | 'Database error: %s.', |
||||||
| 214 | $wpdb->last_error |
||||||
| 215 | ) |
||||||
| 216 | ); |
||||||
| 217 | } |
||||||
| 218 | |||||||
| 219 | \WP_CLI::log( |
||||||
| 220 | sprintf( |
||||||
| 221 | 'Connected %d users and Mollie customers.', |
||||||
| 222 | $result |
||||||
| 223 | ) |
||||||
| 224 | ); |
||||||
| 225 | } |
||||||
| 226 | } |
||||||
| 227 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths