/// <summary> /// Defines WordPress configuration constants and initializes runtime before proceeding to <c>index.php</c>. /// </summary> static void Apply(Context ctx, WordPressConfig config, WpLoader loader) { // see wp-config.php: // The name of the database for WordPress ctx.DefineConstant("DB_NAME", (PhpValue)config.DbName); // define('DB_NAME', 'wordpress'); // MySQL database username ctx.DefineConstant("DB_USER", (PhpValue)config.DbUser); // define('DB_USER', 'root'); // MySQL database password ctx.DefineConstant("DB_PASSWORD", (PhpValue)config.DbPassword); // define('DB_PASSWORD', 'password'); // MySQL hostname ctx.DefineConstant("DB_HOST", (PhpValue)config.DbHost); // define('DB_HOST', 'localhost'); // WordPress Database Table prefix. ctx.Globals["table_prefix"] = (PhpValue)config.DbTablePrefix; // $table_prefix = 'wp_'; // SALT ctx.DefineConstant("AUTH_KEY", (PhpValue)config.SALT.AUTH_KEY); ctx.DefineConstant("SECURE_AUTH_KEY", (PhpValue)config.SALT.SECURE_AUTH_KEY); ctx.DefineConstant("LOGGED_IN_KEY", (PhpValue)config.SALT.LOGGED_IN_KEY); ctx.DefineConstant("NONCE_KEY", (PhpValue)config.SALT.NONCE_KEY); ctx.DefineConstant("AUTH_SALT", (PhpValue)config.SALT.AUTH_SALT); ctx.DefineConstant("SECURE_AUTH_SALT", (PhpValue)config.SALT.SECURE_AUTH_SALT); ctx.DefineConstant("LOGGED_IN_SALT", (PhpValue)config.SALT.LOGGED_IN_SALT); ctx.DefineConstant("NONCE_SALT", (PhpValue)config.SALT.NONCE_SALT); // disable wp_cron() during the request, we have our own scheduler to fire the job ctx.DefineConstant("DISABLE_WP_CRON", PhpValue.True); // define('DISABLE_WP_CRON', true); // $peachpie-wp-loader : WpLoader ctx.Globals["peachpie_wp_loader"] = PhpValue.FromClass(loader); }
/// <summary> /// Defines WordPress configuration constants and initializes runtime before proceeding to <c>index.php</c>. /// </summary> static void Apply(Context ctx, WordPressConfig config, WpLoader loader) { // see wp-config.php: // WordPress Database Table prefix. ctx.Globals["table_prefix"] = (PhpValue)config.DbTablePrefix; // $table_prefix = 'wp_'; // SALT ctx.DefineConstant("AUTH_KEY", (PhpValue)config.SALT.AUTH_KEY); ctx.DefineConstant("SECURE_AUTH_KEY", (PhpValue)config.SALT.SECURE_AUTH_KEY); ctx.DefineConstant("LOGGED_IN_KEY", (PhpValue)config.SALT.LOGGED_IN_KEY); ctx.DefineConstant("NONCE_KEY", (PhpValue)config.SALT.NONCE_KEY); ctx.DefineConstant("AUTH_SALT", (PhpValue)config.SALT.AUTH_SALT); ctx.DefineConstant("SECURE_AUTH_SALT", (PhpValue)config.SALT.SECURE_AUTH_SALT); ctx.DefineConstant("LOGGED_IN_SALT", (PhpValue)config.SALT.LOGGED_IN_SALT); ctx.DefineConstant("NONCE_SALT", (PhpValue)config.SALT.NONCE_SALT); // Additional constants if (config.Constants != null) { foreach (var pair in config.Constants) { ctx.DefineConstant(pair.Key, pair.Value); } } // disable wp_cron() during the request, we have our own scheduler to fire the job ctx.DefineConstant("DISABLE_WP_CRON", PhpValue.True); // define('DISABLE_WP_CRON', true); // $peachpie-wp-loader : WpLoader ctx.Globals["peachpie_wp_loader"] = PhpValue.FromClass(loader); }
/// <summary> /// Defines WordPress configuration constants and initializes runtime before proceeding to <c>index.php</c>. /// </summary> static void Apply(Context ctx, WordPressConfig config, WpLoader loader) { // see wp-config.php: // WordPress Database Table prefix. ctx.Globals["table_prefix"] = (PhpValue)config.DbTablePrefix; // $table_prefix = 'wp_'; // SALT ctx.DefineConstant("AUTH_KEY", (PhpValue)config.SALT.AUTH_KEY); ctx.DefineConstant("SECURE_AUTH_KEY", (PhpValue)config.SALT.SECURE_AUTH_KEY); ctx.DefineConstant("LOGGED_IN_KEY", (PhpValue)config.SALT.LOGGED_IN_KEY); ctx.DefineConstant("NONCE_KEY", (PhpValue)config.SALT.NONCE_KEY); ctx.DefineConstant("AUTH_SALT", (PhpValue)config.SALT.AUTH_SALT); ctx.DefineConstant("SECURE_AUTH_SALT", (PhpValue)config.SALT.SECURE_AUTH_SALT); ctx.DefineConstant("LOGGED_IN_SALT", (PhpValue)config.SALT.LOGGED_IN_SALT); ctx.DefineConstant("NONCE_SALT", (PhpValue)config.SALT.NONCE_SALT); // Additional constants if (config.Constants != null) { foreach (var pair in config.Constants) { ctx.DefineConstant(pair.Key, pair.Value); } } // $peachpie-wp-loader : WpLoader ctx.Globals["peachpie_wp_loader"] = PhpValue.FromClass(loader); }
/// <summary> /// Installs WordPress middleware. /// </summary> /// <param name="app">The application builder.</param> /// <param name="config">WordPress instance configuration.</param> /// <param name="plugins">Container describing what plugins will be loaded.</param> /// <param name="path">Physical location of wordpress folder. Can be absolute or relative to the current directory.</param> public static IApplicationBuilder UseWordPress(this IApplicationBuilder app, WordPressConfig config = null, WpPluginContainer plugins = null, string path = "wordpress") { // wordpress root path: var root = System.IO.Path.GetFullPath(path); var fprovider = new PhysicalFileProvider(root); // plugins & configuration plugins = new WpPluginContainer(plugins); if (config == null) { config = WpConfigurationLoader .CreateDefault() .LoadFromSettings(app.ApplicationServices); } config.LoadFromEnvironment(app.ApplicationServices); // response caching: if (config.EnableResponseCaching) { var cachepolicy = new WpResponseCachingPolicyProvider(); var cachekey = new WpResponseCachingKeyProvider(); plugins.Add(cachepolicy); app.UseMiddleware <ResponseCachingMiddleware>(cachepolicy, cachekey); } var wploader = new WpLoader(CompositionHelpers.GetPlugins(app.ApplicationServices).Concat(plugins.GetPlugins(app.ApplicationServices))); // url rewriting: app.UseRewriter(new RewriteOptions().Add(context => ShortUrlRule(context, fprovider))); // log exceptions: app.UseDiagnostic(); // handling php files: app.UsePhp(new PhpRequestOptions() { ScriptAssembliesName = WordPressAssemblyName.ArrayConcat(config.LegacyPluginAssemblies), BeforeRequest = ctx => Apply(ctx, config, wploader), RootPath = root, }); // static files app.UseStaticFiles(new StaticFileOptions() { FileProvider = fprovider }); // fire wp-cron.php asynchronously if (TryGetWpCronUri(app.ServerFeatures.Get <IServerAddressesFeature>(), out var wpcronUri)) { WpCronScheduler.StartScheduler(HttpMethods.Post, wpcronUri, TimeSpan.FromSeconds(60)); } // return(app); }
/// <summary> /// Defines WordPress configuration constants and initializes runtime before proceeding to <c>index.php</c>. /// </summary> static void Apply(Context ctx, WordPressConfig config, WpLoader loader) { // see wp-config.php: // WordPress Database Table prefix. ctx.Globals["table_prefix"] = (PhpValue)config.DbTablePrefix; // $table_prefix = 'wp_'; // SALT ctx.DefineConstant("AUTH_KEY", (PhpValue)config.SALT.AUTH_KEY); ctx.DefineConstant("SECURE_AUTH_KEY", (PhpValue)config.SALT.SECURE_AUTH_KEY); ctx.DefineConstant("LOGGED_IN_KEY", (PhpValue)config.SALT.LOGGED_IN_KEY); ctx.DefineConstant("NONCE_KEY", (PhpValue)config.SALT.NONCE_KEY); ctx.DefineConstant("AUTH_SALT", (PhpValue)config.SALT.AUTH_SALT); ctx.DefineConstant("SECURE_AUTH_SALT", (PhpValue)config.SALT.SECURE_AUTH_SALT); ctx.DefineConstant("LOGGED_IN_SALT", (PhpValue)config.SALT.LOGGED_IN_SALT); ctx.DefineConstant("NONCE_SALT", (PhpValue)config.SALT.NONCE_SALT); if (!string.IsNullOrEmpty(config.SiteUrl)) { ctx.DefineConstant("WP_SITEURL", config.SiteUrl); } if (!string.IsNullOrEmpty(config.HomeUrl)) { ctx.DefineConstant("WP_HOME", config.HomeUrl); } // Additional constants if (config.Constants != null) { foreach (var pair in config.Constants) { ctx.DefineConstant(pair.Key, pair.Value); } } // $peachpie-wp-loader : WpLoader ctx.Globals["peachpie_wp_loader"] = PhpValue.FromClass(loader); // workaround HTTPS under proxy, // set $_SERVER['HTTPS'] = 'on' // https://wordpress.org/support/article/administration-over-ssl/#using-a-reverse-proxy if (ctx.IsWebApplication && ctx.GetHttpContext().Request.Headers["X-Forwarded-Proto"] == "https") { ctx.Server["HTTPS"] = "on"; } }
/// <summary> /// Installs WordPress middleware. /// </summary> /// <param name="app">The application builder.</param> /// <param name="path">Physical location of wordpress folder. Can be absolute or relative to the current directory.</param> public static IApplicationBuilder UseWordPress(this IApplicationBuilder app, string path = null) { // wordpress root path: if (path == null) { // bin/wordpress path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "/wordpress"; if (Directory.Exists(path) == false) { // cwd/wordpress path = Path.GetDirectoryName(Directory.GetCurrentDirectory()) + "/wordpress"; if (Directory.Exists(path) == false) { // cwd/../wordpress path = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) + "/wordpress"; } } } var root = System.IO.Path.GetFullPath(path); var fprovider = new PhysicalFileProvider(root); // log exceptions: app.UseDiagnostic(); // load options var options = new WordPressConfig() .LoadFromSettings(app.ApplicationServices) // appsettings.json .LoadFromEnvironment(app.ApplicationServices) // environment variables (known cloud hosts) .LoadFromOptions(app.ApplicationServices) // IConfigureOptions<WordPressConfig> service .LoadDefaults(); // // list of plugins: var plugins = new WpPluginContainer(options.PluginContainer); // response caching: if (options.EnableResponseCaching) { // var cachepolicy = new WpResponseCachingPolicyProvider(); // var cachekey = app.ApplicationServices.GetService<WpResponseCachingKeyProvider>(); var cachepolicy = new WpResponseCachePolicy(); plugins.Add(cachepolicy); // app.UseMiddleware<ResponseCachingMiddleware>(cachepolicy, cachekey); app.UseMiddleware <WpResponseCacheMiddleware>(new MemoryCache(new MemoryCacheOptions { }), cachepolicy); } var wploader = new WpLoader(plugins: CompositionHelpers.GetPlugins(options.CompositionContainers.CreateContainer(), app.ApplicationServices) .Concat(plugins.GetPlugins(app.ApplicationServices))); // url rewriting: app.UseRewriter(new RewriteOptions().Add(context => ShortUrlRule(context, fprovider))); // update globals used by WordPress: WpStandard.DB_HOST = options.DbHost; WpStandard.DB_NAME = options.DbName; WpStandard.DB_PASSWORD = options.DbPassword; WpStandard.DB_USER = options.DbUser; // var env = app.ApplicationServices.GetService <IHostingEnvironment>(); WpStandard.WP_DEBUG = options.Debug || env.IsDevelopment(); // handling php files: var startup = new Action <Context>(ctx => Apply(ctx, options, wploader)); app.UsePhp(new PhpRequestOptions() { ScriptAssembliesName = options.LegacyPluginAssemblies?.ToArray(), BeforeRequest = startup, RootPath = root, }); // static files app.UseStaticFiles(new StaticFileOptions() { FileProvider = fprovider }); // fire wp-cron.php asynchronously WpCronScheduler.StartScheduler(startup, TimeSpan.FromSeconds(60)); // return(app); }
private static IApplicationBuilder InstallWordPress(this IApplicationBuilder app, WordPressConfig options, string path = null, string siteurl = null) { // wordpress root path: if (path == null) { // bin/wordpress path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "/wordpress"; if (Directory.Exists(path) == false) { // cwd/wordpress path = Path.GetDirectoryName(Directory.GetCurrentDirectory()) + "/wordpress"; if (Directory.Exists(path) == false) { // cwd/../wordpress path = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) + "/wordpress"; } } } var root = System.IO.Path.GetFullPath(path); var fprovider = new PhysicalFileProvider(root); // log exceptions: app.UseDiagnostic(); // list of plugins: var plugins = new WpPluginContainer(options.PluginContainer); // response caching: if (options.EnableResponseCaching) { // var cachepolicy = new WpResponseCachingPolicyProvider(); // var cachekey = app.ApplicationServices.GetService<WpResponseCachingKeyProvider>(); var cachepolicy = new WpResponseCachePolicy(); plugins.Add(cachepolicy); // app.UseMiddleware<ResponseCachingMiddleware>(cachepolicy, cachekey); app.UseMiddleware <WpResponseCacheMiddleware>(new MemoryCache(new MemoryCacheOptions { }), cachepolicy); } // if (options.LegacyPluginAssemblies != null) // { // options.LegacyPluginAssemblies.ForEach(name => Context.AddScriptReference(Assembly.Load(new AssemblyName(name)))); // } var wploader = new WpLoader(plugins: CompositionHelpers.GetPlugins(options.CompositionContainers.CreateContainer(), app.ApplicationServices, root) .Concat(plugins.GetPlugins(app.ApplicationServices))); // url rewriting: bool multisite = options.Multisite.Enable; bool subdomainInstall = options.Multisite.SubdomainInstall; if (options.Constants != null && !multisite && !subdomainInstall) { // using constants instead MultisiteData if (options.Constants.TryGetValue("MULTISITE", out string multi)) { multisite = bool.TryParse(multi, out bool multiConverted) && multiConverted; } if (options.Constants.TryGetValue("SUBDOMAIN_INSTALL", out string domain)) { subdomainInstall = bool.TryParse(domain, out bool domainConverted) && domainConverted; } } app.UseRewriter(new RewriteOptions().Add(context => ShortUrlRule(context, fprovider, multisite, subdomainInstall, siteurl))); // update globals used by WordPress: WpStandard.DB_HOST = options.DbHost; WpStandard.DB_NAME = options.DbName; WpStandard.DB_PASSWORD = options.DbPassword; WpStandard.DB_USER = options.DbUser; // var env = app.ApplicationServices.GetService <IHostingEnvironment>(); WpStandard.WP_DEBUG = options.Debug || env.IsDevelopment(); // handling php files: var startup = new Action <Context>(ctx => { Apply(ctx, options, wploader); }); // app.UsePhp( // prefix: default, // NOTE: maybe we can handle only index.php and wp-admin/*.php ? // configureContext: startup, // rootPath: root); app.UsePhp(new PhpRequestOptions() { ScriptAssembliesName = options.LegacyPluginAssemblies?.ToArray(), BeforeRequest = startup, RootPath = root, }); // static files app.UseStaticFiles(new StaticFileOptions() { FileProvider = fprovider }); // fire wp-cron.php asynchronously WpCronScheduler.StartScheduler(startup, TimeSpan.FromSeconds(120), root); return(app); }
/// <summary> /// Installs WordPress middleware. /// </summary> /// <param name="app">The application builder.</param> /// <param name="config">WordPress instance configuration.</param> /// <param name="plugins">Container describing what plugins will be loaded.</param> /// <param name="path">Physical location of wordpress folder. Can be absolute or relative to the current directory.</param> public static IApplicationBuilder UseWordPress(this IApplicationBuilder app, WordPressConfig config = null, WpPluginContainer plugins = null, string path = null) { // wordpress root path: if (path == null) { // bin/wordpress path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "/wordpress"; if (Directory.Exists(path) == false) { // cwd/wordpress path = Path.GetDirectoryName(Directory.GetCurrentDirectory()) + "/wordpress"; if (Directory.Exists(path) == false) { // cwd/../wordpress path = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) + "/wordpress"; } } } var root = System.IO.Path.GetFullPath(path); var fprovider = new PhysicalFileProvider(root); // plugins & configuration plugins = new WpPluginContainer(plugins); if (config == null) { config = WpConfigurationLoader .CreateDefault() .LoadFromSettings(app.ApplicationServices); } config.LoadFromEnvironment(app.ApplicationServices); // response caching: if (config.EnableResponseCaching) { var cachepolicy = new WpResponseCachingPolicyProvider(); var cachekey = new WpResponseCachingKeyProvider(); plugins.Add(cachepolicy); app.UseMiddleware <ResponseCachingMiddleware>(cachepolicy, cachekey); } // update globals WpStandard.DB_HOST = config.DbHost; WpStandard.DB_NAME = config.DbName; WpStandard.DB_PASSWORD = config.DbPassword; WpStandard.DB_USER = config.DbUser; // var env = (IHostingEnvironment)app.ApplicationServices.GetService(typeof(IHostingEnvironment)); WpStandard.WP_DEBUG = config.Debug || env.IsDevelopment(); var wploader = new WpLoader(CompositionHelpers.GetPlugins(app.ApplicationServices).Concat(plugins.GetPlugins(app.ApplicationServices))); // url rewriting: app.UseRewriter(new RewriteOptions().Add(context => ShortUrlRule(context, fprovider))); // log exceptions: app.UseDiagnostic(); // handling php files: app.UsePhp(new PhpRequestOptions() { ScriptAssembliesName = WordPressAssemblyName.ArrayConcat(config.LegacyPluginAssemblies), BeforeRequest = ctx => Apply(ctx, config, wploader), RootPath = root, }); // static files app.UseStaticFiles(new StaticFileOptions() { FileProvider = fprovider }); // fire wp-cron.php asynchronously if (TryGetWpCronUri(app.ServerFeatures.Get <IServerAddressesFeature>(), out var wpcronUri)) { WpCronScheduler.StartScheduler(HttpMethods.Post, wpcronUri, TimeSpan.FromSeconds(60)); } // return(app); }