diff --git a/includes/class-platform.php b/includes/class-platform.php index 9f4ffb6..ff6a0b1 100644 --- a/includes/class-platform.php +++ b/includes/class-platform.php @@ -132,6 +132,7 @@ private function load_dependencies() { $this->include('mailchimp/class-mailchimp.php'); $this->include('media/class-media.php'); $this->include('multisite-post-migration/class-multisite-post-migration.php'); + $this->include('newrelic/class-newrelic.php'); $this->include('newsletter/class-newsletter.php'); $this->include('permalink-rewrites/class-permalink-rewrites.php'); $this->include('post-publish-pipeline/class-post-publish-pipeline.php'); @@ -190,6 +191,7 @@ function() { } ); new Multisite_Post_Migration( $this->get_version(), $this->get_loader() ); + new Newrelic( $this->get_version(), $this->get_loader() ); new Newsletter( $this->get_version(), $this->get_loader() ); new Permalink_Rewrites( $this->version, $this->get_loader() ); new Post_Publish_Pipeline( $this->get_version(), $this->get_loader() ); diff --git a/includes/newrelic/class-newrelic.php b/includes/newrelic/class-newrelic.php new file mode 100644 index 0000000..9fa5003 --- /dev/null +++ b/includes/newrelic/class-newrelic.php @@ -0,0 +1,33 @@ +init($loader); + } + + public function init($loader = null) { + if ( null !== $loader ) { + $loader->add_action('wp_head', $this, 'script', 2 ); + } + } + + public function log_error() { + // WIP: move the central error logging utility into here but keep a simple php utility exposed. + } + + public function script() { + ob_start(); + ?> + + add_filter( 'post_link', $this, 'modify_post_permalinks', 10, 2 ); $loader->add_filter( 'post_type_link', $this, 'modify_post_permalinks', 10, 2 ); $loader->add_filter( 'rewrite_rules_array', $this, 'add_rewrite_rules', 10, 1 ); + $loader->add_filter( 'facetwp_preload_url_vars', $this, 'rewrite_datasets_archives', 10, 1 ); } /** @@ -134,7 +135,7 @@ public function add_rewrite_rules($rules) { $new_rules[$term_name . '/feature/[^/]+/([^/]{5,})/?$'] = 'index.php?attachment=$matches[1]'; // $new_rules[$term_name . '/feature/([^/]+)/([^/]{5,})/?$'] = 'index.php?attachment=$matches[1]'; } else if ( 'dataset' === $post_type ) { - $new_rules[$term_name . '/datasets'] = 'index.php?post_type=dataset&research-teams=' . $term_name; + $new_rules[$term_name . '/datasets'] = 'index.php?post_type=dataset'; $new_rules[$term_name . '/dataset/([^/]+)/?$'] = 'index.php?datasets=$matches[1]'; // Add attachment rule: $new_rules[$term_name . '/dataset/[^/]+/([^/]+)/?$'] = 'index.php?attachment=$matches[1]'; @@ -144,6 +145,30 @@ public function add_rewrite_rules($rules) { return array_merge($new_rules, $rules); } + /** + * Rewrites pewresearch.org/{research-team-name}/datasets to preload the selected facet + * @hook facetwp_preload_url_vars + */ + public function rewrite_datasets_archives($url_vars) { + $current_url = FWP()->helper->get_uri(); + if ( strpos( $current_url, 'datasets' ) === false ) { + return $url_vars; + } + $terms = get_terms(array( + 'taxonomy' => self::$taxonomy, + 'hide_empty' => false, + )); + $term_names = array_map(function ($term) { + return $term->slug; + }, $terms); + foreach($term_names as $term_name) { + if ( strpos( $current_url, $term_name . '/datasets' ) !== false && empty( $url_vars['research_teams'] ) ){ + $url_vars['research_teams'] = [ $term_name ]; + } + } + return $url_vars; + } + /** * Add rewrite tag to post permalinks. * @hook post_link