Advertisement
Guest User

Untitled

a guest
Apr 6th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.04 KB | None | 0 0
  1. <?php
  2.  
  3. function addPost( $post ) {
  4.     $image = null;
  5.    
  6.     if ($post->picture) {
  7.         $image = extractFeaturedImage( $post->picture );
  8.     }
  9.  
  10.     if ($post->news_photo) {
  11.         $image = extractFeaturedImage( $post->news_photo );
  12.     }
  13.  
  14.     $post->{'featured_media'} = null;
  15.  
  16.     if ( $image ) {
  17.         $result = invokeApi( getFullUrl( 'media' ), $image['file'], [
  18.             'cache-control: no-cache',
  19.             "content-disposition: attachment; filename={$image['name']}",
  20.             'content-type: image/jpg'
  21.         ] );
  22.  
  23.         invokeApi( getFullUrl( "media/{$result->id}" ), [ 'description' => $post->tags ], [ 'Content-type: application/json' ], 1 );
  24.  
  25.         $post->featured_media = $result->id;
  26.     }
  27.  
  28.     invokeApi( getFullUrl( 'posts' ), extractPost( $post ), [ 'Content-type: application/json' ], 1 );
  29. }
  30.  
  31. function invokeApi( $url, $data, $headers, $json = 0 ) {
  32.     $ch       = curl_init();
  33.     $username = 'admin';
  34.     $password = '1234qwer';
  35.  
  36.     $mainHeader = [ 'Authorization: Basic ' . base64_encode( $username . ':' . $password ) ];
  37.  
  38.     $combined = array_merge( $headers, $mainHeader );
  39.  
  40.     if ( $json ) {
  41.         $data = json_encode( $data );
  42.     }
  43.  
  44.     curl_setopt( $ch, CURLOPT_URL, $url );
  45.     curl_setopt( $ch, CURLOPT_POST, 1 );
  46.     curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
  47.     curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  48.     curl_setopt( $ch, CURLOPT_HTTPHEADER, $combined );
  49.     $result = curl_exec( $ch );
  50.     curl_close( $ch );
  51.  
  52.     return json_decode( $result );
  53. }
  54.  
  55. function getFullUrl( $uri ) {
  56.     return "http://newsmaker-wp.dev/wp-json/wp/v2/{$uri}";
  57. }
  58.  
  59. function extractPost( $post ) {
  60.     $data = [];
  61.  
  62.     $content = extractInlineImages($post);
  63.  
  64.     $data['date']           = $post->created;
  65.     $data['date_gmt']       = $post->created;
  66.     $data['modified']       = $post->modified;
  67.     $data['modified_gmt']   = $post->modified;
  68.     $data['slug']           = $post->slug;
  69.     $data['title']          = $post->title_rus;
  70.     $data['excerpt']        = $post->subtitle_rus;
  71.     $data['content']        = $content;
  72.     $data['status']         = $post->published ? 'publish' : 'draft';
  73.     $data['featured_media'] = $post->featured_media;
  74.  
  75.     return $data;
  76. }
  77.  
  78. function extractFeaturedImage( $image ) {
  79.     $buff = explode( '/', $image );
  80.     $name = end( $buff );
  81.  
  82.     $image = str_replace( ' ', '%20', $image );
  83.  
  84.     return [
  85.         'file' => file_get_contents( "http://newsmaker.md/{$image}" ),
  86.         'name' => $name,
  87.     ];
  88. }
  89.  
  90.  
  91. //Extract images from inline
  92. function extractInlineImages ($post) {
  93.     $doc=new DOMDocument();
  94.     $html = $post->content_rus;
  95.     $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
  96.  
  97.     libxml_use_internal_errors(true);
  98.  
  99.     $doc->loadHTML($html);
  100.  
  101.     libxml_use_internal_errors(false);
  102.     $images = $doc->getElementsByTagName('object');
  103.     echo '<pre>';
  104.     foreach ($images as $img) {
  105.  
  106.         $content = $img->textContent;
  107.  
  108.         $mey = explode('  ', $content);
  109.         $mey = explode(' ', $mey[2]);
  110.         $mey = $mey[0];
  111.         echo $mey . '</br>';
  112.  
  113.         $node = $doc->createElement('p', 'Value');
  114.         $img->appendChild($node);
  115.  
  116.     }
  117.  
  118.     return $doc->saveHTML();
  119. }
  120.  
  121. function printData( $post ) {
  122.     echo '<pre>';
  123.     print_r( $post );
  124.     echo '</pre>';
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement