{"id":230939,"date":"2022-12-21T13:05:00","date_gmt":"2022-12-21T10:05:00","guid":{"rendered":"https:\/\/wordpress.mediadoma.com\/?p=230939"},"modified":"2022-12-07T10:29:40","modified_gmt":"2022-12-07T07:29:40","slug":"wordpressi-vidinad-uembertoeoetamine-11-osa","status":"publish","type":"post","link":"https:\/\/wordpress.mediadoma.com\/et\/wordpressi-vidinad-uembertoeoetamine-11-osa\/","title":{"rendered":"WordPressi vidinad: \u00fcmbert\u00f6\u00f6tamine, 11. osa"},"content":{"rendered":"\n<p>Eelmises postituses k\u00e4isime l\u00e4bi palju \u00fcmberkujundamist, mis jagas mured oma klassidesse.<\/p>\n<p>L\u00f5ppkokkuv\u00f5ttes aitab see n\u00e4idata, kuidas saame s\u00e4ilitada k\u00f5rgetasemelise \u00fchtekuuluvuse, t\u00f6\u00f6tades mitte ainult WordPressi klassidega, vaid tehes seda koos juba olemasolevate API-dega.<\/p>\n<p>Kuna m\u00f5ned viimased postitused koodibaasi \u00fcmbert\u00f6\u00f6tamise kohta on olnud nii pikad, on praegune postituste komplekt keskendunud v\u00e4ikestele j\u00e4rkj\u00e4rgulistele muudatustele ja seega l\u00fchematele, rohkem keskendunud postitustele.<\/p>\n<p>Nagu eelmises artiklis mainitud:<\/p>\n<blockquote>\n<p>Kui aga lehte v\u00e4rskendate, v\u00f5ite m\u00e4rgata, et desinfitseerimine ja serialiseerimine ei paista andmete toomisel t\u00f6\u00f6tavat. Ja seda me j\u00e4rgmises postituses uurimegi.<\/p>\n<\/blockquote>\n<p>Nii et see on koht, kus me selles artiklis \u00fcles v\u00f5tame.<\/p>\n<h2>WordPressi vidina katlaplaat: \u00fcmberkujundamine, 11. osa<\/h2>\n<p>Enne mis tahes koodi kirjutamist tuleb k\u00f5igepealt t\u00e4hele panna, et kui t\u00e4idate \u00fche vidina sisuala (nt pealkirja) millegi <a href=\"https:\/\/gist.github.com\/tommcfarlin\/39038338730b7141a19c3d2f48801dac#file-00-example-title-js\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">sellisega<\/a> :<\/p>\n<pre><code>&lt;script type=\"text\/javascript\"&gt;This is the Title&lt;\/script&gt;<\/code><\/pre>\n<p>Seej\u00e4rel kl\u00f5psake nuppu Salvesta, tegelik sisu desinfitseeritakse ja kirjutatakse andmebaasi. Seda n\u00e4ete, kui vaatate andmebaasis vidina v\u00e4\u00e4rtust.<\/p>\n<p>Lisaks n\u00e4ivad andmed esmapilgul korras, kuid kui v\u00e4rskendate lehte, kuvatakse desinfitseerimata sisu. Kui navigeerite teisele lehele (nt Men\u00fc\u00fcd) ja tulete seej\u00e4rel tagasi, kuvatakse vidina sisu, kuid see on korralikult puhastatud.<\/p>\n<p>Miks see siis teatud toimingute tegemisel n\u00e4itab \u00fcht asja andmebaasis ja \u00fcht asja haldusala esiotsas?<\/p>\n<p>See on seotud vidina vahem\u00e4luga ja \u00f5nneks saame seda vahem\u00e4lu oma \u00e4ran\u00e4gemise j\u00e4rgi t\u00fchjendada, kasutades soovitud konksu (st saame tellida mis tahes s\u00fcndmuse ja seej\u00e4rel lasta sellel vahem\u00e4lu t\u00fchjendada).<\/p>\n<\/p>\n<p><a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_cache_delete\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">Koodi<\/a> viitest :<\/p>\n<blockquote>\n<p>Eemaldab vahem\u00e4lu sisu, mis vastab v\u00f5tmele ja r\u00fchmale.<\/p>\n<\/blockquote>\n<p>Pange t\u00e4hele, et see n\u00f5uab v\u00f5tme ja valikulise r\u00fchma esitamist. Boilerplate&#8217;is oleme v\u00f5tmena kasutanud vidina n\u00e4ppu ja r\u00fchm on vidin.<\/p>\n<h3>Vahem\u00e4lu t\u00fchjendamine<\/h3>\n<p>Kuna funktsiooni saab siduda mis tahes s\u00fcndmusega, saame luua abonendi, mille saame siduda mis tahes s\u00fcndmusega. See t\u00e4hendab <a href=\"https:\/\/gist.github.com\/tommcfarlin\/39038338730b7141a19c3d2f48801dac#file-01-delete-widget-cache-subscriber-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">, et saame luua DeleteWidgetCache&#8217;i abonendi oma abonendi nimeruumis:<\/a><\/p>\n<pre><code>&lt;?php\n\n&lt;?php\n\n\/*\n * This file is part of WordPress Widget Boilerplate\n * (c) Tom McFarlin &lt;tom@tommcfarlin.com&gt;\n *\n * This source file is subject to the GPL license that is bundled\n * with this source code in the file LICENSE.\n *\/\n\nnamespace WordPressWidgetBoilerplateSubscriber;\n\n\/**\n * Deletes the cached contents of the widget.\n *\/\nclass DeleteWidgetCacheSubscriber extends AbstractSubscriber\n{\n    \/**\n     * {@inheritdoc}\n     *\/\n    public function __construct(string $hook)\n    {\n        parent::__construct($hook);\n    }\n\n    \/**\n     * Flushes the widget's cache based on the key that's specified in the function arguments.\n     *\/\n    public function load()\n    {\n        \/* Because we're implementing an abstract class, we'll parse arguments from the\n         * func_get_args().\n         *\/\n        $args = func_get_args();\n        if (!$this-&gt;hasValidArguments($args)) {\n            return;\n        }\n\n        \/\/ TODO: More to come...\n    }\n\n    \/**\n     * Verifies that we have valid arguments with which to work.\n     *\n     * @param array $args the array of arguments we are validating\n     *\n     * @return bool true if the arguments are valid; otherwise, false\n     *\/\n    private function hasValidArguments(array $args): bool\n    {\n        \/\/ First, check the initial index of the arguments.\n        if (!isset($args[0])) {\n            return false;\n        }\n\n        \/\/ Next, check the values of the arguments for the widget key and group.\n        $args = $args[0];\n        if (!isset($args[0]) &amp;&amp; !isset($args[1])) {\n            return false;\n        }\n\n        return true;\n    }\n}\n<\/code><\/pre>\n<p>Seej\u00e4rel <a href=\"https:\/\/gist.github.com\/tommcfarlin\/39038338730b7141a19c3d2f48801dac#file-02-wordpress-widget-plugin-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">v\u00e4rskendame<\/a> abonendi registrisse lisamiseks alglaadimist ja kasutame kohandatud konksu flush_widget_cache, mida kasutame hetkeks.<\/p>\n<pre><code>&lt;?php\n\/**\n * WordPress Widget Boilerplate\n *\n * The WordPress Widget Boilerplate is an organized, maintainable boilerplate for building\n * widgets using WordPress best practices.\n *\n * @package   WordPressWidgetBoilerplate\n * @author    Your Name &lt;email@example.com&gt;\n * @license   GPL-3.0+\n * @link      http:\/\/example.com\n * @copyright 2018 - 2019 Your Name or Company Name\n *\n * @wordpress-plugin\n * Plugin Name:       WordPress Widget Boilerplate\n * Plugin URI:        https:\/\/github.com\/tommcfarlin\/wordpress-widget-boilerplate\n * Description:       An object-oriented foundation for building WordPress Widgets.\n * Version:           1.0.0\n * Author:            Tom McFarlin\n * Author URI:        https:\/\/tommcfarlin.com\n * Text Domain:       widget-name\n * License:           GPL-3.0+\n * License URI:       http:\/\/www.gnu.org\/licenses\/gpl-3.0.txt\n * Domain Path:       \/lang\n *\/\n\nnamespace WordPressWidgetBoilerplate;\n\nuse WordPressWidgetBoilerplateUtilitiesRegistry;\nuse WordPressWidgetBoilerplatePlugin;\nuse WordPressWidgetBoilerplateSubscriberWidgetSubscriber;\nuse WordPressWidgetBoilerplateSubscriberDeleteWidgetCacheSubscriber;\n\n\/\/ Prevent this file from being called directly.\ndefined('WPINC') || die;\n\n\/\/ Include the autoloader.\nrequire_once __DIR__. '\/vendor\/autoload.php';\n\n\/\/ Setup a filter so we can retrieve the registry throughout the plugin.\n$registry = new Registry();\nadd_filter('wpwBoilerplateRegistry', function() use ($registry) {\n    return $registry;\n});\n\n\/\/ Add subscribers.\n$registry-&gt;add('deleteWidgetCacheSubscriber', new DeleteWidgetCacheSubscriber('flush_widget_cache'));\n\n\/\/ Add the Widget base class to the Registry.\n$registry-&gt;add('widgetSubscriber', new WidgetSubscriber('widgets_init'));\n\n\/\/ Start the machine.\n(new Plugin($registry))-&gt;start();\n<\/code><\/pre>\n<p>Boilerplate&#8217;i jaoks kasutame kohandatud s\u00fcndmust alati, kui kutsutakse vidina serialiseerimiskood.<\/p>\n<p>Esmalt defineerime\u00a0 k\u00f5ne <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/do_action\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">do_action<\/a>, identifitseerime selle kui flush_widget_cache ja seej\u00e4rel <a href=\"https:\/\/gist.github.com\/tommcfarlin\/39038338730b7141a19c3d2f48801dac#file-03-widget-serialization-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">edastame<\/a> s\u00fcndmusele vajalikud argumendid, et tellija saaks neid lugeda:<\/p>\n<pre><code>&lt;?php\n\n\/*\n * This file is part of WordPress Widget Boilerplate\n * (c) Tom McFarlin &lt;tom@tommcfarlin.com&gt;\n *\n * This source file is subject to the GPL license that is bundled\n * with this source code in the file LICENSE.\n *\/\n\nnamespace WordPressWidgetBoilerplateWordPress;\n\n\/**\n * Santiizes and saves the data for the widget.\n *\/\nclass WidgetSerializer\n{\n    \/**\n     * @var string a reference to the slug of the widget to which the serialier is associated\n     *\/\n    private $widgetSlug;\n\n    \/**\n     * Initializes the class.\n     *\n     * @param string a reference to the slug of the widget to which the serialier is associated\n     *\/\n    public function __construct(string $widgetSlug)\n    {\n        $this-&gt;widgetSlug = $widgetSlug;\n    }\n\n    \/**\n     * Updates the values of the widget. Sanitizes the information before saving it.\n     *\n     * @param array $newInstance the array of new options to save\n     *\/\n    public function update($newInstance)\n    {\n        $instance = [];\n        foreach ($newInstance as $key =&gt; $value) {\n            $instance[$key] = strip_tags(\n                stripslashes($value)\n            );\n        }\n\n        do_action('flush_widget_cache', [$this-&gt;widgetSlug, 'widget']);\n\n        return $instance;\n    }\n}\n<\/code><\/pre>\n<p>Ja siis abonendis t\u00fchjendame sissetulevate argumentide p\u00f5hjal <a href=\"https:\/\/gist.github.com\/tommcfarlin\/39038338730b7141a19c3d2f48801dac#file-04-delete-widget-cache-subscriber-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">vahem\u00e4lu :<\/a><\/p>\n<pre><code>&lt;?php\n\n&lt;?php\n\n\/*\n * This file is part of WordPress Widget Boilerplate\n * (c) Tom McFarlin &lt;tom@tommcfarlin.com&gt;\n *\n * This source file is subject to the GPL license that is bundled\n * with this source code in the file LICENSE.\n *\/\n\nnamespace WordPressWidgetBoilerplateSubscriber;\n\n\/**\n * Deletes the cached contents of the widget.\n *\/\nclass DeleteWidgetCacheSubscriber extends AbstractSubscriber\n{\n    \/**\n     * {@inheritdoc}\n     *\/\n    public function __construct(string $hook)\n    {\n        parent::__construct($hook);\n    }\n\n    \/**\n     * Flushes the widget's cache based on the key that's specified in the function arguments.\n     *\/\n    public function load()\n    {\n        \/* Because we're implementing an abstract class, we'll parse arguments from the\n         * func_get_args().\n         *\/\n        $args = func_get_args();\n        if (!$this-&gt;hasValidArguments($args)) {\n            return;\n        }\n\n        $args = $args[0];\n        wp_cache_delete($args[0], $args[1]);\n    }\n\n    \/**\n     * Verifies that we have valid arguments with which to work.\n     *\n     * @param array $args the array of arguments we are validating\n     *\n     * @return bool true if the arguments are valid; otherwise, false\n     *\/\n    private function hasValidArguments(array $args): bool\n    {\n        \/\/ First, check the initial index of the arguments.\n        if (!isset($args[0])) {\n            return false;\n        }\n\n        \/\/ Next, check the values of the arguments for the widget key and group.\n        $args = $args[0];\n        if (!isset($args[0]) &amp;&amp; !isset($args[1])) {\n            return false;\n        }\n\n        return true;\n    }\n}\n<\/code><\/pre>\n<p>Ja see teebki seda.<\/p>\n<h2>Esiotsa kasutamiseks valmis<\/h2>\n<p>Praegu on meil paigas mehhanism, mis saab vidinate vahem\u00e4lu igal ajal t\u00fchjendada \u2013 mitte ainult kohandatud s\u00fcndmusega, vaid ka k\u00f5igi WordPressi pakutavate s\u00fcndmustega.<\/p>\n<p>See v\u00f5ib olla kasulik, kui kasutate Boilerplate&#8217;i millegi jaoks, mis kasutab vahem\u00e4llu salvestatud p\u00e4ringut v\u00f5i muud vahem\u00e4llu salvestamise mehhanismi, ja soovite veenduda, et sisu on selge.<\/p>\n<p>J\u00e4rgmisena vaatame sisu renderdamist esiotsas. Oleme Boilerplate&#8217;i \u00fcmbert\u00f6\u00f6tlemise l\u00f5pule j\u00f5udmas, kuid veel on vaja natuke teha, enne kui oleme valmis \u00fchendama selle koodibaasi p\u00f5hiharuga.<\/p>\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>Miks siis WordPress n\u00e4itab \u00fcht asja andmebaasis ja \u00fcht asja haldusalas? See on seotud vidina vahem\u00e4luga.<\/p>\n","protected":false},"author":1,"featured_media":235955,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_wp_rev_ctl_limit":""},"categories":[718,833,894],"tags":[1165],"class_list":["post-230939","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arendaja","category-juhend-algajatele","category-kood","tag-affiai-et"],"_links":{"self":[{"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/posts\/230939","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=230939"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/posts\/230939\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/media\/235955"}],"wp:attachment":[{"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/media?parent=230939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/categories?post=230939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/tags?post=230939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}