/
extras.php
123 lines (98 loc) · 3.3 KB
/
extras.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* Custom functions that act independently of the theme templates.
*
* Eventually, some of the functionality here could be replaced by core features.
*
* @package _s
*/
/**
* Returns true if a blog has more than 1 category, else false.
*
* @author WebDevStudios
*
* @return bool Whether the blog has more than one category.
*/
function _s_categorized_blog() {
$category_count = get_transient( '_s_categories' );
if ( false === $category_count ) {
$category_count_query = get_categories( [ 'fields' => 'count' ] );
$category_count = isset( $category_count_query[0] ) ? (int) $category_count_query[0] : 0;
set_transient( '_s_categories', $category_count );
}
return $category_count > 1;
}
/**
* Get an attachment ID from it's URL.
*
* @author WebDevStudios
*
* @param string $attachment_url The URL of the attachment.
*
* @return int The attachment ID.
*/
function _s_get_attachment_id_from_url( $attachment_url = '' ) {
global $wpdb;
$attachment_id = false;
// If there is no url, return.
if ( '' === $attachment_url ) {
return false;
}
// Get the upload directory paths.
$upload_dir_paths = wp_upload_dir();
// Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image.
if ( false !== strpos( $attachment_url, $upload_dir_paths['baseurl'] ) ) {
// If this is the URL of an auto-generated thumbnail, get the URL of the original image.
$attachment_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url );
// Remove the upload path base directory from the attachment URL.
$attachment_url = str_replace( $upload_dir_paths['baseurl'] . '/', '', $attachment_url );
// Do something with $result.
// phpcs:ignore phpcs:ignore WordPress.DB -- db call ok, cache ok, placeholder ok.
$attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM {$wpdb->posts} wposts, {$wpdb->postmeta} wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = %s AND wposts.post_type = 'attachment'", $attachment_url ) );
}
return $attachment_id;
}
/**
* Shortcode to display copyright year.
*
* @author Haris Zulfiqar
*
* @param array $atts Optional attributes.
* $starting_year Optional. Define starting year to show starting year and current year e.g. 2015 - 2018.
* $separator Optional. Separator between starting year and current year.
*
* @return string Copyright year text.
*/
function _s_copyright_year( $atts ) {
// Setup defaults.
$args = shortcode_atts(
[
'starting_year' => '',
'separator' => ' - ',
],
$atts
);
$current_year = gmdate( 'Y' );
// Return current year if starting year is empty.
if ( ! $args['starting_year'] ) {
return $current_year;
}
return esc_html( $args['starting_year'] . $args['separator'] . $current_year );
}
add_shortcode( '_s_copyright_year', '_s_copyright_year', 15 );
/**
* Retrieve the URL of the custom logo uploaded, if one exists.
*
* @author Corey Collins
*/
function _s_get_custom_logo_url() {
$custom_logo_id = get_theme_mod( 'custom_logo' );
if ( ! $custom_logo_id ) {
return;
}
$custom_logo_image = wp_get_attachment_image_src( $custom_logo_id, 'full' );
if ( ! isset( $custom_logo_image[0] ) ) {
return;
}
return $custom_logo_image[0];
}