Skip to content

Instantly share code, notes, and snippets.

@gaupoit
Last active May 29, 2020 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaupoit/320c1d8e781dc13e83f51ed4a9ad1b66 to your computer and use it in GitHub Desktop.
Save gaupoit/320c1d8e781dc13e83f51ed4a9ad1b66 to your computer and use it in GitHub Desktop.
Integrate PDA Gold raw URL
<?php
add_filter( 'pda_after_filter_internal_url', 'ppwp_support_raw_internal_url', 10, 2 );
add_filter( 'ppwp_before_get_size_and_attachment_id_by_attachment_url', 'ppwp_get_raw_url_cb', 10, 2 );
add_filter( 'pda_token_urls_before_merge', 'ppwp_make_unique_token_urls', 10, 2 );
/**
* Support raw internal URLs when user enables raw URL option.
*
* @param array $filtered_urls Filtered URLs.
* @param array $urls Non-filtered URLs.
*
* @return array
*/
function ppwp_support_raw_internal_url( $filtered_urls, $urls ) {
if ( ! class_exists( 'Pda_Gold_Functions' ) ) {
return $filtered_urls;
}
$pda_gold_func = new Pda_Gold_Functions();
if ( ! $pda_gold_func->get_site_settings( PDA_v3_Constants::USE_REDIRECT_URLS ) ) {
return $filtered_urls;
}
$raw_url = array_filter(
$urls,
function ( $url ) {
return false !== strpos( $url, 'index.php?' . PDA_v3_Constants::$secret_param );
}
);
return array_merge( $raw_url, $filtered_urls );
}
/**
* Get raw URL callback.
*
* @param array $result Raw URL result.
* @param string $url The URL.
*
* @return array
*/
function ppwp_get_raw_url_cb( $result, $url ) {
if ( ! class_exists( 'PPW_Pro_Password_Services' ) ) {
return $result;
}
$password_services = new PPW_Pro_Password_Services();
if ( ! method_exists( $password_services, 'raw_url_to_attachment_id' ) ) {
return $result;
}
$attachment_id = $password_services->raw_url_to_attachment_id( $url );
if ( 0 === $attachment_id ) {
return $result;
}
return array( $attachment_id, '' );
}
/**
* Make unique token URLs.
*
* @param array $token_urls The token URLs.
*
* @return array
*/
function ppwp_make_unique_token_urls( $token_urls ) {
// Should remove duplicate items.
$uniq_token_ids = array_unique( array_column( $token_urls, 'url_to_replace' ) );
return array_filter(
$token_urls,
function ( $token, $key ) use ( $uniq_token_ids ) {
return in_array( $key, array_keys( $uniq_token_ids ), true );
},
ARRAY_FILTER_USE_BOTH
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment