Skip to content

Instantly share code, notes, and snippets.

@tcelestino
Last active November 8, 2017 18:27
Show Gist options
  • Save tcelestino/5504972 to your computer and use it in GitHub Desktop.
Save tcelestino/5504972 to your computer and use it in GitHub Desktop.
Salvando comentário do Facebook em um post no sistema de comentários do WordPress.
// esse script terá que ficar no header.php
window.fbAsyncInit = function() {
FB.init({
appId : 'app id',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// envia comentarios por email
FB.Event.subscribe('comment.create',
function(response) {
var href = response.href;
var postid = "<?php echo $post->ID; ?>"; // pega o id do post, pagina atual
var postagem = $.post('salva_comentario.php', {url:href, id:postid});
postagem.done(function(data){
console.log(data.status); // retorna o status
});
}
);
}
<?php
define('WP_THEMES_USE', false);
require('../../../../wp-load.php');
function quebra_array($arr) {
$id = explode("_", $arr);
return $id[1];
}
function consulta_comentario($id) {
global $wpdb;
$sql = 'SELECT comment_post_ID FROM wp_let_comments WHERE comment_post_ID IN (' .$id. ')';
$exec_query = $wpdb->get_results($wpdb->prepare($sql), OBJECT);
if($exec_query) {
return true;
}
}
if(isset($_POST)) {
$url = $_POST['url'];
$postid = $_POST['id'];
$facebook_comentarios = json_decode( file_get_contents( 'http://graph.facebook.com/comments/?ids='.$url ), true );
$ids = array();
$dados = array();
$status = array();
foreach( $facebook_comentarios as $chave => $valor ) {
foreach( $valor['comments']['data'] as $comentario ) {
$id = $comentario['id'];
$ids[] .= quebra_array($id);
$dados[] = array(
'id_usuario' => $comentario['from']['id'],
'nome' => $comentario['from']['name'],
'mensagem' => $comentario['message']
);
}
}
$ids_invertidos = array_reverse($ids);
$dados_invertidos = array_reverse($dados);
$pesquisa_tabela = consulta_comentario($ids_invertidos[0]);
// se o id nao existir, ele adiciona
if(is_bool($pesquisa_tabela) != true) {
$tempo = current_time( 'mysql' );
$agente = $_SERVER['HTTP_USER_AGENT'];
$args = array(
'comment_post_ID' => $postid,
'comment_author' => $dados_invertidos[0]['nome'],
'comment_content' => $dados_invertidos[0]['mensagem'],
'comment_parent' => 0,
'comment_type' => 'comments',
'user_id' => $dados_invertidos[0]['id_usuario'],
'comment_author_IP' => '127.0.0.1',
'comment_agent' => $agente,
'comment_date' => $tempo,
'comment_approved' => 0,
);
$salva_comentario = wp_insert_comment($args); // insere o novo comentario
if($salva_comentario) {
$para = '[email protected]';
// Cabeçalhos
$headers = "From: [email protected]" . "\r\n" .
"Content-type: text/html; charset=utf-8" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$assunto = "Comentário site - {$nome}";
$mensagem = "<h1>Comentários</h1>
<p><strong>Nome</strong> : {$nome}</p>
<p><strong>Comentario</strong> : {$mensagem}</p>
<p><strong>Link da publicação: </strong> : {$url}</p>
<p><strong>Obs: </strong> : esse comentário foi salvo no WordPress.</p>";
if(mail($para,$assunto,$mensagem,$headers)){
$status = array(
'status' => true,
);
} else {
$status = array(
'tipo' => 'enviar',
'status' => false,
);
}
} else {
$status = array(
'tipo' => 'salvar',
'status' => false,
);
}
} else {
$status = array(
'tipo' => 'verificar',
'status' => false,
);
}
$saida = json_encode($status);
echo $saida;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment