Advertisement
Sjeev

Enable authors to edit their own posts and not others-2

Dec 11th, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.67 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Plugin Name: Live Edit
  5. Plugin URI: http://www.elliotcondon.com/
  6. Description: Edit the title, content and any ACF fields from the front end of your website!
  7. Version: 2.0.0
  8. Author: Elliot Condon
  9. Author URI: http://www.elliotcondon.com/
  10. License: GPL
  11. Copyright: Elliot Condon
  12. */
  13.  
  14. $live_edit = new live_edit();
  15.  
  16. class live_edit
  17. {
  18.     var $dir,
  19.         $path,
  20.         $version,
  21.         $acf_version,
  22.         $data;
  23.    
  24.    
  25.     /*
  26.     *  Constructor
  27.     *
  28.     *  @description:
  29.     *  @since 1.0.0
  30.     *  @created: 23/06/12
  31.     */
  32.    
  33.     function __construct()
  34.     {
  35.  
  36.         // vars
  37.         $this->dir = plugins_url('',__FILE__);
  38.         $this->path = plugin_dir_path(__FILE__);
  39.         $this->version = '2.0.0';
  40.         $this->defaults = array(
  41.             'panel_width'   =>  600,
  42.         );
  43.        
  44.         foreach( $this->defaults as $k => $v )
  45.         {
  46.             $db_v = get_option('live_edit_' . $k, $v);
  47.            
  48.             if( $db_v )
  49.             {
  50.                 $v = $db_v;
  51.             }
  52.            
  53.             $this->data[ $k ] = $v;
  54.         }
  55.        
  56.        
  57.         // set text domain
  58.         load_plugin_textdomain('live-edit', false, basename(dirname(__FILE__)).'/lang' );
  59.        
  60.        
  61.         // actions
  62.         add_action('init', array($this,'init'));
  63.        
  64.        
  65.         return true;
  66.     }
  67.    
  68.    
  69.     /*
  70.     *  init
  71.     *
  72.     *  @description:
  73.     *  @created: 7/09/12
  74.     */
  75.    
  76.     function init()
  77.     {
  78.         // must be logged in
  79.     if( current_user_can( 'edit_post', $post_ID )) 
  80. //if( is_user_logged_in() && current_user_can('edit') || current_user_can('administrator'))
  81.         {
  82.             // actions
  83.             add_action('admin_head', array($this,'admin_head'));
  84.             add_action('admin_menu', array($this,'admin_menu'));
  85.            
  86.            
  87.             add_action('wp_enqueue_scripts', array($this,'wp_enqueue_scripts'));
  88.             add_action('wp_head', array($this,'wp_head'));
  89.             add_action('wp_footer', array($this,'wp_footer'));
  90.             add_action('wp_ajax_live_edit_update_width', array($this, 'ajax_update_width'));
  91.         }
  92.     }
  93.    
  94.    
  95.     /*
  96.     *  admin_head
  97.     *
  98.     *  @description:
  99.     *  @since 1.0.0
  100.     *  @created: 25/07/12
  101.     */
  102.    
  103.     function admin_head()
  104.     {
  105.         echo '<style type="text/css">#menu-settings a[href="options-general.php?page=live-edit-panel"] { display:none; }</style>';
  106.     }
  107.    
  108.    
  109.     /*
  110.     *  admin_menu
  111.     *
  112.     *  @description:
  113.     *  @since 1.0.0
  114.     *  @created: 25/07/12
  115.     */
  116.    
  117.     function admin_menu()
  118.     {
  119.         global $pagenow;
  120.        
  121.         $slug = add_options_page(__("Live Edit Panel",'le'), __("Live Edit Panel",'le'), 'edit_posts', 'live-edit-panel', array($this, 'view_panel'));
  122.        
  123.        
  124.         if( $pagenow == 'options-general.php' && isset($_GET['page']) && $_GET['page'] == 'live-edit-panel')
  125.         {
  126.             add_action('admin_enqueue_scripts', array($this, 'page_admin_enqueue_scripts'));
  127.             add_action('admin_head', array($this, 'page_admin_head'));
  128.         }
  129.     }
  130.    
  131.    
  132.     /*
  133.     *  admin_enqueue_scripts
  134.     *
  135.     *  @description: run after post query but before any admin script / head actions. A good place to register all actions.
  136.     *  @since: 3.6
  137.     *  @created: 26/01/13
  138.     */
  139.    
  140.     function page_admin_enqueue_scripts()
  141.     {
  142.         // actions
  143.         do_action('acf/input/admin_enqueue_scripts');
  144.     }
  145.    
  146.    
  147.     /*
  148.     *  save_post
  149.     *
  150.     *  {description}
  151.     *
  152.     *  @since: 4.0.3
  153.     *  @created: 16/05/13
  154.     */
  155.    
  156.     function save_post()
  157.     {
  158.         // validate
  159.         if( !isset($_POST['post_id']) )
  160.         {
  161.             return;
  162.         }
  163.        
  164.        
  165.         // vars
  166.         $post_id = $_POST['post_id'];
  167.         $post_data = array();
  168.        
  169.        
  170.         foreach( array('post_title', 'post_content', 'post_excerpt') as $v )
  171.         {
  172.             if( isset($_POST['fields'][ $v ]) )
  173.             {
  174.                 $post_data[ $v ] = $_POST['fields'][ $v ];
  175.                
  176.                 unset( $_POST['fields'][ $v ] );   
  177.             }
  178.         }
  179.        
  180.        
  181.         // update post
  182.         if( !empty($post_data) )
  183.         {
  184.             $post_data['ID'] = $post_id;
  185.             wp_update_post( $post_data );
  186.         }
  187.        
  188.        
  189.         // save custom fields
  190.         do_action('acf/save_post', $post_id);
  191.        
  192.        
  193.         // set var
  194.         $this->data['save_post'] = true;
  195.        
  196.     }
  197.    
  198.    
  199.     /*
  200.     *  page_admin_head
  201.     *
  202.     *  @description:
  203.     *  @since: 3.6
  204.     *  @created: 17/03/13
  205.     */
  206.    
  207.     function page_admin_head()
  208.     {  
  209.         // save
  210.         if( isset($_POST['nonce']) && wp_verify_nonce($_POST['nonce'], 'live-edit') )
  211.         {
  212.             $this->save_post();
  213.         }
  214.        
  215.        
  216.         // vars
  217.         $options = array(
  218.             'fields' => false,
  219.             'post_id' => 0,
  220.         );
  221.         $options = array_merge($options, $_GET);
  222.        
  223.        
  224.         // global vars
  225.         global $acf;
  226.        
  227.    
  228.         // Style
  229.         echo '<link rel="stylesheet" type="text/css" href="'.$this->dir.'/css/style.admin.css?ver=' . $this->version . '" />';
  230.    
  231.    
  232.         // Javascript
  233.         echo '<script type="text/javascript" src="'.$this->dir.'/js/functions.admin.js?ver=' . $this->version . '" ></script>';
  234.        
  235.        
  236.         do_action('acf/input/admin_head');
  237.        
  238.        
  239.     }
  240.    
  241.    
  242.     /*
  243.     *  wp_enqueue_scripts
  244.     *
  245.     *  @description:
  246.     *  @since 1.0.0
  247.     *  @created: 25/07/12
  248.     */
  249.    
  250.     function wp_enqueue_scripts() {
  251.        
  252.         wp_enqueue_script(array(
  253.             'jquery',
  254.             'jquery-ui-core',
  255.             'jquery-ui-widget',
  256.             'jquery-ui-mouse',
  257.             'jquery-ui-resizable'
  258.         ));
  259.  
  260.     }
  261.    
  262.    
  263.     /*
  264.     *  wp_head
  265.     *
  266.     *  @description:
  267.     *  @since 1.0.0
  268.     *  @created: 25/07/12
  269.     */
  270.    
  271.     function wp_head()
  272.     {
  273.         // Javascript
  274.         echo '<script type="text/javascript">
  275.             var live_edit = {
  276.                 ajaxurl : "' . admin_url( 'admin-ajax.php' ) . '",
  277.                 panel_url : "' . admin_url( 'options-general.php?page=live-edit-panel' ) . '",
  278.                 panel_width : ' . $this->data['panel_width'] . '
  279.             };
  280.         </script>';
  281.         echo '<script type="text/javascript" src="' . $this->dir . '/js/functions.front.js?ver=' . $this->version . '" ></script>';
  282.        
  283.        
  284.         // Style
  285.         echo '<link rel="stylesheet" type="text/css" href="' . $this->dir . '/css/style.front.css?ver=' . $this->version . '" />';
  286.        
  287.     }
  288.    
  289.    
  290.     /*--------------------------------------------------------------------------------------
  291.     *
  292.     *   wp_footer
  293.     *
  294.     *   @author Elliot Condon
  295.     *   @since 1.0.0
  296.     *
  297.     *-------------------------------------------------------------------------------------*/
  298.    
  299.     function wp_footer()
  300.     {
  301.         ?>
  302.         <div id="live_edit-panel">
  303.             <div id="live_edit-iframe-cover"></div>
  304.             <iframe id="live_edit-iframe"></iframe>
  305.         </div>
  306.         <div id="live_edit-vail"></div>
  307.         <?php
  308.        
  309.     }
  310.    
  311.    
  312.     /*--------------------------------------------------------------------------------------
  313.     *
  314.     *   ajax_update_width
  315.     *
  316.     *   @author Elliot Condon
  317.     *   @since 1.0.0
  318.     *
  319.     *-------------------------------------------------------------------------------------*/
  320.    
  321.     function ajax_update_width()
  322.     {
  323.         // vars
  324.         $options = array(
  325.             'live_edit_panel_width' => 600
  326.         );
  327.        
  328.         $options = array_merge($options, $_POST);
  329.        
  330.        
  331.         // update option
  332.         update_option( 'live_edit_panel_width', $options['panel_width'] );
  333.        
  334.        
  335.         echo "1";
  336.         die;
  337.     }
  338.    
  339.    
  340.     /*
  341.     *  render_live_edit_panel
  342.     *
  343.     *  @description:
  344.     *  @created: 7/09/12
  345.     */
  346.    
  347.     function view_panel()
  348.     {
  349.         global $acf;
  350.        
  351.        
  352.         // vars
  353.         $options = array(
  354.             'fields' => false,
  355.             'post_id' => 0,
  356.         );
  357.         $options = array_merge($options, $_GET);
  358.        
  359.        
  360.         // validate
  361.         if( !$options['post_id'] )
  362.         {
  363.             wp_die( "Error: No post_id parameter found" );
  364.         }
  365.        
  366.         if( !$options['fields'] )
  367.         {
  368.             wp_die( "Error: No fields parameter found" );
  369.         }
  370.        
  371.        
  372.         // loop through and load all fields as objects
  373.         $fields = explode(',',$options['fields']);
  374.  
  375.         if( $fields )
  376.         {
  377.             foreach( $fields as $k => $field_name )
  378.             {
  379.                 $field = null;
  380.                
  381.                
  382.                 if( $field_name == "post_title" ) // post_title
  383.                 {
  384.                     $field = array(
  385.                         'key' => 'post_title',
  386.                         'label' => 'Post Title',
  387.                         'name' => 'post_title',
  388.                         'value' => get_post_field('post_title', $options['post_id']),
  389.                         'type'  =>  'text',
  390.                         'required' => 1
  391.                     );
  392.                 }
  393.                 elseif( $field_name == "post_content" ) // post_content
  394.                 {
  395.                     $field = array(
  396.                         'key' => 'post_content',
  397.                         'label' => 'Post Content',
  398.                         'name' => 'post_content',
  399.                         'value' => get_post_field('post_content', $options['post_id']),
  400.                         'type'  =>  'wysiwyg',
  401.                     );
  402.                 }
  403.                 elseif( $field_name == "post_excerpt" ) // post_excerpt
  404.                 {
  405.                     $field = array(
  406.                         'key' => 'post_excerpt',
  407.                         'label' => 'Post Excerpt',
  408.                         'name' => 'post_excerpt',
  409.                         'value' => get_post_field('post_excerpt', $options['post_id']),
  410.                         'type'  =>  'textarea',
  411.                     );
  412.                 }
  413.                 else // acf field
  414.                 {
  415.                     $field = get_field_object( $field_name, $options['post_id'], array( 'load_value' => false, 'format_value' => false ));
  416.                 }
  417.                
  418.                
  419.                 // load defualts (for post_title, etc)
  420.                 $field = apply_filters('acf/load_field_defaults', $field);
  421.                
  422.                 $fields[ $k ] = $field;
  423.             }
  424.         }
  425.    
  426.         // render fields
  427. ?>
  428. <div class="wrap no_move">
  429.    
  430.     <?php if( isset($this->data['save_post']) ): ?>
  431.         <div class="inner-padding">
  432.             <div id="message" class="updated"><p><?php _e("Fields updated", 'live-edit'); ?></p></div>
  433.         </div>
  434.     <?php endif; ?>
  435.            
  436.     <form id="post" method="post" name="post">
  437.    
  438.         <div style="display:none;">
  439.             <input type="hidden" name="post_id" value="<?php echo $options['post_id']; ?>" />
  440.             <input type="hidden" name="nonce" value="<?php echo wp_create_nonce( 'live-edit' ); ?>" />
  441.         </div>
  442.         <div class="metabox-holder" id="poststuff">
  443.                
  444.             <!-- Main -->
  445.             <div id="post-body">
  446.             <div id="post-body-content">
  447.                 <div class="acf_postbox">
  448.                
  449.                     <?php
  450.                    
  451.                     do_action('acf/create_fields', $fields, $options['post_id']);
  452.                    
  453.                     ?> 
  454.                                    
  455.                     <div id="field-save">
  456.                         <ul class="hl clearfix">
  457.                             <li>
  458.                                 <a class="le-button grey" href="#" id="live_edit-close">
  459.                                     <?php echo isset($this->data['save_post']) ? __("Close", 'live-edit') : __("Cancel", 'live-edit') ?>
  460.                                 </a>
  461.                             </li>
  462.                             <li class="right">
  463.                                 <input type="submit" name="live_edit-save" class="le-button" id="live_edit-save" value="<?php esc_attr_e("Update", 'live-edit') ?>" />
  464.                             </li>
  465.                             <li class="right" id="saving-message">
  466.                                 <?php _e("Saving", 'live-edit'); ?>...
  467.                             </li>
  468.                         </ul>
  469.                     </div>
  470.                    
  471.                 </div>
  472.             </div>
  473.             </div>
  474.        
  475.         </div>
  476.     </form>
  477.    
  478.     <?php if( isset($this->data['save_post']) ): ?>
  479.         <script type="text/javascript">
  480.         (function($){
  481.        
  482.         // does parent exist?
  483.         if( !parent )
  484.         {
  485.             return;
  486.         }
  487.        
  488.         // update the div
  489.         parent.live_edit.update_div();
  490.        
  491.         })(jQuery);
  492.         </script>
  493.     <?php endif; ?>
  494.  
  495. </div>
  496. <?php
  497.  
  498.     }
  499.    
  500. }
  501.  
  502.  
  503. /*
  504. *  live_edit
  505. *
  506. *  @description:
  507. *  @since 1.0.0
  508. *  @created: 25/07/12
  509. */
  510.  
  511. function live_edit( $fields = false, $post_id = false )
  512. {
  513.     // validate fields
  514.     if( !$fields )
  515.     {
  516.         return false;
  517.     }
  518.    
  519.    
  520.     // filter post_id
  521.     $post_id = acf_filter_post_id( $post_id );
  522.    
  523.    
  524.     // turn array into string
  525.     if( is_array($fields) )
  526.     {
  527.         $fields = implode(',', $fields);
  528.     }
  529.    
  530.    
  531.     // remove any white spaces from $fields
  532.     $fields = str_replace(' ', '', $fields);
  533.    
  534.    
  535.     // build atts
  536.     $atts = ' data-live_edit-fields="' . $fields . '" data-live_edit-post_id="' . $post_id . '" ';
  537.    
  538.    
  539.     // echo
  540.     echo $atts;
  541.    
  542. }
  543.  
  544. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement