/// <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);
        }
Example #2
0
        /// <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);
        }
Example #3
0
        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);
        }
Example #4
0
        /// <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);
        }