CarterFromSL

Cars Custom Funcrtions Plugin

Jun 3rd, 2021 (edited)
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.64 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Plugin Name: Cars Custom Site Plugin
  5. * Description: Custom code functions unique to this website.
  6. * Author: Strategy Lab
  7. * Author URI: https://strategylab.ca/
  8. * Version: 1.0
  9. */
  10.  
  11. //-- CUSTOM VEHICLE POST TYPE --//
  12.  
  13. // get vehicle taxonomies terms links
  14. function carMetaBeforePost( $content ) {
  15.   if ( is_singular( 'vehicles' ) ) {
  16.     global $post;
  17.     // the custom vehicle
  18.     $taxonomies = array(
  19.                         "make"=>"Make: ",
  20.                         "models"=>"Model: ",
  21.                         "years"=>"Year: ",
  22.                         "transmission"=>"Transmission: ",
  23.                         "drivetrain"=>"Drivetrain: ",
  24.                         "interior_colour"=>"Interior Colour: ",
  25.                         "exterior_colour"=>"Exterior Colour: ",
  26.                   );
  27.     $out = "<ul class='car-meta'>";
  28.     foreach ($taxonomies as $tax => $taxname) {
  29.         // get the terms related to post
  30.         $terms = get_the_terms( $post->ID, $tax );
  31.         if ( !empty( $terms ) ) {
  32.             foreach ( $terms as $term )
  33.             $out .= "<li class='c_".$tax."'>";
  34.             $out .= "<span>".$taxname."</span>";
  35.             $out .= '<a class="c_'.$term->slug.'" href="' .get_term_link($term->slug, $tax) .'">'.$term->name.'</a> ';
  36.         }
  37.         $out .= "</li>";
  38.     }
  39.       $out .= "</ul>";
  40.       $content = $out . $content;
  41.   }
  42.     return $content;
  43. }
  44.  
  45. add_action('the_content', 'carMetaBeforePost');
  46.  
  47. //-- DASHBOARD CUSTOMIZATIONS --//
  48.  
  49. // Limit member media library and post access to posts and files uploaded by that member
  50.  
  51. add_action('pre_get_posts', 'query_set_only_author' );
  52. function query_set_only_author( $wp_query ) {
  53.  global $current_user;
  54.  if( is_admin() && !current_user_can('edit_others_posts') ) {
  55.     $wp_query->set( 'author', $current_user->ID );
  56.     add_filter('views_edit-vehicles', 'fix_post_counts');
  57.     add_filter('views_upload', 'fix_media_counts');
  58.  }
  59. }
  60.  
  61. // Display post counts only for posts from current member
  62.  
  63. function fix_post_counts($views) {
  64.   global $current_user, $wp_query;
  65.   unset($views['mine']);
  66.   $types = array(
  67.     array( 'status' =>  NULL ),
  68.     array( 'status' => 'publish' ),
  69.     array( 'status' => 'draft' ),
  70.     array( 'status' => 'pending' ),
  71.     array( 'status' => 'trash' )
  72.   );
  73.  
  74.   foreach( $types as $type ) {
  75.     $query = array(
  76.       'author'   => $current_user->ID,
  77.       'post_type'   => 'vehicles',
  78.       'post_status' => $type['status']
  79.     );
  80.    
  81.     $result = new WP_Query($query);
  82.  
  83.     if( $type['status'] == NULL ):
  84.      
  85.       $class = ($wp_query->query_vars['post_status'] == NULL) ? ' class="current"' : '';
  86.       $views['all'] = sprintf(__('<a href="%s"'. $class .'>All <span class="count">(%d)</span></a>', 'all'),
  87.         admin_url('edit.php?post_type=vehicles'),
  88.         $result->found_posts);
  89.  
  90.     elseif( $type['status'] == 'publish' ):
  91.  
  92.       $class = ($wp_query->query_vars['post_status'] == 'publish') ? ' class="current"' : '';
  93.       $views['publish'] = sprintf(__('<a href="%s"'. $class .'>Published <span class="count">(%d)</span></a>', 'publish'),
  94.         admin_url('edit.php?post_status=publish&post_type=vehicles'),
  95.         $result->found_posts);
  96.  
  97.     elseif( $type['status'] == 'draft' ):
  98.  
  99.        $class = ($wp_query->query_vars['post_status'] == 'draft') ? ' class="current"' : '';
  100.        $views['draft'] = sprintf(__('<a href="%s"'. $class .'>Draft'. ((sizeof($result->posts) > 1) ? "s" : "") .' <span class="count">(%d)</span></a>', 'draft'),
  101.          admin_url('edit.php?post_status=draft&post_type=vehicles'),
  102.          $result->found_posts);
  103.  
  104.     elseif( $type['status'] == 'pending' ):
  105.  
  106.         $class = ($wp_query->query_vars['post_status'] == 'pending') ? ' class="current"' : '';
  107.         $views['pending'] = sprintf(__('<a href="%s"'. $class .'>Pending <span class="count">(%d)</span></a>', 'pending'),
  108.           admin_url('edit.php?post_status=pending&post_type=vehicles'),
  109.           $result->found_posts);
  110.  
  111.     elseif( $type['status'] == 'trash' ):
  112.  
  113.         $class = ($wp_query->query_vars['post_status'] == 'trash') ? ' class="current"' : '';
  114.         $views['trash'] = sprintf(__('<a href="%s"'. $class .'>Trash <span class="count">(%d)</span></a>', 'trash'),
  115.           admin_url('edit.php?post_status=trash&post_type=vehicles'),
  116.           $result->found_posts);
  117.  
  118.     endif;
  119.   }
  120.  
  121.   return $views;
  122. }
  123.  
  124. // Hide other users' media counts from current member
  125.  
  126. function fix_media_counts($views) {
  127.   global $wpdb, $current_user, $post_mime_types, $avail_post_mime_types;
  128.   $views = array();
  129.   $_num_posts = array();
  130.   $count = $wpdb->get_results( "
  131.  
  132.    SELECT post_mime_type, COUNT( * ) AS num_posts
  133.    FROM $wpdb->posts
  134.    WHERE post_type = 'attachment'
  135.    AND post_author = $current_user->ID
  136.    AND post_status != 'trash'
  137.    GROUP BY post_mime_type
  138.  
  139. ", ARRAY_A );
  140.  
  141.  foreach( $count as $row )
  142.     $_num_posts[$row['post_mime_type']] = $row['num_posts'];
  143.     $_total_posts = array_sum($_num_posts);
  144.     $detached = isset( $_REQUEST['detached'] ) || isset( $_REQUEST['find_detached'] );
  145.  
  146.  if ( !isset( $total_orphans ) )
  147.     $total_orphans = $wpdb->get_var("
  148.  
  149.        SELECT COUNT( * )
  150.        FROM $wpdb->posts
  151.        WHERE post_type = 'attachment'
  152.        AND post_author = $current_user->ID
  153.        AND post_status != 'trash'
  154.        AND post_parent < 1
  155.  
  156.    ");
  157.  
  158.   $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
  159.  
  160.   foreach ( $matches as $type => $reals )
  161.  
  162.     foreach ( $reals as $real )
  163.  
  164.       $num_posts[$type] = ( isset( $num_posts[$type] ) ) ? $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real];
  165.       $class = ( empty($_GET['post_mime_type']) && !$detached && !isset($_GET['status']) ) ? ' class="current"' : '';
  166.       $views['all'] = "<a href='upload.php'$class>" . sprintf( __('All <span class="count">(%s)</span>', 'uploaded files' ), number_format_i18n( $_total_posts )) . '</a>';
  167.  
  168.    foreach ( $post_mime_types as $mime_type => $label ) {
  169.  
  170.     $class = '';
  171.     if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
  172.       continue;
  173.  
  174.     if ( !empty($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
  175.       $class = ' class="current"';
  176.  
  177.     if ( !empty( $num_posts[$mime_type] ) )
  178.       $views[$mime_type] = "<a href='upload.php?post_mime_type=$mime_type'$class>" . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), $num_posts[$mime_type] ) . '</a>';
  179.  
  180.   }
  181.  
  182.  $views['detached'] = '<a href="upload.php?detached=1"' . ( $detached ? ' class="current"' : '' ) . '>' . sprintf( __( 'Unattached <span class="count">(%s)</span>', 'detached files' ), $total_orphans ) . '</a>';
  183.  
  184.  return $views;
  185. }
  186.  
  187. // Redirect members who access the New Post page to the New Vehicle page instead
  188.  
  189. add_action( 'current_screen', function() {
  190.     $screen = get_current_screen();
  191.     if ( is_admin() && isset( $screen->id ) && $screen->id == 'post' && !current_user_can('edit_others_posts') ) {
  192.         wp_redirect( admin_url( 'post-new.php?post_type=vehicles' ) );
  193.         exit();
  194.     }
  195. } );
  196.  
  197. // Set default colour scheme
  198.  
  199. function set_default_admin_color($user_id) {
  200.     $args = array(
  201.         'ID' => $user_id,
  202.         'admin_color' => 'modern'
  203.     );
  204.     wp_update_user( $args );
  205. }
  206. add_action('user_register', 'set_default_admin_color');
  207.  
  208. /**
  209.  * Limit the upload size limit for non-administrators.
  210.  *
  211.  * @param string $size Upload size limit (in bytes).
  212.  * @return int (maybe) Filtered size limit.
  213.  */
  214.  
  215. function filter_site_upload_size_limit( $size ) {
  216.     // Set the upload size limit to 1 MB for users lacking the 'manage_options' capability.
  217.     if ( ! current_user_can( 'manage_options' ) ) {
  218.         // 2 MB.
  219.         $size = 1024 * 2000;
  220.     }
  221.     return $size;
  222. }
  223. add_filter( 'upload_size_limit', 'filter_site_upload_size_limit', 20 );
Add Comment
Please, Sign In to add comment