Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UtilityService
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 get_insta_api_key
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
6
 get_migration_data
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2namespace NewfoldLabs\WP\Module\Migration\Services;
3
4/**
5 * Utility Service
6 */
7class UtilityService {
8    /**
9     * Get the api key from worker
10     *
11     * @param string $brand name of the brand
12     */
13    public static function get_insta_api_key( $brand ) {
14        $insta_cf_worker = NFD_MIGRATION_PROXY_WORKER . '/token?brand=' . $brand;
15        $insta_cf_data   = wp_remote_get(
16            $insta_cf_worker,
17            array(
18                'headers' => array(
19                    'Content-Type'  => 'application/json',
20                    'Accept'        => 'application/json',
21                    'PHP_VERSION'   => PHP_VERSION,
22                    'migration_key' => true,
23                    'site_url'      => get_option( 'siteurl', '' ),
24                ),
25            )
26        );
27        $insta_response  = json_decode( wp_remote_retrieve_body( $insta_cf_data ) );
28
29        // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
30        return $insta_response ? base64_decode( $insta_response->data ) : '';
31    }
32
33    /**
34     * Get migration status and source url by instaWp api
35     *
36     * @param string $migrate_group_uuid migration group id (it is stored in instawp_last_migration_details option).
37     * @return array
38     */
39    public static function get_migration_data( $migrate_group_uuid ) {
40        if ( ! empty( $migrate_group_uuid ) ) {
41            $token = self::get_insta_api_key( BRAND_PLUGIN );
42            if ( $token ) {
43                $response = wp_remote_get(
44                    'https://app.instawp.io/api/v2/migrates-v3/status/' . $migrate_group_uuid,
45                    array(
46                        'headers' => array(
47                            'Authorization' => 'Bearer ' . $token,
48                        ),
49                    )
50                );
51                if ( wp_remote_retrieve_response_code( $response ) === 200 && ! is_wp_error( $response ) ) {
52                    $body = wp_remote_retrieve_body( $response );
53                    return json_decode( $body, true );
54                }
55            }
56        }
57
58        return array();
59    }
60}