/// <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);
        }
Пример #2
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);
        }