File size: 5,041 Bytes
ccccf90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**

 * Plugin Name: DevOps & Infrastructure Loyalty Program and Rewards WordPress Plugin

 * Description: Points, tiers, referrals, and exclusive access for repeat DevOps & Infrastructure customers. Drive repeat purchases without third-party loyalty apps.

 * Version: 1.0.0

 * Author: Digital Forge

 * Text Domain: ffe80540

 * License: GPL-2.0+

 */

if ( ! defined( 'ABSPATH' ) ) exit;

define( 'FFE80540_VERSION', '1.0.0' );
define( 'FFE80540_DIR', plugin_dir_path( __FILE__ ) );

class Ffe80540 {
    private static $instance = null;

    public static function instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        add_action( 'init', [ $this, 'init' ] );
        add_action( 'admin_menu', [ $this, 'admin_menu' ] );
        add_action( 'admin_init', [ $this, 'settings_init' ] );
        add_action( 'rest_api_init', [ $this, 'register_routes' ] );
        add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_assets' ] );
        load_plugin_textdomain( 'ffe80540', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
    }

    public function init() {}

    public function admin_menu() {
        add_options_page(
            esc_html__( 'DevOps & Infrastructure Loyalty Program and Rewards WordPress Plugin Settings', 'ffe80540' ),
            esc_html__( 'DevOps & Infrastructure Loyalty Program and Rewards WordPress Plugin', 'ffe80540' ),
            'manage_options',
            'ffe80540-settings',
            [ $this, 'settings_page' ]
        );
    }

    public function settings_init() {
        register_setting( 'ffe80540_options', 'ffe80540_options', [ $this, 'sanitize_options' ] );
        add_settings_section( 'ffe80540_main', '', '__return_false', 'ffe80540-settings' );
        add_settings_field( 'enabled', esc_html__( 'Enable', 'ffe80540' ), [ $this, 'field_enabled' ], 'ffe80540-settings', 'ffe80540_main' );
    }

    public function sanitize_options( $input ) {
        $clean = [];
        $clean['enabled'] = ! empty( $input['enabled'] ) ? 1 : 0;
        return $clean;
    }

    public function field_enabled() {
        $opts = get_option( 'ffe80540_options', [] );
        $val  = ! empty( $opts['enabled'] ) ? 1 : 0;
        echo '<input type="checkbox" name="ffe80540_options[enabled]" value="1"' . checked( $val, 1, false ) . ' />';
    }

    public function settings_page() {
        if ( ! current_user_can( 'manage_options' ) ) return;
        ?>
        <div class="wrap">

            <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>

            <form method="post" action="options.php">

                <?php settings_fields( 'ffe80540_options' ); ?>

                <?php do_settings_sections( 'ffe80540-settings' ); ?>

                <?php submit_button(); ?>

            </form>

        </div>

        <?php

    }



    public function register_routes() {
        register_rest_route( 'ffe80540/v1', '/data', [
            [ 'methods' => 'GET',  'callback' => [ $this, 'rest_get' ],  'permission_callback' => '__return_true' ],
            [ 'methods' => 'POST', 'callback' => [ $this, 'rest_post' ], 'permission_callback' => function() { return current_user_can( 'manage_options' ); } ],
        ] );
    }

    public function rest_get( $request ) {
        return rest_ensure_response( [ 'status' => 'ok', 'plugin' => 'ffe80540' ] );
    }

    public function rest_post( $request ) {
        $data = $request->get_json_params();
        return rest_ensure_response( [ 'received' => $data ] );
    }

    public function enqueue_admin_assets( $hook ) {
        if ( 'settings_page_ffe80540-settings' !== $hook ) return;
        wp_enqueue_style( 'ffe80540-admin', plugin_dir_url( __FILE__ ) . 'assets/css/admin.css', [], '1.0.0' );
        wp_enqueue_script( 'ffe80540-admin', plugin_dir_url( __FILE__ ) . 'assets/js/admin.js', [ 'jquery' ], '1.0.0', true );
        wp_localize_script( 'ffe80540-admin', 'ffe80540_vars', [ 'nonce' => wp_create_nonce( 'ffe80540-nonce' ), 'ajax_url' => admin_url( 'admin-ajax.php' ) ] );
    }

    public static function activate() {
        global $wpdb;
        $table = $wpdb->prefix . 'ffe80540';
        $charset = $wpdb->get_charset_collate();
        $sql = "CREATE TABLE IF NOT EXISTS $table (

            id bigint(20) NOT NULL AUTO_INCREMENT,

            data longtext NOT NULL,

            created_at datetime DEFAULT CURRENT_TIMESTAMP,

            PRIMARY KEY (id)

        ) $charset;";
        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        dbDelta( $sql );
        add_option( 'ffe80540_db_version', '1.0.0' );
    }

    public static function deactivate() {}
}

register_activation_hook( __FILE__, [ 'Ffe80540', 'activate' ] );
register_deactivation_hook( __FILE__, [ 'Ffe80540', 'deactivate' ] );

Ffe80540::instance();