Advertisement
Sjeev

Enable authors to edit their own posts and not others

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