error

Home » Snippets » error
0

Created with:

Visibility: 

public

Creator: Kabucho

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/*
Plugin Name: DlerCloud API Wrapper 2.0.0
Description: WordPress wrapper for DlerCloud API
*/

require_once ABSPATH . 'wp-includes/class-wp-http.php';

class DlerCloudAPI {
    private $access_token;
    private $user_id;
    private $host;
    private $base_url;
    private $http;

    public function __construct($access_token = null, $host = 'dler.cloud') {
        $this->access_token = $access_token;
        $this->user_id = null;
        $this->host = $host;
        $this->base_url = 'https://' . $this->host . '/api/v1/';
        $this->http = new WP_Http();
    }

    private function _request($path, $data = null, $args = array()) {
        $data = $data ?? array();
        if ($this->access_token && !in_array($path, array('login', 'logout'))) {
            $data['access_token'] = $this->access_token;
        }
        $url = $this->base_url . $path;
        $response = $this->http->post($url, array_merge($args, array('body' => $data)));
        if (is_wp_error($response)) {
            throw new Exception($response->get_error_message());
        }
        $resp_json = json_decode($response['body'], true);
        error_log('API Response: ' . print_r($resp_json, true));

        if (!$resp_json) {
            throw new Exception('Empty API response.');
        } elseif (!isset($resp_json['ret'])) {
            throw new Exception('Missing "ret" field in API response.');
        } elseif (!isset($resp_json['msg'])) {
            throw new Exception('Missing "msg" field in API response.');
        } elseif (!isset($resp_json['data'])) {
            throw new Exception('Missing "data" field in API response.');
        }

        $ret_code = intval($resp_json['ret']);
        if ($ret_code < 200 || $ret_code >= 300) {
            throw new Exception('[' . $ret_code . '] ' . $resp_json['msg']);
        }

        return $resp_json['data'];
    }

    public function login($email, $password) {
        $data = $this->_request('login', array('email' => $email, 'passwd' => $password));
        if (isset($data['token'])) {
            $this->access_token = $data['token'];
        }
        if (isset($data['user_id'])) {
            $this->user_id = $data['user_id'];
        }

        return $this->access_token;
    }

    public function logout($email = null, $password = null) {
        if ($email && $password) {
            $this->_request('logout', array('email' => $email, 'passwd' => $password));
            $this->access_token = null;
        } elseif ($this->access_token) {
            $this->_request('logout', array('access_token' => $this->access_token));
            $this->access_token = null;
        } else {
            throw new Exception('Email and password, or access token (.access_token) is needed at least');
        }
    }

    public function get_nodes() {
        return new Nodes($this);
    }
}

class Nodes {
    private $api;

    public function __construct(DlerCloudAPI $api) {
        $this->api = $api;
    }

    public function ss() {
        return new Shadowsocks($this->api);
    }
}

class Shadowsocks {
    private $api;

    public function __construct(DlerCloudAPI $api) {
        $this->api = $api;
    }

    public function __call($name, $args) {
        if ($name === 'connection_password') {
            if (!isset($args[0])) {
                throw new Exception('Missing node ID argument.');
            }
            $node_id = intval($args[0]);
            $data = $this->api->_request('node/' . $node_id . '/ss', null, array('method' => 'GET'));
            if (isset($data['ss_pwd'])) {
                return $data['ss_pwd'];
            } else {
                throw new Exception('Missing "ss_pwd" field in API response.');
            }
        } else {
            throw new BadMethodCallException('Call to undefined method ' . __CLASS__ . '::' . $name . '().');
        }
    }
}

function dlercloud_api_settings_page() {
    $email = 'user@example.com';
    $password = 'secret';
    $api = new DlerCloudAPI($access_token = null, $host = 'dler.cloud');
    $access_token = $api->login($email, $password);
    $data = $api->_request('user/' . $api->user_id . '/trojan', null, array('method' => 'GET'));
    $nodes = $api->get_nodes()->ss();
?>
<h1>DlerCloud API Settings</h1>
<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Server</th>
            <th>Port</th>
            <th>Password</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data as $node) { ?>
        <tr>
            <td><?php echo $node['id']; ?></td>
            <td><?php echo $node['address']; ?></td>
            <td><?php echo $node['port']; ?></td>
            <td><?php echo $nodes->connection_password($node['id']); ?></td>
        </tr>
        <?php } ?>
    </tbody>
</table>
<?php
    $api->logout();
}

add_action('admin_menu', function() {
    add_options_page(
        'DlerCloud API',
        'DlerCloud API',
        'manage_options',
        'dlercloud-api',
        'dlercloud_api_settings_page'
    );
});

function wpturbo_my_custom_function($param_1, $param_2 = 'default_value', $param_3 = array()) {
    // My custom function code goes here.
    // Remember to use the best practices for PHP and WordPress.
}

// Call the function with arguments.
wpturbo_my_custom_function('param_value_1', 'param_value_2', array('key' => 'value'));

// Custom hook example
$my_dynamic_hook_name = 'wpturbo_dynamic_hook_' . rand();
add_filter($my_dynamic_hook_name, function($arg1, $arg2) {
    // Custom hook code goes here.
});
// Triggering the dynamic hook with parameters.
apply_filters($my_dynamic_hook_name, 'arg1_value', 'arg2_value');

// Dynamic hook with nested action
class My_Plugin {
    private $hook_name;
    private $param1;

    public function __construct() {
        $this->param1 = 'Value of param1';

        // Generate unique hook name
        $this->hook_name = 'my_plugin_dynamic_hook_' . uniqid();

        // Register the action
        add_action('init', array($this, 'dynamic_hook'));
    }

    public function dynamic_hook() {
        // Execute necessary code before dynamic hook execution
        $param2 = 'Value of param2';

        // Generate nested dynamic hook name
        $nested_hook_name = $this->hook_name . '_nested';

        // Execute the dynamic action
        do_action($nested_hook_name, $this->param1, $param2);

        // Execute necessary code after dynamic hook execution
    }
}

// Using the dynamic hook with a nested action
$my_plugin = new My_Plugin();
add_action($my_plugin->hook_name . '_nested', function($arg1, $arg2) {
    // Insert custom action code here
}, 10, 2);
				

This code shows a dynamic hook with a nested action. The My_Plugin class creates a dynamic hook with a unique name, my_plugin_dynamic_hook_${unique-id}, which is registered with the init action hook.

When the hook is executed, the dynamic_hook() method generates a nested dynamic hook called my_plugin_dynamic_hook_${unique-id}_nested and executes it with two parameters. Finally, the nested hook executes a custom action with printf statements.

This pattern can be used in a variety of scenarios where you want to create hooks or actions whose names depend on varying inputs at runtime. It could be useful, for example, when wanting to modify the output of a plugin based on a user's input dynamically.

Register an account to save your snippets or go Pro to get more features.