Skip to content

Commit aa3192e

Browse files
authored
Merge pull request #4 from tarosky/feature/sitemap-query-filter
Add filter for attachment query.
2 parents 6b5c5f1 + 175a38f commit aa3192e

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

.github/workflows/wordpress.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
runs-on: ${{ matrix.operating-system }}
1616
strategy:
1717
matrix:
18-
operating-system: [ ubuntu-18.04 ] # OS. ubuntu-18.04 is also available.
18+
operating-system: [ ubuntu-latest ] # OS. ubuntu-18.04 is also available.
1919
php: [ '7.2', '7.4', '8.1' ] # PHP versions to check.
2020
wp: [ 'latest', '5.7' ] # WordPress version to check.
2121
services:
@@ -53,7 +53,7 @@ jobs:
5353

5454
assets:
5555
name: Assets Test
56-
runs-on: ubuntu-18.04
56+
runs-on: ubuntu-latest
5757
steps:
5858
- uses: actions/checkout@master
5959

@@ -72,19 +72,19 @@ jobs:
7272
name: Deploy WordPress.org
7373
needs: [ test, assets ]
7474
if: contains(github.ref, 'tags/')
75-
runs-on: ubuntu-18.04
75+
runs-on: ubuntu-latest
7676
steps:
7777
- name: Checkout code
78-
uses: actions/checkout@v2
78+
uses: actions/checkout@master
7979

8080
- name: Setup PHP with composer v2
8181
uses: shivammathur/setup-php@v2
8282
with:
8383
php-version: 7.2
84-
tools: composer:v2.2
84+
tools: composer
8585

8686
- name: Install Node
87-
uses: actions/setup-node@v1
87+
uses: actions/setup-node@v3
8888
with:
8989
node-version: '14'
9090

.wp-env.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"plugins": [ "." ],
33
"themes": [
4-
"https://downloads.wordpress.org/theme/twentytwenty.latest-stable.zip"
4+
"https://downloads.wordpress.org/theme/twentytwenty.latest-stable.zip",
5+
"https://downloads.wordpress.org/theme/stacks.latest-stable.zip"
56
]
67
}

src/Tarosky/Sitemap/Provider/AttachmentSitemapIndexProvider.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,23 @@ public function is_active() {
2828
*/
2929
protected function get_urls() {
3030
global $wpdb;
31-
$query = <<<SQL
31+
$join_clause = apply_filters( 'taro_sitemap_attachment_query_join', '' );
32+
$where_clause = apply_filters( 'taro_sitemap_attachment_query_where', '' );
33+
$query = <<<SQL
3234
SELECT
3335
EXTRACT( YEAR_MONTH from p1.post_date ) as date,
3436
COUNT( p1.ID ) AS total
3537
FROM {$wpdb->posts} AS p1
3638
LEFT JOIN {$wpdb->posts} AS p2
3739
ON p1.post_parent = p2.ID
40+
{$join_clause}
3841
WHERE p1.post_type = 'attachment'
3942
AND p1.post_mime_type LIKE 'image%'
4043
AND p2.post_status = 'publish'
44+
{$where_clause}
4145
GROUP BY EXTRACT( YEAR_MONTH from p1.post_date )
4246
SQL;
43-
$urls = [];
47+
$urls = [];
4448
// Already escaped.
4549
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
4650
foreach ( $wpdb->get_results( $query ) as $row ) {

src/Tarosky/Sitemap/Provider/AttachmentSitemapProvider.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,25 @@ public function is_active() {
3131
*/
3232
protected function get_urls() {
3333
global $wpdb;
34-
$year = get_query_var( 'year' );
35-
$month = get_query_var( 'monthnum' );
36-
$from = sprintf( '%04d-%02d-01 00:00:00', $year, $month );
37-
$to = ( new \DateTimeImmutable )->modify( sprintf( 'last day of %04d-%02d', $year, $month ) )->format( 'Y-m-d 23:59:59' );
38-
$per_page = $this->option()->posts_per_page;
39-
$offset = ( max( 1, get_query_var( 'paged' ) ) - 1 ) * $per_page;
40-
$query = <<<SQL
34+
$year = get_query_var( 'year' );
35+
$month = get_query_var( 'monthnum' );
36+
$from = sprintf( '%04d-%02d-01 00:00:00', $year, $month );
37+
$to = ( new \DateTimeImmutable )->modify( sprintf( 'last day of %04d-%02d', $year, $month ) )->format( 'Y-m-d 23:59:59' );
38+
$per_page = $this->option()->posts_per_page;
39+
$offset = ( max( 1, get_query_var( 'paged' ) ) - 1 ) * $per_page;
40+
$join_clause = apply_filters( 'taro_sitemap_attachment_query_join', '' );
41+
$where_clause = apply_filters( 'taro_sitemap_attachment_query_where', '' );
42+
$query = <<<SQL
4143
SELECT p1.* FROM {$wpdb->posts} AS p1
4244
LEFT JOIN {$wpdb->posts} AS p2
4345
ON p1.post_parent = p2.ID
46+
{$join_clause}
4447
WHERE p1.post_type = 'attachment'
4548
AND p1.post_status = 'inherit'
4649
AND p1.post_date BETWEEN %s AND %s
4750
AND p1.post_mime_type LIKE ':::image:::'
4851
AND p2.post_status = 'publish'
52+
{$where_clause}
4953
ORDER BY p1.post_date DESC
5054
LIMIT %d, %d
5155
SQL;

0 commit comments

Comments
 (0)