├── hm-post-repeat.css ├── phpunit.xml ├── hm-post-repeat.js ├── .travis.yml ├── tests ├── bootstrap.php └── test-repeat-posts.php ├── README.md ├── .scrutinizer.yml ├── LICENSE └── hm-post-repeat.php /hm-post-repeat.css: -------------------------------------------------------------------------------- 1 | .misc-pub-hm-post-repeat { 2 | line-height: 25px; 3 | min-height: 25px; 4 | } 5 | 6 | .misc-pub-hm-post-repeat span.dashicons.dashicons-controls-repeat { 7 | margin-top: 2px; 8 | } 9 | 10 | .misc-pub-hm-post-repeat select { 11 | margin: 0; 12 | } -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | ./tests/ 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /hm-post-repeat.js: -------------------------------------------------------------------------------- 1 | (function($) { 2 | 3 | $( document ).on( 'click', '.edit-hm-post-repeat', function( e ) { 4 | 5 | e.preventDefault(); 6 | 7 | $( this ).hide(); 8 | $( '.misc-pub-hm-post-repeat strong' ).hide(); 9 | $( '#hm-post-repeat' ).show(); 10 | $( '#hm-post-repeat' ).find( 'select' ).focus(); 11 | 12 | } ); 13 | 14 | $( document ).on( 'click', '#hm-post-repeat a', function( e ) { 15 | 16 | e.preventDefault(); 17 | 18 | $( '.misc-pub-hm-post-repeat strong' ).text( $( '#hm-post-repeat' ).find( 'option:selected' ).text() ).show(); 19 | $( '.edit-hm-post-repeat' ).show(); 20 | $( '#hm-post-repeat' ).hide(); 21 | 22 | } ); 23 | 24 | }(jQuery)); -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | sudo: false 3 | php: 4 | - 5.6 5 | - 7.0 6 | 7 | env: 8 | - WP_VERSION=master WP_MULTISITE=0 9 | 10 | matrix: 11 | fast_finish: true 12 | 13 | before_install: 14 | - pip install --user codecov 15 | # set up WP install 16 | - export WP_DEVELOP_DIR=/tmp/wordpress/ 17 | - mkdir -p $WP_DEVELOP_DIR 18 | - git clone --depth=1 git://develop.git.wordpress.org/ $WP_DEVELOP_DIR 19 | # set up tests config 20 | - cd $WP_DEVELOP_DIR 21 | - cp wp-tests-config-sample.php wp-tests-config.php 22 | - sed -i "s/youremptytestdbnamehere/wordpress_test/" wp-tests-config.php 23 | - sed -i "s/yourusernamehere/root/" wp-tests-config.php 24 | - sed -i "s/yourpasswordhere//" wp-tests-config.php 25 | # set up database 26 | - mysql -e 'CREATE DATABASE wordpress_test;' -uroot 27 | 28 | install: 29 | - git config --global user.email "tom@hmn.md" 30 | - git config --global user.name "Tom Willmot" 31 | 32 | notifications: 33 | email: false 34 | 35 | script: 36 | - cd $TRAVIS_BUILD_DIR 37 | - phpunit --coverage-clover=coverage.xml 38 | 39 | after_success: 40 | - codecov 41 | 42 | git: 43 | depth: 1 44 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Repeatable Posts
5 | Designate a post as repeatable and it'll be copied and re-published on a weekly basis. 6 | 7 | 8 | 9 | Coverage via Codecov 10 | 11 |
12 | 13 | Scrutinizer Code Quality Score 14 | 15 |
16 | 17 | Build Status 18 | 19 | 20 | 21 | 22 | 23 | A Human Made project. Maintained by @willmot. 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | ## How does it work? 32 | 33 | Once you set a post as repeating and publish it an exact copy of that post is made and scheduled for publishing a week after the publish date of the original post. Once that repeat post is published it is again copied and set to publish the week after that. This continues until the heat death of the universe, or until you remove the plugin, whichever comes first. 34 | 35 | ## Why does it exist? 36 | 37 | We use this on our internal blogs to schedule recurring reminders and announcements. For example every week a "weekly updates" post is published, prompting everyone to comment with what they've been working on that week. 38 | 39 | ## Want to contribute? 40 | 41 | There's likely some issues already, if not create some! Any code changes should be submitted as Pull Requests. Thanks! 42 | 43 | Interested in joining in on the fun? [Join us, and become human!](https://hmn.md/is/hiring/) 44 | 45 | ## UI 46 | 47 | 48 | 49 | _Repeating and repeat posts are highlighted as such on the edit posts page._ 50 | 51 | ------- 52 | 53 | 54 | 55 | 56 | _When editing a post you can easily set whether it should repeat._ 57 | 58 | ------- 59 | 60 | 61 | 62 | _Repeat posts show a convenient link back to the original._ 63 | 64 | ## Changelog 65 | 66 | ### 0.4 67 | 68 | - Increase test coverage above 85% 69 | - Introduce additional daily & monthly repeating intervals. Props @noplanman. 70 | 71 | ### 0.3 72 | 73 | - Introduce unit tests 74 | - Fix several critical bugs 75 | 76 | ### 0.2 77 | 78 | - Refactor based on feedback from Human Made team 79 | 80 | ### 0.1 81 | 82 | - Initial release 83 | 84 | ## License 85 | 86 | Repeatable Posts is licensed under the GPLv2 or later. 87 | 88 | ## Credits 89 | 90 | Created by Human Made. 91 | 92 | Written and maintained by [Tom Willmot](https://github.com/willmot). 93 | -------------------------------------------------------------------------------- /.scrutinizer.yml: -------------------------------------------------------------------------------- 1 | filter: 2 | excluded_paths: [ 'tests/*' ] 3 | 4 | tools: 5 | php_code_sniffer: 6 | config: { standard: WordPress } 7 | 8 | php_changetracking: 9 | enabled: true 10 | bug_patterns: 11 | - '\bfix(?:es|ed)?\b' 12 | feature_patterns: 13 | - '\badd(?:s|ed)?\b' 14 | - '\bimplement(?:s|ed)?\b' 15 | 16 | js_hint: true 17 | 18 | php_mess_detector: 19 | config: 20 | naming_rules: { boolean_method_name: true } 21 | controversial_rules: { superglobals: false } 22 | 23 | sensiolabs_security_checker: true 24 | 25 | php_loc: true 26 | 27 | php_hhvm: 28 | enabled: true 29 | command: hhvm 30 | extensions: 31 | - php 32 | 33 | php_analyzer: 34 | enabled: true 35 | extensions: 36 | - php 37 | config: 38 | parameter_reference_check: { enabled: false } 39 | checkstyle: { enabled: false } 40 | unreachable_code: { enabled: true } 41 | check_access_control: { enabled: false } 42 | typo_checks: { enabled: true } 43 | check_variables: { enabled: true } 44 | check_calls: { enabled: true, too_many_arguments: true, missing_argument: true, argument_type_checks: lenient } 45 | suspicious_code: { enabled: true, non_existent_class_in_instanceof_check: true, non_existent_class_in_catch_clause: true, non_commented_switch_fallthrough: true, non_commented_empty_catch_block: true, precedence_in_condition_assignment: true, overriding_parameter: false, overriding_closure_use: false, parameter_closure_use_conflict: false, parameter_multiple_times: false, assignment_of_null_return: false, overriding_private_members: false, use_statement_alias_conflict: false } 46 | dead_assignments: { enabled: true } 47 | verify_php_doc_comments: { enabled: true, parameters: true, return: true, suggest_more_specific_types: true, ask_for_return_if_not_inferrable: true, ask_for_param_type_annotation: true } 48 | loops_must_use_braces: { enabled: false } 49 | check_usage_context: { enabled: true, foreach: { value_as_reference: true, traversable: true } } 50 | simplify_boolean_return: { enabled: true } 51 | phpunit_checks: { enabled: false } 52 | reflection_checks: { enabled: false } 53 | precedence_checks: { enabled: true, assignment_in_condition: true, comparison_of_bit_result: true } 54 | basic_semantic_checks: { enabled: true } 55 | unused_code: { enabled: true } 56 | deprecation_checks: { enabled: true } 57 | useless_function_calls: { enabled: true } 58 | metrics_lack_of_cohesion_methods: { enabled: true } 59 | metrics_coupling: { enabled: true, stable_code: { namespace_prefixes: { }, classes: { } } } 60 | doctrine_parameter_binding: { enabled: false } 61 | doctrine_entity_manager_injection: { enabled: false } 62 | symfony_request_injection: { enabled: false } 63 | doc_comment_fixes: { enabled: false } 64 | reflection_fixes: { enabled: false } 65 | use_statement_fixes: { enabled: true, remove_unused: true, preserve_multiple: false, preserve_blanklines: false, order_alphabetically: false } 66 | 67 | # PHP Similarity Analyzer and Copy/paste Detector cannot be used at 68 | # the same time right now. Make sure to either remove, or disable one. 69 | php_cpd: false 70 | 71 | php_pdepend: true 72 | 73 | php_sim: 74 | enabled: true 75 | min_mass: 50 76 | 77 | checks: 78 | php: 79 | code_rating: true 80 | duplication: true 81 | -------------------------------------------------------------------------------- /tests/test-repeat-posts.php: -------------------------------------------------------------------------------- 1 | factory->post->create(); 14 | $this->assertFalse( is_repeating_post( $post_id ) ); 15 | 16 | save_post_repeating_status( $post_id, 'no' ); 17 | $this->assertFalse( is_repeating_post( $post_id ) ); 18 | 19 | } 20 | 21 | function test_setting_post_repeating_status_yes() { 22 | 23 | $post_id = $this->factory->post->create(); 24 | $this->assertFalse( is_repeating_post( $post_id ) ); 25 | 26 | save_post_repeating_status( $post_id, 'weekly' ); 27 | $this->assertTrue( is_repeating_post( $post_id ) ); 28 | 29 | } 30 | 31 | function test_setting_post_repeating_status_blank() { 32 | 33 | $post_id = $this->factory->post->create(); 34 | $this->assertFalse( is_repeating_post( $post_id ) ); 35 | 36 | save_post_repeating_status( $post_id, 'weekly' ); 37 | $this->assertTrue( is_repeating_post( $post_id ) ); 38 | 39 | save_post_repeating_status( $post_id ); 40 | $this->assertTrue( is_repeating_post( $post_id ) ); 41 | 42 | } 43 | 44 | function test_repeating_post_types_filter() { 45 | 46 | $this->assertContains( 'post', repeating_post_types() ); 47 | 48 | add_filter( 'hm_post_repeat_post_types', function( $post_types ) { 49 | $post_types[] = 'page'; 50 | return $post_types; 51 | } ); 52 | 53 | $this->assertContains( 'page', repeating_post_types() ); 54 | 55 | } 56 | 57 | function test_is_repeating_post_save_post_hook() { 58 | 59 | $post_id = $this->factory->post->create(); 60 | $this->assertFalse( is_repeating_post( $post_id ) ); 61 | 62 | $_POST['ID'] = $post_id; 63 | $_POST['hm-post-repeat'] = 'weekly'; 64 | $this->assertTrue( is_repeating_post( $post_id ) ); 65 | 66 | } 67 | 68 | function test_repeat_post_no() { 69 | 70 | $post_id = $this->factory->post->create(); 71 | $this->assertFalse( is_repeat_post( $post_id ) ); 72 | 73 | } 74 | 75 | function test_repeat_post_yes() { 76 | 77 | $parent_post_id = $this->factory->post->create(); 78 | $post_id = $this->factory->post->create( array( 'post_parent' => $parent_post_id ) ); 79 | 80 | $this->assertFalse( is_repeat_post( $post_id ) ); 81 | $this->assertEquals( $parent_post_id, get_post( $post_id )->post_parent ); 82 | 83 | save_post_repeating_status( $parent_post_id, 'weekly' ); 84 | 85 | $this->assertTrue( is_repeat_post( $post_id ) ); 86 | 87 | } 88 | 89 | function test_post_states() { 90 | 91 | global $post_states; 92 | 93 | $parent_post_id = $this->factory->post->create(); 94 | $post_id = $this->factory->post->create( array( 'post_parent' => $parent_post_id ) ); 95 | save_post_repeating_status( $parent_post_id, 'weekly' ); 96 | 97 | // Hack to allow us access to $post_states so we can test it 98 | add_filter( 'display_post_states', function( $states, $post ) { 99 | 100 | global $post_states; 101 | $post_states = $states; 102 | 103 | return $states; 104 | 105 | }, 11, 2 ); 106 | 107 | // We need to output buffer _post_states as it echo's 108 | ob_start(); 109 | _post_states( get_post( $post_id ) ); 110 | ob_end_clean(); 111 | 112 | $this->assertEquals( array( 'hm-post-repeat' => __( 'Repeat', 'hm-post-repeat' ) ), $post_states ); 113 | 114 | ob_start(); 115 | _post_states( get_post( $parent_post_id ) ); 116 | ob_end_clean(); 117 | 118 | $this->assertEquals( array( 'hm-post-repeat' => __( 'Repeating', 'hm-post-repeat' ) ), $post_states ); 119 | 120 | } 121 | 122 | function test_create_get_repeating_post() { 123 | 124 | $parent_post_id = $this->factory->post->create(); 125 | $post_id = $this->factory->post->create( array( 'post_parent' => $parent_post_id ) ); 126 | save_post_repeating_status( $parent_post_id, 'weekly' ); 127 | 128 | $this->assertEquals( $parent_post_id, get_repeating_post( $parent_post_id ) ); 129 | $this->assertEquals( $parent_post_id, get_repeating_post( $post_id ) ); 130 | 131 | } 132 | 133 | function test_create_repeat_post_from_not_repeating() { 134 | 135 | $post_id = $this->factory->post->create(); 136 | $this->assertFalse( create_next_repeat_post( $post_id ) ); 137 | $this->assertCount( 0, get_posts( array( 'post_status' => 'future' ) ) ); 138 | 139 | } 140 | 141 | function test_create_repeat_post_from_published_repeating_post() { 142 | 143 | $post_id = $this->factory->post->create(); 144 | save_post_repeating_status( $post_id, 'weekly' ); 145 | 146 | $this->assertCount( 0, get_posts( array( 'post_status' => 'future' ) ) ); 147 | 148 | $this->assertEquals( $post_id + 1, create_next_repeat_post( $post_id ) ); 149 | $this->assertCount( 1, get_posts( array( 'post_status' => 'future' ) ) ); 150 | 151 | // Prove that another repeat post isn't created 152 | $this->assertFalse( create_next_repeat_post( $post_id ) ); 153 | $this->assertCount( 1, get_posts( array( 'post_status' => 'future' ) ) ); 154 | 155 | } 156 | 157 | function test_create_repeat_post_from_unpublished_repeating_post() { 158 | 159 | $post_id = $this->factory->post->create( array( 'post_status' => 'draft' ) ); 160 | save_post_repeating_status( $post_id, 'weekly' ); 161 | 162 | $this->assertCount( 0, get_posts( array( 'post_status' => 'future' ) ) ); 163 | 164 | $this->assertFalse( create_next_repeat_post( $post_id ) ); 165 | $this->assertCount( 0, get_posts( array( 'post_status' => 'future' ) ) ); 166 | 167 | wp_update_post( array( 'ID' => $post_id, 'post_status' => 'pending' ) ); 168 | $this->assertFalse( create_next_repeat_post( $post_id ) ); 169 | $this->assertCount( 0, get_posts( array( 'post_status' => 'future' ) ) ); 170 | 171 | wp_update_post( array( 'ID' => $post_id, 'post_status' => 'custom' ) ); 172 | $this->assertFalse( create_next_repeat_post( $post_id ) ); 173 | $this->assertCount( 0, get_posts( array( 'post_status' => 'future' ) ) ); 174 | 175 | // future post statuses are converted to publish if the post date is in the past 176 | wp_update_post( array( 'ID' => $post_id, 'post_status' => 'future' ) ); 177 | $this->assertFalse( create_next_repeat_post( $post_id ) ); 178 | $this->assertCount( 1, get_posts( array( 'post_status' => 'future' ) ) ); 179 | 180 | } 181 | 182 | function test_create_repeat_post_from_repeating_post_publish_action() { 183 | 184 | $post_id = $this->factory->post->create( array( 'post_status' => 'draft' ) ); 185 | save_post_repeating_status( $post_id, 'weekly' ); 186 | 187 | $this->assertCount( 0, get_posts( array( 'post_status' => 'future' ) ) ); 188 | 189 | wp_update_post( array( 'ID' => $post_id, 'post_status' => 'publish' ) ); 190 | $this->assertCount( 1, get_posts( array( 'post_status' => 'future' ) ) ); 191 | 192 | } 193 | 194 | function test_create_repeat_unsupported_repeating_post_type() { 195 | 196 | $post_id = $this->factory->post->create( array( 'post_type' => 'middle-out-encryption' ) ); 197 | save_post_repeating_status( $post_id, 'weekly' ); 198 | 199 | $this->assertFalse( create_next_repeat_post( $post_id ) ); 200 | 201 | } 202 | 203 | function test_create_repeat_post_copies_meta() { 204 | 205 | $post_id = $this->factory->post->create(); 206 | save_post_repeating_status( $post_id, 'weekly' ); 207 | 208 | $meta = array( 'NonEmptyString' => 'Test', 'Int' => 134, 'EmptyString' => '', 'bool' => true, 'object' => get_post( $post_id ), 'array' => get_post( $post_id, ARRAY_A ) ); 209 | 210 | foreach ( $meta as $key => $value ) { 211 | add_post_meta( $post_id, $key, $value ); 212 | } 213 | 214 | $post_meta = get_post_meta( $post_id ); 215 | 216 | // `hm-post-repeat` isn't copied over 217 | unset( $post_meta['hm-post-repeat'] ); 218 | 219 | $repeat_post_id = create_next_repeat_post( $post_id ); 220 | $repeat_post_meta = get_post_meta( $repeat_post_id ); 221 | $this->assertEquals( $post_meta, $repeat_post_meta ); 222 | 223 | } 224 | 225 | function test_create_repeat_post_copies_terms() { 226 | 227 | $post_id = $this->factory->post->create(); 228 | $this->factory->term->add_post_terms( $post_id, array( 'Tag 1', 'Tag 2' ), 'post_tag' ); 229 | 230 | $cat1 = $this->factory->term->create_object( array( 'taxonomy' => 'category', 'name' => 'Cat 1' ) ); 231 | $cat2 = $this->factory->term->create_object( array( 'taxonomy' => 'category', 'name' => 'Cat 2' ) ); 232 | 233 | $this->factory->term->add_post_terms( $post_id, array( $cat1, $cat2 ), 'category' ); 234 | 235 | $this->assertTrue( has_tag( 'Tag 1', $post_id ) ); 236 | $this->assertTrue( has_tag( 'Tag 2', $post_id ) ); 237 | 238 | $this->assertTrue( has_category( $cat1, $post_id ) ); 239 | $this->assertTrue( has_category( $cat2, $post_id ) ); 240 | 241 | save_post_repeating_status( $post_id, 'weekly' ); 242 | 243 | $repeat_post_id = create_next_repeat_post( $post_id ); 244 | 245 | $this->assertTrue( has_tag( 'Tag 1', $repeat_post_id ) ); 246 | $this->assertTrue( has_tag( 'Tag 2', $repeat_post_id ) ); 247 | 248 | $this->assertTrue( has_category( $cat1, $repeat_post_id ) ); 249 | $this->assertTrue( has_category( $cat2, $repeat_post_id ) ); 250 | 251 | } 252 | 253 | /** 254 | * Specifically test that the repeating status is saved to post meta and the next 255 | * repeat post is created & scheduled when publishing a new repeating post 256 | */ 257 | function test_publish_repeating_post_creates_repeat_post() { 258 | 259 | $_POST['hm-post-repeat'] = 'weekly'; 260 | $post_id = $this->factory->post->create(); 261 | $this->assertTrue( is_repeating_post( $post_id ) ); 262 | 263 | $this->assertCount( 1, get_posts( array( 'post_status' => 'future' ) ) ); 264 | 265 | } 266 | 267 | function test_repeating_post_interval_invalid() { 268 | 269 | $_POST['hm-post-repeat'] = 'some-day'; 270 | $post = $this->factory->post->create_and_get(); 271 | $this->assertFalse( is_repeating_post( $post->ID ) ); 272 | 273 | $future_posts = get_posts( array( 'post_status' => 'future' ) ); 274 | $this->assertCount( 0, $future_posts ); 275 | 276 | } 277 | 278 | function test_add_custom_repeating_schedule() { 279 | 280 | add_filter( 'hm_post_repeat_schedules', function( $schedules ) { 281 | 282 | $schedules['yearly'] = array( 'interval' => '1 year', 'display' => 'Yearly' ); 283 | return $schedules; 284 | 285 | } ); 286 | 287 | $this->assertTrue( key_exists( 'yearly', get_repeating_schedules() ) ); 288 | 289 | } 290 | 291 | function test_repeating_post_interval_custom() { 292 | 293 | add_filter( 'hm_post_repeat_schedules', function( $schedules ) { 294 | 295 | $schedules['3-days'] = array( 'interval' => '3 days', 'display' => 'Every 3 days' ); 296 | return $schedules; 297 | 298 | } ); 299 | 300 | $_POST['hm-post-repeat'] = '3-days'; 301 | $post = $this->factory->post->create_and_get(); 302 | $this->assertTrue( is_repeating_post( $post->ID ) ); 303 | 304 | $future_posts = get_posts( array( 'post_status' => 'future' ) ); 305 | $this->assertCount( 1, $future_posts ); 306 | 307 | $repeat_post = reset( $future_posts ); 308 | $this->assertTrue( is_repeat_post( $repeat_post->ID ) ); 309 | 310 | $next_post_date = date( 'Y-m-d H:i:s', strtotime( $post->post_date . ' + 3 days' ) ); 311 | $this->assertSame( $repeat_post->post_date, $next_post_date ); 312 | 313 | } 314 | 315 | function test_repeating_post_interval_daily() { 316 | 317 | $_POST['hm-post-repeat'] = 'daily'; 318 | $post = $this->factory->post->create_and_get(); 319 | $this->assertTrue( is_repeating_post( $post->ID ) ); 320 | 321 | $future_posts = get_posts( array( 'post_status' => 'future' ) ); 322 | $this->assertCount( 1, $future_posts ); 323 | 324 | $repeat_post = reset( $future_posts ); 325 | $this->assertTrue( is_repeat_post( $repeat_post->ID ) ); 326 | 327 | $next_post_date = date( 'Y-m-d H:i:s', strtotime( $post->post_date . ' + 1 day' ) ); 328 | $this->assertSame( $repeat_post->post_date, $next_post_date ); 329 | 330 | } 331 | 332 | function test_repeating_post_interval_weekly() { 333 | 334 | $_POST['hm-post-repeat'] = 'weekly'; 335 | $post = $this->factory->post->create_and_get(); 336 | $this->assertTrue( is_repeating_post( $post->ID ) ); 337 | 338 | $future_posts = get_posts( array( 'post_status' => 'future' ) ); 339 | $this->assertCount( 1, $future_posts ); 340 | 341 | $repeat_post = reset( $future_posts ); 342 | $this->assertTrue( is_repeat_post( $repeat_post->ID ) ); 343 | 344 | $next_post_date = date( 'Y-m-d H:i:s', strtotime( $post->post_date . ' + 1 week' ) ); 345 | $this->assertSame( $repeat_post->post_date, $next_post_date ); 346 | 347 | } 348 | 349 | function test_repeating_post_interval_fortnightly() { 350 | 351 | $_POST['hm-post-repeat'] = 'fortnightly'; 352 | $post = $this->factory->post->create_and_get(); 353 | $this->assertTrue( is_repeating_post( $post->ID ) ); 354 | 355 | $future_posts = get_posts( array( 'post_status' => 'future' ) ); 356 | $this->assertCount( 1, $future_posts ); 357 | 358 | $repeat_post = reset( $future_posts ); 359 | $this->assertTrue( is_repeat_post( $repeat_post->ID ) ); 360 | 361 | $next_post_date = date( 'Y-m-d H:i:s', strtotime( $post->post_date . ' + 2 weeks' ) ); 362 | $this->assertSame( $repeat_post->post_date, $next_post_date ); 363 | 364 | } 365 | 366 | function test_repeating_post_interval_monthly() { 367 | 368 | $_POST['hm-post-repeat'] = 'monthly'; 369 | $post = $this->factory->post->create_and_get(); 370 | $this->assertTrue( is_repeating_post( $post->ID ) ); 371 | 372 | $future_posts = get_posts( array( 'post_status' => 'future' ) ); 373 | $this->assertCount( 1, $future_posts ); 374 | 375 | $repeat_post = reset( $future_posts ); 376 | $this->assertTrue( is_repeat_post( $repeat_post->ID ) ); 377 | 378 | $next_post_date = date( 'Y-m-d H:i:s', strtotime( $post->post_date . ' + 1 month' ) ); 379 | $this->assertSame( $repeat_post->post_date, $next_post_date ); 380 | 381 | } 382 | 383 | /** 384 | * This test assumes that the meta data was invalidly set directly in the database. 385 | */ 386 | function test_get_repeating_schedule_invalid_direct_db_entry() { 387 | 388 | $post_id = $this->factory->post->create(); 389 | add_post_meta( $post_id, 'hm-post-repeat', 'some-day' ); 390 | $this->assertNull( get_repeating_schedule( $post_id ) ); 391 | 392 | } 393 | 394 | function test_get_repeating_schedule_invalid() { 395 | 396 | $_POST['hm-post-repeat'] = 'some-day'; 397 | $post_id = $this->factory->post->create(); 398 | $this->assertNull( get_repeating_schedule( $post_id ) ); 399 | 400 | } 401 | 402 | /** 403 | * This test assumes that the meta data was correctly set directly in the database. 404 | */ 405 | function test_get_repeating_schedule_valid_direct_db_entry() { 406 | 407 | $post_id = $this->factory->post->create(); 408 | add_post_meta( $post_id, 'hm-post-repeat', 'daily' ); 409 | $this->assertSame( array( 410 | 'interval' => '1 day', 411 | 'display' => 'Daily', 412 | 'slug' => 'daily', 413 | ), get_repeating_schedule( $post_id ) ); 414 | 415 | } 416 | 417 | function test_get_repeating_schedule_valid() { 418 | 419 | $_POST['hm-post-repeat'] = 'daily'; 420 | $post_id = $this->factory->post->create(); 421 | $this->assertSame( array( 422 | 'interval' => '1 day', 423 | 'display' => 'Daily', 424 | 'slug' => 'daily', 425 | ), get_repeating_schedule( $post_id ) ); 426 | 427 | } 428 | 429 | /** 430 | * This method assumes an existing old schedule format in the post meta. 431 | */ 432 | function test_get_repeating_schedule_backwards_compatible_old() { 433 | 434 | $post_id = $this->factory->post->create(); 435 | add_post_meta( $post_id, 'hm-post-repeat', '1' ); 436 | $this->assertSame( array( 437 | 'interval' => '1 week', 438 | 'display' => 'Weekly', 439 | 'slug' => 'weekly', 440 | ), get_repeating_schedule( $post_id ) ); 441 | 442 | } 443 | 444 | /** 445 | * Tests that a repeat post is modified via filter before being saved/scheduled. 446 | * Only repeat posts with a specific schedule are modified. 447 | */ 448 | function test_edit_repeat_post_before_scheduling() { 449 | 450 | // Edit repeat post title if it's on weekly schedule. 451 | add_filter( 'hm_post_repeat_edit_repeat_post', function( $next_post, $repeating_schedule, $original_post ) { 452 | 453 | if ( $repeating_schedule['slug'] === 'weekly' ) { 454 | $next_post['post_title'] = 'Repeat Post Week 1, 2018'; 455 | } 456 | 457 | return $next_post; 458 | }, 10, 3 ); 459 | 460 | // Create main repeating post and schedule to repeat it weekly. 461 | $_POST['hm-post-repeat'] = 'weekly'; 462 | $post_id = $this->factory->post->create( array( 463 | 'post_title' => 'Repeating Post', 464 | 'post_status' => 'publish', 465 | ) ); 466 | 467 | // Check repeating post is there with its original title. 468 | $this->assertTrue( is_repeating_post( $post_id ) ); 469 | $this->assertSame( get_the_title( $post_id ) , 'Repeating Post' ); 470 | 471 | // Check repeat post is scheduled with a new title. 472 | $repeat_posts = get_posts( array( 'post_status' => 'future' ) ); 473 | 474 | $this->assertNotEmpty( $repeat_posts ); 475 | $this->assertTrue( is_repeat_post( $repeat_posts[0]->ID ) ); 476 | $this->assertSame( $repeat_posts[0]->post_title, 'Repeat Post Week 1, 2018' ); 477 | } 478 | 479 | } 480 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | -------------------------------------------------------------------------------- /hm-post-repeat.php: -------------------------------------------------------------------------------- 1 | 83 | 84 |
85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | post_parent ) ) . '">' . esc_html( get_the_title( get_post_field( 'post_parent', get_the_id() ) ) ) . '' ); ?> 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 |
119 | 120 | ID ) ) { 134 | 135 | // If the schedule has been removed since publishing, let the user know. 136 | if ( get_repeating_schedule( $post->ID ) ) { 137 | $post_states['hm-post-repeat'] = __( 'Repeating', 'hm-post-repeat' ); 138 | } else { 139 | $post_states['hm-post-repeat'] = __( 'Invalid Repeating Schedule', 'hm-post-repeat' ); 140 | } 141 | 142 | } 143 | 144 | if ( is_repeat_post( $post->ID ) ) { 145 | $post_states['hm-post-repeat'] = __( 'Repeat', 'hm-post-repeat' ); 146 | } 147 | 148 | return $post_states; 149 | } 150 | 151 | /** 152 | * Save the repeating status to post meta. 153 | * 154 | * Hooked into `save_post`. When saving a post that has been set to repeat we save a post meta entry. 155 | * 156 | * @param int $post_id The ID of the post. 157 | * @param string $post_repeat_setting Used to manually set the repeating schedule from tests. 158 | */ 159 | function save_post_repeating_status( $post_id = null, $post_repeat_setting = null ) { 160 | 161 | if ( is_null( $post_repeat_setting ) ) { 162 | $post_repeat_setting = isset( $_POST['hm-post-repeat'] ) ? sanitize_text_field( $_POST['hm-post-repeat'] ) : ''; 163 | } 164 | 165 | if ( ! in_array( get_post_type( $post_id ), repeating_post_types() ) || empty( $post_repeat_setting ) ) { 166 | return; 167 | } 168 | 169 | if ( 'no' === $post_repeat_setting ) { 170 | delete_post_meta( $post_id, 'hm-post-repeat' ); 171 | } 172 | 173 | // Make sure we have a valid schedule. 174 | elseif ( in_array( $post_repeat_setting, array_keys( get_repeating_schedules() ) ) ) { 175 | update_post_meta( $post_id, 'hm-post-repeat', $post_repeat_setting ); 176 | } 177 | 178 | } 179 | 180 | 181 | /** 182 | * Create the next repeat post when the last one is published. 183 | * 184 | * When a repeat post (or the original) is published we copy and schedule a new post 185 | * to publish on the correct interval. That way the next repeat post is always ready to go. 186 | * This is hooked into publish_post so that the repeat post is only created when the original 187 | * is published. 188 | * 189 | * @param int $post_id The ID of the post. 190 | */ 191 | function create_next_repeat_post( $post_id ) { 192 | 193 | if ( ! in_array( get_post_type( $post_id ), repeating_post_types() ) ) { 194 | return false; 195 | } 196 | 197 | if ( 'publish' !== get_post_status( $post_id ) ) { 198 | return false; 199 | } 200 | 201 | $original_post_id = get_repeating_post( $post_id ); 202 | 203 | // Bail if we're not publishing a repeat(ing) post 204 | if ( ! $original_post_id ) { 205 | return false; 206 | } 207 | 208 | $original_post = get_post( $original_post_id, ARRAY_A ); 209 | 210 | // If there is already a repeat post scheduled don't create another one 211 | if ( get_next_scheduled_repeat_post( $original_post['ID'] ) ) { 212 | return false; 213 | } 214 | 215 | // Bail if the saved schedule doesn't exist 216 | $repeating_schedule = get_repeating_schedule( $original_post['ID'] ); 217 | 218 | if ( ! $repeating_schedule ) { 219 | return false; 220 | } 221 | 222 | // Bail if the original post isn't already published 223 | if ( 'publish' !== $original_post['post_status'] ) { 224 | return false; 225 | } 226 | 227 | $next_post = $original_post; 228 | 229 | // Create the repeat post as a copy of the original, but ignore some fields 230 | unset( $next_post['ID'] ); 231 | unset( $next_post['guid'] ); 232 | unset( $next_post['post_date_gmt'] ); 233 | unset( $next_post['post_modified'] ); 234 | unset( $next_post['post_modified_gmt'] ); 235 | 236 | // We set the post_parent to the original post_id, so they're related 237 | $next_post['post_parent'] = $original_post['ID']; 238 | 239 | // Set the next post to publish in the future 240 | $next_post['post_status'] = 'future'; 241 | 242 | // Use the date of the current post being saved as the base 243 | $next_post['post_date'] = date( 'Y-m-d H:i:s', strtotime( get_post_field( 'post_date', $post_id ) . ' + ' . $repeating_schedule['interval'] ) ); 244 | 245 | // Make sure the next post will be in the future 246 | if ( strtotime( $next_post['post_date'] ) <= time() ) { 247 | return false; 248 | } 249 | 250 | /** 251 | * Use this filter to modify the scheduled post before it gets stored. 252 | * 253 | * @param array $next_post The post data about to be saved. 254 | * @param array $repeating_schedule Repeating schedule info. 255 | * @param array $original_post The original repeating post. 256 | */ 257 | $next_post = apply_filters( 'hm_post_repeat_edit_repeat_post', $next_post, $repeating_schedule, $original_post ); 258 | 259 | // All checks done, get that post scheduled! 260 | $next_post_id = wp_insert_post( wp_slash( $next_post ), true ); 261 | 262 | if ( is_wp_error( $next_post_id ) ) { 263 | return false; 264 | } 265 | 266 | // Mirror any post_meta 267 | $post_meta = get_post_meta( $original_post['ID'] ); 268 | 269 | if ( $post_meta ) { 270 | 271 | // Ignore some internal meta fields 272 | unset( $post_meta['_edit_lock'] ); 273 | unset( $post_meta['_edit_last'] ); 274 | 275 | // Don't copy the post repeat meta as only the original post should have that 276 | unset( $post_meta['hm-post-repeat'] ); 277 | 278 | foreach ( $post_meta as $key => $values ) { 279 | foreach ( $values as $value ) { 280 | add_post_meta( $next_post_id, $key, maybe_unserialize( $value ) ); 281 | } 282 | } 283 | } 284 | 285 | // Mirror any term relationships 286 | $taxonomies = get_object_taxonomies( $original_post['post_type'] ); 287 | 288 | foreach ( $taxonomies as $taxonomy ) { 289 | wp_set_object_terms( $next_post_id, wp_list_pluck( wp_get_object_terms( $original_post['ID'], $taxonomy ), 'slug' ), $taxonomy ); 290 | } 291 | 292 | return $next_post_id; 293 | 294 | } 295 | 296 | /** 297 | * The post types the feature is enabled on 298 | * 299 | * By default only posts have the feature enabled but others can be added with the `hm_post_repeat_post_types` filter. 300 | * 301 | * @return array An array of post types 302 | */ 303 | function repeating_post_types() { 304 | 305 | /** 306 | * Enable support for additional post types. 307 | * 308 | * @param string[] $post_types Post type slugs. 309 | */ 310 | return apply_filters( 'hm_post_repeat_post_types', array( 'post' ) ); 311 | 312 | } 313 | 314 | /** 315 | * All available repeat schedules. 316 | * 317 | * @return array An array of all available repeat schedules 318 | */ 319 | function get_repeating_schedules() { 320 | 321 | /** 322 | * Enable support for additional schedules. 323 | * 324 | * @param array[] $schedules Schedule array items. 325 | */ 326 | $schedules = apply_filters( 'hm_post_repeat_schedules', array( 327 | 'daily' => array( 'interval' => '1 day', 'display' => __( 'Daily', 'hm-post-repeat' ) ), 328 | 'weekly' => array( 'interval' => '1 week', 'display' => __( 'Weekly', 'hm-post-repeat' ) ), 329 | 'fortnightly' => array( 'interval' => '2 weeks', 'display' => __( 'Fortnightly', 'hm-post-repeat' ) ), 330 | 'monthly' => array( 'interval' => '1 month', 'display' => __( 'Monthly', 'hm-post-repeat' ) ), 331 | ) ); 332 | 333 | foreach ( $schedules as $slug => &$schedule ) { 334 | $schedule['slug'] = $slug; 335 | } 336 | 337 | return $schedules; 338 | 339 | } 340 | 341 | /** 342 | * Get the repeating schedule of the given post_id. 343 | * 344 | * @param int $post_id The id of the post you want to check. 345 | * @return array|null The schedule to repeat by, or null if invalid. 346 | */ 347 | function get_repeating_schedule( $post_id ) { 348 | 349 | if ( ! is_repeating_post( $post_id ) ) { 350 | return; 351 | } 352 | 353 | $repeating_schedule = get_post_meta( $post_id, 'hm-post-repeat', true ); 354 | $schedules = get_repeating_schedules(); 355 | 356 | // Backwards compatibility with 0.3 when we only supported weekly 357 | if ( '1' === $repeating_schedule ) { 358 | $repeating_schedule = 'weekly'; 359 | } 360 | 361 | if ( array_key_exists( $repeating_schedule, $schedules ) ) { 362 | return $schedules[ $repeating_schedule ]; 363 | } 364 | 365 | } 366 | 367 | /** 368 | * Check whether a given post_id is a repeating post. 369 | * 370 | * A repeating post is defined as the original post that was set to repeat. 371 | * 372 | * @param int $post_id The id of the post you want to check. 373 | * @return bool Whether the passed post_id is a repeating post or not. 374 | */ 375 | function is_repeating_post( $post_id ) { 376 | 377 | // We check $_POST data so that this function works inside a `save_post` hook when the post_meta hasn't yet been saved 378 | if ( isset( $_POST['hm-post-repeat'] ) && isset( $_POST['ID'] ) && $_POST['ID'] === $post_id ) { 379 | return true; 380 | } 381 | 382 | if ( get_post_meta( $post_id, 'hm-post-repeat', true ) ) { 383 | return true; 384 | } 385 | 386 | return false; 387 | 388 | } 389 | 390 | /** 391 | * Check whether a given post_id is a repeat post. 392 | * 393 | * A repeat post is defined as any post which is a repeat of the original repeating post. 394 | * 395 | * @param int $post_id The id of the post you want to check. 396 | * @return bool Whether the passed post_id is a repeat post or not. 397 | */ 398 | function is_repeat_post( $post_id ) { 399 | 400 | $post_parent = get_post_field( 'post_parent', $post_id ); 401 | 402 | if ( $post_parent && get_post_meta( $post_parent, 'hm-post-repeat', true ) ) { 403 | return true; 404 | } 405 | 406 | return false; 407 | 408 | } 409 | 410 | /** 411 | * Get the next scheduled repeat post 412 | * 413 | * @param int $post_id The id of a repeat or repeating post 414 | * @return Int|Bool Return the ID of the next repeat post_id or false if it can't find one 415 | */ 416 | function get_next_scheduled_repeat_post( $post_id ) { 417 | 418 | $post = get_post( get_repeating_post( $post_id ) ); 419 | 420 | $repeat_posts = get_posts( array( 'post_status' => 'future', 'post_parent' => $post->ID ) ); 421 | 422 | if ( isset( $repeat_posts[0] ) ) { 423 | return $repeat_posts[0]; 424 | } 425 | 426 | return false; 427 | 428 | } 429 | 430 | /** 431 | * Get the next scheduled repeat post 432 | * 433 | * @param int $post_id The id of a repeat or repeating post 434 | * @return Int|Bool Return the original repeating post_id or false if it can't find it 435 | */ 436 | function get_repeating_post( $post_id ) { 437 | 438 | $original_post_id = false; 439 | 440 | // Are we publishing a repeat post 441 | if ( is_repeat_post( $post_id ) ) { 442 | $original_post_id = get_post( $post_id )->post_parent; 443 | } 444 | 445 | // Or the original 446 | elseif ( is_repeating_post( $post_id ) ) { 447 | $original_post_id = $post_id; 448 | } 449 | 450 | return $original_post_id; 451 | 452 | } 453 | 454 | /** 455 | * Adds admin table view link per available repeat type. 456 | * So that only all posts of specific repeat type are displayed. 457 | * 458 | * Added at the end of link list for All | Mine | Published | Scheduled | Drafts 459 | * 460 | * @param array $views An array of available list table views. 461 | * 462 | * @return array Available list of table views with custom added views per repeat type. 463 | */ 464 | function admin_table_views_links( $views ) { 465 | 466 | // Add status link for each repeat type. 467 | foreach ( get_available_repeat_types() as $repeat_type => $repeat_desc ) { 468 | 469 | $url_args = array( 470 | 'post_type' => get_current_screen()->post_type, 471 | 'hm-post-repeat' => $repeat_type, 472 | ); 473 | 474 | // Custom WP_Query to get posts count of repeat type. 475 | $repeat_type_query = new \WP_Query( get_repeat_type_query_params( $repeat_type ) ); 476 | 477 | $link_label = sprintf( 478 | '%s (%s)', 479 | esc_html( $repeat_desc ), 480 | esc_html( number_format_i18n( $repeat_type_query->post_count ) ) 481 | ); 482 | 483 | // Add current class to the link to highlight it when it's selected. 484 | $class_html = ( get_repeat_type_url_param() === $repeat_type ) ? ' class="current"' : ''; 485 | 486 | $link_html = sprintf( 487 | '%s', 488 | esc_url( add_query_arg( $url_args, 'edit.php' ) ), 489 | $class_html, // html - hardcoded, no need to escape. 490 | $link_label // html - escaped earlier in the code. 491 | ); 492 | 493 | $views[ $repeat_type ] = $link_html; 494 | } 495 | 496 | return $views; 497 | } 498 | 499 | /** 500 | * Customizes main admin query to get posts of specified repeat type 501 | * to be displayed in the admin table. 502 | * 503 | * @param \WP_Query $wp_query Main admin query. 504 | * 505 | * @return mixed Main admin query with edited params to get posts of specified repeat type. 506 | */ 507 | function admin_table_repeat_type_posts_query( $wp_query ) { 508 | 509 | // Stop - if not admin or not main query (there are secondary WP_Query for counting posts for view links, etc). 510 | if ( ! is_admin() || ! $wp_query->is_main_query() ) { 511 | return $wp_query; 512 | } 513 | 514 | // Get URL query param for repeat type and check it's valid. 515 | $repeat_type = get_repeat_type_url_param(); 516 | 517 | if ( ! $repeat_type || ! is_allowed_repeat_type( $repeat_type ) ) { 518 | return $wp_query; 519 | } 520 | 521 | // Add a table view link per each repeat type. 522 | foreach ( get_repeat_type_query_params( $repeat_type ) as $key => $value ) { 523 | $wp_query->set( $key, $value ); 524 | } 525 | 526 | return $wp_query; 527 | } 528 | 529 | /** 530 | * Returns array of custom WP_Query params to get posts of specified repeat type. 531 | * Works for all CPT that support Repeatable Posts. 532 | * 533 | * @param string $repeat_type Repeat type of posts to get. 534 | * 535 | * @return array Array of custom WP_Query params. 536 | */ 537 | function get_repeat_type_query_params( $repeat_type ) { 538 | 539 | $query['post_type'] = get_current_screen()->post_type; 540 | 541 | // Construct custom query to get posts of specified repeat type. 542 | $query['meta_query'] = array( 543 | array( 544 | 'key' => 'hm-post-repeat', 545 | 'compare' => 'EXISTS', 546 | ), 547 | ); 548 | 549 | if ( $repeat_type === 'repeat' ) { 550 | $query['post_parent__not_in'] = array( 0 ); 551 | 552 | } elseif ( $repeat_type === 'repeating' ) { 553 | $query['post_parent__in'] = array( 0 ); 554 | } 555 | 556 | return $query; 557 | } 558 | 559 | /** 560 | * Get URL query param for the repeat type of posts being displayed 561 | * in the admin post table. 562 | * 563 | * @return string Sanitized string of repeat type being displayed. 564 | */ 565 | function get_repeat_type_url_param() { 566 | return isset( $_GET['hm-post-repeat'] ) ? sanitize_text_field( $_GET['hm-post-repeat'] ) : ''; 567 | } 568 | 569 | /** 570 | * Return available repeat types, i.e. repeating or repeat. 571 | * 572 | * @return array Available repeat types. 573 | */ 574 | function get_available_repeat_types() { 575 | return array( 576 | 'repeating' => __( 'Repeating', 'hm-post-repeat' ), 577 | 'repeat' => __( 'Repeat', 'hm-post-repeat' ), 578 | ); 579 | } 580 | 581 | /** 582 | * Check if a repeat type is valid. 583 | * 584 | * @param string $repeat_type Repeat type to check. 585 | * 586 | * @return bool True if repeat type is valid, 587 | * False otherwise. 588 | */ 589 | function is_allowed_repeat_type( $repeat_type ) { 590 | return in_array( $repeat_type, array_keys( get_available_repeat_types() ) ); 591 | } 592 | --------------------------------------------------------------------------------