seb86/Auto-Load-Next-Post

View on GitHub
includes/admin/settings/class-alnp-settings-misc.php

Summary

Maintainability
A
2 hrs
Test Coverage
<?php
/**
 * Auto Load Next Post Settings - Misc
 *
 * @since    1.5.0
 * @version  1.6.0
 * @author   Sébastien Dumont
 * @category Admin
 * @package  Auto Load Next Post/Admin/Settings
 * @license  GPL-2.0+
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( ! class_exists( 'ALNP_Settings_Misc' ) ) {

    class ALNP_Settings_Misc extends ALNP_Settings_Page {

        /**
         * Constructor.
         *
         * @access  public
         * @since   1.5.0
         * @version 1.5.5
         */
        public function __construct() {
            $this->id    = 'misc';
            $this->label = esc_html__( 'Misc', 'auto-load-next-post' );

            parent::__construct();

            add_filter( 'auto_load_next_post_misc_settings', array( __CLASS__, 'lock_js_in_footer' ), 0, 1 );
            add_action( 'auto_load_next_post_settings_misc', array( __CLASS__, 'no_comment_selector_set' ), 0 );
        } // END __construct()

        /**
         * This notifies the user if the comment selector is NOT set.
         *
         * @access public
         * @static
         * @since  1.5.0
         */
        public static function no_comment_selector_set() {
            $comments_container = get_option( 'auto_load_next_post_comments_container' );
            $remove_comments    = get_option( 'auto_load_next_post_remove_comments' );

            if ( empty( $comments_container ) && ! empty( $remove_comments ) ) {
                include( dirname( AUTO_LOAD_NEXT_POST_FILE ) . '/includes/admin/views/html-notice-no-comment-selector.php' );
            }
        } // END no_comment_selector_set()

        /**
         * Checks if the theme locked the JavaScript to load in the footer 
         * and filters the settings to remove the option so it can not 
         * be unset by the user.
         *
         * @access  public
         * @static
         * @since   1.5.0
         * @version 1.5.3
         * @param   array $settings
         * @return  array $settings
         */
        public static function lock_js_in_footer( $settings ) {
            $js_locked_in_footer = get_option( 'auto_load_next_post_lock_js_in_footer' );

            if ( ! empty( $js_locked_in_footer ) && $js_locked_in_footer == 'yes' ) {
                // Setting key to look for.
                $key = 'load_js_in_footer';

                // Find the setting.
                $find_setting = array_search( $key, $settings );

                // Does the setting exist?
                if ( is_bool( $find_setting ) === true ) {
                    unset( $settings[$key] );
                }
            }

            return $settings;
        } // END lock_js_in_footer()
        
        /**
         * Get settings array
         *
         * @access  public
         * @since   1.5.0
         * @version 1.6.0
         * @return  array
         */
        public function get_settings() {
            return apply_filters(
                'alnp_misc_settings', array(

                    'title' => array(
                        'title' => $this->label,
                        'type'  => 'title',
                        'desc'  => esc_html__( 'Further optional options can be found here should you want to use or need.', 'auto-load-next-post' ),
                        'id'    => 'misc_options'
                    ),

                    'remove_comments' => array(
                        'title'    => esc_html__( 'Remove Comments', 'auto-load-next-post' ),
                        'desc'     => esc_html__( 'Enable to remove comments when each post loads including the initial post.', 'auto-load-next-post' ),
                        'id'       => 'auto_load_next_post_remove_comments',
                        'default'  => 'yes',
                        'type'     => 'checkbox',
                        'autoload' => false
                    ),

                    'google_analytics' => array(
                        'title'    => esc_html__( 'Update Google Analytics', 'auto-load-next-post' ),
                        'desc'     => esc_html__( 'Enable to track each post the visitor is reading. This will count as a pageview. You must already have Google Analytics setup.', 'auto-load-next-post' ),
                        'id'       => 'auto_load_next_post_google_analytics',
                        'default'  => 'no',
                        'type'     => 'checkbox',
                        'autoload' => false
                    ),

                    'load_js_in_footer' => array(
                        'title'    => esc_html__( 'JavaScript in Footer?', 'auto-load-next-post' ),
                        'desc'     => sprintf( esc_html__( 'Enable to load %s in the footer instead of the header. Can be useful to optimize your site or if the current theme requires it.', 'auto-load-next-post' ), esc_html__( 'Auto Load Next Post', 'auto-load-next-post' ) ),
                        'id'       => 'auto_load_next_post_load_js_in_footer',
                        'default'  => 'no',
                        'type'     => 'checkbox',
                        'autoload' => false
                    ),

                    'disable_on_mobile' => array(
                        'title'    => esc_html__( 'Disable for Mobile?', 'auto-load-next-post' ),
                        'desc'     => sprintf( esc_html__( 'Enable to disable %s from running on mobile devices.', 'auto-load-next-post' ), esc_html__( 'Auto Load Next Post', 'auto-load-next-post' ) ),
                        'id'       => 'auto_load_next_post_disable_on_mobile',
                        'default'  => 'no',
                        'type'     => 'checkbox',
                        'autoload' => false
                    ),

                    'reset_data' => array(
                        'title' => esc_html__( 'Reset all data?', 'auto-load-next-post' ),
                        'desc'  => esc_html__( 'Press the reset button to clear all settings for this plugin and re-initialize.', 'auto-load-next-post' ),
                        'id'    => 'auto_load_next_post_reset_data',
                        'class' => 'reset-settings',
                        'value' => esc_html__( 'Reset', 'auto-load-next-post' ),
                        'url'   => add_query_arg( array( 'page' => 'auto-load-next-post', 'view' => esc_attr( $this->id ), 'reset-alnp' => 'yes' ), admin_url( 'options-general.php' ) ),
                        'type'  => 'button'
                    ),

                    'uninstall' => array(
                        'title'    => esc_html__( 'Remove all data on uninstall?', 'auto-load-next-post' ),
                        'desc'     => esc_html__( 'If enabled, all settings for this plugin will all be deleted when uninstalling via Plugins > Delete.', 'auto-load-next-post' ),
                        'id'       => 'auto_load_next_post_uninstall_data',
                        'default'  => 'no',
                        'type'     => 'checkbox',
                        'autoload' => false
                    ),

                    'section_end' => array(
                        'type' => 'sectionend',
                        'id'   => 'misc_options'
                    ),
                )
            ); // End misc settings
        } // END get_settings()

        /**
         * Output the settings.
         *
         * @access public
         * @since  1.5.0
         */
        public function output() {
            $settings = $this->get_settings();

            ALNP_Admin_Settings::output_fields( $settings );
        } // END output()

        /**
         * Save settings.
         *
         * @access public
         * @since  1.5.0
         */
        public function save() {
            $settings = $this->get_settings();

            ALNP_Admin_Settings::save_fields( $settings );
        } // END save()

    } // END class

} // END if class exists

return new ALNP_Settings_Misc();