Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multisite: Table creation for new sites just from backend #265

Open
Drivingralle opened this issue Jun 12, 2023 · 1 comment
Open

Multisite: Table creation for new sites just from backend #265

Drivingralle opened this issue Jun 12, 2023 · 1 comment

Comments

@Drivingralle
Copy link
Contributor

Drivingralle commented Jun 12, 2023

Describe the bug
I have created a custom REST-Endpoint that creates new blogs inside the multsite. In my usecase the Statify plugin is network-activated.
As I open the new blog after the creation I get PHP notices that the Statify table is missing.
WordPress database error Table 'local.wp_2_statify' doesn't exist for query SHOW FULL COLUMNS FROM wp_2_statify made by require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), do_action('template_redirect'), WP_Hook->do_action, WP_Hook->apply_filters, Statify_Frontend::track_visit

Reason:
The methods to create the Statify tables is only added to the wp-admin pages.
The hook wp_initialize_site is called no matter the source of the call origin, but as Statify only hooks inside the admin the REST-Call doesn't initiate the table by default.

To Reproduce
Steps to reproduce the behavior:

  1. Install a WP multisite
  2. Use some way to execute
  3. Scroll down to '....'
  4. See error

Expected behavior
Table to be added not matter where what interface is used.

System (please complete the following information):

  • Version 1.8.4
@Drivingralle
Copy link
Contributor Author

Here some code to use as a MU-Plugin.

class we_kiwi_interactions_rest_api_multisite {

	function __construct() {

		add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );

	}

	/**
	 * Add the custom endpoint.
	 *
	 * @param WP_REST_Server $wp_rest_server
	 *
	 * @return void
	 */
	public function rest_api_init( WP_REST_Server $wp_rest_server ): void {

		if ( ! is_multisite() ) {
			return;
		}

		/*
		 * Create a new site
		 */
		register_rest_route( 'wp/v2', '/sites', array(
			'methods'             => WP_REST_Server::CREATABLE,
			'permission_callback' => array( $this, 'permission_callback_create_site' ),
			'callback'            => array( $this, 'callback_create_site' ),
			'args'                => array(
				'title'           => array(
					'required'          => true,
					'sanitize_callback' => 'sanitize_text_field',
				),
			),
		) );

	}

	/*
	 * Permission callbacks
	 */
	public function permission_callback_create_site( WP_REST_Request $request ): bool {
		return true;
		return is_user_logged_in();
	}

	/**
	 * Callback to handle POST requests.
	 *
	 * @param WP_REST_Request $request
	 *
	 * @return WP_Error|WP_HTTP_Response|WP_REST_Response
	 */
	public function callback_create_site( WP_REST_Request $request ): WP_Error|WP_REST_Response|WP_HTTP_Response {

		// Set some vars
		$params = $request->get_params();

		// Check if site registration is allowed
		switch ( get_network_option( get_current_network_id(), 'registration', 'none' ) ) {
			case 'all':
			case 'blog':
				break;
			default:
				return rest_ensure_response( new WP_Error( 'not_allowed', __( 'Action not allowed', 'we-kiwi-interactions' ), array( 'status' => 403, ) ) );
				break;
		}

		// Some settings
		$options = array(
			'public' => 0,
		);

		// Get network data
		$network = get_network();

		// Create new site
		$blog_id = wpmu_create_blog( $network->domain, sanitize_title( $params['title'] ), sanitize_text_field( $params['title'] ), get_current_user_id(), $options );
		if ( is_wp_error( $blog_id ) ) {
			return rest_ensure_response( $blog_id );
		}

		return rest_ensure_response( get_site( $blog_id ) );
	}

	/**
	 * Helper to generate error message for circle connection attempts.
	 *
	 * @return WP_Error|WP_REST_Response|WP_HTTP_Response
	 */
	private function get_connection_error(): WP_Error|WP_REST_Response|WP_HTTP_Response {
		return rest_ensure_response( new WP_Error( 'connection_failed', __( 'Connection failed', 'we-kiwi-interactions' ), array( 'status' => 500, ) ) );
	}

}

new we_kiwi_interactions_rest_api_multisite();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants