| 1 |
<?php |
| 2 |
|
| 3 |
namespace Photonic_Plugin\Admin\Wizard; |
| 4 |
|
| 5 |
use Photonic_Plugin\Core\Utilities; |
| 6 |
|
| 7 |
class Instagram extends Source { |
| 8 |
private static ?Instagram $instance = null; |
| 9 |
|
| 10 |
protected function __construct() { |
| 11 |
parent::__construct(); |
| 12 |
$this->provider = 'instagram'; |
| 13 |
$this->api_base = Wizard::base_apis()['instagram']; |
| 14 |
} |
| 15 |
|
| 16 |
public static function get_instance(): Instagram { |
| 17 |
if (null === self::$instance) { |
| 18 |
self::$instance = new Instagram(); |
| 19 |
} |
| 20 |
return self::$instance; |
| 21 |
} |
| 22 |
|
| 23 |
public function get_screen_2(): array { |
| 24 |
return [ |
| 25 |
'header' => esc_html__('Choose Type of Gallery', 'photonic'), |
| 26 |
'display' => [ |
| 27 |
'display_type' => [ |
| 28 |
'desc' => esc_html__('What do you want to show?', 'photonic'), |
| 29 |
'type' => 'select', |
| 30 |
'options' => [ |
| 31 |
'' => '', |
| 32 |
'single-photo' => esc_html__('Single Photo', 'photonic'), |
| 33 |
'album-photo' => esc_html__('Photos in a Post', 'photonic'), |
| 34 |
'multi-photo' => esc_html__('Multiple Photos', 'photonic'), |
| 35 |
], |
| 36 |
'req' => 1, |
| 37 |
], |
| 38 |
], |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
public function get_screen_3(): array { |
| 43 |
return [ |
| 44 |
'header' => esc_html__('Build your gallery', 'photonic'), |
| 45 |
'single-photo' => [ |
| 46 |
'header' => esc_html__('Pick a photo', 'photonic'), |
| 47 |
'desc' => esc_html__('From the list below pick the single photo you wish to display.', 'photonic'), |
| 48 |
'display' => [ |
| 49 |
'container' => [ |
| 50 |
'type' => 'thumbnail-selector', |
| 51 |
'mode' => 'single', |
| 52 |
'for' => 'selected_data', |
| 53 |
], |
| 54 |
], |
| 55 |
], |
| 56 |
'album-photo' => [ |
| 57 |
'header' => esc_html__('Pick a post (a.k.a. Carousel)', 'photonic'), |
| 58 |
'desc' => esc_html__('From the list below pick the post whose photos you wish to display.', 'photonic'), |
| 59 |
'display' => [ |
| 60 |
'carousel_caption' => [ |
| 61 |
'desc' => esc_html__('Where do you want to show the caption for the post / carousel?', 'photonic'), |
| 62 |
'type' => 'select', |
| 63 |
'options' => [ |
| 64 |
'' => $this->default_from_settings, |
| 65 |
'none' => esc_html__('Do not show the caption', 'photonic'), |
| 66 |
'above' => esc_html__('Show above the photos', 'photonic'), |
| 67 |
'below' => esc_html__('Show below the photos', 'photonic'), |
| 68 |
], |
| 69 |
'std' => '', |
| 70 |
'hint' => sprintf($this->default_under, '<em>Photonic → Settings → Instagram → Instagram Settings → Caption positioning for carousels / posts</em>'), |
| 71 |
], |
| 72 |
'container' => [ |
| 73 |
'type' => 'thumbnail-selector', |
| 74 |
'mode' => 'single', |
| 75 |
'for' => 'selected_data', |
| 76 |
], |
| 77 |
], |
| 78 |
], |
| 79 |
'multi-photo' => [ |
| 80 |
'header' => esc_html__('All your photos', 'photonic'), |
| 81 |
'desc' => esc_html__('You can only show all your photos without filtering. In the following only the latest 25 photos are displayed. You can change this in subsequent screens.', 'photonic'), |
| 82 |
'display' => [ |
| 83 |
'carousel_handling' => [ |
| 84 |
'desc' => esc_html__('How do you want to show photos in a carousel?', 'photonic'), |
| 85 |
'type' => 'select', |
| 86 |
'options' => [ |
| 87 |
'' => '', |
| 88 |
'single' => esc_html__('Show first photo', 'photonic'), |
| 89 |
'expand' => esc_html__('Show all photos', 'photonic'), |
| 90 |
], |
| 91 |
'std' => '', |
| 92 |
], |
| 93 |
'container' => [ |
| 94 |
'type' => 'thumbnail-selector', |
| 95 |
'mode' => 'none', |
| 96 |
'for' => 'selected_data', |
| 97 |
], |
| 98 |
], |
| 99 |
], |
| 100 |
]; |
| 101 |
} |
| 102 |
|
| 103 |
public function get_screen_4(): array { |
| 104 |
return []; |
| 105 |
} |
| 106 |
|
| 107 |
public function get_screen_5(): array { |
| 108 |
global $photonic_instagram_media; |
| 109 |
return [ |
| 110 |
'instagram' => [ |
| 111 |
'media' => [ |
| 112 |
'desc' => esc_html__('Media to Show', 'photonic'), |
| 113 |
'type' => 'select', |
| 114 |
'options' => Utilities::media_options(true, $photonic_instagram_media), |
| 115 |
'std' => '', |
| 116 |
'hint' => sprintf($this->default_under, '<em>Photonic → Settings → Instagram → Instagram Settings → Media to show</em>'), |
| 117 |
], |
| 118 |
] |
| 119 |
]; |
| 120 |
} |
| 121 |
|
| 122 |
public function get_square_size_options(): array { |
| 123 |
return []; |
| 124 |
} |
| 125 |
|
| 126 |
public function get_random_size_options(): array { |
| 127 |
return []; |
| 128 |
} |
| 129 |
|
| 130 |
public function make_request($display_type, $for, $flattened_fields): array { |
| 131 |
require_once PHOTONIC_PATH . '/Platforms/Instagram.php'; |
| 132 |
$module = \Photonic_Plugin\Platforms\Instagram::get_instance(); |
| 133 |
$base_url = 'https://' . $this->api_base . '/me/media?fields=' . $module->field_list . '&access_token=' . $module->access_token; |
| 134 |
$response = wp_remote_request($base_url, ['sslverify' => PHOTONIC_SSL_VERIFY]); |
| 135 |
|
| 136 |
return [$response, [], $base_url]; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Processes a response from Instagram to build it out into a gallery of thumbnails. Instagram only has L1 displays. |
| 141 |
* |
| 142 |
* @param $response |
| 143 |
* @param $display_type |
| 144 |
* @param $url |
| 145 |
* @param array $pagination |
| 146 |
* @return array |
| 147 |
*/ |
| 148 |
public function process_response($response, $display_type, $url = null, &$pagination = []): array { |
| 149 |
$objects = []; |
| 150 |
$body = json_decode($response['body']); |
| 151 |
if (isset($body->data)) { |
| 152 |
$data = $body->data; |
| 153 |
foreach ($data as $photo) { |
| 154 |
if (isset($photo->media_type) && ('image' === strtolower($photo->media_type) || 'carousel_album' === strtolower($photo->media_type) || 'video' === strtolower($photo->media_type))) { |
| 155 |
if (('album-photo' === $display_type && 'carousel_album' === strtolower($photo->media_type)) || 'album-photo' !== $display_type) { |
| 156 |
$object = []; |
| 157 |
$link = $photo->permalink; |
| 158 |
$link = explode('/', $link); |
| 159 |
if (empty($link[count($link) - 1])) { |
| 160 |
$link = $link[count($link) - 2]; |
| 161 |
} |
| 162 |
else { |
| 163 |
$link = $link[count($link) - 1]; |
| 164 |
} |
| 165 |
$object['alt_id'] = $link; |
| 166 |
$object['id'] = $photo->id; |
| 167 |
if (isset($photo->caption)) { |
| 168 |
$object['title'] = esc_attr($photo->caption); |
| 169 |
} |
| 170 |
else { |
| 171 |
$object['title'] = ''; |
| 172 |
} |
| 173 |
$object['thumbnail'] = empty($photo->thumbnail_url) ? $photo->media_url : $photo->thumbnail_url; |
| 174 |
$objects[] = $object; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
if (isset($body->paging) && isset($body->paging->next) && 'single-photo' === $display_type) { |
| 180 |
$pagination['url'] = $body->paging->next; // add_query_arg(['max_id' => $body->pagination->next_max_id], remove_query_arg(['max_id'], $url)); |
| 181 |
} |
| 182 |
} |
| 183 |
return $objects; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* @param $display_type |
| 188 |
* @return array |
| 189 |
*/ |
| 190 |
public function construct_shortcode_from_screen_selections($display_type): array { |
| 191 |
$short_code = []; |
| 192 |
|
| 193 |
if (check_ajax_referer('photonic-wizard-next-' . get_current_user_id())) { |
| 194 |
if ('single-photo' === $display_type) { |
| 195 |
$short_code['media_id'] = sanitize_text_field(wp_unslash($_POST['selected_data'] ?? '')); |
| 196 |
} |
| 197 |
elseif ('album-photo' === $display_type) { |
| 198 |
$short_code['carousel'] = sanitize_text_field(wp_unslash($_POST['selected_data'] ?? '')); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return $short_code; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* @param $input |
| 207 |
* @return array |
| 208 |
*/ |
| 209 |
public function deconstruct_shortcode_to_screen_selections($input): array { |
| 210 |
$deconstructed = []; |
| 211 |
|
| 212 |
if (!empty($input->media_id)) { |
| 213 |
$deconstructed['display_type'] = 'single-photo'; |
| 214 |
$deconstructed['selected_data'] = $input->media_id; |
| 215 |
} |
| 216 |
elseif (!empty($input->carousel)) { |
| 217 |
$deconstructed['display_type'] = 'album-photo'; |
| 218 |
$deconstructed['selected_data'] = $input->carousel; |
| 219 |
} |
| 220 |
else { |
| 221 |
$deconstructed['display_type'] = 'multi-photo'; |
| 222 |
} |
| 223 |
|
| 224 |
return $deconstructed; |
| 225 |
} |
| 226 |
} |
| 227 |
|