[ Index ]

MailPress 544

[ 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: 5.4.4
  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_action('MailPress_get_icon_mails',         array(__CLASS__, 'get_icon_mails'), 8, 1);
  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_update_meta_boxes_write',    array(__CLASS__, 'update_meta_boxes_write'));
  31                  add_filter('MailPress_scripts',             array(__CLASS__, 'scripts'), 8, 2);
  32                  add_action('MailPress_add_meta_boxes_write',    array(__CLASS__, 'add_meta_boxes_write'), 8, 2);
  33          }
  34  // trash post
  35           add_action('trash_post',             array(__CLASS__, 'trash_post'), 8, 1);
  36  // for ajax in write post
  37          add_action('wp_ajax_add-mpdraft',        array(__CLASS__, 'wp_ajax_add_mpdraft'));
  38          add_action('wp_ajax_delete-mpdraft',    array(__CLASS__, 'wp_ajax_delete_mpdraft'));
  39  // for ajax in write mail
  40          add_action('mp_action_order_mppost',    array(__CLASS__, 'mp_action_order_mppost'));
  41          add_action('mp_action_delete_mppost',    array(__CLASS__, 'mp_action_delete_mppost'));
  42  
  43  // template when posts
  44          add_filter('MailPress_draft_template', array(__CLASS__, 'draft_template'), 8, 2);
  45      }
  46  
  47  ////  ADMIN  ////
  48  ////  ADMIN  ////
  49  ////  ADMIN  ////
  50  ////  ADMIN  ////
  51  
  52  // for role & capabilities
  53  	public static function capabilities($capabilities) 
  54      {
  55          $capabilities['MailPress_manage_posts'] = array(    'name'  => __('Posts', MP_TXTDOM), 
  56                                              'group' => 'mails'
  57          );
  58          return $capabilities;
  59      }
  60  
  61  // for mails list
  62  	public static function get_icon_mails($mail_id)
  63      { 
  64          if (!MP_Post::object_have_relations($mail_id)) return;
  65  ?>
  66              <span class='icon post' title="<?php _e('Posts', MP_TXTDOM); ?>"></span>
  67  <?php
  68      }
  69  
  70  // trash post
  71  	public static function trash_post($post_id)
  72      { 
  73          MP_Post::delete_post($post_id);
  74      }
  75  
  76  // for meta box in write post  ////
  77  	public static function add_meta_boxes_post($page, $type, $post)
  78      {
  79          if ('post' != $page) return;
  80          if ('side' != $type) return;
  81          if (!current_user_can('MailPress_manage_posts')) return;
  82  
  83          wp_register_script('mp-meta-box-post-drafts',     '/' . MP_PATH . 'mp-includes/js/meta_boxes/post/drafts.js', false, false, 1);
  84          wp_enqueue_script('mp-meta-box-post-drafts');
  85  
  86          wp_register_script( 'mp-thickbox',         '/' . MP_PATH . 'mp-includes/js/mp_thickbox.js', array('thickbox'), false, 1);
  87          wp_enqueue_script('mp-thickbox');
  88  
  89          add_meta_box('MailPress_drafts', __('MailPress drafts', MP_TXTDOM), array(__CLASS__, 'meta_box_post'), 'post', 'side', 'core');
  90      }
  91  
  92  	public static function meta_box_post($post) 
  93      {
  94          include  (MP_ABSPATH . 'mp-includes/meta_boxes/post/drafts.php');
  95      }
  96  
  97  	public static function wp_ajax_add_mpdraft()
  98      {
  99          if (!current_user_can('MailPress_manage_posts')) MP_::mp_die('-1');
 100  
 101          if ( !isset($_POST['post_id']) || !$_POST['post_id'] ) 
 102          {
 103              $x = new WP_Ajax_Response( array(    'what' => 'mpdraft', 
 104                                      'id' => new WP_Error( 'post_id', __('Post id unknown, save post first !', MP_TXTDOM) )
 105                                     ) );
 106              $x->send();
 107          }
 108  
 109          $mpdraft = MP_Post::insert( $_POST['newmpdraft'], $_POST['post_id'] );
 110  
 111          if ( is_wp_error($mpdraft) ) 
 112          {
 113              $x = new WP_Ajax_Response( array(    'what' => 'mpdraft', 
 114                                      'id' => $mpdraft
 115                                    ) );
 116              $x->send();
 117          }
 118  
 119          $x = new WP_Ajax_Response( array(    'what' => 'mpdraft', 
 120                                  'id' => $_POST['newmpdraft'], 
 121                                  'data' => self::get_draft_row($_POST['newmpdraft'], stripslashes($_POST['newmpdraft_txt'])),
 122                                ) );
 123          $x->send();
 124          break;
 125      }
 126  
 127  	public static function get_draft_row($id, $subject)
 128      {
 129          $edit_url = esc_url(MailPress_edit . "&id=$id");
 130          $title    = esc_attr(sprintf( __('Edit "%1$s"', MP_TXTDOM) , $subject ));
 131          $actions['edit'] = "<a href='$edit_url' title=\"$title\">$id</a>";
 132  
 133          $view_url = esc_url(add_query_arg( array('action' => 'iview', 'id' => $id, 'preview_iframe' => 1, 'TB_iframe' => 'true'), MP_Action_url ));
 134          $title    = esc_attr(sprintf( __('View "%1$s"', MP_TXTDOM) , $subject ));
 135          $actions['view'] = "<a href='$view_url' class='thickbox thickbox-preview' title=\"$title\">$subject</a>";
 136  
 137          $delete_url = esc_url(MP_::url( '#', array(), "delete-mpdraft_$id" ));
 138          $title      = esc_attr(__('Delete link', MP_TXTDOM));
 139          $actions['delete'] = "<a class='delete:mpdraftchecklist:mpdraft-{$id}' data-wp-lists='delete:mpdraftchecklist:mpdraft-{$id}' href='$delete_url' title=\"$title\"><img src='" . site_url() . '/' . MP_PATH . "mp-admin/images/trash.png' alt='' /></a>";
 140  
 141          $out  = "<li id='mpdraft-{$id}' style='margin:0;'>\n";
 142          $out .= "\t<table class='widefat' style='background-color:transparent;'>\n";
 143          $out .= "\t\t<tr>\n";
 144          $out .= "\t\t\t<td style='width:10%;border:none;'>\n";
 145          $out .= "\t\t\t\t{$actions['edit']}\n";
 146          $out .= "\t\t\t</td>\n";
 147          $out .= "\t\t\t<td style='width:100%;border:none;'>\n";
 148          $out .= "\t\t\t\t{$actions['view']}\n";
 149          $out .= "\t\t\t</td>\n";
 150          $out .= "\t\t\t<td style='width:16px;border:none;'>\n";
 151          $out .= "\t\t\t\t{$actions['delete']}\n";
 152          $out .= "\t\t\t</td>\n";
 153          $out .= "\t\t</tr>\n";
 154          $out .= "\t</table>\n";
 155          $out .= "</li>\n";
 156          return $out;
 157      }
 158  
 159  	public static function wp_ajax_delete_mpdraft()
 160      {
 161          $x = MP_Post::delete($_POST['id'], $_POST['post_id']);
 162          $x = ($x) ? $_POST['id'] : '-1';
 163          MP_::mp_die($x);
 164      }
 165  
 166          
 167  // for meta box in write page
 168  	public static function update_meta_boxes_write()
 169      {
 170      }
 171  
 172  	public static function scripts($scripts, $screen) 
 173      {
 174          if ($screen != MailPress_page_write) return $scripts;
 175  
 176          wp_register_script( 'mailpress_write_posts', '/' . MP_PATH . 'mp-admin/js/write_posts.js', array('jquery-ui-sortable', 'mp-lists'), false, 1);
 177          $scripts[] = 'mailpress_write_posts';
 178  
 179          return $scripts;
 180      }
 181  
 182  	public static function add_meta_boxes_write($mail_id, $mp_screen)
 183      {
 184          if (!$mail_id) return;
 185          if (!current_user_can('MailPress_manage_posts')) return;
 186  
 187          if ( !MP_Post::get_object_terms($mail_id) ) return;
 188  
 189          add_meta_box('write_posts', __('Posts', MP_TXTDOM), array(__CLASS__, 'meta_box'), MP_AdminPage::screen, 'normal', 'core');
 190      }
 191  /**/
 192  	public static function meta_box($mail)
 193      {
 194          $id = (isset($mail->id)) ? $mail->id : 0;
 195          $post_ids = MP_Post::get_object_terms($id);
 196          if ( !$post_ids ) return;
 197  ?>
 198  <div id='mpposts'>
 199      <div id='mppostchecklist' class='list:mppost' data-wp-lists='list:mppost'>
 200  <?php        foreach ( $post_ids as $post_id ) echo self::get_post_row( $post_id ); ?>
 201      </div>
 202      <span id="mppost-ajax-response"></span>
 203  </div>
 204  <?php
 205      }
 206  
 207      // for ajax
 208  	public static function get_post_row( $id )
 209      {
 210                  $_post = get_post($id);
 211                  if (!$_post) continue;
 212  
 213          $delete_nonce = wp_create_nonce( 'delete-write-post_' . $id );
 214  
 215          $ptitle = $_post->post_title;
 216  
 217          $actions['sortable'] = "<img class='mppost-handle' style='cursor:move' alt='" . __('up/down', MP_TXTDOM) . "' title=\"" . esc_attr(__('up/down', MP_TXTDOM)) . "\" src='" . site_url() . '/' . MP_PATH . "mp-admin/images/sortable.png' />";
 218  
 219          $edit_url = esc_url('post.php?action=edit&post=' . $id);
 220          $title    = esc_attr(sprintf( __('Edit "%1$s"', MP_TXTDOM) , $ptitle ));
 221          $actions['edit'] = "<a href='$edit_url' title=\"$title\">$id</a>";
 222  
 223          $view_url = $_post->guid;
 224          $title    = esc_attr(sprintf( __('View "%1$s"', MP_TXTDOM) , $ptitle ));
 225          $actions['view'] = "<a href='$view_url' target='_new' title=\"$title\">$ptitle</a>";
 226  
 227          $delete_url = esc_url(MP_::url( '#', array(), "delete-mppost_$id" ));
 228          $title      = esc_attr(__('Delete link', MP_TXTDOM));
 229          $actions['delete'] = "<a class='delete:mppostchecklist:mppost-{$id}' href='$delete_url' title=\"$title\"><img src='" . site_url() . '/' . MP_PATH . "mp-admin/images/trash.png' alt='' /></a>";
 230  
 231          $out  = "<div id='mppost-{$id}'>\n";
 232          $out .= "\t<table class='widefat' style='background-color:transparent;'>\n";
 233          $out .= "\t\t<tr>\n";
 234          $out .= "\t\t\t<td style='width:16px;border:none;'>\n";
 235          $out .= "\t\t\t\t{$actions['sortable']}\n";
 236          $out .= "\t\t\t</td>\n";
 237          $out .= "\t\t\t<td style='width:10%;border:none;'>\n";
 238          $out .= "\t\t\t\t{$actions['edit']}\n";
 239          $out .= "\t\t\t</td>\n";
 240          $out .= "\t\t\t<td style='width:100%;border:none;'>\n";
 241          $out .= "\t\t\t\t{$actions['view']}\n";
 242          $out .= "\t\t\t</td>\n";
 243          $out .= "\t\t\t<td style='width:16px;border:none;'>\n";
 244          $out .= "\t\t\t\t{$actions['delete']}\n";
 245          $out .= "\t\t\t</td>\n";
 246          $out .= "\t\t</tr>\n";
 247          $out .= "\t</table>\n";
 248          $out .= "</div>\n";
 249          return $out;
 250      }
 251  
 252  	public static function mp_action_order_mppost()
 253      {
 254              $mp_mail_id = $_POST['id'];
 255          $posts = explode(',', $_POST['posts']);
 256          foreach($posts as $post)
 257          {
 258              $post_id = str_replace('mppost-', '', $post);
 259              $meta_value[$post_id] = $post_id;
 260          }
 261          if (!MP_Mail_meta::add($mp_mail_id, self::meta_key_order, $meta_value, true))
 262              MP_Mail_meta::update($mp_mail_id, self::meta_key_order, $meta_value);
 263      }
 264  
 265  	public static function mp_action_delete_mppost()
 266      {
 267          $x = MP_Post::delete($_POST['mail_id'], $_POST['id']);
 268          $x = ($x) ? $_POST['id'] : '-1';
 269          MP_::mp_die($x);
 270      }
 271  
 272  // template when posts
 273  	public static function draft_template($template, $main_id)
 274      { 
 275          global $MP_post_ids, $mp_general;
 276  
 277          $MP_post_ids = MP_Post::get_object_terms($main_id);
 278          if (empty($MP_post_ids)) return $template;
 279  
 280          $query_posts = array('post__in' => $MP_post_ids, 'ignore_sticky_posts' => 1);
 281          if (class_exists('MailPress_newsletter') && isset($mp_general['post_limits']) && !empty($mp_general['post_limits']))
 282              $query_posts['posts_per_page'] = $mp_general['post_limits'];
 283  
 284          add_filter('posts_orderby',     array(__CLASS__, 'posts_orderby'), 8, 1);
 285          query_posts($query_posts);
 286          remove_filter('posts_orderby',array(__CLASS__, 'posts_orderby'));
 287          return 'manual';
 288      }
 289  
 290  	public static function posts_orderby($orderby = '')
 291      {
 292          global $wpdb, $MP_post_ids;
 293          $orderby = " FIELD({$wpdb->posts}.ID, " . implode(',', $MP_post_ids) . ')';
 294          return $orderby;
 295      }
 296  }
 297  new MailPress_post();
 298  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1