Skip to content

Commit

Permalink
Remove "page=CiviCRM" query string from WordPress front-end
Browse files Browse the repository at this point in the history
  • Loading branch information
christianwach committed May 20, 2020
1 parent 0e66c49 commit 4106c57
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
26 changes: 17 additions & 9 deletions civicrm.php
Expand Up @@ -419,8 +419,8 @@ public function setup_instance() {
*/
public function civicrm_in_wordpress_set() {

// Get identifying query var
$page = get_query_var( 'page' );
// Get identifying query var.
$page = get_query_var( 'civiwp' );

// Store
self::$in_wordpress = ( $page == 'CiviCRM' ) ? TRUE : FALSE;
Expand Down Expand Up @@ -578,6 +578,9 @@ public function register_hooks() {
// Go no further if CiviCRM not installed yet
if ( ! CIVICRM_INSTALLED ) return;

// Add our query vars.
add_filter( 'query_vars', array( $this, 'query_vars' ) );

// Delay everything else until query has been parsed
add_action( 'parse_query', array( $this, 'register_hooks_front_end' ) );

Expand Down Expand Up @@ -610,6 +613,14 @@ public function register_hooks_front_end( $query ) {
}
$alreadyRegistered = TRUE;

// Redirect if old query var is present.
if ( 'CiviCRM' == get_query_var( 'page' ) && 'CiviCRM' != get_query_var( 'civiwp' ) ) {
$redirect_url = remove_query_arg( 'page', false );
$redirect_url = add_query_arg( 'civiwp', 'CiviCRM', $redirect_url );
wp_redirect( $redirect_url, 301 );
exit();
}

// Store context
$this->civicrm_in_wordpress_set();

Expand Down Expand Up @@ -720,9 +731,6 @@ public function register_hooks_clean_urls() {

}

// Add our query vars.
add_filter( 'query_vars', array( $this, 'query_vars' ) );

}


Expand Down Expand Up @@ -813,7 +821,7 @@ public function rewrite_rules( $flush_rewrite_rules = false ) {
// Let's add rewrite rule when viewing the basepage
add_rewrite_rule(
'^' . $config->wpBasePage . '/([^?]*)?',
'index.php?page_id=' . $basepage->ID . '&page=CiviCRM&q=civicrm%2F$matches[1]',
'index.php?page_id=' . $basepage->ID . '&civiwp=CiviCRM&q=civicrm%2F$matches[1]',
'top'
);

Expand Down Expand Up @@ -853,7 +861,7 @@ public function query_vars( $query_vars ) {

// Build our query vars
$civicrm_query_vars = array(
'page', 'q', 'reset', 'id', 'html', 'snippet', // URL query vars
'civiwp', 'q', 'reset', 'id', 'html', 'snippet', // URL query vars
'action', 'mode', 'cid', 'gid', 'sid', 'cs', 'force', // Shortcode query vars
);

Expand Down Expand Up @@ -1537,7 +1545,7 @@ private function remove_wp_magic_quotes() {
$q = get_query_var( 'q' );
if (!empty($q)) {

$page = get_query_var( 'page' );
$page = get_query_var( 'civiwp' );
$reset = get_query_var( 'reset' );
$id = get_query_var( 'id' );
$html = get_query_var( 'html' );
Expand All @@ -1552,7 +1560,7 @@ private function remove_wp_magic_quotes() {
$force = get_query_var( 'force' );

$_REQUEST['q'] = $_GET['q'] = $q;
$_REQUEST['page'] = $_GET['page'] = 'CiviCRM';
$_REQUEST['civiwp'] = $_GET['civiwp'] = 'CiviCRM';
if (!empty($reset)) { $_REQUEST['reset'] = $_GET['reset'] = $reset; }
if (!empty($id)) { $_REQUEST['id'] = $_GET['id'] = $id; }
if (!empty($html)) { $_REQUEST['html'] = $_GET['html'] = $html; }
Expand Down
6 changes: 3 additions & 3 deletions includes/civicrm.basepage.php
Expand Up @@ -558,14 +558,14 @@ public function basepage_canonical_url( $canonical ) {
* It would be better to specify which params are okay to accept as the
* canonical URLs, but this will work for the time being.
*/
if ( empty( $_GET['page'] )
if ( empty( $_GET['civiwp'] )
|| empty( $_GET['q'] )
|| 'CiviCRM' !== $_GET['page'] ) {
|| 'CiviCRM' !== $_GET['civiwp'] ) {
return $canonical;
}
$path = $_GET['q'];
unset( $_GET['q'] );
unset( $_GET['page'] );
unset( $_GET['civiwp'] );
$query = http_build_query( $_GET );

}
Expand Down
2 changes: 1 addition & 1 deletion includes/civicrm.compat.php
Expand Up @@ -137,7 +137,7 @@ public function rewrite_rules_polylang( $flush_rewrite_rules, $basepage ) {
foreach ($rewrite as $path) {
add_rewrite_rule(
'^' . $path . '([^?]*)?',
'index.php?page_id=' . $post_id . '&page=CiviCRM&q=civicrm%2F$matches[1]',
'index.php?page_id=' . $post_id . '&civiwp=CiviCRM&q=civicrm%2F$matches[1]',
'top'
);
}
Expand Down
4 changes: 1 addition & 3 deletions includes/civicrm.shortcodes.php
Expand Up @@ -398,7 +398,7 @@ private function render_multiple( $post_id = FALSE, $shortcode = FALSE, $multipl
if (!$config->cleanURL) {

// Construct query parts
$queryParts[] = 'page=CiviCRM';
$queryParts[] = 'civiwp=CiviCRM';
if (isset($args['q'])) {
$queryParts[] = 'q=' . $args['q'];
}
Expand Down Expand Up @@ -815,8 +815,6 @@ public function get_data( $atts, $args ) {
*/
$params = apply_filters( 'civicrm_shortcode_api_params', array(
'version' => 3,
'page' => 'CiviCRM',
'q' => 'civicrm/ajax/rest',
'sequential' => '1',
), $atts, $args );

Expand Down

0 comments on commit 4106c57

Please sign in to comment.