{"id":231072,"date":"2022-12-25T16:00:00","date_gmt":"2022-12-25T13:00:00","guid":{"rendered":"https:\/\/wordpress.mediadoma.com\/?p=231072"},"modified":"2022-12-07T11:07:44","modified_gmt":"2022-12-07T08:07:44","slug":"kuidas-manipuleerida-dom-iga-php-abil","status":"publish","type":"post","link":"https:\/\/wordpress.mediadoma.com\/et\/kuidas-manipuleerida-dom-iga-php-abil\/","title":{"rendered":"Kuidas manipuleerida DOM-iga PHP abil"},"content":{"rendered":"\n<p>Kui r\u00e4\u00e4kida DOM-i manipuleerimisest, siis esimene asi, millele paljud meist ilmselt m\u00f5tlevad, on JavaScripti kasutamine, et teha k\u00f5ik, mida me vajame.<\/p>\n<p>Keel mitte ainult ei toeta selle jaoks funktsioone, vaid <a href=\"http:\/\/es6-features.org\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">ES6<\/a> uuemad funktsioonid annavad meile v\u00f5imsamaid viise kliendipoolsete skriptide loomiseks. Ja kui kasutate <a href=\"https:\/\/jquery.com\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">jQueryt<\/a> koos WordPressiga, on teil DOM-i p\u00e4ringute tegemiseks sama funktsioonide kogu, mis meil on olnud aastaid.<\/p>\n<p>Kuid kliendipoolne DOM-i manipuleerimine ei ole alati parim valik. Selle asemel v\u00f5iksite seda teha serveri poolel. Ja m\u00f5nede PHP-sse sisseehitatud funktsioonide t\u00f5ttu ei erine see palju sellest, kuidas me JavaScripti kasutame.<\/p>\n<p>Peale selle, et me teeme seda muidugi serveris.<\/p>\n<p>Nii et kui t\u00f6\u00f6tate kunagi postituse sisuga (postituse t\u00fc\u00fcp v\u00f5i kohandatud postituse t\u00fc\u00fcp) ja teil on vaja silte manipuleerida sarnaselt JavaScriptiga, on <code>DomDocument<\/code>teegi kasutamine \u00fcks v\u00f5imsamaid. t\u00f6\u00f6riistad on teie k\u00e4sutuses.<\/p>\n<p>Oletame n\u00e4iteks, et soovite korrata l\u00e4bi k\u00f5ik postituse sisus olevad l\u00f5iguelemendid.<\/p>\n<p>Seda on lihtne teha, kasutades nimetatud teeki. Esiteks peaksite <a href=\"https:\/\/gist.github.com\/tommcfarlin\/5bd22e01487fd22df526c1a2e8687d13#file-00-use-dom-document-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">veenduma, et teek<\/a> on oma klassis (v\u00f5i lihtsalt funktsioonide kogus) seadistatud:<\/p>\n<p>J\u00e4rgmisena seadistage <a href=\"https:\/\/gist.github.com\/tommcfarlin\/5bd22e01487fd22df526c1a2e8687d13#file-01-the-content-hook-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">sisu jaoks konks:<\/a><\/p>\n<pre><code>&lt;?php\n\nadd_filter('the_content', __NAMESPACE__. 'updateParagraphElements');<\/code><\/pre>\n<p>Ja selle funktsiooni raames laadige kindlasti postituse sisu teeki ja seej\u00e4rel otsige \u00fcles k\u00f5ik <code>p<\/code>elemendid <a href=\"https:\/\/gist.github.com\/tommcfarlin\/5bd22e01487fd22df526c1a2e8687d13#file-02-update-the-content-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">sarnaselt JavaScriptiga<\/a> (\u00fche oluline m\u00e4rkus on see, et \u00fclaindeksite v\u00f5i emotikonide korral peate teabe \u00f5igesti kodeerima kasutatud):<\/p>\n<pre><code>&lt;?php\n\nfunction updateParagraphElements($content) \n{\n    \/* If we're not on a single page or it's not the main query, \n     * we won't do anything.\n     *\/\n    if (!is_single() || !is_main_query()) {\n        return $content;\n    }\n\n    \/\/ Make sure there is content to parse (and properly encode the HTML entities).\n    $domDocument = new DOMDocument();\n    $domContent = $domDocument-&gt;loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES'));\n    if (false === $domContent) {\n        return $content;\n    }\n\n    $paragraphs = $domDocument-&gt;getElementsByTagName('p');\n    if (0 === count($paragraphs)) {\n        return $content;\n    }\n\n    \/\/ More to come...\n}<\/code><\/pre>\n<p>Siit saate teha mitmeid erinevaid toiminguid, n\u00e4iteks lisada postituse ID iga l\u00f5igu elemendi kohandatud atribuudile. T\u00e4ielik funktsioon n\u00e4eks siis v\u00e4lja <a href=\"https:\/\/gist.github.com\/tommcfarlin\/5bd22e01487fd22df526c1a2e8687d13#file-04-full-code-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">umbes selline:<\/a><\/p>\n<pre><code>&lt;?php\n\nnamespace Acme;\n\nuse DOMDocument;\n\nadd_filter('the_content', __NAMESPACE__. 'updateParagraphElements');\n\/**\n * Updates the content by locating all of the `p` elements in the content,\n * then adds a class attribute of the post ID to them.\n *\n * The gist of the work is done in this function.\n *\n * @param string $content the post content\n *\n * @return string the updated post content with the aforemented markup.\n *\/\nfunction updateParagraphElements($content) \n{\n    \/* If we're not on a single page or it's not the main query, \n     * we won't do anything.\n     *\/\n    if (!is_single() || !is_main_query()) {\n        return $content;\n    }\n\n    \/\/ Make sure there is content to parse (and properly encode the HTML entities).\n    $domDocument = new DOMDocument();\n    $domContent = $domDocument-&gt;loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES'));\n    if (false === $domContent) {\n        return $content;\n    }\n\n    $paragraphs = $domDocument-&gt;getElementsByTagName('p');\n    if (0 === count($paragraphs)) {\n        return $content;\n    }\n\n    \/\/ If so, iterate through the elements and add the post ID as a custom attribute.\n    $updatedContent = '';\n    foreach ($paragraphs as $paragraph) {\n        $paragraph-&gt;setAttribute('data-id', get_the_ID());\n    }\n\n    return wp_kses_post($domDocument-&gt;saveHTML());\n}<\/code><\/pre>\n<p>Muidugi saate teha ka muid asju. \u00dcks asi on m\u00e4rgistuse manipuleerimine enne selle brauserisse saatmist. Teine asi on aga andmetele atribuutide lisamine, kui neid veel pole.<\/p>\n<p>See t\u00e4hendab, et pole m\u00f5eldud \u2013 v\u00e4lja arvatud v\u00f5ib-olla ainult omamoodi \u2013 sisu m\u00f5nele teisele postitusele.<\/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>DOM-iga manipuleerimine kliendi poolel ei ole alati parim valik. Saame seda teha PHP sisseehitatud funktsioonide abil.<\/p>\n","protected":false},"author":1,"featured_media":236123,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_wp_rev_ctl_limit":""},"categories":[718,833,894,802,863],"tags":[1165],"class_list":["post-231072","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arendaja","category-juhend-algajatele","category-kood","category-php-4","category-wordpress-4","tag-affiai-et"],"_links":{"self":[{"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/posts\/231072","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=231072"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/posts\/231072\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/media\/236123"}],"wp:attachment":[{"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/media?parent=231072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/categories?post=231072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/tags?post=231072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}