khalequzzaman17

Nelio Handler for Ext Image

Jul 31st, 2022 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.01 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Nelio Handler for replacing the external image with self-hosted image
  4.  */
  5. function mlsbd_nelio_replacement($post_ID)
  6. {
  7.     $nelio_url = get_post_meta($post_ID, '_nelioefi_url', true);
  8.     $mlsbd_url = get_post_meta($post_ID, '_mlsbd_featured_url', true);
  9.     $mlsbd_url = !empty($mlsbd_url) ? $mlsbd_url['original'] : '';
  10.     $key       = '598565ba4f08c775b47b7f7a293be1f846de766edfb975a4e1cf9c0c8723b53888d3ba89fea152f04aed94c0b698fe871cf31699e07199e0f1c37aee1a5b55cc';
  11.    
  12.     if ($nelio_url === '' || $nelio_url === $mlsbd_url) {
  13.         return true;
  14.     }
  15.    
  16.     $json = json_encode(array(
  17.         'payload' => array(
  18.             $nelio_url
  19.         )
  20.     ));
  21.    
  22.     $res = wp_remote_post('https://image-cdn.mlsbd.co/post.php', array(
  23.         'sslverify' => false,
  24.         'timeout' => 30,
  25.         'body' => array(
  26.             'secure_key' => $key,
  27.             'encoded_json' => urlencode(base64_encode($json))
  28.         )
  29.     ));
  30.    
  31.     $body                = json_decode($res['body']);
  32.     $result              = $body->result[0];
  33.     $compressed_jpg_img  = $result->compressed->jpg;
  34.     $compressed_webp_img = $result->compressed->webp;
  35.     $original_img        = $result->original;
  36.     $status              = $res['response']['code'];
  37.    
  38.     $meta_value = array(
  39.         'webp' => $compressed_webp_img,
  40.         'jpg' => $compressed_jpg_img,
  41.         'original' => $original_img
  42.     );
  43.    
  44.     if ($status == 200 && $compressed_jpg_img != null && $compressed_webp_img != null) {
  45.         $success = add_post_meta($post_ID, '_mlsbd_featured_url', $meta_value, true);
  46.         if ($success === false) {
  47.             update_post_meta($post_ID, '_mlsbd_featured_url', $meta_value);
  48.         }
  49.     }
  50. }
  51. add_action('save_post', 'mlsbd_nelio_replacement', 1000);
  52.  
  53. /**
  54.  * Replace HTTP links with HTTPS in post content
  55.  */
  56. function mlsbd_http_to_https($content)
  57. {
  58.     return str_replace('href="http://', 'href="https://', $content);
  59. }
  60. add_action('the_content', 'mlsbd_http_to_https');
  61. /**
  62.  * Nelio Handler for replacing the external image with self-hosted image
  63.  */
  64. function mlsbd_nelio_replacement($post_ID)
  65. {
  66.     $nelio_url = get_post_meta($post_ID, '_nelioefi_url', true);
  67.     $mlsbd_url = get_post_meta($post_ID, '_mlsbd_featured_url', true);
  68.     $mlsbd_url = !empty($mlsbd_url) ? $mlsbd_url['original'] : '';
  69.     $key       = '598565ba4f08c775b47b7f7a293be1f846de766edfb975a4e1cf9c0c8723b53888d3ba89fea152f04aed94c0b698fe871cf31699e07199e0f1c37aee1a5b55cc';
  70.    
  71.     if ($nelio_url === '' || $nelio_url === $mlsbd_url) {
  72.         return true;
  73.     }
  74.    
  75.     $json = json_encode(array(
  76.         'payload' => array(
  77.             $nelio_url
  78.         )
  79.     ));
  80.    
  81.     $res = wp_remote_post('https://image-cdn.mlsbd.co/post.php', array(
  82.         'sslverify' => false,
  83.         'timeout' => 30,
  84.         'body' => array(
  85.             'secure_key' => $key,
  86.             'encoded_json' => urlencode(base64_encode($json))
  87.         )
  88.     ));
  89.    
  90.     $body                = json_decode($res['body']);
  91.     $result              = $body->result[0];
  92.     $compressed_jpg_img  = $result->compressed->jpg;
  93.     $compressed_webp_img = $result->compressed->webp;
  94.     $original_img        = $result->original;
  95.     $status              = $res['response']['code'];
  96.    
  97.     $meta_value = array(
  98.         'webp' => $compressed_webp_img,
  99.         'jpg' => $compressed_jpg_img,
  100.         'original' => $original_img
  101.     );
  102.    
  103.     if ($status == 200 && $compressed_jpg_img != null && $compressed_webp_img != null) {
  104.         $success = add_post_meta($post_ID, '_mlsbd_featured_url', $meta_value, true);
  105.         if ($success === false) {
  106.             update_post_meta($post_ID, '_mlsbd_featured_url', $meta_value);
  107.         }
  108.     }
  109. }
  110. add_action('save_post', 'mlsbd_nelio_replacement', 1000);
  111.  
  112. /**
  113.  * Replace HTTP links with HTTPS in post content
  114.  */
  115. function mlsbd_http_to_https($content)
  116. {
  117.     return str_replace('href="http://', 'href="https://', $content);
  118. }
  119. add_action('the_content', 'mlsbd_http_to_https');
Add Comment
Please, Sign In to add comment