lewtilities/classes/init.php
2023-02-25 11:08:48 +00:00

99 lines
2.8 KiB
PHP

<?php
global $enqueued_scripts;
class Lewtilities {
public function __construct()
{
$this->init();
}
public function init() {
add_action('admin_menu', array ( $this, 'submenu_page' ));
add_action('init', array ( $this, 'on_init' ));
add_action( 'rest_api_init', array( $this, 'add_md_to_rest' ));
add_filter( 'share_on_mastodon_status', array($this, 'format_post_for_mastodon'), 10, 2 );
}
public function submenu_page() {
add_menu_page( 'Lewtilities', 'Lewtilities', 'manage_options', 'lewtilities-main-menu',
array( $this, 'admin_styles' ), '', 84 );
add_submenu_page( 'lewtilities-main-menu', __( 'Dashboard', 'wp-ld' ),
__( 'Dashboard', 'wp-ld' ), 'manage_options',
'lewtilities-main-menu', array( $this, 'admin_styles' ) );
}
public function admin_settings() {
// echo "Settings!";
}
public function admin_styles() {
echo "Admin styles section";
}
public function admin_options_page() {
add_menu_page(
'Lewtilities',
'Lewtilities',
'manage_options',
'lewtilities',
array( $this, 'admin_options_page_display' )
);
}
public function admin_options_page_display() {
echo "Options page";
}
public function rewrite_well_known() {
add_rewrite_rule('^\.well-known/webfinger$', 'https://dapchat.online/.well-known/webfinger?resource=acct:lewisdaleuk@dapchat.online');
}
public function add_md_to_rest() {
register_rest_field( 'post', 'markdown', array(
'get_callback' => function( $post_arr ) {
return $content = html_entity_decode(get_post_field('post_content_filtered', $post_arr['id'], 'edit'), ENT_QUOTES | ENT_HTML5, 'UTF-8');
},
'update_callback' => function( $karma, $comment_obj ) {
return true;
},
'schema' => array(
'description' => __( 'Markdown content.' ),
'type' => 'string'
),
) );
}
public function on_init() {
add_action('save_post', array( $this, 'trigger_frontend_build' ));
add_action('after_delete_post', array( $this, 'trigger_frontend_build' ));
add_action('lewtilities_build_hook', array( $this, 'build' ));
if ( ! wp_next_scheduled( 'lewtilities_build_hook' ) ) {
wp_schedule_event( time(), 'one_hour', 'lewtilities_build_hook' );
}
}
public function build() {
// Trigger build
$out = null;
$ret = null;
exec("./build.sh 2>&1", $out, $ret);
file_put_contents("build.output", $ret . "\n" . print_r($out, true));
}
public function trigger_frontend_build($post) {
if (get_post_status( $post ) === "publish") {
$this->build();
}
}
public function format_post_for_mastodon( $status, $post ) {
$status = "New post: " . $post->post_title . "\n\n";
$status .= html_entity_decode( get_the_excerpt($post) );
$status .= "\n\n" . get_permalink( $post );
return $status;
}
}