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