Passed
Branch develop (5de766)
by Paul
13:18
created

compatibility.php (1 issue)

Labels
Severity
1
<?php
2
3
use GeminiLabs\SiteReviews\Application;
4
use GeminiLabs\SiteReviews\Compatibility;
5
use GeminiLabs\SiteReviews\Contracts\BuilderContract;
6
use GeminiLabs\SiteReviews\Database\ReviewManager;
7
use GeminiLabs\SiteReviews\Modules\Paginate;
8
use GeminiLabs\SiteReviews\Review;
9
10
defined('ABSPATH') || exit;
11
12
/**
13
 * This fixes the "Product Reviews Style" setting in the Salient theme when
14
 * the option is set to "Submission Form Off Canvas".
15
 *
16
 * @see https://themenectar.com/salient/
17
 */
18
add_action('wp_head', function () {
19
    if (!class_exists('WooCommerce') || !function_exists('is_product')) {
20
        return;
21
    }
22
    if ('Salient' !== wp_get_theme(get_template())->get('Name')) {
23
        return;
24
    }
25
    if ('yes' !== get_option('woocommerce_enable_reviews', 'yes')) {
26
        return;
27
    }
28
    if (!glsr_get_option('integrations.woocommerce.enabled', false, 'bool')) {
29
        return;
30
    }
31
    if (is_product()) {
32
        add_filter('woocommerce_reviews_title', function ($title, $count) {
33
            return $title.(0 === $count ? apply_filters('nectar_woocommerce_no_reviews_title', '') : '');
34
        }, 10, 2);
35
        printf('<style>%s</style>',
36
            '.wp-theme-salient.single-product #reviews[data-integration="site-reviews"] #comments .commentlist{'.
37
                'width:100%;'.
38
            '}'.
39
            '@media only screen and (min-width:1000px){'.
40
                '.wp-theme-salient.single-product #reviews[data-integration="site-reviews"] #comments .commentlist{'.
41
                    'flex:1;'.
42
                    'padding-left:7.5%;'.
43
                '}'.
44
            '}'
45
        );
46
    }
47
});
48
49
/**
50
 * This fixes the Site Reviews settings when the User Activity Log plugin is enabled.
51
 * 
52
 * @see https://wordpress.org/support/topic/this-breaks-the-wordpress-color-picker/
53
 */
54
add_action('admin_enqueue_scripts', function () {
55
    $screen = get_current_screen();
0 ignored issues
show
Are you sure the assignment to $screen is correct as get_current_screen() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
56
    if (str_starts_with($screen->post_type, glsr()->post_type)) {
57
        wp_dequeue_script('chats-js');
58
    }
59
}, 20);
60
61
/**
62
 * This fixes the "Show reviews count" option in the WoodMart theme.
63
 *
64
 * @see https://woodmart.xtemos.com/
65
 */
66
function glsr_fix_woodmart_product_reviews_count_option($html): string
67
{
68
    global $product;
69
    if (!function_exists('woodmart_get_opt') || !function_exists('woodmart_show_reviews_count')) {
70
        return $html;
71
    }
72
    if (!is_a($product, 'WC_Product')) {
73
        return $html;
74
    }
75
    if (!glsr_get_option('integrations.woocommerce.enabled', false, 'bool')) {
76
        return $html;
77
    }
78
    if (str_contains($html, 'wd-star-rating')) {
79
        return $html;
80
    }
81
    if (woodmart_get_opt('show_reviews_count')) {
82
        ob_start();
83
        woodmart_show_reviews_count();
84
        $html .= ob_get_clean();
85
    }
86
    return sprintf('<div class="wd-star-rating">%s</div>', $html);
87
}
88
add_filter('woocommerce_product_get_rating_html', 'glsr_fix_woodmart_product_reviews_count_option', 30);
89
90
/**
91
 * This fixes the button classes in themes using the Avia framework.
92
 *
93
 * @see https://kriesi.at/themes/enfold-overview/
94
 */
95
function glsr_filter_avia_button_classes(array $defaults): array
96
{
97
    $defaults['button'] = 'glsr-button avia-button avia-color-theme-color';
98
    return $defaults;
99
}
100
add_action('avia_action_before_framework_init', function () {
101
    add_filter('site-reviews/defaults/style-classes/defaults', 'glsr_filter_avia_button_classes');
102
});
103
104
/**
105
 * This fixes the Duplicate option provided by the Enfold theme.
106
 *
107
 * @see https://kriesi.at/themes/enfold-overview/
108
 */
109
add_action('avf_duplicate_post_added', function ($newPostId, $post) {
110
    if (!Review::isReview($post)) {
111
        return;
112
    }
113
    $review = glsr_get_review($post->ID);
114
    if ($review->isValid()) {
115
        glsr(ReviewManager::class)->createFromPost((int) $newPostId, $review->toArray());
116
    }
117
}, 10, 2);
118
119
/**
120
 * Classic Editor.
121
 *
122
 * @return array
123
 *
124
 * @see https://wordpress.org/plugins/classic-editor/
125
 */
126
add_action('edit_form_top', function ($post) {
127
    if (class_exists('Classic_Editor') && glsr()->post_type === get_post_type($post)) {
128
        remove_action('edit_form_top', ['Classic_Editor', 'remember_classic_editor']);
129
    }
130
}, 0);
131
132
/**
133
 * Bootstrap pagination.
134
 *
135
 * @filter site-reviews/paginate_link
136
 */
137
function glsr_filter_bootstrap_pagination_link(array $link, array $args, BuilderContract $builder): array
138
{
139
    $args['class'] = 'page-link';
140
    if ('current' === $link['type']) {
141
        $class = 'page-item active';
142
        $text = $builder->span($args);
143
    }
144
    if ('dots' === $link['type']) {
145
        $text = $builder->span($args);
146
    }
147
    $link['link'] = $builder->li([
148
        'text' => $text ?? $builder->a($args),
149
        'class' => $class ?? 'page-item',
150
    ]);
151
    return $link;
152
}
153
add_filter('site-reviews/paginate_links', function (string $links, array $args): string {
154
    if ('bootstrap' !== glsr_get_option('general.style')) {
155
        return $links;
156
    }
157
    $args = wp_parse_args(['mid_size' => 1], $args);
158
    add_filter('site-reviews/paginate_link', 'glsr_filter_bootstrap_pagination_link', 10, 3);
159
    $links = (new Paginate($args))->links();
160
    remove_filter('site-reviews/paginate_link', 'glsr_filter_bootstrap_pagination_link');
161
    $links = wp_list_pluck($links, 'link');
162
    return implode("\n", $links);
163
}, 10, 2);
164
165
/**
166
 * @param array $editors
167
 * @param string $postType
168
 *
169
 * @return array
170
 *
171
 * @see https://wordpress.org/plugins/classic-editor/
172
 */
173
add_filter('classic_editor_enabled_editors_for_post_type', function ($editors, $postType) {
174
    return glsr()->post_type === $postType
175
        ? ['block_editor' => false, 'classic_editor' => false]
176
        : $editors;
177
}, 10, 2);
178
179
/**
180
 * Add human-readable capability names.
181
 *
182
 * @return void
183
 *
184
 * @see https://wordpress.org/plugins/members/
185
 */
186
add_action('members_register_caps', function () {
187
    $labels = [
188
        'create_site-reviews' => _x('Create Reviews', 'admin-text', 'site-reviews'),
189
        'delete_others_site-reviews' => _x("Delete Others' Reviews", 'admin-text', 'site-reviews'),
190
        'delete_site-reviews' => _x('Delete Reviews', 'admin-text', 'site-reviews'),
191
        'delete_private_site-reviews' => _x('Delete Private Reviews', 'admin-text', 'site-reviews'),
192
        'delete_published_site-reviews' => _x('Delete Approved Reviews', 'admin-text', 'site-reviews'),
193
        'edit_others_site-reviews' => _x("Edit Others' Reviews", 'admin-text', 'site-reviews'),
194
        'edit_site-reviews' => _x('Edit Reviews', 'admin-text', 'site-reviews'),
195
        'edit_private_site-reviews' => _x('Edit Private Reviews', 'admin-text', 'site-reviews'),
196
        'edit_published_site-reviews' => _x('Edit Approved Reviews', 'admin-text', 'site-reviews'),
197
        'publish_site-reviews' => _x('Approve Reviews', 'admin-text', 'site-reviews'),
198
        'read_private_site-reviews' => _x('Read Private Reviews', 'admin-text', 'site-reviews'),
199
        'respond_to_site-reviews' => _x('Respond To Reviews', 'admin-text', 'site-reviews'),
200
        'respond_to_others_site-reviews' => _x("Respond To Others' Reviews", 'admin-text', 'site-reviews'),
201
        'assign_site-review_terms' => _x('Assign Review Categories', 'admin-text', 'site-reviews'),
202
        'delete_site-review_terms' => _x('Delete Review Categories', 'admin-text', 'site-reviews'),
203
        'edit_site-review_terms' => _x('Edit Review Categories', 'admin-text', 'site-reviews'),
204
        'manage_site-review_terms' => _x('Manage Review Categories', 'admin-text', 'site-reviews'),
205
    ];
206
    array_walk($labels, function ($label, $capability) {
207
        members_register_cap($capability, ['label' => $label]);
208
    });
209
});
210
211
/**
212
 * Remove Oxygen Builder metabox from reviews.
213
 *
214
 * @see https://oxygenbuilder.com
215
 */
216
add_action('plugins_loaded', function () {
217
    global $ct_ignore_post_types;
218
    if (!empty($ct_ignore_post_types) && is_array($ct_ignore_post_types)) {
219
        $ct_ignore_post_types[] = Application::POST_TYPE;
220
        add_filter('pre_option_oxygen_vsb_ignore_post_type_'.Application::POST_TYPE, '__return_true');
221
    }
222
});
223
224
/**
225
 * Fix to display all reviews when sorting by rank.
226
 *
227
 * @param array $query
228
 *
229
 * @return array
230
 *
231
 * @see https://searchandfilter.com/
232
 */
233
add_filter('sf_edit_query_args', function ($query) {
234
    if (!empty($query['meta_key']) && '_glsr_ranking' === $query['meta_key']) {
235
        unset($query['meta_key']);
236
        $query['meta_query'] = [
237
            'relation' => 'OR',
238
            ['key' => '_glsr_ranking', 'compare' => 'NOT EXISTS'], // this comes first!
239
            ['key' => '_glsr_ranking', 'compare' => 'EXISTS'],
240
        ];
241
    }
242
    return $query;
243
}, 20);
244
245
/**
246
 * Prevent SG Optimizer from breaking the Site Reviews javascript file.
247
 *
248
 * @param array $excluded
249
 *
250
 * @return array
251
 *
252
 * @see https://wordpress.org/plugins/sg-cachepress/
253
 */
254
add_filter('sgo_js_minify_exclude', function ($excluded) {
255
    if (is_array($excluded) && !in_array(glsr()->id, $excluded)) {
256
        $excluded[] = glsr()->id;
257
    }
258
    return $excluded;
259
});
260
261
/**
262
 * Load the Ninja Forms (v3) CSS if the plugin style is selected.
263
 *
264
 * @see https://ninjaforms.com/
265
 */
266
function glsr_is_ninja_forms_compatible(): bool
267
{
268
    return class_exists('Ninja_Forms')
269
        && class_exists('NF_Display_Render')
270
        && method_exists('Ninja_Forms', 'get_setting')
271
        && method_exists('NF_Display_Render', 'enqueue_styles_display');
272
}
273
function glsr_load_ninja_forms_css(): void
274
{
275
    if ('ninja_forms' === glsr_get_option('general.style') && glsr_is_ninja_forms_compatible()) {
276
        NF_Display_Render::enqueue_styles_display(Ninja_Forms::$url.'assets/css/');
277
    }
278
}
279
add_action('enqueue_block_editor_assets', 'glsr_load_ninja_forms_css');
280
add_action('wp_enqueue_scripts', 'glsr_load_ninja_forms_css');
281
add_filter('site-reviews/config/styles/ninja_forms', function (array $config): array {
282
    if (glsr_is_ninja_forms_compatible()) {
283
        $formClass = 'nf-style-'.Ninja_Forms()->get_setting('opinionated_styles');
284
        $config = glsr_set($config, 'classes.form', $formClass);
285
    }
286
    return $config;
287
});
288
289
/**
290
 * Load the WPForms stylesheet when using the WPForms plugin style.
291
 *
292
 * @see https://wordpress.org/plugins/wpforms-lite/
293
 */
294
add_filter('site-reviews/build/template/reviews-form', function (string $template): string {
295
    if ('wpforms' === glsr_get_option('general.style')) {
296
        add_filter('wpforms_frontend_missing_assets_error_js_disable', '__return_true', PHP_INT_MAX);
297
        add_filter('wpforms_global_assets', '__return_true');
298
    }
299
    return $template;
300
});
301
302
/**
303
 * Remove the "Launch Thrive Architect" button from reviews.
304
 *
305
 * @return array
306
 *
307
 * @see https://thrivethemes.com/architect/
308
 */
309
add_filter('tcb_post_types', function ($blacklist) {
310
    $blacklist[] = glsr()->post_type;
311
    return $blacklist;
312
});
313
314
/**
315
 * This disables OptimizePress v2 assets an notices on Site Reviews admin pages.
316
 */
317
function glsr_remove_optimizepress()
318
{
319
    if (!defined('OP_VERSION')) {
320
        return;
321
    }
322
    if (!str_starts_with(glsr_current_screen()->post_type, glsr()->post_type)) {
323
        return;
324
    }
325
    glsr(Compatibility::class)->removeHook('admin_enqueue_scripts', 'print_scripts', '\OptimizePress_Admin_Init');
326
    remove_action('admin_notices', 'checkApiKeyValidity');
327
    remove_action('admin_notices', 'checkEligibility');
328
    remove_action('admin_notices', 'compatibilityCheck');
329
    remove_action('admin_notices', 'goToWebinarNewAPI');
330
    remove_action('admin_notices', 'pluginAndThemeAreRunning');
331
    remove_action('admin_notices', 'update_nag_screen');
332
    remove_filter('use_block_editor_for_post_type', 'op2_disable_gutenberg', 100);
333
    remove_filter('gutenberg_can_edit_post', 'op2_disable_gutenberg');
334
}
335
336
add_action('load-post.php', 'glsr_remove_optimizepress');
337
add_action('load-post-new.php', 'glsr_remove_optimizepress');
338