PHP code example of digitalerase / wp-remote-users-sync
1. Go to this page and download the library: Download digitalerase/wp-remote-users-sync 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/ */
digitalerase / wp-remote-users-sync example snippets
/*
Plugin Name: Example of User Action Extension for WP Remote Users Sync
Version: 1.0
Text Domain: my-domain
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! defined( 'WPRUS_EXTEND_PLUGIN_PATH' ) ) {
define( 'WPRUS_EXTEND_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
}
if ( ! defined( 'WPRUS_EXTEND_PLUGIN_URL' ) ) {
define( 'WPRUS_EXTEND_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
add_action( 'wprus_loaded', 'wprus_example', 10, 0 );
function wprus_example() {
urn $endpoint_handler;
}
add_filter( 'wprus_actions', 'wprus_actions_example', 10, 1 );
function wprus_actions_example( $actions ) {
$actions['example'] = __( 'Example', 'example-domain' );
return $actions;
}
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class Wprus_Api_Example extends Wprus_Api_Abstract {
/*******************************************************************
* Public methods
*******************************************************************/
public function init_notification_hooks() {
add_action( 'wp', array( $this, 'notify_remote' ), 10, 0 );
}
public function has_async_actions() {
return true;
}
public function handle_notification() {
$result = false;
$data = $this->get_data();
if ( ! $this->validate( $data ) ) {
Wprus_Logger::log(
__( 'Example action failed - received invalid data.', 'example-domain' ),
'alert',
'db_log'
);
return $result;
}
$data = $this->sanitize( $data );
$site = $this->get_active_site_for_action( $this->endpoint, $data['base_url'] );
if ( $site ) {
$user = get_user_by( 'login', $data['username'] );
if ( $user ) {
$result = true;
wprus_log(
array(
'message' => sprintf(
// translators: %1$s is the username, %2$s is the caller
__( 'Example action - successfully received data for user "%1$s" from %2$s.', 'example-domain' ),
$data['username'],
$site['url']
),
'data' => $data,
),
'success',
'db_log'
);
} else {
wprus_log(
sprintf(
// translators: %1$s is the username, %2$s is the caller
__( 'Example action aborted - user "%1$s" from %2$s does not exist locally.', 'example-domain' ),
$data['username'],
$site['url']
),
'warning',
'db_log'
);
}
} else {
wprus_log(
sprintf(
// translators: %s is the url of the caller
__( 'Example action failed - incoming example action not enabled for %s', 'example-domain' ),
$data['base_url']
),
'alert',
'db_log'
);
}
return $result;
}
public function notify_remote() {
if ( ! is_user_logged_in() ) {
return;
}
$user = get_user_by( 'ID', get_current_user_id() );
$sites = $this->settings->get_sites( $this->endpoint, 'outgoing' );
if ( $user && ! empty( $sites ) ) {
$data = array( 'username' => $user->user_login );
foreach ( $sites as $index => $site ) {
$data['example'] = 'example data - asynchronous action';
$this->add_async_action( $site['url'], $data );
$data['example'] = 'example data - synchronous action';
$this->fire_action( $site['url'], $data );
}
}
}
/*******************************************************************
* Protected methods
*******************************************************************/
protected function validate( $data ) {
$valid =
parent::validate( $data ) &&
username_exists( $data['username'] ) &&
is_string( $data['example'] );
return $valid;
}
protected function sanitize( $data ) {
$data['example'] = 'sanitized ' . $data['example'];
return $data;
}
}
array (
$uniq_id => array( // $uniq_id is the unique ID of the action
'username' => 'username', // The user name of the user to act on
'base_url' => 'https://local-website.com', // The URL of the local site sending the request
'url' => 'https://remote-site.com/', // The URL of the remote site supposed to receive the request
[...] // Other data sent to perform the action
),
[...] // More data for other requests to other remote sites if any
);
array (
$uniq_id => array( // $uniq_id is the unique ID of the action
'username' => 'username', // The user name of the user to act on
'base_url' => 'https://local-website.com', // The URL of the local site sending the request
'url' => 'https://remote-site.com/', // The URL of the remote site supposed to receive the request
[...] // Other data sent to perform the action
),
[...] // More data for other requests to other remote sites if any
);
array(
'username' => 'username', // The user name of the user to act on
'base_url' => 'https://local-website.com', // The URL of the local site sending the request
[...] // Other data sent to perform the action
);
array(
'username' => 'username', // The user name of the user to act on
'base_url' => 'https://local-website.com', // The URL of the local site sending the request
[...] // Other data sent to perform the action
);
array(
'username' => 'username', // The user name of the user to act on
'base_url' => 'https://remote-website.com', // The URL of the remote site sending the request
[...] // Other data sent to perform the action
);
array(
'username' => 'username', // The user name of the user to act on
'base_url' => 'https://remote-website.com', // The URL of the remote site sending the request
[...] // Other data sent to perform the action
);
array(
'id' => array( // Each item is index by its metabox ID
'title' => __( 'Metabox title', 'domain' ), // The title to display
'callback' => 'metabox_callback_function', // The callback to output the metabox
'position' => 'nomal', // The position on the screen - one of 'normal', 'side' or 'advanced'
'priority' => 'default', // The display priority - one of 'default', 'high' or 'low'
'data' => $data, // The data to be passed to the callback
),
[...] // Other metabox items
);
array (
$slug => array( // $slug is the unique integration slug
'plugin' => 'plugin_name', // name of the plugin being integrated ; passed to is_plugin_active() to determine if the integration needs to be loaded
'class_name' => 'Integration_Class', // Class name of the integration class ; the class must be included using the action wprus_