1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*Кросспосинг вконтакте*//////////////////////////////////////////////////////////////////////////////////////////////// add_action('post_submitbox_misc_actions','post_check_box'); function post_check_box() { if (is_super_admin()) { echo '<div class="misc-pub-section">'; echo '<p><input type="checkbox" name="post_to_vk" /> Опубликовать на стене ВКонтакте </p>'; echo '</div>'; } } //кросспостинг вконтакте add_action('transition_post_status', 'send_to_cross_post', '100', '3'); function send_to_cross_post($new_status, $old_status, $post){ if (($new_status == 'publish' && $old_status != 'publish') || ( isset( $_POST['post_to_vk'] ) && $_POST['post_to_vk'] && $new_status == 'publish' )) { cross_post($post); } } function cross_post($post){ if ($post->post_type == 'post') { $title = $post->post_title; $title = strip_tags($title); $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8'); $title = htmlspecialchars_decode($title); $url = get_permalink($post->ID); $shorturl = wp_get_shortlink($post->ID); $category = get_the_category($post->ID); $parentCatList = get_category_parents($category[0]->cat_ID,false,','); $parentCatListArray = explode(",",$parentCatList); $topParentName = $parentCatListArray[0]; $sdacReplace = array(" " => "-", "(" => "", ")" => ""); $topParent = strtolower(strtr($topParentName,$sdacReplace)); $topParent = str_replace("-","_", $topParent); //Берем картинку поста $post_images = get_children( array( 'post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order id', 'order' => 'ASC', 'numberposts' => '1' )); //Если есть картинка if ($post_images) { $i = 1; foreach($post_images as $image) { $images['file'.$i] = new CURLFile(get_attached_file($image->ID)); $i++; } } //Инфо $token = ""; // Ваш токен $group_id = ""; //id группы $group_slug = "good_webmaste"; // slug группы, например для группы https://vk.com/ko_samui это "ko_samui" //Загружаем картинку в вк $args = array ( 'body' => array ('group_id' => $group_id,'access_token' => $token), 'sslverify' => false ); $data = wp_remote_post('https://api.vk.com/method/photos.getWallUploadServer', $args); $resp = json_decode($data['body'], true); // декодируем данные $curl = new Wp_Http_Curl(); $ch = $curl->request( $resp['response']['upload_url'], array( 'body' => $images, 'method' => 'POST' )); $res2 = json_decode($ch['body'],true); //Сохраняем загруженные картинки $params = array( 'access_token' => $token, 'gid' => abs($group_id), 'server' => $res2['server'], 'photo' => $res2['photo'], 'hash' => $res2['hash'], ); $args = array ( 'body' => $params, 'sslverify' => false ); $data = wp_remote_post('https://api.vk.com/method/photos.saveWallPhoto', $args); //декодируем данные после сохранения картинок, что бы получить их ввиде photo4096966_396804305 $data = json_decode($data['body'],true); //записываем все картинки (photo4096966_396804305) в массив foreach($data['response'] as $r) { $attachments[] = $r['id']; } //делаем из массива с картинками строку (photo4096966_396804305,photo4096966_396804305) $attch = implode(",", $attachments); //Формируем окончательный запрос на отправку поста в группу $args = array( 'body' => 'owner_id=-'.$group_id.'&from_group=1&message='.$title.'%0A%0A'.$url.'%0A%0A#'.$topParent.'@'.$group_slug.'&attachments='.$attch.','.$shorturl.'&access_token='.$token, 'user-agent' => 'Yoast Coolness' ); //отправляем пост в группу $sendpost = wp_remote_post('https://api.vk.com/method/wall.post', $args); } } |
