Pink-Crab / Perique-Framework

The Perqiue Plugin Framework for WordPress
5 stars 0 forks source link

Set default paths at app_config #57

Closed gin0115 closed 2 years ago

gin0115 commented 2 years ago

As we are passing the plugins base path to the App_Factory, we can also correctly set the App_Config path and url args.

/**
 * Generates some default paths for the app_config based on base path.
 *
 * @return array{
 *   url:array{
 *    plugin:string,
 *    view:string,
 *    assets:string,
 *    upload_root:string,
 *    upload_current:string,
 *  },
 *  path:array{
 *    plugin:string,
 *    view:string,
 *    assets:string,
 *    upload_root:string,
 *    upload_current:string,
 *  }
 * }
 */
private function default_config_paths(): array {
    $wp_uploads = \wp_upload_dir();

    return array(
        'path'   => array(
            'plugin'         => $this->base_path,
            'view'           => $this->base_path . '/views',
            'assets'         => $this->base_path . '/assets',
            'upload_root'    => $wp_uploads['basedir'],
            'upload_current' => $wp_uploads['path'],
        ),
        'url'    => array(
            'plugin'         => plugins_url( basename( $this->base_path ) ),
            'view'           => plugins_url( basename( $this->base_path ) ) . '/views',
            'assets'         => plugins_url( basename( $this->base_path ) ) . '/assets',
            'upload_root'    => $wp_uploads['baseurl'],
            'upload_current' => $wp_uploads['url'],
        ),
    );
}

requires a small change to App_Factory::boot()

/**
 * Returns a booted version of the app.
 *
 * @return \PinkCrab\Perique\Application\App
 */
public function boot(): App {
    // Sets default settings if not already set.
    if ( ! $this->app->has_app_config() ) {
        $this->app_config( $this->default_config_paths() );
    }

    return $this->app->boot();
}
gin0115 commented 2 years ago

Working example

image