Example #1
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 #2
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);
        }