Ver Mensaje Individual
  #3 (permalink)  
Antiguo 18/02/2012, 15:54
Avatar de Naahuel
Naahuel
 
Fecha de Ingreso: marzo-2011
Ubicación: localhost
Mensajes: 796
Antigüedad: 13 años, 1 mes
Puntos: 192
Respuesta: Uso los comentarios de facebook en mi wordpress, como se que alguien ha co

Respuesta 2 :P

En la práctica, me di cuenta que no todas las notificaciones llegaban. Yo se lo atribuí a el hecho de usar un servidor gratuito y lento, pero tal vez haya errores de otro tipo.

Lo que hice entonces fue lo siguiente:

El archivo comentarios.php (el que manda el email) lo modifiqué de esta forma:

Código PHP:
Ver original
  1. <?php
  2. // Incluir WordPress
  3. define('WP_USE_THEMES', false);
  4. require('/home/public_html/wordpress/wp-load.php');// Incluir wp-load.php
  5. ?>
  6.  
  7. <?php
  8.     $recipient  = "[email protected]";
  9.     $from       = "Quien Manda <[email protected]>";
  10.    
  11.     $name       = urldecode($_REQUEST['name']);
  12.     $message    = urldecode($_REQUEST['message']);
  13.     $page_url   = urldecode($_REQUEST['page_url']);
  14.     $page_title = urldecode($_REQUEST['page_title']);
  15.    
  16.    
  17.     $subject    = $name . " hizo un comentario en Tu Sitio";
  18.    
  19.     if($name=='' || $message == ''){
  20.         die('<h1>Forbidden</h1>');
  21.     }
  22.    
  23.     // si llegamos hasta aca, puede ser un comentario genuino. Entonces agregamos el meta key al post para marcar que tiene un comentario.
  24.     $slug_to_get = getLastPathSegment($page_url);
  25.     $args=array(
  26.       'name' => $slug_to_get,
  27.       'post_type' => 'post',
  28.       'post_status' => 'publish'
  29.     );
  30.     $my_posts = get_posts($args);
  31.     if( $my_posts && $slug_to_get!='') {
  32.          update_post_meta($my_posts[0]->ID, 'comentarios_nuevos','true');
  33.     }
  34.    
  35.     $HTML = '<html><body style="text-align:center; padding:20px;">';
  36.     $HTML.= '<h1>' . $subject . '</h1>';
  37.     $HTML.= '<table align="center" width="500" style="text-align:left; vertical-align:top;">';
  38.     $HTML.= "<tr><td style='vertical-align:top; color:#000; width:20%;'><strong>Nombre</strong></td><td>$name</td></tr>";
  39.     $HTML.= "<tr><td style='vertical-align:top; color:#000; width:20%;'><strong>Mensaje</strong></td><td>$message</td></tr>";
  40.     $HTML.= "<tr><td style='vertical-align:top; color:#000; width:20%;'><strong>P&aacute;gina</strong></td><td>$page_url</td></tr>";
  41.     $HTML.= "<tr><td style='vertical-align:top; color:#000; width:20%;'><strong>T&iacute;tulo</strong></td><td>$page_title</td></tr>";
  42.     $HTML.= "</table>";
  43.     $HTML.= "<hr />";
  44.     $HTML.= "<p><a href='$page_url'#encargar>Revis&aacute; siempre que recibas este mail que no halla m&aacute;s comentarios! Click ac&aacute;.</a></p>";
  45.     $HTML.= "</body></html>";
  46.    
  47.     if(sendHTMLemail($HTML,$from, "$name <$email>", $recipient, $subject)){
  48.         echo 'Exito';
  49.     }
  50.     die;
  51.    
  52.     function sendHTMLemail($HTML,$from,$reply_to,$to,$subject)
  53.     {
  54.         $headers = "From: $from\r\n";
  55.         $headers .= "Reply-To:$reply_to\r\n";
  56.         $headers .= "Return-Path:$reply_to\r\n";
  57.         $headers .= "MIME-Version: 1.0\r\n";
  58.         $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
  59.        return mail($to,$subject,$HTML,$headers);
  60.     }
  61.    
  62.     function getLastPathSegment($url) {
  63.         $path = parse_url($url, PHP_URL_PATH); // to get the path from a whole URL
  64.         $pathTrimmed = trim($path, '/'); // normalise with no leading or trailing slash
  65.         $pathTokens = explode('/', $pathTrimmed); // get segments delimited by a slash
  66.  
  67.         if (substr($path, -1) !== '/') {
  68.             array_pop($pathTokens);
  69.         }
  70.         return end($pathTokens); // get the last segment
  71.     }
  72. ?>

Lo que hice fue incluir Wordpress arriba. Luego, antes de enviar el email, lo que hago es agregar un meta key al post que recibió el comentario. De esta forma, puedo después obtener todos los posts que tienen comentarios.

Lo que hice fue incluir esto en functions.php

Código PHP:
Ver original
  1. // *********************************************
  2. // Agregar la pagina de comentarios en el panel
  3. // *********************************************
  4. add_action('admin_menu', 'comentarios_op');
  5.  
  6. function comentarios_op() {
  7.     add_menu_page( 'Comentarios', 'Commentarios', 'read', 'comentarios', 'comentarios_handler',get_bloginfo('template_directory') . '/images/facebook-icon.png');
  8. }
  9.  
  10.  
  11. function comentarios_handler() { ?>
  12.     <div id="fb-root"></div>
  13.     <script>window.fbAsyncInit = function() {
  14.         FB.init({appId: 'TU-AP-ID', status: true, cookie: true, xfbml: true});
  15.     };
  16.     (function(d, s, id) {
  17.       var js, fjs = d.getElementsByTagName(s)[0];
  18.       if (d.getElementById(id)) return;
  19.       js = d.createElement(s); js.id = id;
  20.       js.src = "//connect.facebook.net/es_ES/all.js#xfbml=1&appId=TU-AP-ID";
  21.       fjs.parentNode.insertBefore(js, fjs);  
  22.     }(document, 'script', 'facebook-jssdk'));</script>
  23.     <script>
  24.     jQuery(function(){
  25.         jQuery('a.mark_as_read').click(function(e){
  26.             var anchor = jQuery(this);
  27.             e.preventDefault();
  28.             var page_url = jQuery(this).attr('data-posturl');
  29.             jQuery.post("<?php bloginfo('template_directory'); ?>/comentario-leido.php", { page_url: page_url } , function(data){
  30.                 if(data=='exito'){
  31.                     anchor.parent().parent().fadeOut('fast',function(){
  32.                         jQuery(this).remove();
  33.                         if(!jQuery('').length){
  34.                             jQuery('#wpbody-content').append('<h2>No hay comentarios nuevos.</h2>');
  35.                         }
  36.                     });
  37.                 }else{
  38.                     alert('Hubo un error marcando los comentarios :( Intenta de nuevo re-cargando la pagina');
  39.                 }
  40.             });
  41.            
  42.         });
  43.     });
  44.     </script>
  45.    
  46.     <?php
  47.     global $post;
  48.     $tmp_post = $post;
  49.     $args = array( 'numberposts' => -1 , 'meta_key' => 'comentarios_nuevos', 'meta_value' => 'true');
  50.     $myposts = get_posts( $args );
  51.     $hay_comentarios = false;
  52.     foreach( $myposts as $post ) : setup_postdata($post); $hay_comentarios=true; ?>
  53.     <div style="padding:20px 0; border-bottom:2px dashed #CCC;">
  54.         <h2><a href="<? the_permalink(); ?>"><?php the_title(); ?></a> <iframe src="http://www.facebook.com/plugins/comments.php?href=<?php the_permalink(); ?>&amp;permalink=1" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:130px; height:16px;" allowTransparency="true"></iframe> <a class="mark_as_read" style="font-size:12px; text-decoration:underline; color:red;" data-posturl='<?php the_permalink();?>' href="#">Marcar como leidos</a></h2>
  55.         <p><a href="<? the_permalink(); ?>"><?php echo get_the_post_thumbnail($post->ID, 'thumbnail',array('title'=>get_the_title())); ?></a></p>
  56.         <div class="fb-comments" data-href="<? the_permalink(); ?>" data-num-posts="5" data-width="600"></div>
  57.     </div>
  58.     <?php endforeach; ?>
  59.     <?php if(!$hay_comentarios){ ?>
  60.         <h2>No hay comentarios nuevos.</h2>
  61.     <?php } ?>
  62.     <?php $post = $tmp_post; ?>
  63.    
  64.    
  65. <?php }

Y ahora necesitás incluir también otro archivo php para marcar los comentarios leídos: comentario-leido.php

Código PHP:
Ver original
  1. <?php
  2. // Incluir WordPress
  3. define('WP_USE_THEMES', false);
  4. require('/home/public_html/wordpress/wp-load.php');
  5. ?>
  6. <?php
  7.  
  8.     $page_url   = urldecode($_REQUEST['page_url']);
  9. // si llegamos hasta aca, puede ser un comentario genuino. Entonces agregamos el meta key al post para marcar que tiene un comentario.
  10.     $slug_to_get = getLastPathSegment($page_url);
  11.     $args=array(
  12.       'name' => $slug_to_get,
  13.       'post_type' => 'post',
  14.       'post_status' => 'publish'
  15.     );
  16.     $my_posts = get_posts($args);
  17.     if( $my_posts && $slug_to_get!='') {
  18.         delete_post_meta($my_posts[0]->ID, 'comentarios_nuevos');
  19.         echo 'exito';
  20.         die;
  21.     }
  22.    
  23. function getLastPathSegment($url) {
  24.     $path = parse_url($url, PHP_URL_PATH); // to get the path from a whole URL
  25.     $pathTrimmed = trim($path, '/'); // normalise with no leading or trailing slash
  26.     $pathTokens = explode('/', $pathTrimmed); // get segments delimited by a slash
  27.  
  28.     if (substr($path, -1) !== '/') {
  29.         array_pop($pathTokens);
  30.     }
  31.     return end($pathTokens); // get the last segment
  32. }
  33. ?>

Lo que todo esto hace es incluir una página en el panel de Wordpress que te muestra las entradas con comentarios nuevos y te da la posibilidad de marcarlos como leídos.

  • En esta y en la anterior solucion, tenés que cambiar el ID de tu app donde veas ID-DE-TU-APP
  • Es MUY MUY seguro que tengas que modificar parte de estos códigos para adaptarlos a tus necesidades.
  • Cuando incluyas wordpress en los archivos PHP, asegurate de cambiar la ruta de wp-load.php por la tuya
  • En el archivo comentarios.php, cambia bien las direcciones de email.
__________________
nahueljose.com.ar