PHP code example of log1x / sage-password-protected

1. Go to this page and download the library: Download log1x/sage-password-protected library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

log1x / sage-password-protected example snippets


/**
 * Default configuration for Sage Password Protected
 * 
 * @return array
 */
add_filter('password_protected', function () {
    return [
        'active'             => false,
        'password'           => false,
        'secret'             => $this->secret,
        'allowFeeds'         => false,
        'allowAdmins'        => false,
        'allowUsers'         => false,
        'allowIpAddresses'   => false,
        'allowedIpAddresses' => [],
        'title'              => $this->name()
    ];
});

/**
 * Configuration for Sage Password Protected.
 * 
 * @return array
 */
add_filter('password_protected', function () {
    return [
        'active'             => Acf::option('password_protected')->get(),
        'password'           => Acf::option('password')->get(),
        'allowFeeds'         => Acf::option('password_show_feeds')->get(),
        'allowAdmins'        => Acf::option('password_allow_administrators')->get(),
        'allowUsers'         => Acf::option('password_allow_users')->get(),
        'allowIpAddresses'   => Acf::option('password_allow_by_ip_address')->get(),
        'allowedIpAddresses' => Acf::option('password_allowed_ip_addresses')->get(),
    ];
});



namespace App;

use StoutLogic\AcfBuilder\FieldsBuilder;

$config = (object) [
    'ui'      => 1,
    'wrapper' => ['width' => 30],
    'ip'      => $_SERVER['X-Forwarded-For'] ?? $_SERVER['REMOTE_ADDR'],
];

$password = new FieldsBuilder('password_protected');

$password
    ->addTab('password_protected', ['placement' => 'left']);

$password
    ->addTrueFalse('password_protected', ['ui' => $config->ui])
        ->setInstructions('Enable site-wide password protection?')

    ->addField('password', 'encrypted_password', ['wrapper' => $config->wrapper])
        ->setInstructions('Enter the login password.')
        ->conditional('password_protected', '==', '1')

    ->addTrueFalse('password_show_feeds', ['label' => 'Show Feeds?', 'ui' => $config->ui])
        ->setInstructions('Enable RSS Feeds without a password?')
        ->conditional('password_protected', '==', '1')

    ->addTrueFalse('password_allow_ip_address', ['label' => 'Allow by IP Address', 'ui' => $config->ui])
        ->setInstructions('Enable whitelisting users by their IP Address.')
        ->conditional('password_protected', '==', '1')

    ->addRepeater('password_allowed_ip_addresses', ['label' => 'Allowed IP Addresses', 'button_label' => 'Add IP Address'])
        ->conditional('password_protected', '==', '1')
            ->and('password_allow_ip_address', '==', '1')
        ->setInstructions('Current IP Address: ' . $config->ip)

        ->addText('ip_address', ['label' => 'IP Address', 'placeholder' => $config->ip])
            ->setInstructions('The IP Address of the user to allow through password protection.')

        ->addText('ip_address_comment', ['label' => 'Comment', 'placeholder' => 'John Doe\'s Home'])
            ->setInstructions('A comment containing an identifier for this IP address. This is strictly for organization purposes.')
    ->endRepeater()

    ->addTrueFalse('password_allow_users', ['ui' => $config->ui])
        ->setInstructions('Allow bypassing password protection while logged in as a user.')
        ->conditional('password_protected', '==', '1')

    ->addTrueFalse('password_allow_administrators', ['ui' => $config->ui])
        ->conditional('password_protected', '==', '1')
            ->and('password_allow_users', '==', '0')
        ->setInstructions('Allow bypassing password protection while logged in as an administrator.');

return $password;