| @@ -1,120 +1,144 @@ | ||
| 1 | -<?php | |
| 2 | -function ___tagembed__wpApiCall($apiUrl, $body, $header = null, $breakResponse = false) | |
| 3 | -{ | |
| 4 | - $header = (null != $header ? $header : []); | |
| 5 | - $args = ['body' => $body, 'timeout' => '30', 'redirection' => '5', 'httpversion' => '1.0', 'blocking' => true, 'headers' => $header, 'cookies' => []]; | |
| 6 | - $response = wp_remote_post($apiUrl, $args); | |
| 7 | - if ($breakResponse) : | |
| 8 | - ___tagembed__dd($response); | |
| 9 | - endif; | |
| 10 | - if (!is_wp_error($response)) : | |
| 11 | - if (isset($response['body']) && !empty($response['body'])) : | |
| 12 | - return json_decode($response['body']); | |
| 13 | - else : | |
| 14 | - return; | |
| 15 | - endif; | |
| 16 | - else : | |
| 17 | - ___tagembed__wpApiCall($apiUrl, $body, $header = null, $breakResponse = false); | |
| 18 | - endif; | |
| 19 | -} | |
| 20 | -function ___tagembed__manageApiResponse($response) | |
| 21 | -{ | |
| 22 | - if (empty($response->head)) : | |
| 23 | - return ___tagembed__exitWithDanger(); | |
| 24 | - endif; | |
| 25 | - $responseCode = $response->head->code; | |
| 26 | - switch ($responseCode) { | |
| 27 | - case 200: | |
| 28 | - if ($response->head->status) : | |
| 29 | - if (!empty($response->body)) : | |
| 30 | - return $response->body; | |
| 31 | - endif; | |
| 32 | - if (!empty($response->head->message)) : | |
| 33 | - return ___tagembed__exitWithSuccess($response->head->message); | |
| 34 | - else : | |
| 35 | - return ___tagembed__exitWithSuccess(); | |
| 36 | - endif; | |
| 37 | - else : | |
| 38 | - if (!empty($response->head->message)) : | |
| 39 | - return ___tagembed__exitWithDanger($response->head->message); | |
| 40 | - else : | |
| 41 | - return ___tagembed__exitWithDanger(); | |
| 42 | - endif; | |
| 43 | - endif; | |
| 44 | - break; | |
| 45 | - case 412: | |
| 46 | - /* --Start-- Manage Validation Error */ | |
| 47 | - if (empty($response->body)) : | |
| 48 | - return ___tagembed__exitWithDanger(); | |
| 49 | - else : | |
| 50 | - return ___tagembed__exitWithDanger('Validation Error', $response->body); | |
| 51 | - endif; | |
| 52 | - /* --End-- Manage Validation Error */ | |
| 53 | - break; | |
| 54 | - default: | |
| 55 | - if (!empty($response->head->message)) : | |
| 56 | - return ___tagembed__exitWithDanger($response->head->message); | |
| 57 | - else : | |
| 58 | - return ___tagembed__exitWithDanger(); | |
| 59 | - endif; | |
| 60 | - } | |
| 61 | -} | |
| 62 | -function ___tagembed__IsBase64($data) | |
| 63 | -{ | |
| 64 | - $decoded_data = base64_decode($data, true); | |
| 65 | - $encoded_data = base64_encode($decoded_data); | |
| 66 | - if ($encoded_data != $data) : | |
| 67 | - return false; | |
| 68 | - elseif (!ctype_print($decoded_data)) : | |
| 69 | - return false; | |
| 70 | - else : | |
| 71 | - return true; | |
| 72 | - endif; | |
| 73 | -} | |
| 74 | -function ___tagembed__exitWithSuccess($data = null) | |
| 75 | -{ | |
| 76 | - echo wp_json_encode(['status' => (bool)true, 'data' => (array)$data, 'message' => (string)'OK']); | |
| 77 | - exit; | |
| 78 | -} | |
| 79 | -function ___tagembed__exitWithDanger($error = null, $data = []) | |
| 80 | -{ | |
| 81 | - echo wp_json_encode(['status' => (bool)false, 'data' => (array)$data, 'message' => (string)('' != $error ? $error : 'Oh snap! Something went wrong.')]); | |
| 82 | - exit; | |
| 83 | -} | |
| 84 | -function ___tagembed__d($data = 'NONE') | |
| 85 | -{ | |
| 86 | - echo '<pre>'; | |
| 87 | - print_r($data); | |
| 88 | - echo '</pre>'; | |
| 89 | -} | |
| 90 | -function ___tagembed__dd($data = 'NONE') | |
| 91 | -{ | |
| 92 | - echo '<pre>'; | |
| 93 | - print_r($data); | |
| 94 | - echo '</pre>'; | |
| 95 | - die; | |
| 96 | -} | |
| 97 | -function ___tagembed__convertObjectToArray($data) | |
| 98 | -{ | |
| 99 | - $data = wp_json_encode($data); | |
| 100 | - return json_decode($data, true); | |
| 101 | -} | |
| 102 | -/* --Start__ Sanetize All Input */ | |
| 103 | -function ___tagembed__inputSanetize($data) | |
| 104 | -{ | |
| 105 | - $data = (string)$data; | |
| 106 | - if (preg_match('/<[^>]*>/', $data)) : | |
| 107 | - return ___tagembed__exitWithDanger('Special characters are not allowed. Please remove them and try again.'); | |
| 108 | - endif; | |
| 109 | -} | |
| 110 | -/* --End Sanetize All Input */ | |
| 111 | -/* --Start-- Sanitize Request Data */ | |
| 112 | -function ___tagembed__sanitizeRequestData($__tagembed__request_input_data) | |
| 113 | -{ | |
| 114 | - $__tagembed__Input_return_data = []; | |
| 115 | - foreach ($__tagembed__request_input_data as $__tagembed__request_input_key => $__tagembed__request_input) : | |
| 116 | - $__tagembed__Input_return_data[$__tagembed__request_input_key] = sanitize_text_field($__tagembed__request_input); | |
| 117 | - endforeach; | |
| 118 | - return $__tagembed__Input_return_data; | |
| 119 | -} | |
| 120 | -/*--End-- Sanitize Request Data*/ | |
| 1 | +<?php | |
| 2 | +if (!defined('ABSPATH')) : | |
| 3 | + exit; | |
| 4 | +endif; | |
| 5 | +function ___tagembed__wpApiCall($apiUrl, $body, $header = null) | |
| 6 | +{ | |
| 7 | + $header = (null != $header ? $header : []); | |
| 8 | + $args = ['body' => $body, 'timeout' => '30', 'redirection' => '5', 'httpversion' => '1.0', 'blocking' => true, 'headers' => $header, 'cookies' => []]; | |
| 9 | + $response = wp_remote_post($apiUrl, $args); | |
| 10 | + if (is_wp_error($response)) : | |
| 11 | + return; | |
| 12 | + endif; | |
| 13 | + if (isset($response['body']) && !empty($response['body'])) : | |
| 14 | + return json_decode($response['body']); | |
| 15 | + endif; | |
| 16 | + return; | |
| 17 | +} | |
| 18 | +function ___tagembed__manageApiResponse($response) | |
| 19 | +{ | |
| 20 | + if (empty($response->head)) : | |
| 21 | + return ___tagembed__exitWithDanger(); | |
| 22 | + endif; | |
| 23 | + $responseCode = $response->head->code; | |
| 24 | + switch ($responseCode) { | |
| 25 | + case 200: | |
| 26 | + if ($response->head->status) : | |
| 27 | + if (!empty($response->body)) : | |
| 28 | + return $response->body; | |
| 29 | + endif; | |
| 30 | + if (!empty($response->head->message)) : | |
| 31 | + return ___tagembed__exitWithSuccess($response->head->message); | |
| 32 | + else : | |
| 33 | + return ___tagembed__exitWithSuccess(); | |
| 34 | + endif; | |
| 35 | + else : | |
| 36 | + if (!empty($response->head->message)) : | |
| 37 | + return ___tagembed__exitWithDanger($response->head->message); | |
| 38 | + else : | |
| 39 | + return ___tagembed__exitWithDanger(); | |
| 40 | + endif; | |
| 41 | + endif; | |
| 42 | + break; | |
| 43 | + case 412: | |
| 44 | + /* --Start-- Manage Validation Error */ | |
| 45 | + if (empty($response->body)) : | |
| 46 | + return ___tagembed__exitWithDanger(); | |
| 47 | + else : | |
| 48 | + return ___tagembed__exitWithDanger('Validation Error', $response->body); | |
| 49 | + endif; | |
| 50 | + /* --End-- Manage Validation Error */ | |
| 51 | + break; | |
| 52 | + default: | |
| 53 | + if (!empty($response->head->message)) : | |
| 54 | + return ___tagembed__exitWithDanger($response->head->message); | |
| 55 | + else : | |
| 56 | + return ___tagembed__exitWithDanger(); | |
| 57 | + endif; | |
| 58 | + } | |
| 59 | +} | |
| 60 | + | |
| 61 | +function ___tagembed__exitWithSuccess($data = null) | |
| 62 | +{ | |
| 63 | + wp_send_json(['status' => (bool)true, 'data' => (array)$data, 'message' => (string)'OK']); | |
| 64 | +} | |
| 65 | +function ___tagembed__exitWithDanger($error = null, $data = []) | |
| 66 | +{ | |
| 67 | + wp_send_json(['status' => (bool)false, 'data' => (array)$data, 'message' => (string)('' != $error ? $error : 'Oh snap! Something went wrong.')]); | |
| 68 | +} | |
| 69 | + | |
| 70 | +/* --Start__ Sanetize All Input */ | |
| 71 | +function ___tagembed__inputSanetize($data) | |
| 72 | +{ | |
| 73 | + if (is_array($data)) : | |
| 74 | + foreach ($data as $__tagembed__input_sanetize_item) : | |
| 75 | + ___tagembed__inputSanetize($__tagembed__input_sanetize_item); | |
| 76 | + endforeach; | |
| 77 | + return; | |
| 78 | + endif; | |
| 79 | + $data = (string)$data; | |
| 80 | + if (preg_match('/<[^>]*>/', $data)) : | |
| 81 | + return ___tagembed__exitWithDanger('Special characters are not allowed. Please remove them and try again.'); | |
| 82 | + endif; | |
| 83 | +} | |
| 84 | +/* --End Sanetize All Input */ | |
| 85 | +/* --Start-- Sanitize Request Data */ | |
| 86 | +function ___tagembed__sanitizeRequestData($__tagembed__request_input_data) | |
| 87 | +{ | |
| 88 | + $__tagembed__Input_return_data = []; | |
| 89 | + foreach ($__tagembed__request_input_data as $__tagembed__request_input_key => $__tagembed__request_input) : | |
| 90 | + if (is_array($__tagembed__request_input)) : | |
| 91 | + $__tagembed__Input_return_data[$__tagembed__request_input_key] = ___tagembed__sanitizeRequestData($__tagembed__request_input); | |
| 92 | + else : | |
| 93 | + $__tagembed__Input_return_data[$__tagembed__request_input_key] = sanitize_text_field($__tagembed__request_input); | |
| 94 | + endif; | |
| 95 | + endforeach; | |
| 96 | + return $__tagembed__Input_return_data; | |
| 97 | +} | |
| 98 | +/*--End-- Sanitize Request Data*/ | |
| 99 | + | |
| 100 | +function ___tagembed__expandLinkedinShortUrl($__tagembed__linkedinShortUrl) | |
| 101 | +{ | |
| 102 | + $__tagembed__expandedUrl = $__tagembed__linkedinShortUrl; | |
| 103 | + for ($__tagembed__hop = 0; $__tagembed__hop < 5; $__tagembed__hop++) : | |
| 104 | + $__tagembed__currentHost = wp_parse_url($__tagembed__expandedUrl, PHP_URL_HOST); | |
| 105 | + $__tagembed__currentHost = is_string($__tagembed__currentHost) ? strtolower($__tagembed__currentHost) : ''; | |
| 106 | + if ($__tagembed__currentHost !== 'lnkd.in' && substr($__tagembed__currentHost, -8) !== '.lnkd.in') : | |
| 107 | + break; | |
| 108 | + endif; | |
| 109 | + $__tagembed__response = wp_remote_head($__tagembed__expandedUrl, ['timeout' => 10, 'redirection' => 0, 'httpversion' => '1.1', 'blocking' => true]); | |
| 110 | + if (is_wp_error($__tagembed__response)) : | |
| 111 | + return ''; | |
| 112 | + endif; | |
| 113 | + $__tagembed__location = wp_remote_retrieve_header($__tagembed__response, 'location'); | |
| 114 | + if (is_array($__tagembed__location)) : | |
| 115 | + $__tagembed__location = end($__tagembed__location); | |
| 116 | + endif; | |
| 117 | + $__tagembed__location = is_string($__tagembed__location) ? trim($__tagembed__location) : ''; | |
| 118 | + if ($__tagembed__location === '') : | |
| 119 | + break; | |
| 120 | + endif; | |
| 121 | + if (stripos($__tagembed__location, 'http') !== 0) : | |
| 122 | + return ''; | |
| 123 | + endif; | |
| 124 | + $__tagembed__expandedUrl = $__tagembed__location; | |
| 125 | + endfor; | |
| 126 | + return $__tagembed__expandedUrl === $__tagembed__linkedinShortUrl ? '' : $__tagembed__expandedUrl; | |
| 127 | +} | |
| 128 | +function ___tagembed__parseLinkedinPostUrl($__tagembed__linkedinPostUrl) | |
| 129 | +{ | |
| 130 | + $__tagembed__postPath = wp_parse_url($__tagembed__linkedinPostUrl, PHP_URL_PATH); | |
| 131 | + if (!is_string($__tagembed__postPath) || $__tagembed__postPath === '') : | |
| 132 | + return []; | |
| 133 | + endif; | |
| 134 | + $__tagembed__postPath = rtrim($__tagembed__postPath, '/'); | |
| 135 | + if (!preg_match('/(?:urn:li:|[-_\/])(activity|ugcPost|share)[-_:]([0-9]{6,30})/i', $__tagembed__postPath, $__tagembed__matchedPost)) : | |
| 136 | + return []; | |
| 137 | + endif; | |
| 138 | + $__tagembed__postTypeMap = ['activity' => 'activity', 'ugcpost' => 'ugcPost', 'share' => 'share']; | |
| 139 | + $__tagembed__postType = strtolower($__tagembed__matchedPost[1]); | |
| 140 | + if (!isset($__tagembed__postTypeMap[$__tagembed__postType])) : | |
| 141 | + return []; | |
| 142 | + endif; | |
| 143 | + return ['value2' => $__tagembed__postTypeMap[$__tagembed__postType], 'value3' => $__tagembed__matchedPost[2]]; | |
| 144 | +} | |