From bebac6cacee18594bd2263c5abaafd661f200c51 Mon Sep 17 00:00:00 2001 From: Lewis Dale Date: Sat, 25 Feb 2023 11:08:48 +0000 Subject: [PATCH] Trigger builds & share to mastodon properly --- classes/init.php | 68 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/classes/init.php b/classes/init.php index da0b331..d22948b 100644 --- a/classes/init.php +++ b/classes/init.php @@ -11,17 +11,9 @@ class Lewtilities { public function init() { add_action('admin_menu', array ( $this, 'submenu_page' )); - add_action('wp_print_styles', array( $this, 'enqueue_styles' ), 9999); - } - - public function enqueue_styles() { - global $wp_styles; - global $enqueued_styles; - $enqueued_styles = array(); - foreach( $wp_styles->queue as $handle ) { - $enqueued_styles[] = $wp_styles->registered[$handle]->src; - } - print_r($wp_styles->queue); + 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() { @@ -38,8 +30,6 @@ class Lewtilities { public function admin_styles() { echo "Admin styles section"; - global $enqueued_styles; - echo var_dump($enqueued_styles); } public function admin_options_page() { @@ -55,4 +45,54 @@ class Lewtilities { public function admin_options_page_display() { echo "Options page"; } -} \ No newline at end of file + + 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; + } +}