[ Index ]

MailPress 7.1

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

title

Body

[close]

/mp-content/add-ons/ -> MailPress_post.php (source)

   1  <?php
   2  if ( class_exists( 'MailPress' ) && !class_exists( 'MailPress_post' ) )
   3  {
   4  /*
   5  Plugin Name: MailPress_post
   6  Plugin URI: http://blog.mailpress.org/tutorials/add-ons/post/
   7  Description: New Mail : select posts for draft mails ( <span style="color:red;">required !</span> 'manual' template for your mp theme )
   8  Author: Roberto Morales O., Andre Renaut
   9  Version: 7.1
  10  Author URI: http://www.mailpress.org
  11  */
  12  
  13  class MailPress_post
  14  {
  15      const meta_key        = '_MailPress_post_';
  16      const meta_key_order  = '_MailPress_post-order';
  17  
  18  	function __construct()
  19      {
  20  // for wp admin
  21          if ( is_admin() )
  22          {
  23          // for role & capabilities
  24              add_filter( 'MailPress_capabilities',         array( __CLASS__, 'capabilities' ), 1, 1 );
  25          // for mails list
  26              add_filter( 'MailPress_get_icon_mails',         array( __CLASS__, 'get_icon_mails' ), 8, 2 );
  27          // for meta box in write post
  28                  add_action( 'do_meta_boxes',                 array( __CLASS__, 'add_meta_boxes_post' ), 8, 3 );
  29          // for meta box in write page
  30                  add_action( 'MailPress_add_help_tab_write',    array( __CLASS__, 'add_help_tab_write' ), 8 );
  31                  add_action( 'MailPress_update_meta_boxes_write',array( __CLASS__, 'update_meta_boxes_write' ) );
  32                  add_filter( 'MailPress_scripts',             array( __CLASS__, 'scripts' ), 8, 2 );
  33                  add_action( 'MailPress_add_meta_boxes_write',    array( __CLASS__, 'add_meta_boxes_write' ), 8, 2 );
  34          }
  35  // trash post
  36           add_action( 'trash_post',             array( __CLASS__, 'trash_post' ), 8, 1 );
  37  // for ajax in write post
  38          add_action( 'mp_action_add_mpdraft',        array( __CLASS__, 'mp_action_add_mpdraft' ) );
  39          add_action( 'mp_action_delete_mpdraft',    array( __CLASS__, 'mp_action_delete_mpdraft' ) );
  40  // for ajax in write mail
  41          add_action( 'mp_action_order_mppost',    array( __CLASS__, 'mp_action_order_mppost' ) );
  42          add_action( 'mp_action_delete_mppost',array( __CLASS__, 'mp_action_delete_mppost' ) );
  43  
  44  // template when posts
  45          add_filter( 'MailPress_draft_template', array( __CLASS__, 'draft_template' ), 8, 2 );
  46      }
  47  
  48  ////  ADMIN  ////
  49  ////  ADMIN  ////
  50  ////  ADMIN  ////
  51  ////  ADMIN  ////
  52  
  53  // for role & capabilities
  54  	public static function capabilities( $capabilities ) 
  55      {
  56          $capabilities['MailPress_manage_posts'] = array(     'name'  => __( 'Posts', 'MailPress' ), 
  57                                              'group' => 'mails'
  58           );
  59          return $capabilities;
  60      }
  61  
  62  // for mails list
  63  	public static function get_icon_mails( $out, $mail_id )
  64      { 
  65          if ( MP_Post::object_have_relations( $mail_id ) ) $out .= '<span class="mp_icon mp_icon_post" title="' . esc_attr( __( 'Posts', 'MailPress' ) ) . '"></span>';
  66          return $out;
  67      }
  68  
  69  // trash post
  70  	public static function trash_post( $post_id )
  71      { 
  72          MP_Post::delete_post( $post_id );
  73      }
  74  
  75  // for meta box in write post  ////
  76  	public static function add_help_tab_write()
  77      {
  78          global $current_screen;
  79  
  80          $content = '';
  81          $content .= '<p><strong>' . __( 'Posts :', 'MailPress' ) . '</strong></p>';
  82          $content .= '<p>' . __( 'When mixing text with posts, do as follow:', 'MailPress' ) . '</p>';
  83          $content .= '<ul>';
  84          $content .= '<li>' . sprintf( __( 'Create a new %s.', 'MailPress' ), sprintf( '<a href="' . MailPress_write . '" target="_blank">%s</a>', __( 'Mail', 'MailPress' ) ) ) . '</li>';
  85          $content .= '<li>' . __( 'For each post required, edit them and look for the box &#8220;MailPress drafts&#8221; and select the required draft(s).', 'MailPress' ) . '</li>';
  86          $content .= '<li>' . __( 'Going back to the required draft(s), look for the new box &#8220;Posts&#8221; with the selected post(s). You can order them as you like with a drag & drop selecting the left icon of the list', 'MailPress' ) . '</li>';
  87          $content .= '<li>' . sprintf( __( 'Draft mail with posts can be quickly identified in %1$s with a little pin icon : %2$s', 'MailPress' ), sprintf( '<a href="' . MailPress_mails . '" target="_blank">%s</a>', __( 'Mails list', 'MailPress' ) ) , '<span class="mp_icon mp_icon_post" title="' . esc_attr( __('Posts', 'MailPress' ) ) . '"></span>' ) . '</li>';
  88          $content .= '</ul>';
  89  
  90          $current_screen->add_help_tab( array(     'id'        => 'posts',
  91                                          'title'    => __( 'Posts', 'MailPress' ),
  92                                          'content'    => $content )
  93          );
  94      }
  95  
  96  	public static function add_meta_boxes_post( $page, $type, $post )
  97      {
  98          if ( 'post' != $page ) return;
  99          if ( 'side' != $type ) return;
 100          if ( !current_user_can( 'MailPress_manage_posts' ) ) return;
 101  
 102  
 103          wp_register_style( 'mp-meta-box-post-drafts',     '/' . MP_PATH . 'mp-admin/css/mp-meta-box-post-drafts.css' );
 104          wp_register_style( 'mp_icons',                 '/' . MP_PATH . 'mp-admin/css/_icons.css', array( 'mp-meta-box-post-drafts' ) );
 105          wp_enqueue_style( 'mp_icons' );
 106  
 107          wp_register_script( 'mp-ajax-response',    '/' . MP_PATH . 'mp-includes/js/mp_ajax_response.js', array( 'jquery' ), false, 1 );
 108          wp_localize_script( 'mp-ajax-response',     'wpAjax', array( 
 109              'noPerm' => __( 'An unidentified error has occurred.' ), 
 110              'broken' => __( 'An unidentified error has occurred.' ), 
 111              'l10n_print_after' => 'try{convertEntities( wpAjax );}catch( e ){};' 
 112           ) );
 113  
 114          wp_register_script( 'mp-lists',         '/' . MP_PATH . 'mp-includes/js/mp_lists.js', array( 'mp-ajax-response' ), false, 1 );
 115  
 116          wp_register_script( 'mp-meta-box-post-drafts',     '/' . MP_PATH . 'mp-includes/js/meta_boxes/post/drafts.js',  array( 'mp-lists' ), false, 1 );
 117          wp_enqueue_script( 'mp-meta-box-post-drafts' );
 118  
 119          wp_register_script( 'mp-thickbox',         '/' . MP_PATH . 'mp-includes/js/mp_thickbox.js', array( 'thickbox' ), false, 1 );
 120          wp_enqueue_script( 'mp-thickbox' );
 121  
 122          add_meta_box( 'MailPress_drafts', __( 'MailPress drafts', 'MailPress' ), array( __CLASS__, 'meta_box_post' ), 'post', 'side', 'core' );
 123      }
 124  
 125  	public static function meta_box_post( $post ) 
 126      {
 127          include  ( MP_ABSPATH . 'mp-includes/meta_boxes/post/drafts.php' );
 128      }
 129  
 130  	public static function mp_action_add_mpdraft()
 131      {
 132          if ( !current_user_can( 'MailPress_manage_posts' ) ) MP_::mp_die( '-1' );
 133  
 134          if ( !isset( MP_WP_Ajax::$pst_['post_id'] ) || !MP_WP_Ajax::$pst_['post_id'] ) 
 135          {
 136              $x = new WP_Ajax_Response( array(     'what' => 'mpdraft', 
 137                                      'id' => new WP_Error( 'post_id', __( 'Post id unknown, save post first !', 'MailPress' ) )
 138                                     ) );
 139              $x->send();
 140          }
 141  
 142          $mpdraft = MP_Post::insert( MP_WP_Ajax::$pst_['newmpdraft'], MP_WP_Ajax::$pst_['post_id'] );
 143  
 144          if ( is_wp_error( $mpdraft ) ) 
 145          {
 146              $x = new WP_Ajax_Response( array(     'what' => 'mpdraft', 
 147                                      'id' => $mpdraft
 148                                    ) );
 149              $x->send();
 150          }
 151  
 152          $x = new WP_Ajax_Response( array(     'what' => 'mpdraft', 
 153                                  'id' => MP_WP_Ajax::$pst_['newmpdraft'], 
 154                                  'data' => self::get_draft_row( MP_WP_Ajax::$pst_['newmpdraft'], stripslashes( MP_WP_Ajax::$pst_['newmpdraft_txt'] ) ),
 155                                ) );
 156          $x->send();
 157      }
 158  
 159  	public static function get_draft_row( $id, $subject )
 160      {
 161          $edit_url = esc_url( MailPress_edit . "&id=$id" );
 162          $actions['edit'] = '<a href="' . $edit_url . '" title="' . esc_attr( sprintf( __( 'Edit &#8220;%1$s&#8221;', 'MailPress' ) , $subject ) ) . '">' . $id . '</a>';
 163  
 164          $args = array( 'id' => $id, 'action' => 'mp_ajax', 'mp_action' => 'iview', 'TB_iframe' => 'true' );
 165          $view_url = esc_url( add_query_arg( $args, admin_url( 'admin-ajax.php' ) ) );    
 166          $actions['view'] = '<a href="' . $view_url . '" class="thickbox thickbox-preview" title="' . esc_attr( sprintf( __( 'View &#8220;%1$s&#8221;', 'MailPress' ) , $subject ) ) . '">' . $subject . '</a>';
 167  
 168          $delete_url = esc_url( MP_::url( '#', array(), "delete-mpdraft_$id" ) );
 169          $actions['delete'] = '<a class="delete:mpdraftchecklist:mpdraft-' . $id . '" data-wp-lists="delete:mpdraftchecklist:mpdraft-' . $id . '" href="' . $delete_url . '" title="' . esc_attr( __( 'Delete link', 'MailPress' ) ) . '"><span class="mp_icon mp_icon_trash" title="' . esc_attr( __( 'Trash', 'MailPress' ) ). '"></span></a>';
 170  
 171          $out  = '<li id="mpdraft-' . $id . '">';
 172          $out .= '<table class="widefat">';
 173          $out .= '<td class="mp_edit">';
 174          $out .= $actions['edit'];
 175          $out .= '</td>';
 176          $out .= '<td>';
 177          $out .= $actions['view'];
 178          $out .= '</td>';
 179          $out .= '<td>';
 180          $out .= $actions['delete'];
 181          $out .= '</td>';
 182          $out .= '</tr>';
 183          $out .= '</table>';
 184          $out .= '</li>';
 185          return $out;
 186      }
 187  
 188  	public static function mp_action_delete_mpdraft()
 189      {
 190          $x = MP_Post::delete( MP_WP_Ajax::$pst_['id'], MP_WP_Ajax::$pst_['post_id'] );
 191          $x = ( $x ) ? MP_WP_Ajax::$pst_['id'] : '-1';
 192          MP_::mp_die( $x );
 193      }
 194  
 195          
 196  // for meta box in write page
 197  	public static function update_meta_boxes_write()
 198      {
 199      }
 200  
 201  	public static function scripts( $scripts, $screen ) 
 202      {
 203          if ( $screen != MailPress_page_write ) return $scripts;
 204  
 205          wp_register_script( 'mailpress_write_posts', '/' . MP_PATH . 'mp-admin/js/write_posts.js', array( 'jquery-ui-sortable', 'mp-lists' ), false, 1 );
 206          wp_localize_script( 'mailpress_write_posts',     'mp_postsL10n', array( 
 207              'order_mppost' => wp_create_nonce(  'order_mppost' ),
 208           ) );
 209  
 210          $scripts[] = 'mailpress_write_posts';
 211  
 212          return $scripts;
 213      }
 214  
 215  	public static function add_meta_boxes_write( $mail_id, $mp_screen )
 216      {
 217          if ( !$mail_id ) return;
 218          if ( !current_user_can( 'MailPress_manage_posts' ) ) return;
 219  
 220          if ( !MP_Post::get_object_terms( $mail_id ) ) return;
 221  
 222          add_meta_box( 'write_posts', __( 'Posts', 'MailPress' ), array( __CLASS__, 'meta_box' ), MP_AdminPage::screen, 'normal', 'core' );
 223      }
 224  /**/
 225  	public static function meta_box( $mail )
 226      {
 227          $id = ( isset( $mail->id ) ) ? $mail->id : 0;
 228          $post_ids = MP_Post::get_object_terms( $id );
 229          if ( !$post_ids ) return;
 230  
 231          $out  = '<div id="mpposts">' . "\r\n";
 232          $out .= '    <div id="mppostchecklist" class="list:mppost" data-wp-lists="list:mppost">' . "\r\n";
 233          foreach ( $post_ids as $post_id ) $out .= self::get_post_row( $post_id ) . "\r\n";
 234          $out .= '    </div>' . "\r\n";
 235          $out .= '    <span id="mppost-ajax-response"></span>' . "\r\n";
 236          $out .= '</div>' . "\r\n";
 237  
 238          echo $out;
 239      }
 240  
 241      // for ajax
 242  	public static function get_post_row( $id )
 243      {
 244                  $post = get_post( $id );
 245                  if ( !$post ) return '';
 246  
 247          $delete_nonce = wp_create_nonce( 'delete-write-post_' . $id );
 248  
 249          $ptitle = $post->post_title;
 250  
 251          $actions['sortable'] = '<span class="mppost-handle mp_icon mp_icon_sortable" title="' . esc_attr( __( 'up/down', 'MailPress' ) ) . '"></span>';
 252  
 253          $edit_url = esc_url( add_query_arg( array( 'action' => 'edit', 'post' => $id ) , 'post.php' ) );
 254          $actions['edit'] = '<a href="' . $edit_url . '" target="_blank" title="' . esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;', 'MailPress' ) , $ptitle ) ) . '">' . $id . '</a>';
 255  
 256          $view_url = $post->guid;
 257          $actions['view'] = '<a href="' . $view_url . '" target="_blank" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;', 'MailPress' ) , $ptitle ) ) . '">' . $ptitle . '</a>';
 258  
 259          $delete_url = esc_url( MP_::url( '#', array(), "delete-mppost_$id" ) );
 260          $actions['delete'] = '<a class="delete:mppostchecklist:mppost-' . $id . '" href="' . $delete_url . '" title="' . esc_attr( __( 'Delete link', 'MailPress' ) ) . '"><span class="mp_icon mp_icon_trash" title="' . esc_attr( __('Trash', 'MailPress' ) ) . '"></span></a>';
 261  
 262          $out  = '<div id="mppost-' . $id . '">';
 263          $out .= '<table class="widefat">';
 264          $out .= '<tr>';
 265          $out .= '<td>';
 266          $out .= $actions['sortable'];
 267          $out .= '</td>';
 268          $out .= '<td>';
 269          $out .= $actions['edit'];
 270          $out .= '</td>';
 271          $out .= '<td>';
 272          $out .= $actions['view'];
 273          $out .= '</td>';
 274          $out .= '<td>';
 275          $out .= $actions['delete'];
 276          $out .= '</td>';
 277          $out .= '</tr>';
 278          $out .= '</table>';
 279          $out .= '</div>';
 280          return $out;
 281      }
 282  
 283  	public static function mp_action_order_mppost()
 284      {
 285          check_ajax_referer( 'order_mppost', 'security' );
 286  
 287          $meta_value = array();
 288  
 289          $mp_mail_id = MP_WP_Ajax::$pst_['id'];
 290          $posts = explode( ',', MP_WP_Ajax::$pst_['posts'] );
 291          foreach( $posts as $post )
 292          {
 293              $post_id = str_replace( 'mppost-', '', $post );
 294              $meta_value[$post_id] = $post_id;
 295          }
 296          if ( !MP_Mail_meta::add( $mp_mail_id, self::meta_key_order, $meta_value, true ) )
 297              MP_Mail_meta::update( $mp_mail_id, self::meta_key_order, $meta_value );
 298  
 299          MP_::mp_die( 1 );
 300      }
 301  
 302  	public static function mp_action_delete_mppost()
 303      {
 304          $x = MP_Post::delete( MP_WP_Ajax::$pst_['mail_id'], MP_WP_Ajax::$pst_['id'] );
 305          $x = ( $x ) ? MP_WP_Ajax::$pst_['id'] : '-1';
 306          MP_::mp_die( $x );
 307      }
 308  
 309  // template when posts
 310  	public static function draft_template( $template, $main_id )
 311      { 
 312          global $MP_post_ids, $mp_general;
 313  
 314          $MP_post_ids = MP_Post::get_object_terms( $main_id );
 315          if ( empty( $MP_post_ids ) ) return $template;
 316  
 317          $query_posts = array( 'post__in' => $MP_post_ids, 'ignore_sticky_posts' => 1 );
 318          if ( class_exists( 'MailPress_newsletter' ) )
 319              $query_posts['posts_per_page'] = MailPress_newsletter::get_post_limits();
 320  
 321          add_filter( 'posts_orderby',     array( __CLASS__, 'posts_orderby' ), 8, 1 );
 322          query_posts( $query_posts );
 323          remove_filter( 'posts_orderby',array( __CLASS__, 'posts_orderby' ) );
 324          return 'manual';
 325      }
 326  
 327  	public static function posts_orderby( $orderby = '' )
 328      {
 329          global $wpdb, $MP_post_ids;
 330          $orderby = " FIELD( {$wpdb->posts}.ID, " . implode( ',', $MP_post_ids ) . ' )';
 331          return $orderby;
 332      }
 333  }
 334  new MailPress_post();
 335  }


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1