WPUInstaller/inc/base_functions.php
2026-04-04 23:38:54 +02:00

161 lines
5.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/*
Plugin Name: [wpuprojectname] Functions
Description: Website common functions
*/
/* ----------------------------------------------------------
Disabler
---------------------------------------------------------- */
add_action('plugins_loaded', function () {
/* Disable REST API */
add_filter('wpudisabler__disable_wp_api__logged_in', '__return_true');
/* Disable the author page */
add_filter('wpudisabler__disable_author_page', '__return_true');
/* Disable feeds */
//add_filter('wpudisabler__disable_feeds', '__return_true');
});
/* ----------------------------------------------------------
Disable block editor
---------------------------------------------------------- */
add_filter('use_block_editor_for_post_type', '__return_false', 100);
/* ----------------------------------------------------------
WPUSEO - Disable Cookie Tracking
---------------------------------------------------------- */
add_filter('wpuseo__cookie_notice_tracking_feature_flag', '__return_false', 10, 1);
/* ----------------------------------------------------------
Options - Do not use default language fallback
---------------------------------------------------------- */
add_filter('wputh_l18n_get_option__usedefaultlang', '__return_false', 10, 3);
/* ----------------------------------------------------------
Core Sitemaps : Disable authors list
---------------------------------------------------------- */
add_filter('wp_sitemaps_enabled', function ($is_enabled) {
global $wp_query;
if (is_object($wp_query) && isset($wp_query->query_vars['sitemap']) && $wp_query->query_vars['sitemap'] == 'users') {
return false;
}
return $is_enabled;
}, 10, 1);
/* ----------------------------------------------------------
Author
---------------------------------------------------------- */
/* Hide author in JSON Metas
-------------------------- */
add_filter('wpuseo_metas_json_after_settings', function ($metas_json) {
if (isset($metas_json['author'])) {
$metas_json['author'] = get_bloginfo('name');
}
return $metas_json;
}, 10, 1);
/* ----------------------------------------------------------
Uploads
---------------------------------------------------------- */
/* Prevent ugly uploads
-------------------------- */
add_filter('wp_handle_upload_prefilter', function ($file) {
$size = $file['size'] / 1024;
$blocked_types = array(
'image/png' => array(
'name' => 'PNG',
'limit' => 500,
'extra_error' => __('Shouldnt this image be converted to JPG?', 'wpuprojectid')
),
'image/svg+xml' => array(
'name' => 'SVG',
'limit' => 200
),
'image/gif' => array(
'name' => 'GIF',
'limit' => 300,
'extra_error' => __('Shouldnt this image be converted to a Video?', 'wpuprojectid')
)
);
foreach ($blocked_types as $type => $type_infos) {
if ($file['type'] == $type && $size > $type_infos['limit']) {
$file['error'] = sprintf(__('%s files should weight less than %s ko !', 'wpuprojectid'), $type_infos['name'], $type_infos['limit']);
if (isset($type_infos['extra_error'])) {
$file['error'] .= ' ' . $type_infos['extra_error'];
}
}
}
return $file;
});
/* Allow SVG upload for admins
-------------------------- */
add_filter('upload_mimes', function ($mimes) {
if (current_user_can('remove_users')) {
$mimes['svg'] = 'image/svg+xml';
}
return $mimes;
});
/* ----------------------------------------------------------
Search only in posts
---------------------------------------------------------- */
add_filter('pre_get_posts', function ($query) {
if (!function_exists('relevanssi_do_query') && $query->is_search && !is_admin()) {
$query->set('post_type', array('post'));
}
return $query;
});
/* ----------------------------------------------------------
Force HTTPS via PHP
---------------------------------------------------------- */
add_action('plugins_loaded', function () {
$site_url = site_url();
if (defined('WP_SITEURL')) {
$site_url = WP_SITEURL;
}
/* Only if HTTPS is needed */
if (substr($site_url, 0, 5) != 'https') {
return;
}
if ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'http') || (isset($_SERVER['HTTP_X_REMOTE_PROTO']) && $_SERVER['HTTP_X_REMOTE_PROTO'] == 'http')) {
header("Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
exit();
}
}, 10, 1);
/* ----------------------------------------------------------
Disable some plugins not needed in frontend
---------------------------------------------------------- */
$request_uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$is_admin = strpos($request_uri, '/wp-admin/');
if (false === $is_admin) {
add_filter('option_active_plugins', function ($plugins) {
$disabled_plugins = array(
'happyfiles-pro/happyfiles-pro.php'
);
foreach ($disabled_plugins as $plugin_path) {
$k = array_search($plugin_path, $plugins);
if (false !== $k) {
unset($plugins[$k]);
}
}
return $plugins;
});
}