Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Photon-Plugin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jan Grewe
Photon-Plugin
Commits
d5956ab9
Commit
d5956ab9
authored
14 years ago
by
Michael Pretty
Browse files
Options
Downloads
Patches
Plain Diff
initial merge of cdn rewrite plugin
parent
20b836bf
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
cloudfiles-cdn.php
+134
-1
134 additions, 1 deletion
cloudfiles-cdn.php
with
134 additions
and
1 deletion
cloudfiles-cdn.php
+
134
−
1
View file @
d5956ab9
...
@@ -363,3 +363,136 @@ class CloudfilesCdn {
...
@@ -363,3 +363,136 @@ class CloudfilesCdn {
require_once
(
'voce-settings.php'
);
require_once
(
'voce-settings.php'
);
$cdn
=
new
CloudfilesCdn
();
$cdn
=
new
CloudfilesCdn
();
class
CDN_Rewrite
{
private
$file_extensions
;
private
$blog_details
;
private
$cdn_root_url
;
public
function
__construct
()
{
$this
->
file_extensions
=
'bmp|bz2|css|gif|ico|gz|jpg|jpeg|js|mp3|pdf|png|rar|rtf|swf|tar|tgz|txt|wav|zip'
;
$this
->
cdn_root_url
=
trailingslashit
(
CloudfilesCdn
::
get_setting
(
'root_url'
));
}
/**
* Initializes class and registers hooks
*
*/
public
function
initialize
()
{
if
(
!
empty
(
$this
->
cdn_root_url
))
{
add_action
(
'template_redirect'
,
array
(
$this
,
'start_buffer'
),
1
);
}
}
/**
* start output buffering.
*
*/
public
function
start_buffer
()
{
ob_start
(
array
(
$this
,
'filter_urls'
));
}
/**
* Callback for output buffering. Search content for urls to replace
*
* @param string $content
* @return string
*/
public
function
filter_urls
(
$content
)
{
$root_url
=
$this
->
get_site_root_url
();
$regex
=
'#(?<=[(\"\'])'
.
quotemeta
(
$root_url
)
.
'(?:(/[^\"\')]+\.('
.
$this
->
file_extensions
.
')))#'
;
$content
=
preg_replace_callback
(
$regex
,
array
(
$this
,
'url_rewrite'
),
$content
);
return
$content
;
}
/**
* Returns the root url of the current site
*
* @return string
*/
public
function
get_site_root_url
()
{
if
(
is_multisite
()
&&
!
is_subdomain_install
())
{
$root_blog
=
get_blog_details
(
1
);
$root_url
=
$root_blog
->
siteurl
;
}
else
{
$root_url
=
site_url
();
}
return
$root_url
;
}
/**
* Returns the details for the current blog
*
* @return object
*/
public
function
get_this_blog_details
()
{
if
(
!
isset
(
$this
->
blog_details
))
{
global
$blog_id
;
$this
->
blog_details
=
get_blog_details
(
$blog_id
);
}
return
$this
->
blog_details
;
}
/**
* Callback for url preg_replace_callback. Returns corrected URL
*
* @param array $match
* @return string
*/
public
function
url_rewrite
(
$match
)
{
global
$blog_id
;
$path
=
$match
[
1
];
//if is subfolder install and isn't root blog and path starts with site_url and isnt uploads dir
if
(
is_multisite
()
&&
!
is_subdomain_install
()
&&
$blog_id
!==
1
)
{
$bloginfo
=
$this
->
get_this_blog_details
();
if
((
0
===
strpos
(
$path
,
$bloginfo
->
path
))
&&
(
0
!==
strpos
(
$path
,
$bloginfo
->
path
.
'files/'
)))
{
$path
=
'/'
.
substr
(
$path
,
strlen
(
$bloginfo
->
path
));
}
}
return
$this
->
cdn_root_url
.
$path
;
}
}
add_action
(
'init'
,
array
(
new
CDN_Rewrite
(),
'initialize'
));
class
CDN_VersionAssets
{
private
$default_version
=
''
;
private
$root_url
;
public
function
__construct
()
{
$this
->
root_url
=
site_url
();
}
public
function
initialize
()
{
add_filter
(
'style_loader_src'
,
array
(
$this
,
'replace_version'
),
10
);
add_filter
(
'script_loader_src'
,
array
(
$this
,
'replace_version'
),
10
);
add_filter
(
'style_loader_src'
,
array
(
$this
,
'replace_version'
),
10
);
}
public
function
on_template_redirect
()
{
$this
->
default_version
=
@
filemtime
(
get_stylesheet_directory
()
.
'/style.css'
);
}
private
function
get_version
(
$url
)
{
if
(
0
===
strpos
(
$url
,
$this
->
root_url
))
{
$parts
=
parse_url
(
$url
);
$file_path
=
str_replace
(
site_url
(
'/'
),
ABSPATH
,
$parts
[
'scheme'
]
.
'://'
.
$parts
[
'host'
]
.
$parts
[
'path'
]);
if
(
!
(
$version
=
@
filemtime
(
$file_path
))
)
{
$version
=
$this
->
default_version
;
}
return
$version
;
}
return
false
;
}
public
function
replace_version
(
$src
)
{
if
(
$new_version
=
$this
->
get_version
(
$src
)
)
{
return
add_query_arg
(
'ver'
,
$new_version
,
$src
);
}
return
$src
;
}
}
add_action
(
'init'
,
array
(
new
CDN_VersionAssets
(),
'initialize'
));
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment