Plugin Directory

Changeset 2542760


Ignore:
Timestamp:
06/04/2021 06:36:42 PM (5 years ago)
Author:
bogi1991
Message:

Facebook auto publish

Location:
social-media-publish/trunk
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • social-media-publish/trunk/README.txt

    r2542236 r2542760  
    22Contributors: bogi1991
    33Donate link: https://www.paypal.com/donate?hosted_button_id=X2L4JMQ5C9DYN
    4 Tags: social media, automation, instagram, auto post, auto publish
    5 Requires at least: 3.0.1
     4Tags: social media, automation, facebook instagram, auto post, auto publish
     5Requires at least: 5.0.
    66Tested up to: 5.7.2
    77Requires PHP: 7.2
    8 Stable tag: 1.0.0
     8Stable tag: 1.0.1
    99License: GPLv2 or later
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1414== Description ==
    1515
    16 This Plugin automatizate your Social Media processes, by publishing your posts to Social Media plattforms like Instagram.
    17 We are using the open [Facebook API Graph](https://developers.facebook.com/docs) to communicate with Social Media plattforms.
     16This Plugin automatizate your Social Media processes, by publishing your posts to Social Media plattforms.
    1817
    1918= A quick look into Social Media Publish: =
    2019
    2120* **Auto Publish** - to Instagram
     21* **Auto Publish** - to Facebook
    2222* **Reupload** - your post manually
    2323
    2424
    2525== Changelog ==
     26
     27= 1.0.1 =
     28* Disable Reupload button during sending process
     29* Facebook auto publish after post
     30* Show tooltip with required permissions
    2631
    2732= 1.0.0 =
  • social-media-publish/trunk/admin/css/smp-admin.css

    r2542221 r2542760  
    1010   color: #f02929;
    1111}
     12
     13
     14/* -------------------------------------------------------------------------- */
     15/*                                   Tooltip                                  */
     16/* -------------------------------------------------------------------------- */
     17
     18.smp-tooltip {
     19   position: relative;
     20   display: inline-block;
     21 }
     22 
     23 .smp-tooltip .smp-tooltiptext {
     24   visibility: hidden;
     25   /* width: 120px; */
     26   top: 100%;
     27   left: 50%;
     28   margin-left: -60px; /* Use half of the width (120/2 = 60), to center the tooltip */
     29   background-color: black;
     30   color: #fff;
     31   text-align: center;
     32   border-radius: 5px;
     33   padding: 3px;
     34 
     35   /* Position the tooltip */
     36   position: absolute;
     37   z-index: 1;
     38 }
     39 
     40 .smp-tooltip:hover .smp-tooltiptext {
     41   visibility: visible;
     42 }
  • social-media-publish/trunk/admin/js/facebook.js

    r2542221 r2542760  
     1
     2/**
     3 * Initialize Facebook SDK
     4 * @param {*} clientAppId
     5 */
    16function initFacebookSdk(clientAppId) {
    27   FB.init({
     
    813}
    914
     15/**
     16 * Show Facebook Login Dialog and get long live access token
     17 * @param {*} app_id
     18 * @param {*} app_secret
     19 */
    1020function showLoginDialog(app_id, app_secret) {
    1121   FB.login(function (loginResponse) {
  • social-media-publish/trunk/admin/js/smp-gutenberg.js

    r2542221 r2542760  
    3838   }
    3939
     40
     41   /* -------------------------------------------------------------------------- */
     42   /*                                  Reupload                                  */
     43   /* -------------------------------------------------------------------------- */
     44
     45   function reupload(socialMediaName) {
     46      // Disable button during send process
     47      jQuery(`#${socialMediaName}-reupload`).attr("disabled", true);
     48
     49      jQuery.ajax({
     50         type: 'POST',
     51         url: smp_gutenberg.ajaxurl,
     52         data: {
     53            action: `smp_${socialMediaName}_reupload`,
     54            post_id: smp_gutenberg.post_id
     55         },
     56         success: function (data) {
     57            data = data.replace("0", "");
     58            showNotice(data);
     59
     60            // Enable button
     61            jQuery(`#${socialMediaName}-reupload`).attr("disabled", false);
     62         },
     63         error: function (errorThrown) {
     64            var errorMessage = `${errorThrown['status']} ${errorThrown['statusText']}: ${errorThrown['responseText']}`;
     65            showNotice(errorMessage, "error");
     66
     67            // Enable button
     68            jQuery(`#${socialMediaName}-reupload`).attr("disabled", false);
     69         }
     70      });
     71   }
     72
    4073   /* -------------------------------------------------------------------------- */
    4174   /*                                Button Action                               */
     
    4477   // https://www.user-mind.de/ajax-richtig-in-wordpress-nutzen/
    4578   jQuery("#instagram-reupload").click(function () {
    46       // Send POST Request to call php function
    47       jQuery.ajax({
    48          type: 'POST',
    49          url: smp_gutenberg.ajaxurl,
    50          data: {
    51             action: 'smp_instagram_reupload',
    52             post_id: smp_gutenberg.post_id
    53          },
    54          success: function (data) {
    55             data = data.replace("0", "");
    56             showNotice(data);
    57          },
    58          error: function (errorThrown) {
    59             var errorMessage = `${errorThrown['status']} ${errorThrown['statusText']}: ${errorThrown['responseText']}`;
    60             showNotice(errorMessage, "error");
    61          }
    62       });
     79      reupload("instagram");
     80   });
     81
     82   jQuery("#facebook-reupload").click(function () {
     83      reupload("facebook");
    6384   });
    6485});
  • social-media-publish/trunk/admin/menu-tab-settings.php

    r2542221 r2542760  
    2424            <th scope="row">Instagram</th>
    2525            <td>
    26                <label for="woocommerce_cart_redirect_after_add">
    27                   <input type="checkbox" name="smp_instagram_auto_publish" value="1" <?php checked( esc_attr( get_option('smp_instagram_auto_publish')), 1 ); ?>> Enable auto publish after post                       
     26               <label>
     27                  <div class="smp-tooltip"><span class="dashicons dashicons-editor-help"></span>
     28                     <span class="smp-tooltiptext">Required permissions: ads_management, business_management, instagram_basic, instagram_content_publish, pages_read_engagement</span>
     29                  </div>
     30
     31                  <input type="checkbox" name="smp_instagram_auto_publish" value="1"
     32                     <?php checked( esc_attr( get_option('smp_instagram_auto_publish')), 1 ); ?>
     33                  > Enable auto publish after post                     
    2834               </label>
    2935            </td>
    30          </tr>     
     36         </tr>   
     37
     38         <tr>
     39            <th scope="row">Facebook</th>
     40            <td>
     41               <label>               
     42                  <div class="smp-tooltip"><span class="dashicons dashicons-editor-help"></span>
     43                     <span class="smp-tooltiptext">Required permissions: pages_manage_posts, pages_read_engagement</span>
     44                  </div>
     45
     46                  <input type="checkbox" name="smp_facebook_auto_publish" value="1"
     47                     <?php checked( esc_attr( get_option('smp_facebook_auto_publish')), 1 ); ?>
     48                  > Enable auto publish after post                     
     49               </label>
     50            </td>
     51         </tr>   
    3152      </table>   
    3253     
  • social-media-publish/trunk/admin/menu.php

    r2542221 r2542760  
    6161
    6262      // Enqueue scripts
     63      wp_enqueue_script('smp-admin', plugins_url('/js/smp-admin.js', __FILE__), array());
    6364      // https://connect.facebook.net/en_US/sdk.js
    6465      wp_enqueue_script('facebook-sdk', plugins_url('/js/facebook-sdk.js', __FILE__), array(), '10.0', true);
     
    8687      register_setting( 'smp-plugin-settings-group', 'smp_fb_app_secret' );
    8788      register_setting( 'smp-plugin-settings-group', 'smp_instagram_auto_publish' ); 
     89      register_setting( 'smp-plugin-settings-group', 'smp_facebook_auto_publish' );     
    8890   }
    8991
  • social-media-publish/trunk/admin/post-hook.php

    r2542231 r2542760  
    1010            // Retrive image thumbnail
    1111            $post_thumbnail_id = get_post_thumbnail_id( $post );
    12             $featured_image_url = wp_get_attachment_image_url( $post_thumbnail_id, 'original' );
     12            $featured_image_url = wp_get_attachment_image_url( $post_thumbnail_id, 'original' );           
     13
     14            /* -------------------------------------------------------------------------- */
     15            /*                                  INSTAGRAM                                 */
     16            /* -------------------------------------------------------------------------- */
    1317
    1418            // Get instagram published date
    1519            $instagramPublishedPostMeta = get_post_meta( $post_id, 'smp-ig-published', true );
    16 
    17             // Featured image exist and instagram auto publish is enabled
    18             // Check post already published
    19             if( $featured_image_url &&
    20                 esc_attr( get_option('smp_instagram_auto_publish')) &&
    21                 empty( $instagramPublishedPostMeta )
     20           
     21            if( $featured_image_url && // Featured image exist
     22                esc_attr( get_option('smp_instagram_auto_publish')) && // Instagram auto publish is enabled
     23                empty( $instagramPublishedPostMeta ) // Post already published
    2224               ) {         
    2325               // Set Instagram published flag
    2426               add_post_meta( $post_id, 'smp-ig-published' , date("d.m.Y H:i"));   
    2527               smp_post_instagram($post->post_title, $featured_image_url); 
     28            }
     29
     30            /* -------------------------------------------------------------------------- */
     31            /*                                  FACEBOOK                                  */
     32            /* -------------------------------------------------------------------------- */
     33
     34            $facebookPublishedPostMeta = get_post_meta( $post_id, 'smp-fb-published', true );
     35
     36            if ( esc_attr( get_option('smp_facebook_auto_publish')) && empty($facebookPublishedPostMeta) ) {
     37
     38               smp_publish_facebook( $post->post_title, get_post_permalink( $post) );
     39
     40               // Set Facebook published flag
     41               add_post_meta( $post_id, 'smp-fb-published' , date("d.m.Y H:i"));   
    2642            }
    2743         }       
  • social-media-publish/trunk/includes/facebook.php

    r2542221 r2542760  
    7070      );
    7171
    72       if ( is_wp_error( $response ) && smp_is_facebook_error( $response )) {
     72      if ( is_wp_error( $response ) || smp_is_facebook_error( $response )) {
    7373         $error_message = $response->get_error_message();
    7474         error_log('IG CONTAINER ERROR: ' . $error_message);
     
    102102         $jsonResponse = json_decode($response['body']);
    103103
    104          $publishId = $jsonResponse->id;
    105          error_log('PUBLISH INSTAGRAM ID: ' . $publishId);
    106 
    107          return $publishId;
     104         return $jsonResponse->id;
    108105      }
    109106   }
     
    111108   /**
    112109    * Upload instagram photo
     110    * https://developers.facebook.com/docs/instagram-api/guides/content-publishing/
     111    * Permissions: ads_management, business_management, instagram_basic, instagram_content_publish, pages_read_engagement
    113112    */
    114113   function smp_post_instagram($caption, $image_url) {
     114      $image_url = 'https://devcraft.de/wp-content/uploads/2020/08/favicon.png';
    115115      $facebookPageId = smp_get_facebook_page_id();
    116116
     
    128128      }   
    129129   }
     130
     131   /**
     132    * Publish a link to facebook page
     133    * https://developers.facebook.com/docs/pages/publishing/
     134    * Permissions: pages_manage_posts, pages_read_engagement
     135    */
     136   function smp_publish_facebook($message, $permalink) {
     137      error_log('PERMA' . $permalink);
     138      $facebookPageId = smp_get_facebook_page_id();
     139
     140      $requestUrl = smp_get_facebook_graph_url() . $facebookPageId . '/feed?message=' . $message
     141      . '&link=' . $permalink . '&access_token=' . $_COOKIE['smp_a'];
     142
     143      $response = wp_safe_remote_post( $requestUrl , array(
     144         'method' => 'POST',
     145         'timeout' => 45,
     146         'redirection' => 5,
     147         'httpversion' => '1.0',
     148         'blocking' => true,
     149         'headers' => array(),
     150         'cookies' => array()
     151         )
     152      );
     153
     154      if ( is_wp_error( $response ) || smp_is_facebook_error( $response )) {
     155         $error_message = $response->get_error_message();
     156         error_log('FACEBOOK PUBLISH ERROR: ' . $error_message);
     157      } else {
     158         // FB response will be page-post-id
     159         error_log('PUBLISH FACEBOOK BODY: ' . $response['body']);
     160
     161         $jsonResponse = json_decode($response['body']);
     162
     163         // Return page-post-id
     164         return $jsonResponse->id;
     165      }
     166   }
    130167?>
  • social-media-publish/trunk/includes/metabox.php

    r2542221 r2542760  
    88   }
    99
     10   /**
     11    * Initialize Meta Box
     12    */
    1013   function smp_init_meta_box(){
    1114
    1215      if ( get_post_status ( get_the_id() ) == 'publish' ) {
    13          $meta = get_post_meta( get_the_id(), 'smp-ig-published', true );
     16         $instagramMeta = get_post_meta( get_the_id(), 'smp-ig-published', true );
    1417
     18         if ( !empty($instagramMeta) ) {
    1519         ?>
    1620         <div class="components-panel__row">     
    17             <p class="dashicons-before dashicons-instagram text-success">Published: <?php echo  $meta ?></p>
     21            <p class="dashicons-before dashicons-instagram text-success">Published: <?php echo $instagramMeta ?></p>
    1822            <button id="instagram-reupload" class="components-button is-secondary is-small">Reupload</button>
    1923         </div>
    2024         <?php
     25         }
     26
     27         $facebookMeta = get_post_meta( get_the_id(), 'smp-fb-published', true );
     28         if ( !empty($facebookMeta) )
     29         {
     30            ?>
     31            <div class="components-panel__row">     
     32               <p class="dashicons-before dashicons-facebook text-success">Published: <?php echo $facebookMeta ?></p>
     33               <button id="facebook-reupload" class="components-button is-secondary is-small">Reupload</button>
     34            </div>
     35            <?php
     36         }
    2137      } else {
    2238         ?>
     
    2844   add_action( 'add_meta_boxes', 'smp_add_meta_box' );
    2945
     46   /**
     47    * Reupload post to instagram
     48    */
    3049   function smp_instagram_reupload() {
    3150      $post_id = sanitize_text_field($_POST['post_id']); 
     
    4059         smp_post_instagram( $title, $featured_image_url );
    4160         update_post_meta( $post_id, 'smp-ig-published' , date("d.m.Y H:i"));   
     61
     62         // Show notice on gutenberg screen
    4263         echo $title . ' uploaded to Instagram.';
    4364      } else {
    44          echo 'Whoops... something went wrong';
     65         echo 'No post ID available';
     66      }
     67   }
     68
     69   /**
     70    * Reupload to Facebeook
     71    */
     72   function smp_facebook_reupload() {
     73      $post_id = sanitize_text_field($_POST['post_id']); 
     74
     75      if ( !empty($post_id) ) {
     76         $title = get_the_title( $post_id );
     77         $post   = get_post( $post_id );
     78
     79         smp_publish_facebook( $title, get_post_permalink( $post) );
     80         update_post_meta( $post_id, 'smp-fb-published' , date("d.m.Y H:i"));   
     81
     82         // Show notice on gutenberg screen
     83         echo $title . ' uploaded to Instagram.';
     84      } else {
     85         echo 'No post ID available';
    4586      }
    4687   }
  • social-media-publish/trunk/includes/notices.php

    r2542221 r2542760  
    1717   }
    1818
    19    function smp_admin_notices() {       
    20       error_log('ADMIN_NOTICES');   
    21 
     19   function smp_admin_notices() {         
    2220      // SESSION
    2321      if ( array_key_exists( 'smp_errors', $_SESSION ) ) {
    2422
    25          $type = 'warning';
    2623         $message = $_SESSION['smp_errors'];
    27          $class = 'notice notice-' . $type . ' is-dismissible';
    2824
    2925         smp_show_notification($message);
  • social-media-publish/trunk/social-media-publish.php

    r2542221 r2542760  
    44* Plugin URI: http://yourdomain.com
    55* Description: Insert a brief description of what your plugin does here.
    6 * Version: 1.0.0
     6* Version: 1.0.1
    77* Author: Bogdan Schreiber
    88* Author URI: https://devcraft.de
Note: See TracChangeset for help on using the changeset viewer.