Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Oct 24, 2024
1 parent 4b5f57a commit 017990f
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 1 deletion.
23 changes: 23 additions & 0 deletions admin/page-settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Admin page settings
*
* @package Pronamic\WordPressCloudflare
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>

<form method="post" action="options.php">
<?php settings_fields( 'pronamic_post_expiration' ); ?>

<?php do_settings_sections( 'pronamic_post_expiration' ); ?>

<?php submit_button(); ?>
</form>
</div>
71 changes: 71 additions & 0 deletions admin/settings-field-post-types-support.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Settings field post types
*
* @author Pronamic <[email protected]>
* @copyright 2005-2024 Pronamic
* @license GPL-2.0-or-later
* @package Pronamic\WordPressCloudflare
*/

$post_type_objects = get_post_types(
[
'public' => true,
],
'objects'
);

?>
<style type="text/css">
.form-table .widefat th,
.form-table .widefat td {
padding: 8px 10px;
}
</style>

<table class="widefat">
<thead>
<tr>
<th><?php esc_html_e( 'Post type', 'pronamic-post-expiration' ); ?></th>
<th><?php esc_html_e( 'Label', 'pronamic-post-expiration' ); ?></th>
<th><?php esc_html_e( 'Support', 'pronamic-post-expiration' ); ?></th>
</tr>
</thead>

<tbody>

<?php foreach ( $post_type_objects as $post_type_info ) : ?>

<tr>
<td>
<code><?php echo \esc_html( $post_type_info->name ); ?></code>
</td>
<td>
<?php echo \esc_html( $post_type_info->label ); ?>
</td>
<td>
<?php

$supports = \get_all_post_type_supports( $post_type_info->name );

$checked = false;
$disabled = false;

if ( post_type_supports( $post_type_info->name, 'expiration' ) ) {
$checked = true;
$disabled = true;

if ( isset( $supports['expiration']['source'] ) ) {
$disabled = ( 'option' !== $supports['expiration']['source'] );
}
}

?>
<input type="checkbox" name="pronamic_post_expiration_post_types[]" value="<?php echo \esc_attr( $post_type_info->name ); ?>" <?php checked( $checked ); ?> <?php disabled( $disabled ); ?> />
</td>
</tr>

<?php endforeach; ?>

</tbody>
</table>
26 changes: 25 additions & 1 deletion php/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static function instance() {
*/
private function __construct() {
$this->controllers = [
new SettingsController(),
new YoastSeoSchemaController(),
];
}
Expand All @@ -58,6 +59,7 @@ private function __construct() {
*/
public function setup() {
\add_action( 'init', [ $this, 'init' ] );
\add_action( 'init', [ $this, 'add_post_type_support_by_option' ], 9000 );

\add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] );

Expand Down Expand Up @@ -91,8 +93,22 @@ public function init() {
'show_in_admin_status_list' => true,
]
);
}

\add_post_type_support( 'post', 'expiration' );
/**
* Add post type support by option.
*
* @return void
*/
public function add_post_type_support_by_option() {
$post_types = \get_option( 'pronamic_post_expiration_post_types' );
$post_types = \wp_parse_list( $post_types );

foreach ( $post_types as $post_type ) {
if ( ! \post_type_supports( $post_type, 'expiration' ) ) {
\add_post_type_support( $post_type, 'expiration', ...[ 'source' => 'option' ] );
}
}
}

/**
Expand Down Expand Up @@ -233,6 +249,10 @@ private function maybe_schedule_expiration_event( $post_id, $meta_value = null )
return;
}

if ( ! \post_type_supports( \get_post_type( $post_id ), 'expiration' ) ) {
return;
}

\as_unschedule_action(
'pronamic_expire_post',
[
Expand Down Expand Up @@ -318,6 +338,10 @@ public function expire_post( $post_id ) {
return;
}

if ( ! \post_type_supports( \get_post_type( $post_id ), 'expiration' ) ) {
return;
}

$result = \wp_update_post(
[
'ID' => $post_id,
Expand Down
77 changes: 77 additions & 0 deletions php/SettingsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Settings controller
*
* @author Pronamic <[email protected]>
* @copyright 2005-2024 Pronamic
* @license GPL-2.0-or-later
* @package Pronamic\PostExpiration
*/

namespace Pronamic\PostExpiration;

/**
* Settings controller class
*/
final class SettingsController {
/**
* Setup.
*/
public function setup() {
\add_action( 'init', [ $this, 'init' ] );

\add_action( 'admin_init', [ $this, 'admin_init' ] );

\add_action( 'admin_menu', [ $this, 'admin_menu' ] );
}

/**
* Initialize.
*/
public function init() {
\register_setting(
'pronamic_post_expiration',
'pronamic_post_expiration_post_types'
);
}

/**
* Admin initialize.
*/
public function admin_init() {
\add_settings_section(
'pronamic_post_expiration_general',
\__( 'General', 'pronamic-post-expiration' ),
function () { },
'pronamic_post_expiration'
);

\add_settings_field(
'pronamic_post_expiration_post_types',
\__( 'Post types', 'pronamic-moneybird' ),
function () {
include __DIR__ . '/../admin/settings-field-post-types-support.php';
},
'pronamic_post_expiration',
'pronamic_post_expiration_general'
);
}

/**
* Admin menu.
*
* @link https://developer.wordpress.org/reference/functions/add_options_page/
* @return void
*/
public function admin_menu() {
\add_options_page(
\__( 'Pronamic Post Expiration', 'pronamic-post-expiration' ),
\__( 'Pronamic Post Expiration', 'pronamic-post-expiration' ),
'manage_options',
'pronamic_post_expiration',
function () {
include __DIR__ . '/../admin/page-settings.php';
}
);
}
}

0 comments on commit 017990f

Please sign in to comment.