Files
local-content-pro/includes/classes/backend.php
2025-01-02 03:07:43 -08:00

353 lines
13 KiB
PHP

<?php
// Create backend pages
function lcp_backend_pages() {
// 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_action( 'admin_menu', 'lcp_backend_pages' );
// Function to render the theme settings page
// Function to render the theme settings page
// 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_settings_field(
'enable_highlight_to_share', // Field ID
'Enable Highlight-to-Share', // Field title
'lcp_enable_highlight_to_share', // 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;
}
// 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 ) . ' />';
}
function lcp_enable_highlight_to_share() {
// Retrieve current value for 'enable_highlight_to_share'
$options = get_option( 'lcp_theme_settings', array() );
$checked = isset( $options['enable_highlight_to_share'] ) ? $options['enable_highlight_to_share'] : false;
// Output the checkbox
echo '<input type="checkbox" name="lcp_theme_settings[enable_highlight_to_share]" value="1" ' . checked( $checked, 1, false ) . ' />';
}
// The callback function to display the custom code inserterpage content
// 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');
});
// Register the settings page with tabs
function render_lcp_theme_settings_page() {
?>
<div class="wrap">
<h1>Theme Settings</h1>
<!-- Tab Structure -->
<div class="tab-container">
<!-- Tabs -->
<ul class="tabs">
<li class="tab-link active" data-tab="custom-meta">Custom Meta</li>
<li class="tab-link" data-tab="icons">Icons</li>
<li class="tab-link" data-tab="custom-code">Custom Code</li>
<li class="tab-link" data-tab="misc">Miscellaneous</li>
</ul>
<!-- Tab Panes -->
<!-- Icons Tab -->
<div id="icons" class="tab-pane">
<!-- Icons Tab Content -->
<h2>Custom SVG Icons</h2>
<form method="post" action="options.php">
<?php
// Output necessary settings fields for the 'Icons' tab
// This outputs all the fields defined by add_settings_field
lcp_display_icon_sets();
?>
</form>
</div>
<!-- Custom Meta Tab Pane -->
<div id="custom-meta" class="tab-pane active">
<?php
settings_fields('lcp_theme_settings_group');
do_settings_sections('lcp_theme_settings_page');
?>
</div>
<div id="custom-code" class="tab-pane">
<!-- Custom Code Tab Content -->
<h2>Custom Code Settings</h2>
<form method="post" action="options.php">
<?php
// Output necessary settings fields for the 'Custom Code' tab
settings_fields('custom_code_group');
?>
<table class="form-table">
<tr valign="top">
<th scope="row">CSS </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>
<div id="misc" class="tab-pane">
<!-- Misc Settings -->
<h2>Miscellaneous Settings</h2>
<!-- Button to trigger the AJAX import demo posts -->
<button id="import-demo-posts" class="button button-primary">Import Demo Posts</button>
</div>
</div>
</div>
<?php
}
/* HIGHLIGHT-TO-SHARE */
// Render the highlight-to-share popup
function highlight_to_share_popup() {
echo "
<div class=\"popup\" id=\"popup\">
<p>Hey there! Share this:</p>
<a href=\"#\" id=\"facebook-share\">Facebook</a> |
<a href=\"#\" id=\"twitter-share\">Twitter/X</a>
</div>
";
}
/* HOOKS */
//Move to custom hooks class or file in future
function lcp_wp_head_hooks(){
// Define lcp_theme_settings array from wp_options
$options = get_option('lcp_theme_settings', array());
// Echo highlight-to-share markup and enqueue javascript
// Highlight-to-share css is already in style.css
if ( isset($options['enable_highlight_to_share']) && $options['enable_highlight_to_share'] ) {
highlight_to_share_popup();
wp_enqueue_script( 'lcp-highlight-to-share', get_template_directory_uri() . '/assets/js/highlight-to-share.js', array(), null, true );
}
}
add_action('wp_head', 'lcp_wp_head_hooks');
/* ICONS */