From bfdb038cc1a14842d7fb228e418f40efa2c204a7 Mon Sep 17 00:00:00 2001 From: Herb Miller Date: Sat, 17 Mar 2018 13:02:18 +0000 Subject: [PATCH] Issue #20 - first pass setting decision for a post --- admin/class-oik-block-editor-opinion.php | 42 +++++++++++++++++- admin/class-oik-block-editor-opinions.php | 51 +++++++++++++++++++++- oik-block-opinions.php | 6 +-- opinions/class-oik-block-type-opinions.php | 4 +- 4 files changed, 96 insertions(+), 7 deletions(-) diff --git a/admin/class-oik-block-editor-opinion.php b/admin/class-oik-block-editor-opinion.php index 0e0c16e..9802c78 100644 --- a/admin/class-oik-block-editor-opinion.php +++ b/admin/class-oik-block-editor-opinion.php @@ -69,7 +69,6 @@ class oik_block_editor_opinion { * Advice on what to do */ public $choice_of_action; - public function __construct( $preferred_editor='A', $mandatory=false, $level="P", $observation="", $choice_of_action="" ) { $this->set_preferred_editor( $preferred_editor ); @@ -115,6 +114,47 @@ public function get_mandatory() { return $mandatory; } + + /** + * + * Current | Next opinion | New current | Notes + * ------- | ------------ | ----------- | ------- + * AO | AO | AO + * AM | + * BO | + * BM | + * CO | + * CM | + */ + public function consider( $current_decision ) { + $decisions = array( "AOAM" => "AM" + , "AOBM" => "BM" + , "AOCM" => "CM" + , "AMBM" => "BM" + , "AMCM" => "CM" + , "BOAO" => "AO" + , "BOAM" => "AM" + , "BOBM" => "BM" + , "BOCO" => "AO" + , "BOCM" => "CM" + , "BMAM" => "BM" + , "BMCM" => "CM" // Oops + , "COAO" => "AO" + , "COAM" => "AM" + , "COBO" => "AO" + , "COBM" => "BM" + , "COCM" => "CM" + , "CMAM" => "CM" // Oops + , "CMBM" => "CM" // Oops + ); + $next_opinion = $this->get_preferred_editor(); + $next_opinion .= $this->get_mandatory(); + + $new_decision = bw_array_get( $decisions, $current_decision . $next_opinion, $current_decision ); + + return $new_decision; + } + public function report() { $row = array(); $row[] = $this->get_preferred_editor(); diff --git a/admin/class-oik-block-editor-opinions.php b/admin/class-oik-block-editor-opinions.php index 133f526..68938d8 100644 --- a/admin/class-oik-block-editor-opinions.php +++ b/admin/class-oik-block-editor-opinions.php @@ -60,13 +60,62 @@ public function add_opinions( $opinions ) { /** * Considers the opinions to come to a decision * + * It shouldn't matter at which level the opinion is set + * but it does matter which opinion has precedence. + * We'll consider the "opinion" as two chars combining the Editor and Mandatory fields + * Initial opinion is 'AO'. There are 36 combinations. * */ public function consider_opinions() { - $decision = 'C'; + $decision = 'AO'; + foreach ( $this->opinions as $opinion ) { + $decision = $opinion->consider( $decision ); + } return $decision; } + public function report_summary() { + $decision = $this->consider_opinions(); + echo "Decision: $decision" . PHP_EOL; + } + + /** + * Implements the decision of the considered opinions + * + * Do we need to fetch the current decision? + * + * @param object $post the post object that's been analysed in context + * + + */ + public function implement_decision( $post ) { + $decision = $this->consider_opinions(); + $current_decision = $this->get_current_decision( $post ); + $this->update_decision( $post, $decision ); + } + + /** + * Fetch the current decision + * + * If not set then we'll need to determine it + * If it is, then we'll just re-determine it + */ + + public function get_current_decision( $post ) { + $decision = get_post_meta( $post->ID, "_oik_block_editor", true ); + if ( !$decision ) { + $decision = "AO"; + } + return $decision; + } + + public function update_decision( $post, $decision ) { + update_post_meta( $post->ID, "_oik_block_editor", $decision ); + + } + + + /** * Gathers the opinions at Site level * diff --git a/oik-block-opinions.php b/oik-block-opinions.php index 0b7ed33..f479e5f 100644 --- a/oik-block-opinions.php +++ b/oik-block-opinions.php @@ -80,13 +80,11 @@ function oik_block_options_validate_post_type( $post_type ) { */ function oik_block_opinions_post_type( $post_type ) { $opinions = oik_block_editor_opinions::instance(); - $opinions->gather_site_opinions(); $opinions->gather_post_type_opinions( $post_type ); $opinions->gather_all_post_opinions( $post_type ); $opinions->report(); - //$opinions->report_summary(); - + $opinions->report_summary(); } @@ -101,6 +99,8 @@ function oik_block_opinions_post( $post, $post_type ) { $opinions->gather_post_type_opinions( $post_type ); $opinions->gather_post_opinions( $post ); $opinions->report(); + $opinions->report_summary(); + $opinions->implement_decision( $post ); } function oik_block_prepare_opinions() { diff --git a/opinions/class-oik-block-type-opinions.php b/opinions/class-oik-block-type-opinions.php index 27d29ae..c042c0f 100644 --- a/opinions/class-oik-block-type-opinions.php +++ b/opinions/class-oik-block-type-opinions.php @@ -72,10 +72,10 @@ public function supports_editor() { public function show_in_rest() { if ( $this->post_type_object->show_in_rest ) { $this->can_edit = true; - return new oik_block_editor_opinion( 'A', false, 'T', "REST API not enabled" ); + return new oik_block_editor_opinion( 'A', false, 'T', "REST API enabled." ); } else { $this->can_edit = false; - return new oik_block_editor_opinion( 'C', true, 'T', "REST API not enabled", "Set show_in_rest true to enable the Block editor" ); + return new oik_block_editor_opinion( 'C', true, 'T', "REST API not enabled.", "Set show_in_rest true to enable the Block editor" ); } }