PluginProbe
Cooked – Recipe Management / 1.11.4
Cooked – Recipe Management v1.11.4
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.11.4, at includes/class.cooked-users.php

193 lines 7.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[2]&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 = false, $basic = false, $user_nicename = false ) {
48 if ( !$user_id && $user_nicename ) {
49 $user = get_user_by( 'slug', $user_nicename );
50 if ( $user ) {
51 $user_id = $user->ID;
52 } else {
53 return false;
54 }
55 }
56
57 $_user = get_userdata( $user_id );
58 $_user_meta = get_user_meta( $user_id, 'cooked_user_meta', true );
59
60 if ( !empty($_user) ) {
61 if ( isset( $_user_meta['profile_photo_id'] ) && $_user_meta['profile_photo_id'] && wp_attachment_is_image( $_user_meta['profile_photo_id'] ) ) {
62 $profile_photo = wp_get_attachment_image( $_user_meta['profile_photo_id'], 'cooked-profile-photo' );
63 $profile_photo_src = wp_get_attachment_image_src( $_user_meta['profile_photo_id'], 'cooked-profile-photo' );
64 $profile_photo_src = isset($profile_photo_src[0]) && $profile_photo_src[0] ? $profile_photo_src[0] : false;
65 } else {
66 $profile_photo = get_avatar($_user->user_email, 96);
67 $profile_photo_src = get_avatar_url($_user->user_email, ['size' => 96]);
68 }
69
70 if ( is_array($_user_meta) ) {
71 $_user_data = $_user_meta;
72 }
73
74 $_user_data['id'] = $user_id;
75 $_user_data['user_nicename'] = $_user->user_nicename;
76 $_user_data['name'] = self::format_author_name( $_user->display_name );
77 $_user_data['profile_photo'] = $profile_photo;
78 $_user_data['profile_photo_src'] = $profile_photo_src;
79
80 if ( !$basic ) {
81 $_user_data['raw'] = $_user;
82
83 $user_recipe_args = [
84 'post_type' => 'cp_recipe',
85 'post_status' => ['publish', 'pending', 'draft'],
86 'posts_per_page' => -1,
87 'author' => $user_id
88 ];
89
90 $_user_data['recipes'] = Cooked_Recipes::get( $user_recipe_args, false, true );
91 }
92 return $_user_data;
93 } else {
94 return false;
95 }
96 }
97
98 public static function format_author_name( $name, $format = false ) {
99 if ( !$name ) return false;
100
101 global $_cooked_settings;
102
103 $_cooked_settings = !$_cooked_settings || $_cooked_settings && empty($_cooked_settings) ? Cooked_Settings::get() : $_cooked_settings;
104
105 if ( !$format && isset($_cooked_settings['author_name_format']) && $_cooked_settings['author_name_format'] ):
106 $format = $_cooked_settings['author_name_format'];
107 elseif (!$format):
108 $format = 'full';
109 endif;
110
111 $filtered = apply_filters( 'cooked_format_author_name', $name, $format );
112
113 // If the filter returns an array with a second element true, treat as safe.
114 if ( is_array( $filtered ) && isset( $filtered[1] ) && $filtered[1] === true ) {
115 $name = $filtered[0];
116 $safe = true;
117 } else {
118 $name = is_array( $filtered ) ? $filtered[0] : $filtered;
119 $safe = false;
120 }
121
122 switch ( $format ) {
123 case 'full':
124 if ( $safe ) {
125 return $name;
126 } else {
127 return esc_html( $name );
128 }
129 case 'first_last_initial':
130 $name = explode( ' ', $name );
131 if ( isset($name[1]) ):
132 return $name[0] . ' ' . substr( $name[1], 0, 1) . '.';
133 endif;
134 return $name[0];
135 case 'first_initial_last':
136 $name = explode( ' ', $name );
137 if ( isset($name[1]) ):
138 return substr( $name[0], 0, 1) . '. ' . esc_html( $name[1] );
139 endif;
140 return $name[0];
141 case 'first_only':
142 $name = explode( ' ', $name );
143 return esc_html( $name[0] );
144 }
145
146 if ( $safe ) {
147 return $name;
148 } else {
149 return esc_html( $name );
150 }
151 }
152
153 function recipe_count_column($column_headers) {
154 $column_headers['cooked_recipe_count'] = __('Recipes', 'cooked');
155
156 return $column_headers;
157 }
158
159 function recipe_count_column_sortable( $columns ) {
160 $columns['cooked_recipe_count'] = 'cooked_recipe_count';
161
162 return $columns;
163 }
164
165 function recipe_count_column_value( $value, $column_name, $user_id ) {
166 if ( 'cooked_recipe_count' === $column_name ) {
167 $value = count_user_posts( $user_id, 'cp_recipe' );
168 }
169
170 return $value;
171 }
172
173 function pre_user_query( $query ) {
174 global $wpdb, $current_screen;
175
176 // Only filter in the admin.
177 if ( ! is_admin() ) return;
178
179 // Only filter on the users screen.
180 if ( ! ( isset( $current_screen ) && 'users' === $current_screen->id ) ) return;
181
182 // Only filter if orderby is set to 'cooked_recipe_count'.
183 if ( isset( $query->query_vars ) && isset( $query->query_vars[ 'orderby' ] ) && ( 'cooked_recipe_count' == $query->query_vars[ 'orderby' ] ) ) {
184 // We need the order - default is ASC.
185 $order = isset( $query->query_vars ) && isset( $query->query_vars[ 'order' ] ) && strcasecmp( $query->query_vars[ 'order' ], 'desc' ) == 0 ? 'DESC' : 'ASC';
186
187 // Order the posts by recipe count.
188 $query->query_orderby = "ORDER BY ( SELECT COUNT(*) FROM {$wpdb->posts} posts WHERE posts.post_type = 'cp_recipe' AND posts.post_status IN ('publish', 'pending', 'draft') AND posts.post_author = {$wpdb->users}.ID ) {$order}";
189 }
190 }
191
192 }
193