| @@ -1,98 +1,144 @@ | ||
| 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*/ | |
| 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 | +} | |