PluginProbe
Cooked – Recipe Management / 1.9.5
Cooked – Recipe Management v1.9.5
1.16.1 1.16.0 1.15.0 trunk 1.10.0 1.11.0 1.11.1 1.11.2 1.11.3 1.11.4 1.12.0 1.13.0 1.14.0 1.7.10 1.7.11 1.7.12 1.7.13 1.7.15.1 1.7.15.3 1.7.15.4 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 All 37 releases
cooked / includes / class.cooked-users.php

class.cooked-users.php in Cooked – Recipe Management 1.9.5, at includes/class.cooked-users.php

165 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Cooked User-Specific Functions
4 *
5 * @package Cooked
6 * @subpackage User-Specific Functions
7 * @since 1.0.0
8 */
9
10 // Exit if accessed directly
11 if ( ! defined( 'ABSPATH' ) ) exit;
12
13 /**
14 * Cooked_User Class
15 *
16 * This class handles the Cooked Recipe Meta Box creation.
17 *
18 * @since 1.0.0
19 */
20 class Cooked_Users {
21
22 function __construct(){
23 add_action( 'init', [&$this, 'recipe_author_rewrite'], 10 );
24
25 add_filter( 'manage_users_columns', [&$this, 'recipe_count_column'] );
26 add_filter( 'manage_users_sortable_columns', [&$this, 'recipe_count_column_sortable'] );
27 add_filter( 'manage_users_custom_column', [&$this, 'recipe_count_column_value'], 10, 3 );
28 add_action( 'pre_user_query', [&$this, 'pre_user_query'], 1 );
29 }
30
31 public static function recipe_author_rewrite() {
32 global $_cooked_settings;
33
34 $browse_page_id = ( isset($_cooked_settings['browse_page']) && $_cooked_settings['browse_page'] ? $_cooked_settings['browse_page'] : false );
35 $front_page_id = get_option( 'page_on_front' );
36 $browse_page_slug = ( $browse_page_id ? basename( get_permalink( $browse_page_id ) ) : false );
37
38 if ( $browse_page_id != $front_page_id ) {
39 add_rewrite_tag('%recipe_author%', '([^&]+)');
40 if ( isset( $_cooked_settings['browse_page'] ) ) {
41 add_rewrite_rule('^' . $browse_page_slug . '/' . $_cooked_settings['recipe_author_permalink'] . '/([^/]*)/([^/]*)/page/([^/]*)/?', 'index.php?page_id=' . esc_attr( $_cooked_settings['browse_page'] ) . '&paged=$matches[3]&recipe_author=$matches[1]', 'top' );
42 add_rewrite_rule('^' . $browse_page_slug . '/' . $_cooked_settings['recipe_author_permalink'] . '/([^/]*)/?', 'index.php?page_id=' . esc_attr( $_cooked_settings['browse_page'] ) . '&recipe_author=$matches[1]', 'top' );
43 }
44 }
45 }
46
47 public static function get( $user_id, $basic = false ) {
48 $_user = get_userdata( $user_id );
49 $_user_meta = get_user_meta( $user_id, 'cooked_user_meta', true );
50
51 if ( !empty($_user) ) {
52 if ( isset( $_user_meta['profile_photo_id'] ) && $_user_meta['profile_photo_id'] && wp_attachment_is_image( $_user_meta['profile_photo_id'] ) ) {
53 $profile_photo = wp_get_attachment_image( $_user_meta['profile_photo_id'], 'cooked-square' );
54 $profile_photo_src = wp_get_attachment_image_src( $_user_meta['profile_photo_id'], 'cooked-square' );
55 $profile_photo_src = ( isset($profile_photo_src[0]) && $profile_photo_src[0] ? $profile_photo_src[0] : false );
56 } else {
57 $profile_photo = get_avatar($_user->user_email);
58 $profile_photo_src = get_avatar_url($_user->user_email);
59 }
60
61 if ( is_array($_user_meta) ) {
62 $_user_data = $_user_meta;
63 }
64
65 $_user_data['id'] = $user_id;
66 $_user_data['name'] = self::format_author_name( $_user->display_name );
67 $_user_data['profile_photo'] = $profile_photo;
68 $_user_data['profile_photo_src'] = $profile_photo_src;
69
70 if ( !$basic ) {
71 $_user_data['raw'] = $_user;
72
73 $user_recipe_args = [
74 'post_type' => 'cp_recipe',
75 'post_status' => 'publish',
76 'posts_per_page' => -1,
77 'author' => $user_id
78 ];
79
80 $_user_data['recipes'] = Cooked_Recipes::get( $user_recipe_args, false, true );
81 }
82 return $_user_data;
83 } else {
84 return false;
85 }
86 }
87
88 public static function format_author_name( $name, $format = false ) {
89 if ( !$name ) return false;
90
91 global $_cooked_settings;
92
93 $_cooked_settings = !$_cooked_settings || $_cooked_settings && empty($_cooked_settings) ? Cooked_Settings::get() : $_cooked_settings;
94
95 if ( !$format && isset($_cooked_settings['author_name_format']) && $_cooked_settings['author_name_format'] ):
96 $format = $_cooked_settings['author_name_format'];
97 elseif (!$format):
98 $format = 'full';
99 endif;
100
101 switch ( $format ) {
102 case 'full':
103 return $name;
104 case 'first_last_initial':
105 $name = explode( ' ', $name );
106 if ( isset($name[1]) ):
107 return $name[0] . ' ' . substr( $name[1], 0, 1) . '.';
108 endif;
109 return $name[0];
110 case 'first_initial_last':
111 $name = explode( ' ', $name );
112 if ( isset($name[1]) ):
113 return substr( $name[0], 0, 1) . '. ' . esc_html( $name[1] );
114 endif;
115 return $name[0];
116 case 'first_only':
117 $name = explode( ' ', $name );
118 return esc_html( $name[0] );
119 }
120
121 return esc_html( $name );
122 }
123
124 function recipe_count_column($column_headers) {
125 $column_headers['cooked_recipe_count'] = __('Recipes', 'cooked');
126
127 return $column_headers;
128 }
129
130 function recipe_count_column_sortable( $columns ) {
131 $columns['cooked_recipe_count'] = 'cooked_recipe_count';
132
133 return $columns;
134 }
135
136 function recipe_count_column_value( $value, $column_name, $user_id ) {
137 if ( 'cooked_recipe_count' === $column_name ) {
138 $value = count_user_posts( $user_id, 'cp_recipe' );
139 }
140
141 return $value;
142 }
143
144
145 function pre_user_query( $query ) {
146 global $wpdb, $current_screen;
147
148 // Only filter in the admin
149 if ( ! is_admin() ) return;
150
151 // Only filter on the users screen
152 if ( ! ( isset( $current_screen ) && 'users' === $current_screen->id ) ) return;
153
154 // Only filter if orderby is set to 'cooked_recipe_count'
155 if ( isset( $query->query_vars ) && isset( $query->query_vars[ 'orderby' ] ) && ( 'cooked_recipe_count' == $query->query_vars[ 'orderby' ] ) ) {
156 // We need the order - default is ASC
157 $order = isset( $query->query_vars ) && isset( $query->query_vars[ 'order' ] ) && strcasecmp( $query->query_vars[ 'order' ], 'desc' ) == 0 ? 'DESC' : 'ASC';
158
159 // Order the posts by recipe count
160 $query->query_orderby = "ORDER BY ( SELECT COUNT(*) FROM {$wpdb->posts} posts WHERE posts.post_type = 'cp_recipe' AND posts.post_status = 'publish' AND posts.post_author = {$wpdb->users}.ID ) {$order}";
161 }
162 }
163
164 }
165