{"id":228988,"date":"2022-11-03T12:54:00","date_gmt":"2022-11-03T09:54:00","guid":{"rendered":"https:\/\/wordpress.mediadoma.com\/?p=228988"},"modified":"2022-11-09T05:10:05","modified_gmt":"2022-11-09T02:10:05","slug":"kiire-prototueuepimine-prototueuep-koodiks-1-osa","status":"publish","type":"post","link":"https:\/\/wordpress.mediadoma.com\/et\/kiire-prototueuepimine-prototueuep-koodiks-1-osa\/","title":{"rendered":"Kiire protot\u00fc\u00fcpimine: protot\u00fc\u00fcp koodiks, 1. osa"},"content":{"rendered":"\n<p>Mis puudutab kiirprotot\u00fc\u00fcpe ja WordPressi, siis oleme siiani teinud kahte asja:<\/p>\n<ol>\n<li><a href=\"https:\/\/wordpress.mediadoma.com\/et\/kiire-prototueuepimine-wordpressiga-kontseptsioonist-pistikprogrammini\/\" title=\"planeeris pistikprogrammi\" >planeeris pistikprogrammi<\/a> ,<\/li>\n<li><a href=\"https:\/\/wordpress.mediadoma.com\/et\/kiire-prototueuepimine-wordpressiga-kontseptsiooni-analueues\/\" title=\"visandas skeemi selle kohta, kuidas koodi saab korraldada\" >visandas skeemi selle kohta, kuidas koodi saab korraldada<\/a><\/li>\n<\/ol>\n<p>Praegusel hetkel oleme teinud piisavalt t\u00f6\u00f6d, et \u00f5igustada koodi \u00fcmberkujundamise alustamist. See t\u00e4hendab, et hakkame protot\u00fc\u00fcpi koodiks teisendama. Kuid see on midagi, mida tuleb teha kahes etapis.<\/p>\n<p>Esiteks tutvustame lihtsalt klasse, mis esindavad eelmise postituse diagramme ja mis h\u00f5lmavad iga projekti vastutust.<\/p>\n<p>P\u00e4rast seda vaatame koodi korraldamist nimeruumidesse ja pakettidesse. Enne kui saame seda teha, peame siiski veenduma, et kood on objektorienteeritud ja t\u00f6\u00f6tab. Nii et see selles postituses juhtub.<\/p>\n<h2>Protot\u00fc\u00fcp koodiks<\/h2>\n<p>Kui olete varasemaid postitusi lugenud, siis pange t\u00e4hele, et kavatsen j\u00e4rgida eelmises postituses visandatud korraldust. Loomulikult ei pea te seda konkreetset disaini j\u00e4rgima.<\/p>\n<h3>M\u00f5ni s\u00f5na allika juhtimise kohta<\/h3>\n<p>Kui kasutate allika juhtimist, soovitan siin luua <a href=\"https:\/\/git-scm.com\/book\/en\/v1\/Git-Branching\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">p\u00f5hiharu<\/a> haru (kui kasutate Giti), et saaksite oma t\u00f6\u00f6d teha ilma koodi stabiilset versiooni karmistamata.<\/p>\n<p>See j\u00e4\u00e4b seeriast veidi kaugemale, nii et kui te ei kasuta allika juhtimist, \u00e4rge muretsege. Kui olete, valin selle filiaali nimeks <strong>arendada .<\/strong> Kui olen kindel, et see toimib, \u00fchendan selle tagasi <strong>masteriks\u00a0 .<\/strong><\/p>\n<h3>Koodi kirjutamine<\/h3>\n<p>Eile visandatud t\u00f6\u00f6 kohaselt loon kaks klassi:<\/p>\n<ol>\n<li>metaboksi klass,<\/li>\n<li>metakasti kuvamise klass.<\/li>\n<\/ol>\n<p>Koodi taaskasutatakse osaliselt sellest, mida oleme juba n\u00e4inud, nagu n\u00e4ete j\u00e4rgmises koodis.<\/p>\n<h4>Kood<\/h4>\n<p>Esiteks <a href=\"https:\/\/gist.github.com\/tommcfarlin\/1876be773467946ec2972ae32e0c97a3#file-00-class-meta-box-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">meie metakast<\/a> :<\/p>\n<pre><code>&lt;?php\n\/**\n * Registers the Meta Box with WordPress.\n *\n * @author Tom McFarlin\n * @since  0.2.0\n *\/\n\n\/**\n * Registers the Meta Box with WordPress. Defines the ID, title, display function,\n * and the post type on which it will live.\n *\n * @author Tom McFarlin\n * @since  0.2.0\n *\/\nclass Meta_Box {\n\n    \/**\n     * A reference to the class that will display the contents in the meta box.\n     *\n     * @access private\n     * @var    Meta_Box_Display\n     *\/\n    private $meta_box_display;\n\n    \/**\n     * Instantiates the class by setting its property equal to a reference to its display.\n     *\/\n    public function __construct() {\n        $this-&gt;meta_box_display = new Meta_Box_Display();\n    }\n\n    \/**\n     * The function responsible for hooking into the WordPress API.\n     *\/\n    public function init() {\n\n        add_meta_box(\n            'three-recent-posts',\n            'Three Recent Posts',\n            array( $this-&gt;meta_box_display, 'display' ),\n            'post',\n            'side'\n        );\n    }\n}\n<\/code><\/pre>\n<p>Ja j\u00e4rgmiseks <a href=\"https:\/\/gist.github.com\/tommcfarlin\/1876be773467946ec2972ae32e0c97a3#file-01-class-meta-box-display-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">meie ekraan<\/a> :<\/p>\n<pre><code>&lt;?php\n\/**\n * Defines the display for the meta box.\n *\n * @author Tom McFarlin\n * @since  0.2.0\n *\/\n\n\/**\n * Defines the display for the meta box that will render the content in the\n * context of its meta box.\n *\n * @author Tom McFarlin\n * @since  0.2.0\n *\/\nclass Meta_Box_Display {\n\n    \/**\n     * A reference to the class that will display the contents in the meta box.\n     *\n     * @access private\n     * @var    Post_Messenger\n     *\/\n    private $messenger;\n\n    \/**\n     * Instantiates the object by setting a property equal to that of the class\n     * responsible for rendering the messages from the post query.\n     *\/\n    public function __construct() {\n        $this-&gt;messenger = new Post_Messenger( $this );\n    }\n\n    \/**\n     * If there are posts to display, renders them in the metabox. Otherwise, displays\n     * a note that there are no posts to display.\n     *\/\n    public function display( $message) {\n        $this-&gt;messenger-&gt;get_message();\n    }\n}\n<\/code><\/pre>\n<p>Et metaboksi koodis metaboksi koodis me eksplitsiitselt instantseerime kuva, et saaksime vajadusel kutsuda seda kuvamismeetodiks.<\/p>\n<p>Teine v\u00f5imalus oleks instantseerida kaks objekti eraldi ja seej\u00e4rel sisestada ekraan konstruktoris\u00fcsti v\u00f5i muu sarnase kaudu metakasti. Seda tuleks teha kolmanda osapoole klassis.<\/p>\n<p>Selle eelised tulenevad kahe klassi lahti\u00fchendamisest. V\u00f5ib-olla vaatame j\u00e4rgmises postituses \u00fcle, kuidas seda teha.<\/p>\n<p>P\u00e4rast seda peame edasi minema ja m\u00e4\u00e4ratlema klassi, mis vastutab s\u00f5numite kuvamise eest metaboksi kuva kontekstis. Seda me nimetame <a href=\"https:\/\/gist.github.com\/tommcfarlin\/1876be773467946ec2972ae32e0c97a3#file-02-class-post-messenger-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">Post Messengeriks<\/a> :<\/p>\n<pre><code>&lt;?php\n\/**\n * Display content for the meta box when requested.\n *\n * @author Tom McFarlin\n * @since  0.2.0\n *\/\n\n\/**\n * Retrieves information from the class responsible for querying the database and\n * renders it in the context of our meta box when called via the Meta Box Display.\n *\n * @author Tom McFarlin\n * @since  0.2.0\n *\/\nclass Post_Messenger {\n\n    \/**\n     * A reference to the query resonsible for retrieving post information from\n     * the database.\n     *\n     * @access private\n     * @var    WP_Query\n     *\/\n    private $query;\n\n    \/**\n     * A reference to the message that's displayed in the view of the\n     * meta box.\n     *\n     * @access private\n     *\/\n    private $message;\n\n    \/**\n     * Instantiates the class by setting a reference to the query.\n     *\/\n    public function __construct() {\n        $this-&gt;query = new Post_Query();\n    }\n\n    \/**\n     * Retrieves the content to be displayed in the meta box.\n     *\/\n    public function get_message() {\n\n        $this-&gt;get_description();\n\n        if ($this-&gt;query-&gt;has_posts()) {\n            $this-&gt;get_post_message();\n        } else {\n            $this-&gt;get_no_posts_message();\n        }\n    }\n\n    \/**\n     * Displays the description of the content of the meta box.\n     *\n     * @access private\n     *\/\n    private function get_post_message() {\n        include_once 'post-list.php';\n    }\n\n    \/**\n     * Displays the description of the content of the meta box.\n     *\n     * @access private\n     *\/\n    private function get_description() {\n        include_once 'message-description.php';\n    }\n\n    \/**\n     * Displays a message of there are no recent posts.\n     *\n     * @access private\n     *\/\n    private function get_no_posts_message() {\n        include_once 'no-post-list.php';\n    }\n}\n<\/code><\/pre>\n<p>Pange t\u00e4hele, et Post Messenger viitab ka <a href=\"https:\/\/gist.github.com\/tommcfarlin\/1876be773467946ec2972ae32e0c97a3#file-03-post-query-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">postitusp\u00e4ringule<\/a>. See on klass, kus toimub side andmebaasiga. Lisasin ka m\u00f5ned abifunktsioonid, et muuta vaatamiskood veidi lihtsamaks, nagu me kohe n\u00e4eme.<\/p>\n<pre><code>&lt;?php\n\/**\n * Queries the database for three most recent posts.\n *\n * @author Tom McFarlin\n * @since  0.2.0\n *\/\n\n\/**\n * Queries the database for three most recent posts. Returns the query to the\n * caller so that it can be interrogates for posts or not.\n *\n * @author Tom McFarlin\n * @since  0.2.0\n *\/\nclass Post_Query {\n\n    \/**\n     * A reference to the WP_Query this class wraps.\n     *\n     * @access private\n     * @var    WP_Query\n     *\/\n    private $query;\n\n    \/**\n     * Instantiates the class by preparing instance data and executing the\n     * query so the display can render the contents.\n     *\/\n    public function __construct() {\n\n        $this-&gt;query = null;\n        $this-&gt;get_posts();\n    }\n\n    \/**\n     * Executes the query for returning the post recent posts ordered by date.\n     *\n     * @access private\n     *\/\n    private function get_posts() {\n\n        $args = array(\n            'post_type'   =&gt; 'post',\n            'post_status' =&gt; 'publish',\n            'orderby'     =&gt; 'date',\n            'order'       =&gt; 'desc',\n        );\n        $this-&gt;query = new WP_Query( $args );\n\n        return $this-&gt;query;\n    }\n\n    \/**\n     * A helper function to determine if the query has any posts.\n     *\/\n    public function has_posts() {\n        return! $this-&gt;query-&gt;have_posts();\n    }\n\n    \/**\n     * A helper function for retrieving the next post in the list of\n     * posts\n     *\/\n    public function the_post() {\n        return $this-&gt;query-&gt;the_post();\n    }\n}\n<\/code><\/pre>\n<p>Ja see ongi p\u00f5hiklasside jaoks. Muidugi tuleb veel vaadetest r\u00e4\u00e4kida.<\/p>\n<h4>Vaated<\/h4>\n<p>Vaated vastutavad HTML-i renderdamise eest metakasti kontekstis. Mulle ei meeldi PHP kontekstis HTML-i kirjutamine (ega ka PHP segamine HTML-i kontekstis, kuid see on selle projekti puhul paratamatu).<\/p>\n<p>Selle lihtsamaks muutmiseks on m\u00f5ned suurep\u00e4rased malliprojektid, kuid ma kaldun k\u00f5rvale. Igatahes m\u00e4rkad, et <strong>post-list.php<\/strong> failis on viited abifunktsioonidele klassis Post Query. Selle eesm\u00e4rk on olla kindel, et ma ei paljasta liiga palju omadusi ega riku Demeteri seadust.<\/p>\n<p>Vaatame k\u00f5igepealt seda faili, kuna <a href=\"https:\/\/gist.github.com\/tommcfarlin\/1876be773467946ec2972ae32e0c97a3#file-04-post-list-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">see on k\u00f5ige keerulisem<\/a> :<\/p>\n<pre><code>&lt;ol&gt;\n    &lt;?php while ($this-&gt;query-&gt;has_posts()) {  ?&gt;\n        &lt;?php $this-&gt;query-&gt;the_post(); ?&gt;\n        &lt;li&gt;\n            &lt;a href=\"&lt;?php get_the_permalink(); ?&gt;\" target=\"_blank\"&gt;\n                &lt;?php echo get_the_title(); ?&gt;\n            &lt;\/a&gt;\n        &lt;\/li&gt;\n    &lt;?php } ?&gt;\n&lt;\/ol&gt;<\/code><\/pre>\n<p>See n\u00e4eb v\u00e4lja nagu standardne WordPressi kood, kuid pidage meeles, et kuna seda faili kutsutakse v\u00e4lja <strong>Post Messengeris<\/strong>, viitab see p\u00e4ringule kui p\u00e4ringule, mille see klass \u00fcmbritseb.<\/p>\n<p>Kaks viimast faili on \u00fcsna sirgjoonelised. \u00dcks neist annab <a href=\"https:\/\/gist.github.com\/tommcfarlin\/1876be773467946ec2972ae32e0c97a3#file-06-message-description-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">kirjelduse<\/a> :<\/p>\n<pre><code>&lt;p&gt;\n    &lt;span class=\"description\"&gt;\n        Displays up to the three most recent posts.\n    &lt;\/span&gt;&lt;!-- .description --&gt;\n&lt;\/p&gt;<\/code><\/pre>\n<p>Teine annab s\u00f5numi, kui <a href=\"https:\/\/gist.github.com\/tommcfarlin\/1876be773467946ec2972ae32e0c97a3#file-07-no-post-list-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">postitusi pole<\/a> :<\/p>\n<pre><code>&lt;p&gt;There are no recent posts.&lt;\/p&gt;<\/code><\/pre>\n<p>Peale selle on p\u00f5hifunktsioonid tehtud.<\/p>\n<h3>Plugina alglaadimine<\/h3>\n<p>Viimane asi, mida peame tegema, on pistikprogrammi k\u00e4ivitamine. Selleks muudame peamise pistikprogrammi faili koodi nii, et see n\u00e4eks v\u00e4lja <a href=\"https:\/\/gist.github.com\/tommcfarlin\/1876be773467946ec2972ae32e0c97a3#file-08-three-recent-posts-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">j\u00e4rgmine<\/a> :<\/p>\n<pre><code>&lt;?php\n\/**\n * Three Recent Posts\n *\n * @package     TRP\n * @author      Tom McFarlin\n * @copyright   2017 Tom McFarlin\n * @license     MIT\n *\n * @wordpress-plugin\n * Plugin Name: Three Recent Posts\n * Plugin URI:  https:\/\/tommcfarlin.com\/three-recent-posts\/\n * Description: Displays the three mot recent posts in your post editor screen.\n * Version:     0.2.0\n * Author:      Tom McFarlin\n * Author URI:  https:\/\/tommcfarlin.com\n * Text Domain: three-recent-posts\n * License:     GPL\n * License URI: http:\/\/www.gnu.org\/licenses\/gpl-3.0.txt\n *\/\n\ninclude 'class-meta-box.php';\ninclude 'class-meta-box-display.php';\ninclude 'class-post-messenger.php';\ninclude 'class-post-query.php';\n\nadd_action( 'add_meta_boxes', 'trp_start' );\n\/**\n * Starts the plugin.\n *\/\nfunction trp_start() {\n\n    $meta_box = new Meta_Box();\n    $meta_box-&gt;init();\n}\n<\/code><\/pre>\n<p>See haakub WordPressiga, loob meie pistikprogrammi ja paneb selle siis liikuma. Kui k\u00e4ivitate selle WordPressi installimise ajal, peaks see v\u00e4lja n\u00e4gema t\u00e4pselt nii, nagu see oli esimese versiooni ajal.<\/p>\n<p>Ainus erinevus on see, et meil on n\u00fc\u00fcd asjad korraldatud klasside kaupa, mitte \u00fcksikute funktsioonide kaupa.<\/p>\n<h2>M\u00e4rkmed<\/h2>\n<p>Esiteks on siin v\u00f5imalusi \u00fcmberkujundamiseks, mis v\u00e4hendaks lahtisidumist veelgi (nt erinevat t\u00fc\u00fcpi <a href=\"https:\/\/en.wikipedia.org\/wiki\/Dependency_injection\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">s\u00f5ltuvuse s\u00fcstimine<\/a> jne), kuid selle seeria eesm\u00e4rk ei ole seda h\u00f5lmata.<\/p>\n<p>Selle asemel on m\u00f5te n\u00e4ha paljude protseduuriliste funktsioonide poolt kirjutatud pistikprogramme ja seej\u00e4rel jagada need kontseptuaalsemateks klassideks, mis h\u00f5lmavad nende kohustusi.<\/p>\n<p>Teiseks, kui vaatate projekti selle versiooni hoidlas oleva l\u00e4htekoodi \u00fcle, n\u00e4ete, et tutvustasin ka faili composer.json. Seda selleks, et saaksin koodi kirjutamisel \u00e4ra kasutada <a href=\"https:\/\/tommcfarlin.com\/php-codesniffer-with-wordpress\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">PHP CodeSnifferi<\/a> ja <a href=\"https:\/\/tommcfarlin.com\/following-the-wordpress-coding-standards\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">WordPressi kodeerimisstandardeid<\/a>.<\/p>\n<p>Sarja viimases osas vaatame l\u00e4bi <a href=\"https:\/\/wordpress.mediadoma.com\/et\/nimeruumid-ja-automaatne-laadimine-wordpressis\/\" title=\"nimevahed\">nimevahed<\/a> ja korraldame failid \u00fcmber. Kui aega lubab, lisame automaatlaaduri, et me ei peaks oma pistikprogrammi faili \u00fclaossa faile k\u00e4sitsi lisama.<\/p>\n<p>L\u00f5puks \u00fchendasin selle koodi p\u00f5hikoodiga <a href=\"https:\/\/github.com\/tommcfarlin\/three-recent-posts\/tree\/0.2.1\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">ja m\u00e4rgistasin selle kui 0.2.1<\/a> (kuna pidin tegema v\u00e4ikese kiirparanduse), kuna see on veel pooleli.<\/p>\n<h2>Sarja postitused<\/h2>\n<ol>\n<li><a href=\"https:\/\/wordpress.mediadoma.com\/et\/kiire-prototueuepimine-wordpressiga-kontseptsioonist-pistikprogrammini\/\" title=\"Kiire protot\u00fc\u00fcpimine WordPressiga: kontseptsioonist pistikprogrammini\">Kiire protot\u00fc\u00fcpimine WordPressiga: kontseptsioonist pistikprogrammini<\/a><\/li>\n<li><a href=\"https:\/\/wordpress.mediadoma.com\/et\/kiire-prototueuepimine-wordpressiga-kontseptsiooni-analueues\/\" title=\"Kiire protot\u00fc\u00fcpimine WordPressiga: kontseptsiooni anal\u00fc\u00fcs\">Kiire protot\u00fc\u00fcpimine WordPressiga: kontseptsiooni anal\u00fc\u00fcs<\/a><\/li>\n<li><a href=\"https:\/\/tommcfarlin.com\/prototype-to-code-1\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">Kiire protot\u00fc\u00fcpimine: protot\u00fc\u00fcp koodiks, 1. osa<\/a><\/li>\n<li><a href=\"https:\/\/wordpress.mediadoma.com\/et\/kiirprototueuepimine-prototueuep-koodiks-2-osa\/\" title=\"Kiirprotot\u00fc\u00fcpimine: protot\u00fc\u00fcp koodiks, 2. osa\">Kiirprotot\u00fc\u00fcpimine: protot\u00fc\u00fcp koodiks, 2. osa<\/a><\/li>\n<li><a href=\"https:\/\/wordpress.mediadoma.com\/et\/kiire-prototueuepimine-automaatse-laadimise-tutvustamine\/\" title=\"Kiire protot\u00fc\u00fcpimine: automaatse laadimise tutvustamine\">Kiire protot\u00fc\u00fcpimine: automaatse laadimise tutvustamine<\/a><\/li>\n<\/ol>\n<p><div id=\"PostUnique_PostSource\" style=\"padding-top: 50px\">:  <a target=\"_blank\" rel=\"noopener nofollow\" href=\"\/\/tommcfarlin.com\" class=\"external external_icon\">tommcfarlin.com<\/a><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Siin hakkame oma protot\u00fc\u00fcpi koodiks teisendama. See peaks n\u00e4itama, miks on kasulik projekti ette m\u00f5elda.<\/p>\n","protected":false},"author":1,"featured_media":223902,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_wp_rev_ctl_limit":""},"categories":[718,916,842],"tags":[1165],"class_list":["post-228988","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arendaja","category-muud","category-opetused","tag-affiai-et"],"_links":{"self":[{"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/posts\/228988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/comments?post=228988"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/posts\/228988\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/media\/223902"}],"wp:attachment":[{"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/media?parent=228988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/categories?post=228988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/tags?post=228988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}