Advertisement
3pepe3

[ACF] live-edit per post_type cap

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