Пример #1
0
 /**
  * Sets the OAuth2 app-only auth bearer token
  *
  * @param string $token OAuth2 bearer token
  *
  * @return void
  */
 public static function setBearerToken($token)
 {
     self::$_oauth_bearer_token = $token;
 }
   /**
   * Saves the note for the given post.
   *
   * @since    0.1.0
   * @param  $post_id  The ID of the post that we're serializing
   */
  function save_note( $post_id ) {

    if( isset( $_POST['WP_Notes_nonce'] ) && isset( $_POST['post_type'] ) ) {

      // Don't save if the user hasn't submitted the changes
      if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
      } 

      // Verify that the input is coming from the proper form
      if( ! wp_verify_nonce( $_POST['WP_Notes_nonce'], plugin_basename( __FILE__ ) ) ) {
        return;
      } 

      // Make sure the user has permissions to post
      if( 'nw-item' == $_POST['post_type']) {
        if( ! current_user_can( 'edit_post', $post_id ) ) {
          return;
        } 
      } 

    
      /*==========  SAVE NOTE META DATA  ==========*/

      //sanitize all of the POST data and place it into an array. 
      $wp_notes_data = array();
      $wp_notes_data['text']                 = isset( $_POST['WP_Notes_text'] ) ?  implode( "\n", array_map( 'sanitize_text_field', explode( "\n", $_POST['WP_Notes_text'] ) ))  : '';
      //the filter for this text only allows for newline characters as well as regular characters

      $wp_notes_data['action_text']         = isset( $_POST['WP_Notes_action_text'] )           ? sanitize_text_field($_POST['WP_Notes_action_text']) : '';
      $wp_notes_data['action_link']         = isset( $_POST['WP_Notes_action_link'])            ? wpnw_addhttp( sanitize_text_field($_POST['WP_Notes_action_link'])) : '';
      $wp_notes_data['image_id']            = isset( $_POST['WP_Notes_image_id'] )              ? sanitize_text_field($_POST['WP_Notes_image_id']) : '';
      $wp_notes_data['download_id']         = isset( $_POST['WP_Notes_download_id'] )           ? sanitize_text_field($_POST['WP_Notes_download_id']) : '';
      $wp_notes_data['plain_link_new_tab']  = isset( $_POST['WP_Notes_plain_link_new_tab'] )    ? sanitize_text_field($_POST['WP_Notes_plain_link_new_tab']) : '';
      $wp_notes_data['action_link_type']    = isset( $_POST['WP_Notes_action_link_type'] )      ? sanitize_text_field($_POST['WP_Notes_action_link_type']) : '';
      
      //Wordpress automatically serializes the data and updates database with the new values.
      update_post_meta( $post_id, 'WP_Notes_data', $wp_notes_data );
      


      /*==========  PARSE AND SAVE TWITTER RELATED DATA  ==========*/
      
      $push_tweet = 0;
      if (isset($_POST['WP_Notes_push_tweet'])) {
        $push_tweet = 1;
      }

      $wp_notes_twitter_data['push_tweet']       = null; //set this value to null so user will have to manually check the checkbox again if they want to send another tweet
      $wp_notes_twitter_data['tweet_body']       = isset( $_POST['WP_Notes_tweet_body'] )     ? sanitize_text_field($_POST['WP_Notes_tweet_body']) : '';
      
      //Wordpress automatically serializes the data and updates database with the new values.
      update_post_meta( $post_id, 'WP_Notes_twitter_data', $wp_notes_twitter_data );



      /*==========  TWITTER POSTING  ==========*/
      
      if ($push_tweet) {
        
        $twitter_credentials = get_option( 'wp_notes_widget_twitter_credentials' );  
        if (!empty($twitter_credentials['api_key']) && 
          !empty($twitter_credentials['api_secret']) && 
          !empty($twitter_credentials['token']) && 
          !empty($twitter_credentials['token_secret']) ) { 
          //if Twitter credentials are set

          //clean up potential whitespace. 
          $twitter_credentials['api_key']       = trim($twitter_credentials['api_key']);
          $twitter_credentials['api_secret']    = trim($twitter_credentials['api_secret']);
          $twitter_credentials['token']         = trim($twitter_credentials['token']);
          $twitter_credentials['token_secret']  = trim($twitter_credentials['token_secret']);

          require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/codebird/src/codebird.php';
          
          WP_Notes_Widget_Codebird::setConsumerKey($twitter_credentials['api_key'], $twitter_credentials['api_secret']); // static, see 'Using multiple Codebird instances'
          $cb = WP_Notes_Widget_Codebird::getInstance();
          $cb->setToken($twitter_credentials['token'], $twitter_credentials['token_secret']);

          //parse status content and embed link, if needed
          switch ($wp_notes_data['action_link_type']) {
            case "plain":
              $embed_link = $wp_notes_data['action_link'];

              break;
            case "download":

              if ((bool)$wp_notes_data['download_id']) {
                $embed_link = wp_get_attachment_url( $wp_notes_data['download_id'] );
              } else {
                $embed_link = '';  
              }

              break;
            default:
              $embed_link = '';

              break;
            
          }

          $tweet_text =str_replace("*url*", $embed_link, $wp_notes_twitter_data['tweet_body']);
           
          //send the tweet to twitter
          if (empty($wp_notes_data['image_id'] )) {
            $reply = $cb->statuses_update('status=' . $tweet_text);
          
          } else {
            
            $file = wp_get_attachment_image_src( $wp_notes_data['image_id'], array(600,600) );
            $file = $file[0];
            $reply = $cb->media_upload(array(
                'media' => $file
            ));

            $media_id = $reply->media_id_string;
            $reply = $cb->statuses_update(array(
                'status' => $tweet_text,
                'media_ids' => $media_id
            ));

          }

          //set up appropriate notice based on twitter response
          $status_class = substr($reply->httpstatus,0, 1); 
          switch ($status_class) {
            case "2":

              $wp_notes_tweet_history = get_post_meta( $post_id, 'WP_Notes_tweet_history', true );
              
              //if tweet is sent successfully, we add a timestamp to the tweet history
              if (empty($wp_notes_tweet_history)) {
                $wp_notes_tweet_history = array();
              }
              array_push($wp_notes_tweet_history, current_time('timestamp') );
              update_post_meta( $post_id, 'WP_Notes_tweet_history', $wp_notes_tweet_history );

              add_filter( 'redirect_post_location', array( $this, 'add_notice_twitter_success' ), 99 );
              break;
            case "4":
              add_filter( 'redirect_post_location', array( $this, 'add_notice_twitter_error' ), 99 );
              break;
            case "5":
              add_filter( 'redirect_post_location', array( $this, 'add_notice_twitter_down' ), 99 );
              break;
            
          }
        }
      }
    } // end if

  } // end save_note