static function Add_fb_link($post) { $user_ID = WPAL2Facebook::Get_user_ID($post); // Get link URL if (get_user_meta($user_ID, c_al2fb_meta_shortlink, true)) { $link = wp_get_shortlink($post->ID); } if (empty($link)) { $link = get_permalink($post->ID); } // Get URL param $url_param_name = get_user_meta($user_ID, c_al2fb_meta_param_name, true); $url_param_value = get_user_meta($user_ID, c_al2fb_meta_param_value, true); if (empty($url_param_name)) { $url_param_name = get_post_meta($post->ID, c_al2fb_meta_url_param_name, true); } if (empty($url_param_value)) { $url_param_value = get_post_meta($post->ID, c_al2fb_meta_url_param_value, true); } if (!empty($url_param_name)) { $link = add_query_arg($url_param_name, $url_param_value, $link); } $link = apply_filters('al2fb_link', $link, $post); // Get processed texts $texts = WPAL2Facebook::Get_texts($post); $excerpt = $texts['excerpt']; $content = $texts['content']; $description = $texts['description']; if (!$description) { $description = ' '; } // Convert character sets if needed $excerpt = WPAL2Int::Convert_encoding($user_ID, $excerpt); $content = WPAL2Int::Convert_encoding($user_ID, $content); $description = WPAL2Int::Convert_encoding($user_ID, $description); // Get name $name = strip_tags(get_the_title($post->ID)); $name = html_entity_decode($name, ENT_QUOTES, get_bloginfo('charset')); $name = WPAL2Int::Convert_encoding($user_ID, $name); $name = apply_filters('al2fb_name', $name, $post); // Get caption $caption = ''; if (get_user_meta($user_ID, c_al2fb_meta_caption, true)) { $caption = strip_tags(get_bloginfo('title')); $caption = html_entity_decode($caption, ENT_QUOTES, get_bloginfo('charset')); $caption = WPAL2Int::Convert_encoding($user_ID, $caption); } $caption = apply_filters('al2fb_caption', $caption, $post); // Get link picture $picture_info = WPAL2Facebook::Get_link_picture($post, $user_ID); $picture = $picture_info['picture']; $picture_type = $picture_info['picture_type']; // Get user note $message = ''; if (get_user_meta($user_ID, c_al2fb_meta_msg, true)) { $message = $excerpt; if (empty($message)) { $message = strip_tags(get_bloginfo('title')); $message = html_entity_decode($message, ENT_QUOTES, get_bloginfo('charset')); } $message = WPAL2Int::Convert_encoding($user_ID, $message); } $message = apply_filters('al2fb_message', $message, $post); // Get wall $login = false; if (get_option(c_al2fb_option_login_add_links) && WPAL2Int::Get_login_access_token($user_ID) && get_user_meta($user_ID, c_al2fb_meta_facebook_page, true)) { $login = true; $page_ids = array(); $page_ids[] = get_user_meta($user_ID, c_al2fb_meta_facebook_page, true); } else { $page_ids = WPAL2Int::Get_page_ids($user_ID); } // Build request $query_array = array('link' => $link, 'name' => $name, 'caption' => $caption, 'description' => $description, 'message' => $message, 'ref' => 'AL2FB'); // Add home link // $actions = array( // name => __('Website', c_al2fb_text_domain), // link => get_home_url()); // $query_array['actions'] = json_encode($actions); // Add video $video = WPAL2Facebook::Get_link_video($post, $user_ID); if (!empty($video)) { $query_array['source'] = $video; // Picture is mandatory if (!$picture) { $picture = WPAL2Int::Redirect_uri() . '?al2fb_image=1'; } } // Add picture if ($picture) { $query_array['picture'] = $picture; } // Add icon $icon = get_user_meta($user_ID, c_al2fb_meta_icon, true); $icon = apply_filters('al2fb_icon', $icon, $post); if ($icon) { $query_array['icon'] = $icon; } // Get me info (needed for malformed link id's) try { $me = WPAL2Int::Get_fb_me_cached($user_ID, true); } catch (Exception $e) { update_post_meta($post->ID, c_al2fb_meta_error, 'Get me: ' . $e->getMessage()); update_post_meta($post->ID, c_al2fb_meta_error_time, date('c')); return; } // Add link foreach ($page_ids as $page_id) { // Do not disturb WordPress try { // https://developers.facebook.com/docs/reference/api/user/#posts // https://developers.facebook.com/docs/reference/api/post/ // https://developers.facebook.com/docs/reference/dialogs/feed/ // Personal page if (empty($page_id)) { $page_id = 'me'; } // Get access tokendevded $token = WPAL2Int::Get_access_token_by_page($user_ID, $page_id); if ($token) { // Get URL $url = 'https://graph.facebook.com/v2.2/' . $page_id . (get_option(c_al2fb_option_uselinks) ? '/links' : '/feed'); $url = apply_filters('al2fb_url', $url); // Add privacy option if ($page_id == 'me') { $privacy = get_user_meta($user_ID, c_al2fb_meta_privacy, true); if ($privacy) { $p = array('value' => $privacy); if ($privacy == 'SOME_FRIENDS') { $p['value'] = 'CUSTOM'; $p['friends'] = 'SOME_FRIENDS'; $p['allow'] = get_user_meta($user_ID, c_al2fb_meta_some_friends, true); $p['deny'] = ''; } $query_array['privacy'] = json_encode($p); } } else { if (isset($query_array['privacy'])) { unset($query_array['privacy']); } } // Get access token $query_array['access_token'] = $token; // Build query $query_array = apply_filters('al2fb_query', $query_array); $query = http_build_query($query_array, '', '&'); // Log request update_option(c_al2fb_last_request, print_r($query_array, true) . $query); update_option(c_al2fb_last_request_time, date('c')); update_option(c_al2fb_last_texts, print_r($texts, true) . $query); if (get_option(c_al2fb_option_debug)) { add_post_meta($post->ID, c_al2fb_meta_log, date('c') . ' ' . $url . ' request=' . print_r($query_array, true)); add_post_meta($post->ID, c_al2fb_meta_log, date('c') . ' texts=' . print_r($texts, true)); } // Execute request $response = WPAL2Int::Request($url, $query, 'POST'); // Log response update_option(c_al2fb_last_response, $response); update_option(c_al2fb_last_response_time, date('c')); if (get_option(c_al2fb_option_debug)) { add_post_meta($post->ID, c_al2fb_meta_log, date('c') . ' ' . $url . ' response=' . $response); } // Decode response $fb_link = json_decode($response); // Workaround for some links if (!empty($fb_link->id) && strpos($fb_link->id, '_') === false) { $fb_link->id = ($page_id == 'me' ? $me->id : $page_id) . '_' . $fb_link->id; } // Register link/date add_post_meta($post->ID, c_al2fb_meta_link_id, $fb_link->id); update_post_meta($post->ID, c_al2fb_meta_link_time, date('c')); update_post_meta($post->ID, c_al2fb_meta_link_picture, $picture_type . '=' . $picture); delete_post_meta($post->ID, c_al2fb_meta_error); delete_post_meta($post->ID, c_al2fb_meta_error_time); } } catch (Exception $e) { update_post_meta($post->ID, c_al2fb_meta_error, 'Add link: ' . $e->getMessage()); update_post_meta($post->ID, c_al2fb_meta_error_time, date('c')); update_post_meta($post->ID, c_al2fb_meta_link_picture, $picture_type . '=' . $picture); } } // Auto refresh access token if (!$login && !get_option(c_al2fb_option_notoken_refresh)) { try { WPAL2Int::Refresh_fb_token($user_ID); } catch (Exception $e) { update_post_meta($post->ID, c_al2fb_meta_error, 'Refresh token: ' . $e->getMessage()); update_post_meta($post->ID, c_al2fb_meta_error_time, date('c')); } } }