Advertisement
Guest User

Utils.php Realia Template

a guest
May 18th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.71 KB | None | 0 0
  1. class Submission_MetaBox extends WPAlchemy_MetaBox {
  2.     function force_save( $post_id ) {
  3.         /**
  4.          * note: the "save_post" action fires for saving revisions and post/pages,
  5.          * when saving a post this function fires twice, once for a revision save,
  6.          * and again for the post/page save ... the $post_id is different for the
  7.          * revision save, this means that "get_post_meta()" will not work if trying
  8.          * to get values for a revision (as it has no post meta data)
  9.          * see http://alexking.org/blog/2008/09/06/wordpress-26x-duplicate-custom-field-issue
  10.          *
  11.          * why let the code run twice? wordpress does not currently save post meta
  12.          * data per revisions (I think it should, so users can do a complete revert),
  13.          * so in the case that this functionality changes, let it run twice
  14.          */
  15.  
  16.         $real_post_id = isset( $_POST['post_ID'] ) ? $_POST['post_ID'] : NULL;
  17.  
  18. //        // check autosave
  19. //        if (defined('DOING_AUTOSAVE') AND DOING_AUTOSAVE AND !$this->autosave) return $post_id;
  20. //
  21. //        // make sure data came from our meta box, verify nonce
  22. //        $nonce = isset($_POST[$this->id.'_nonce']) ? $_POST[$this->id.'_nonce'] : NULL ;
  23. //        if (!wp_verify_nonce($nonce, $this->id)) return $post_id;
  24. //
  25. //        // check user permissions
  26. //        if ($_POST['post_type'] == 'page')
  27. //        {
  28. //            if (!current_user_can('edit_page', $post_id)) return $post_id;
  29. //        }
  30. //        else
  31. //        {
  32. //            if (!current_user_can('edit_post', $post_id)) return $post_id;
  33. //        }
  34.  
  35.         // authentication passed, save data
  36.  
  37.         $new_data = isset( $_POST[$this->id] ) ? $_POST[$this->id] : NULL;
  38.  
  39.         WPAlchemy_MetaBox::clean( $new_data );
  40.  
  41.         if ( empty( $new_data ) ) {
  42.             $new_data = NULL;
  43.         }
  44.  
  45.         // filter: save
  46.         if ( $this->has_filter( 'save' ) ) {
  47.             $new_data = $this->apply_filters( 'save', $new_data, $real_post_id );
  48.  
  49.             /**
  50.              * halt saving
  51.              * @since 1.3.4
  52.              */
  53.             if ( FALSE === $new_data ) return $post_id;
  54.  
  55.             WPAlchemy_MetaBox::clean( $new_data );
  56.         }
  57.  
  58.         // get current fields, use $real_post_id (checked for in both modes)
  59.         $current_fields = get_post_meta( $real_post_id, $this->id . '_fields', TRUE );
  60.  
  61.         if ( $this->mode == WPALCHEMY_MODE_EXTRACT ) {
  62.             $new_fields = array();
  63.  
  64.             if ( is_array( $new_data ) ) {
  65.                 foreach ( $new_data as $k => $v ) {
  66.                     $field = $this->prefix . $k;
  67.  
  68.                     array_push( $new_fields, $field );
  69.  
  70.                     $new_value = $new_data[$k];
  71.  
  72.                     if ( is_null( $new_value ) ) {
  73.                         delete_post_meta( $post_id, $field );
  74.                     }
  75.                     else {
  76.                         update_post_meta( $post_id, $field, $new_value );
  77.                     }
  78.                 }
  79.             }
  80.  
  81.             $diff_fields = array_diff( (array) $current_fields, $new_fields );
  82.  
  83.             if ( is_array( $diff_fields ) ) {
  84.                 foreach ( $diff_fields as $field ) {
  85.                     delete_post_meta( $post_id, $field );
  86.                 }
  87.             }
  88.  
  89.             delete_post_meta( $post_id, $this->id . '_fields' );
  90.  
  91.             if ( ! empty( $new_fields ) ) {
  92.                 add_post_meta( $post_id, $this->id . '_fields', $new_fields, TRUE );
  93.             }
  94.  
  95.             // keep data tidy, delete values if previously using WPALCHEMY_MODE_ARRAY
  96.             delete_post_meta( $post_id, $this->id );
  97.         }
  98.         else {
  99.             if ( is_null( $new_data ) ) {
  100.                 delete_post_meta( $post_id, $this->id );
  101.             }
  102.             else {
  103.                 update_post_meta( $post_id, $this->id, $new_data );
  104.             }
  105.  
  106.             // keep data tidy, delete values if previously using WPALCHEMY_MODE_EXTRACT
  107.             if ( is_array( $current_fields ) ) {
  108.                 foreach ( $current_fields as $field ) {
  109.                     delete_post_meta( $post_id, $field );
  110.                 }
  111.  
  112.                 delete_post_meta( $post_id, $this->id . '_fields' );
  113.             }
  114.         }
  115.  
  116.         // action: save
  117.         if ( $this->has_action( 'save' ) ) {
  118.             $this->do_action( 'save', $new_data, $real_post_id );
  119.         }
  120.  
  121.         return $post_id;
  122.     }
  123. }
  124.  
  125.  
  126. function aviators_dropdown_categories( $args = '' ) {
  127.     $defaults = array(
  128.         'show_option_all' => '', 'show_option_none' => '',
  129.         'orderby' => 'id', 'order' => 'ASC',
  130.         'show_count' => 0,
  131.         'hide_empty' => 1, 'child_of' => 0,
  132.         'exclude' => '', 'echo' => 1,
  133.         'selected' => 0, 'hierarchical' => 0,
  134.         'name' => 'cat', 'id' => '',
  135.         'class' => 'postform', 'depth' => 0,
  136.         'tab_index' => 0, 'taxonomy' => 'category',
  137.         'hide_if_empty' => false,
  138.         'parent' => '',
  139.     );
  140.  
  141.  
  142.  
  143.     // Back compat.
  144.     if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
  145.         _deprecated_argument( __FUNCTION__, '3.0', '' );
  146.         $args['taxonomy'] = 'link_category';
  147.     }
  148.  
  149.     $r = wp_parse_args( $args, $defaults );
  150.  
  151.     if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
  152.         $r['pad_counts'] = true;
  153.     }
  154.  
  155.     extract( $r );
  156.  
  157.     $tab_index_attribute = '';
  158.     if ( (int) $tab_index > 0 )
  159.         $tab_index_attribute = " tabindex=\"$tab_index\"";
  160.  
  161.     $categories = get_terms( $taxonomy, $r );
  162.     $name = esc_attr( $name );
  163.     $class = esc_attr( $class );
  164.     $id = $id ? esc_attr( $id ) : $name;
  165.  
  166.     if ( ! $r['hide_if_empty'] || ! empty($categories) )
  167.         $output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
  168.     else
  169.         $output = '';
  170.  
  171.     if ( empty($categories) && ! $r['hide_if_empty'] && !empty($show_option_none) ) {
  172.  
  173.         /**
  174.          * Filter a taxonomy drop-down display element.
  175.          *
  176.          * A variety of taxonomy drop-down display elements can be modified
  177.          * just prior to display via this filter. Filterable arguments include
  178.          * 'show_option_none', 'show_option_all', and various forms of the
  179.          * term name.
  180.          *
  181.          * @since 1.2.0
  182.          *
  183.          * @see wp_dropdown_categories()
  184.          *
  185.          * @param string $element Taxonomy element to list.
  186.          */
  187.         $show_option_none = apply_filters( 'list_cats', $show_option_none );
  188.         $output .= "\t<option value='-1' selected='selected'>$show_option_none</option>\n";
  189.     }
  190.  
  191.     if ( ! empty( $categories ) ) {
  192.  
  193.         if ( $show_option_all ) {
  194.  
  195.             /** This filter is documented in wp-includes/category-template.php */
  196.             $show_option_all = apply_filters( 'list_cats', $show_option_all );
  197.             $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
  198.             $output .= "\t<option value='0'$selected>$show_option_all</option>\n";
  199.         }
  200.  
  201.         if ( $show_option_none ) {
  202.  
  203.             /** This filter is documented in wp-includes/category-template.php */
  204.             $show_option_none = apply_filters( 'list_cats', $show_option_none );
  205.             $selected = ( '-1' === strval($r['selected']) ) ? " selected='selected'" : '';
  206.             $output .= "\t<option value='-1'$selected>$show_option_none</option>\n";
  207.         }
  208.  
  209.         $output .= walk_category_dropdown_tree( $categories, $depth, $r );
  210.     }
  211.  
  212.     if ( ! $r['hide_if_empty'] || ! empty($categories) )
  213.         $output .= "</select>\n";
  214.  
  215.     /**
  216.      * Filter the taxonomy drop-down output.
  217.      *
  218.      * @since 2.1.0
  219.      *
  220.      * @param string $output HTML output.
  221.      * @param array  $r      Arguments used to build the drop-down.
  222.      */
  223.     $output = apply_filters( 'wp_dropdown_cats', $output, $r );
  224.  
  225.     if ( $echo )
  226.         echo $output;
  227.  
  228.     return $output;
  229. }
  230.  
  231. /**
  232.  * Output an unordered list of checkbox <input> elements labelled
  233.  * with term names. Taxonomy independent version of wp_category_checklist().
  234.  *
  235.  * @since 3.0.0
  236.  *
  237.  * @param int   $post_id
  238.  * @param array $args
  239.  */
  240. function aviators_terms_checklist( $post_id = 0, $args = array() ) {
  241.     $defaults = array(
  242.         'descendants_and_self' => 0,
  243.         'selected_cats'        => false,
  244.         'popular_cats'         => false,
  245.         'walker'               => null,
  246.         'taxonomy'             => 'category',
  247.         'checked_ontop'        => true
  248.     );
  249.     $args     = apply_filters( 'wp_terms_checklist_args', $args, $post_id );
  250.  
  251.     extract( wp_parse_args( $args, $defaults ), EXTR_SKIP );
  252.  
  253.     if ( empty( $walker ) || ! is_a( $walker, 'Walker' ) )
  254.         $walker = new Walker_Category_Checklist;
  255.  
  256.     $descendants_and_self = (int) $descendants_and_self;
  257.  
  258.     $args = array( 'taxonomy' => $taxonomy );
  259.  
  260.     $tax = get_taxonomy( $taxonomy );
  261. //    $args['disabled'] = !current_user_can($tax->cap->assign_terms);
  262.  
  263.     if ( is_array( $selected_cats ) )
  264.         $args['selected_cats'] = $selected_cats;
  265.     elseif ( $post_id )
  266.         $args['selected_cats'] = wp_get_object_terms( $post_id, $taxonomy, array_merge( $args, array( 'fields' => 'ids' ) ) );
  267.     else
  268.         $args['selected_cats'] = array();
  269.  
  270.     if ( is_array( $popular_cats ) )
  271.         $args['popular_cats'] = $popular_cats;
  272.     else
  273.         $args['popular_cats'] = get_terms( $taxonomy, array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
  274.  
  275.     if ( $descendants_and_self ) {
  276.         $categories = (array) get_terms( $taxonomy, array( 'child_of' => $descendants_and_self, 'hierarchical' => 0, 'hide_empty' => 0 ) );
  277.         $self       = get_term( $descendants_and_self, $taxonomy );
  278.         array_unshift( $categories, $self );
  279.     }
  280.     else {
  281.         $categories = (array) get_terms( $taxonomy, array( 'get' => 'all' ) );
  282.     }
  283.  
  284.     if ( $checked_ontop ) {
  285.         // Post process $categories rather than adding an exclude to the get_terms() query to keep the query the same across all posts (for any query cache)
  286.         $checked_categories = array();
  287.         $keys               = array_keys( $categories );
  288.  
  289.         foreach ( $keys as $k ) {
  290.             if ( in_array( $categories[$k]->term_id, $args['selected_cats'] ) ) {
  291.                 $checked_categories[] = $categories[$k];
  292.                 unset( $categories[$k] );
  293.             }
  294.         }
  295.  
  296.         // Put checked cats on top
  297.  
  298.         echo call_user_func_array( array( &$walker, 'walk' ), array( $checked_categories, 0, $args ) );
  299.     }
  300.     // Then the rest of them
  301.     echo call_user_func_array( array( &$walker, 'walk' ), array( $categories, 0, $args ) );
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement