Files
local-content-pro/includes/classes/backend.php

330 lines
12 KiB
PHP

<?php
// Create backend pages
function lcp_backend_pages() {
// Separator
// Theme Settings page
add_menu_page(
'Local Content Pro', // Page title
'Local Content Pro', // Menu title
'manage_options', // Capability required to access this menu
'local-content-pro', // Menu slug
'render_lcp_theme_settings_page', // Function to render the page
'dashicons-admin-generic', // Icon for the menu
25 // Position
);
// Register the Custom Code settings page
add_menu_page(
'Custom Code Inserter', // Page title
'Code Inserter', // Menu title
'manage_options', // Capability required - admin only
'lcp-code-inserter', // Menu slug
'custom-code-inserter', // Callback function
'', // Icon
25
);
}
add_action( 'admin_menu', 'lcp_backend_pages' );
// Function to render the theme settings page
// Function to render the theme settings page
function lcp_register_custom_svg_settings() {
// Register the settings group and option for custom SVGs
register_setting(
'lcp_theme_settings_group', // Settings group
'lcp_custom_svgs', // Option name
'lcp_custom_svgs_sanitize' // Sanitization function
);
// Add a section to organize the settings
add_settings_section(
'lcp_custom_svgs_section', // Section ID
'Custom SVGs', // Section title
'', // Section callback (optional)
'lcp_theme_settings_page' // Page where to show this section
);
// Add the custom SVGs field
add_settings_field(
'lcp_custom_svgs', // Field ID
'Add Custom SVGs', // Field title
'lcp_custom_svgs_field', // Field callback function
'lcp_theme_settings_page', // Page where this field appears
'lcp_custom_svgs_section' // Section where this field belongs
);
}
add_action('admin_init', 'lcp_register_custom_svg_settings');
// Callback to display the custom SVGs field
function lcp_custom_svgs_field() {
// Retrieve current custom SVGs
$custom_svgs = get_option( 'lcp_custom_svgs', array() );
?>
<div id="svg-repeater-container">
<?php
// Loop through each SVG and display its name and path
if (!empty($custom_svgs)) {
foreach ($custom_svgs as $svg) {
?>
<div class="repeater-row">
<input type="text" name="lcp_custom_svgs[][name]" value="<?php echo esc_attr($svg['name']); ?>" placeholder="SVG Name" />
<textarea name="lcp_custom_svgs[][path]" placeholder="SVG Path"><?php echo esc_textarea($svg['path']); ?></textarea>
<button type="button" class="remove-row">Remove</button>
</div>
<?php
}
}
?>
</div>
<button type="button" id="add-svg-row">Add SVG</button>
<?php
}
// Function to register settings for the theme
function lcp_register_settings() {
// Register the settings group and the option for lcp_theme_settings
register_setting(
'lcp_theme_settings_group', // Settings group
'lcp_theme_settings', // Option name
'lcp_theme_settings_sanitize' // Sanitization callback (optional)
);
// Add a section to organize the settings
add_settings_section(
'lcp_theme_settings_section', // Section ID
'Custom Code Settings', // Section title
'', // Section callback (optional)
'lcp_theme_settings_page' // Page where to show this section
);
// Add the field for the 'Enable Key Points Meta' checkbox
add_settings_field(
'enable_key_points_meta', // Field ID
'Enable Key Points Meta', // Field title
'lcp_enable_key_points_meta_field', // Field callback function
'lcp_theme_settings_page', // Page where this field appears
'lcp_theme_settings_section' // Section where this field belongs
);
// Add fields for custom breakpoints
add_settings_field(
'mobile_breakpoint', // Field ID
'Mobile Breakpoint (px)', // Field title
'lcp_mobile_breakpoint_field', // Field callback function
'lcp_theme_settings_page', // Page where this field appears
'lcp_theme_settings_section' // Section where this field belongs
);
add_settings_field(
'tablet_breakpoint', // Field ID
'Tablet Breakpoint (px)', // Field title
'lcp_tablet_breakpoint_field', // Field callback function
'lcp_theme_settings_page', // Page where this field appears
'lcp_theme_settings_section' // Section where this field belongs
);
}
add_action('admin_init', 'lcp_register_settings');
// Callback function for the 'Mobile Breakpoint' field
function lcp_mobile_breakpoint_field() {
$options = get_option('lcp_theme_settings');
$mobile_breakpoint = isset($options['mobile_breakpoint']) ? $options['mobile_breakpoint'] : 768;
echo '<input type="number" id="mobile_breakpoint" name="lcp_theme_settings[mobile_breakpoint]" value="' . esc_attr($mobile_breakpoint) . '" class="small-text" />';
}
// Callback function for the 'Tablet Breakpoint' field
function lcp_tablet_breakpoint_field() {
$options = get_option('lcp_theme_settings');
$tablet_breakpoint = isset($options['tablet_breakpoint']) ? $options['tablet_breakpoint'] : 1024;
echo '<input type="number" id="tablet_breakpoint" name="lcp_theme_settings[tablet_breakpoint]" value="' . esc_attr($tablet_breakpoint) . '" class="small-text" />';
}
function lcp_theme_settings_sanitize($input) {
// Sanitize mobile breakpoint
$input['mobile_breakpoint'] = isset($input['mobile_breakpoint']) ? absint($input['mobile_breakpoint']) : 768;
// Sanitize tablet breakpoint
$input['tablet_breakpoint'] = isset($input['tablet_breakpoint']) ? absint($input['tablet_breakpoint']) : 1024;
return $input;
}
// Inject breakpoints to frontned
function lcp_inject_breakpoints_to_frontend() {
// Get the breakpoints from the settings
$options = get_option('lcp_theme_settings');
$mobile_breakpoint = isset($options['mobile_breakpoint']) ? $options['mobile_breakpoint'] : 768;
$tablet_breakpoint = isset($options['tablet_breakpoint']) ? $options['tablet_breakpoint'] : 1024;
// Inject the CSS variables into the head of the document
echo "<style>
:root {
--mobile-breakpoint: {$mobile_breakpoint}px;
--tablet-breakpoint: {$tablet_breakpoint}px;
}
</style>";
}
add_action('wp_head', 'lcp_inject_breakpoints_to_frontend');
// Function to output the checkbox for 'Enable Key Points Meta'
function lcp_enable_key_points_meta_field() {
// Retrieve current value for 'enable_key_points_meta'
$options = get_option( 'lcp_theme_settings', array() );
$checked = isset( $options['enable_key_points_meta'] ) ? $options['enable_key_points_meta'] : false;
// Output the checkbox
echo '<input type="checkbox" name="lcp_theme_settings[enable_key_points_meta]" value="1" ' . checked( $checked, 1, false ) . ' />';
}
// The callback function to display the custom code inserterpage content
function lcp_custom_code_inserter_page() {
// Get the stored options from wp_options
$custom_code = get_option( 'custom_code_options', array() );
?>
<div class="wrap">
<h1>Custom Code Settings</h1>
<form method="post" action="options.php">
<?php settings_fields( 'custom_code_group' );
if (defined('LCP_DISABLE_CODE_INSERTER') && LCP_DISABLE_CODE_INSERTER === true) {
echo "<h2 style=\"margin:10px 5px\"> LCP Custom Code Inserter is currently disabled. </h2>
<h3 style=\"margin:5px 5px\"> You can add and modify code snippets, but they will not be in effect. </h3> ";
}
?>
<table class="form-table">
<tr valign="top">
<th scope="row">CSS in Header</th>
<td><textarea name="custom_code_options[css_header]" rows="10" cols="50"><?php echo esc_textarea( isset( $custom_code['css_header'] ) ? $custom_code['css_header'] : '' ); ?></textarea></td>
</tr>
<tr valign="top">
<th scope="row">JavaScript in Header</th>
<td><textarea name="custom_code_options[js_header]" rows="10" cols="50"><?php echo esc_textarea( isset( $custom_code['js_header'] ) ? $custom_code['js_header'] : '' ); ?></textarea></td>
</tr>
<tr valign="top">
<th scope="row">JavaScript in Footer</th>
<td><textarea name="custom_code_options[js_footer]" rows="10" cols="50"><?php echo esc_textarea( isset( $custom_code['js_footer'] ) ? $custom_code['js_footer'] : '' ); ?></textarea></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
// Register a custom code setting in wp_options to store the custom code snippets
function lcp_custom_code_settings_init() {
register_setting( 'custom_code_group', 'custom_code_options', 'sanitize_custom_code' );
}
add_action( 'admin_init', 'lcp_custom_code_settings_init' );
// Sanitize the custom code input before saving it
function sanitize_custom_code( $input ) {
// Sanitize each field
if ( isset( $input['js_header'] ) ) {
$input['js_header'] = sanitize_textarea_field( $input['js_header'] );
}
if ( isset( $input['js_footer'] ) ) {
$input['js_footer'] = sanitize_textarea_field( $input['js_footer'] );
}
if ( isset( $input['css_header'] ) ) {
$input['css_header'] = sanitize_textarea_field( $input['css_header'] );
}
if ( isset( $input['css_footer'] ) ) {
$input['css_footer'] = sanitize_textarea_field( $input['css_footer'] );
}
return $input;
}
function lcp_custom_code_output($hook) {
// Disable the custom code inserter if wp_env is set
if (defined('LCP_DISABLE_CODE_INSERTER') && LCP_DISABLE_CODE_INSERTER === true) {
// Retrieve the custom code options
$custom_code = get_option( 'custom_code_options', array() );
// Prevent execution in the admin area
if ( is_admin() ) return;
// Check if the hook is for the header
if ($hook == "header") {
// Check and output JS for header
if ( isset( $custom_code['js_header'] ) && ! empty( $custom_code['js_header'] ) ) {
echo '<script type="text/javascript">' . $custom_code['js_header'] . '</script>';
}
// Check and output CSS for header
if ( isset( $custom_code['css_header'] ) && ! empty( $custom_code['css_header'] ) ) {
echo '<style type="text/css">' . $custom_code['css_header'] . '</style>';
}
}
// Check if the hook is for the footer
if ($hook == "footer") {
// Check and output JS for footer
if ( isset( $custom_code['js_footer'] ) && ! empty( $custom_code['js_footer'] ) ) {
echo '<script type="text/javascript">' . $custom_code['js_footer'] . '</script>';
}
}
}
}
// Attach to wp_head and wp_footer, passing the location as a parameter
add_action( 'wp_head', function() {
lcp_custom_code_output('header');
});
add_action( 'wp_footer', function() {
lcp_custom_code_output('footer');
});
function render_lcp_theme_settings_page() {
?>
<div class="wrap">
<h1>Theme Settings</h1>
<form method="post" action="options.php">
<?php
// Output necessary settings fields for WordPress to save
settings_fields('lcp_theme_settings_group');
do_settings_sections('lcp_theme_settings_page'); // This outputs all the fields defined by add_settings_field
?>
<?php submit_button(); ?>
</form>
</div>
<?php
}