Issues (13)

src/helpers.php (9 issues)

Labels
Severity
1
<?php
2
3
namespace Helick\RelatedPosts;
4
5
use WP_Query;
0 ignored issues
show
The type WP_Query was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
/**
8
 * Get the related post ids.
9
 *
10
 * @param int   $postId
11
 * @param array $args
12
 *
13
 * @return array
14
 */
15
function get(int $postId, array $args = []): array
16
{
17
    $defaultArgs = [
18
        'ep_integrate' => defined('EP_VERSION'),
19
        'post_types'   => ['post'],
20
        'taxonomies'   => ['category'],
21
        'terms'        => [],
22
        'terms_not_in' => [],
23
        'limit'        => 10,
24
    ];
25
26
    $args = wp_parse_args($args, $defaultArgs);
0 ignored issues
show
The function wp_parse_args was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
    $args = /** @scrutinizer ignore-call */ wp_parse_args($args, $defaultArgs);
Loading history...
27
28
    /**
29
     * Control the args.
30
     *
31
     * @param array $args
32
     * @param int   $postId
33
     */
34
    $args = apply_filters('helick_related_posts_args', $args, $postId);
0 ignored issues
show
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
    $args = /** @scrutinizer ignore-call */ apply_filters('helick_related_posts_args', $args, $postId);
Loading history...
35
36
    $transient = sprintf('helick_related_posts_%d_%s', $postId, hash('md5', json_encode((array)$args)));
37
38
    $relatedPostIds = get_transient($transient);
0 ignored issues
show
The function get_transient was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
    $relatedPostIds = /** @scrutinizer ignore-call */ get_transient($transient);
Loading history...
39
40
    if (is_array($relatedPostIds)) {
41
        return $relatedPostIds;
42
    }
43
44
    $manualRelatedPostIds = array_column((array)carbon_get_post_meta($postId, 'helick_related_posts'), 'id');
45
    $manualRelatedPostIds = array_map('intval', $manualRelatedPostIds);
46
47
    $limit = $args['limit'] - count($manualRelatedPostIds);
48
49
    if ($limit > 0) {
50
        $queryArgs = [
51
            'ep_integrate'   => $args['ep_integrate'],
52
            'fields'         => 'ids',
53
            'post_type'      => $args['post_types'],
54
            'post_status'    => 'publish',
55
            'post__not_in'   => array_merge([$postId], $manualRelatedPostIds),
56
            'posts_per_page' => $limit,
57
            'tax_query'      => [],
58
            'order'          => 'DESC',
59
        ];
60
61
        $terms = $args['terms'];
62
63
        if (empty($terms)) {
64
            $terms = wp_get_object_terms($postId, $args['taxonomies']);
0 ignored issues
show
The function wp_get_object_terms was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
            $terms = /** @scrutinizer ignore-call */ wp_get_object_terms($postId, $args['taxonomies']);
Loading history...
65
            if (is_wp_error($terms)) {
0 ignored issues
show
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
            if (/** @scrutinizer ignore-call */ is_wp_error($terms)) {
Loading history...
66
                $terms = [];
67
            }
68
        }
69
70
        foreach ($terms as $term) {
71
            if (!isset($queryArgs['tax_query'][$term->taxonomy])) {
72
                $queryArgs['tax_query'][$term->taxonomy] = [
73
                    'taxonomy' => $term->taxonomy,
74
                    'field'    => 'term_id',
75
                    'terms'    => [],
76
                ];
77
            }
78
79
            array_push($queryArgs['tax_query'][$term->taxonomy]['terms'], $term->term_id);
80
        }
81
82
        foreach ($args['terms_not_in'] as $term) {
83
            if (!isset($queryArgs['tax_query']['not_' . $term->taxonomy])) {
84
                $queryArgs['tax_query']['not_' . $term->taxonomy] = [
85
                    'taxonomy' => $term->taxonomy,
86
                    'field'    => 'term_id',
87
                    'terms'    => [],
88
                    'operator' => 'NOT IN',
89
                ];
90
            }
91
92
            array_push($queryArgs['tax_query']['not_' . $term->taxonomy]['terms'], $term->term_id);
93
        }
94
95
        $queryArgs['tax_query']             = array_values($queryArgs['tax_query']);
96
        $queryArgs['tax_query']['relation'] = 'OR';
97
98
        if ($args['ep_integrate']) {
99
            $relatedTaxonomies = array_map(function ($taxonomy) {
100
                return "terms.{$taxonomy}.name";
101
            }, $args['taxonomies']);
102
103
            $relatedFields = apply_filters('helick_related_posts_fields', array_merge([
104
                'post_title',
105
                'post_content',
106
            ], $relatedTaxonomies));
107
108
            $queryArgs['more_like']        = $postId;
109
            $queryArgs['more_like_fields'] = $relatedFields;
110
        }
111
112
        /**
113
         * Control the query args.
114
         *
115
         * @param array $queryArgs
116
         * @param int   $postId
117
         * @param array $args
118
         */
119
        $queryArgs = apply_filters('helick_related_posts_query_args', $queryArgs, $postId, $args);
120
121
        $query = new WP_Query($queryArgs);
122
        wp_reset_postdata();
0 ignored issues
show
The function wp_reset_postdata was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
        /** @scrutinizer ignore-call */ 
123
        wp_reset_postdata();
Loading history...
123
124
        $relatedPostIds = array_merge($manualRelatedPostIds, $query->posts);
125
        $relatedPostIds = array_map('intval', $relatedPostIds);
126
        $relatedPostIds = array_unique($relatedPostIds);
127
    }
128
129
    $relatedPostIds = array_slice($relatedPostIds, 0, $args['limit']);
130
131
    set_transient($transient, $relatedPostIds, HOUR_IN_SECONDS);
0 ignored issues
show
The function set_transient was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
    /** @scrutinizer ignore-call */ 
132
    set_transient($transient, $relatedPostIds, HOUR_IN_SECONDS);
Loading history...
The constant Helick\RelatedPosts\HOUR_IN_SECONDS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
132
133
    return $relatedPostIds;
134
}
135