[ Index ]

MailPress 7.2

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/class/ -> MP_Mail_revision.class.php (source)

   1  <?php
   2  class MP_Mail_revision
   3  {
   4      const meta_key         = '_MailPress_mail_revisions';
   5  
   6  	public static function has( $id ) 
   7      {
   8          return MP_Mail_meta::has( $id,  self::meta_key );
   9      }
  10  
  11  	public static function get( $id ) 
  12      {
  13          return MP_Mail_meta::get( $id, self::meta_key );
  14      }
  15  
  16  	public static function get_all( $id, $o = 'DESC' ) 
  17      {
  18          global $wpdb;
  19          $revisions = array();
  20          $mails[] = ( int ) $id;
  21  
  22          $o = ( $o == 'DESC' ) ? $o : 'ASC';
  23  
  24          $rev_ids = self::get( $id );
  25          foreach( $rev_ids as $rev_id ) { $mails[] = ( int ) $rev_id; }
  26          $ms = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->mp_mails WHERE id IN ( %s ) ORDER BY created %s;", implode( ', ', $mails ), $o ) );
  27          foreach ( $ms as $m ) { $revisions[$m->id] = $m; }
  28                  
  29          return $revisions;
  30      }
  31  
  32  	public static function get_ui_diff( $id, $compare_from, $compare_to ) 
  33          {
  34          if ( !$mail = MP_Mail::get( $id ) )
  35              return false;
  36  
  37          if ( $compare_from ) {
  38              if ( ! $compare_from = MP_Mail::get( $compare_from ) )
  39                  return false;
  40          } else {
  41              // If we're dealing with the first revision...
  42              $compare_from = false;
  43          }
  44  
  45          if ( ! $compare_to = MP_Mail::get( $compare_to ) )
  46              return false;
  47  
  48          // Add default title if title field is empty
  49          if ( $compare_from && empty( $compare_from->subject ) )
  50              $compare_from->subject = __( '(no subject)', 'MailPress' );
  51          if ( empty( $compare_to->subject ) )
  52              $compare_to->subject = __( '(no subject)', 'MailPress' );
  53  
  54          $return = array();
  55  
  56          foreach ( self::autosave_data() as $field => $name ) {
  57  
  58              $content_from = $compare_from ? apply_filters( "_mp_mail_revision_field_{$field}", $compare_from->$field, $field, $compare_from, 'from' ) : '';
  59      
  60              /** This filter is documented in wp-admin/includes/revision.php */
  61              $content_to = apply_filters( "_mp_mail_revision_field_{$field}", $compare_to->$field, $field, $compare_to, 'to' );
  62      
  63              $args = array( 
  64                  'show_split_view' => true
  65               );
  66      
  67              $args = apply_filters( 'revision_text_diff_options', $args, $field, $compare_from, $compare_to );
  68      
  69              $diff = wp_text_diff( $content_from, $content_to, $args );
  70  
  71              if ( ! $diff && 'subject' === $field ) {
  72                  // It's a better user experience to still show the Subject, even if it didn't change.
  73                  // No, you didn't see this.
  74                  $diff = '<table class="diff"><colgroup><col class="content diffsplit left"><col class="content diffsplit middle"><col class="content diffsplit right"></colgroup><tbody><tr>';
  75                  $diff .= '<td>' . esc_html( $compare_from->subject ) . '</td><td></td><td>' . esc_html( $compare_to->subject ) . '</td>';
  76                  $diff .= '</tr></tbody>';
  77                  $diff .= '</table>';
  78              }
  79  
  80              if ( $diff ) {
  81                  $return[] = array( 
  82                      'id' => $field,
  83                      'name' => $name,
  84                      'diff' => $diff,
  85                   );
  86              }
  87          }
  88          return apply_filters( 'mp_get_revision_ui_diff', $return, $compare_from, $compare_to );
  89      }
  90  
  91  	public static function update( $id, $rev_id, $wp_user_id ) 
  92      {
  93          $rev_ids = self::get( $id );
  94  
  95          $revs = ( is_array( $rev_ids ) ) ? $rev_ids : array();
  96          $revs[$wp_user_id] = $rev_id;
  97  
  98          if ( !MP_Mail_meta::add(    $id,  self::meta_key, $revs, true ) )
  99              MP_Mail_meta::update( $id,  self::meta_key, $revs );
 100      }
 101  
 102  	public static function delete( $id ) 
 103      {
 104          $rev_ids = self::get( $id );
 105  
 106          if ( !$rev_ids ) return;
 107  
 108          global $wpdb;
 109          foreach( $rev_ids as $rev_id )
 110          {    
 111              $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->mp_mails WHERE id = %d ; ", $rev_id ) );
 112          }
 113      }
 114  
 115  	public static function restore( $mail, $revision ) 
 116      {
 117          global $wpdb;
 118  
 119          $fields = array_keys( self::autosave_data() );
 120          $types  = array( 'mail' => $revision->id, 'revision' => $mail->id );
 121  
 122          foreach ( $types as $type => $id )
 123          {
 124                          $data = $format = $where = $where_format = array();
 125  
 126              foreach ( $fields as $field ) 
 127              {
 128                                  $data[$field] = ${$type}->{$field};                 $format[] = '%s';
 129              }
 130  
 131              $where['id']         = $id;                        $where_format[] = '%d';
 132   
 133              $wpdb->update( $wpdb->mp_mails, $data, $where, $format, $where_format );
 134                  }
 135      }
 136  
 137  	public static function autosave_data()
 138      {
 139          $autosave_data['fromemail']= __( 'From', 'MailPress' ); 
 140          $autosave_data['fromname'] = __( 'Name (from)', 'MailPress' ); 
 141          $autosave_data['toemail']     = __( 'To', 'MailPress' ); 
 142          $autosave_data['toname']     = __( 'Name (to)', 'MailPress' ); 
 143          $autosave_data['theme']    = __( 'Theme', 'MailPress' );
 144          $autosave_data['subject']     = __( 'Subject', 'MailPress' ); 
 145          $autosave_data['html']     = __( 'Html' );
 146          $autosave_data['plaintext']= __( 'Plain Text', 'MailPress' );
 147          return $autosave_data;
 148      }
 149  
 150  	public static function title( $revision, $link = true ) 
 151      {
 152  
 153          $author = get_the_author_meta( 'display_name', $revision->created_user_id );
 154          $datef = _x( 'F j, Y @ H:i:s', 'revision date format' );
 155  
 156          $gravatar = get_avatar( $revision->created_user_id, 24 );
 157  
 158          $date = date_i18n( $datef, strtotime( $revision->created ) );
 159          if ( $link ) $date = '<a href="' . esc_url( $link ) . '">' . $date . '</a>';
 160  
 161          $revision_date_author = sprintf( 
 162              __( '%1$s %2$s, %3$s ago (%4$s)' ),
 163              $gravatar,
 164              $author,
 165              human_time_diff( strtotime( $revision->created ), current_time( 'timestamp' ) ),
 166              $date
 167           );
 168  
 169          /* translators: %s: revision date with author avatar */
 170          $autosavef = __( '%s [Autosave]' );
 171          /* translators: %s: revision date with author avatar */
 172          $currentf  = __( '%s [Current Revision]' );
 173                  
 174          if ( '' == $revision->status )     $revision_date_author = sprintf( $autosavef, $revision_date_author );
 175          else                        $revision_date_author = sprintf( $currentf, $revision_date_author );
 176  
 177          return $revision_date_author;
 178      }
 179  
 180  	public static function listing( $id = 0, $type = 'all' ) 
 181      {
 182          $revisions = self::get_all( $id );
 183          $mail      = $revisions[$id];
 184          unset( $revisions[$id] );
 185  
 186          $rows = '';
 187  
 188          foreach ( $revisions as $revision ) 
 189          {
 190              $link = ( '' == $revision->status ) ? add_query_arg( array( 'id' => $mail->id, 'revision' => $revision->id ), MailPress_revision ) : add_query_arg( 'id', $mail->id, MailPress_write );
 191  
 192              $rows .= "\t" . '<li>' . self::title( $revision, $link ) . '</li>' . "\n";
 193          }
 194      
 195          echo '<div class="hide-if-js"><p>' . __( 'JavaScript must be enabled to use this feature.' ) . '</p></div>' . "\n";
 196  
 197          echo '<ul class="mail-revisions hide-if-no-js">' . "\n";
 198          echo $rows;
 199          echo '</ul>';
 200      }
 201  }


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1