{"id":229466,"date":"2022-11-16T10:21:00","date_gmt":"2022-11-16T07:21:00","guid":{"rendered":"https:\/\/wordpress.mediadoma.com\/?p=229466"},"modified":"2022-11-16T19:28:20","modified_gmt":"2022-11-16T16:28:20","slug":"wordpressi-mallid-algajatele-html-i-kutsumine-php-le","status":"publish","type":"post","link":"https:\/\/wordpress.mediadoma.com\/et\/wordpressi-mallid-algajatele-html-i-kutsumine-php-le\/","title":{"rendered":"WordPressi mallid algajatele: HTML-i kutsumine PHP-le"},"content":{"rendered":"\n<p><a href=\"https:\/\/wordpress.mediadoma.com\/et\/wordpressi-mallid-algajatele-kuidas-see-on\/\" title=\"Eile\">Eile<\/a> visandasin, kuidas m\u00f5ista, kui paljud meist on t\u00f5en\u00e4oliselt harjunud WordPressi mallidega t\u00f6\u00f6tama.<\/p>\n<p>Ehkki CSS-i ja JavaScripti probleemide eraldamine on kindel, on mallid problemaatilised alati, kui meie m\u00e4rgistusega on segatud palju PHP-d.<\/p>\n<p>Selguse huvides \u00fctlen, et me ei saa j\u00e4tta lisamata mallim\u00e4rgendeid, kuna see on WordPressi ja \u00fcldiste PHP-p\u00f5histe sisuhalduss\u00fcsteemide t\u00f6\u00f6p\u00f5him\u00f5te.<\/p>\n<p>Probleem ilmneb alati, kui t\u00f6\u00f6tame mallidega, mis sisaldavad koodi, mis teeb keerukamaid k\u00f5nesid erinevatele API-dele. Kuigi ma demonstreerisin seda <a href=\"https:\/\/codex.wordpress.org\/Class_Reference\/WP_Query\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">WP_Query<\/a> abil (ja teen seda ka edaspidi), pole see ainult see p\u00e4ring.<\/p>\n<p>Igatahes, mida me peaksime sellega peale hakkama?<\/p>\n<h2>WordPressi mallid: kui HTML kutsub PHP-d<\/h2>\n<p>Esiteks on oluline m\u00f5elda, miks on nii palju PHP-d ja juurdehindlust segada halb m\u00f5te.<\/p>\n<p>Lihtne on \u00f6elda, et see raskendab koodi <a href=\"https:\/\/en.wikipedia.org\/wiki\/Don%27t_repeat_yourself\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">kuivana<\/a> hoidmist (ja selles on t\u00f5de), aga mis siis, kui kasutame ainult \u00fchte p\u00e4ringut v\u00f5i \u00fchte koodiplokki? Kas KUIV olemine on siis t\u00f5esti nii oluline?<\/p>\n<p>Selguse huvides, jah, ma arvan, et see on oluline koodi kuivana hoidmiseks, kuid kui me kasutame k\u00f5net ainult korra, siis arvan, et see on oluline mitte muul p\u00f5hjusel kui malli koodi puhtana hoidmine. v\u00f5imalik.<\/p>\n<p>M\u00f5elge sellele j\u00e4rgmiselt: kui tahame kuvada postituse pealkirja, sisu ja autorit, vajame ainult kolme funktsiooni:<\/p>\n<ul>\n<li><a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/the_title\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">the_title();<\/a><\/li>\n<li><a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/the_content\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">sisu();<\/a><\/li>\n<li><a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/the_author_meta\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">the_autor_meta();<\/a><\/li>\n<\/ul>\n<p><a href=\"https:\/\/gist.github.com\/tommcfarlin\/2c02b22272f645076f7f735bd61a4034#file-00-content-container-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">Nii et kujutage ette j\u00e4rgmise koodi<\/a> kohandamist (mida vaatasime eelmises postituses):<\/p>\n<pre><code>&lt;div id=\"content-container\"&gt;\n\n  &lt;p&gt;\n    Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! \n    Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that \n    thing away! You're going to get us all killed.\n  &lt;\/p&gt;\n\n  &lt;h2&gt;List of Post Titles For Acme Post Type&lt;\/h2&gt;\n  &lt;?php\n  $args = array(\n    'post_status'    =&gt; 'publish',\n    'post_type'      =&gt; 'acme',\n    'posts_per_page' =&gt; '10'\n  );\n  $custom_query = new WP_Query( $args );\n\n  if ($custom_query-&gt;have_posts()) {\n    echo '&lt;ul&gt;';\n    while ($custom_query-&gt;have_posts()) {\n      $custom_query-&gt;the_post();\n      echo '&lt;li&gt;'. get_the_title(). '&lt;\/li&gt;';\n    }\n    echo '&lt;\/ul&gt;';\n    wp_reset_postdata();\n  }\n  ?&gt;\n\n  &lt;p&gt;\n    Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not \n    going to take them long to figure out what happened to us. It could be worst... It's worst. \n    There's something alive in here! That's your imagination. Something just moves past my leg! \n    Look! Did you see that? What? Help!\n  &lt;\/p&gt;\n\n&lt;\/div&gt;&lt;!-- #content-container --&gt;<\/code><\/pre>\n<p>Et v\u00e4lja n\u00e4ha midagi sellist:<\/p>\n<pre><code>&lt;div id=\"content-container\"&gt;\n\n  &lt;p&gt;\n    Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! \n    Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that \n    thing away! You're going to get us all killed.\n  &lt;\/p&gt;\n\n  &lt;h2&gt;List of Post Titles For Acme Post Type&lt;\/h2&gt;\n  &lt;?php acme_get_titles(); ?&gt;\n\n  &lt;p&gt;\n    Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not \n    going to take them long to figure out what happened to us. It could be worst... It's worst. \n    There's something alive in here! That's your imagination. Something just moves past my leg! \n    Look! Did you see that? What? Help!\n  &lt;\/p&gt;\n\n&lt;\/div&gt;&lt;!-- #content-container --&gt;<\/code><\/pre>\n<p>Palju puhtam, eks? Kuid see, mida n\u00e4ete, on rohkem kui see. T\u00e4psemalt, kui me t\u00f6\u00f6tame HTML-iga kulisside taga, peame veenduma, et desinfitseerime selle <a href=\"https:\/\/gist.github.com\/tommcfarlin\/2c02b22272f645076f7f735bd61a4034#file-02-acme-get-titles-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">\u00f5igesti<\/a>, kasutades midagi sellist nagu <a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/wp_kses\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">wp_kses<\/a> :<\/p>\n<pre><code>&lt;?php\n\n\/**\n * Runs a custom query to get the most recent ten published articles of the\n * acme post type and then prints a sanitized list of the the titles.\n *\/\nfunction acme_get_titles()\n{\n\n  $args = array(\n    'post_status'    =&gt; 'publish',\n    'post_type'      =&gt; 'acme',\n    'posts_per_page' =&gt; '10'\n  );\n  $custom_query = new WP_Query( $args );\n\n  if ($custom_query-&gt;have_posts()) {\n    $html = '&lt;ul&gt;';\n    while ($custom_query-&gt;have_posts()) {\n      $custom_query-&gt;the_post();\n      $html .= '&lt;li&gt;'. get_the_title(). '&lt;\/li&gt;';\n    }\n    $html .= '&lt;\/ul&gt;';\n    wp_reset_postdata();\n  }\n\n  echo _acme_sanitize_titles($html);\n}\n\n\/**\n * Uses the WordPress wp_kses() API to sanitize and echo the incoming markup.\n *\n * @param string $html The HTML to sanitize.\n *\/\nfunction _acme_sanitize_titles($html) {\n\n  if(empty($html)) {\n    echo $html;\n  }\n\n  echo wp_kses(\n    $html,\n    array(\n      'ul' =&gt; array(),\n      'li' =&gt; array(),\n    );\n  );\n}<\/code><\/pre>\n<p>N\u00fc\u00fcd peavad m\u00f5ned arendajad ideed HTML-i kajast PHP-st WordPressi mallidesse vastukaaluks. Ja see pole alusetu.<\/p>\n<p>Selle sarja jooksul tuleb siiski m\u00e4ngida tasakaalustavat tegevust: tuleb tutvustada, kuidas inimesed sageli teavet esitavad, ja seej\u00e4rel seda pisut puhastada.<\/p>\n<p>Et seda natuke kaugemale viia, on v\u00f5imalik \u2013 ja see pole \u00fcldse halb m\u00f5te \u2013 eraldada funktsioon WP_Query veelgi, et kasutada <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_template_part\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">get_template_part<\/a>. See sisaldab natuke puhtamat koodi, kuid n\u00f5uab ka seda, et me vaataksime seda funktsiooni p\u00f5hjalikult.<\/p>\n<p>Ja see on midagi, millest ma selles konkreetses sarjas ei ole huvitatud.<\/p>\n<h2>Kuidas peaksime seda visualiseerima?<\/h2>\n<p>Mis puutub mallidesse, siis saame seda kontseptualiseerida mitmel erineval viisil.<\/p>\n<ol>\n<li>M\u00f5nes mallikeeles on kasulik m\u00f5elda, et PHP sisestab teabe malli,<\/li>\n<li>\u00dclaltoodud n\u00e4ites on kasulik m\u00f5elda mallifailile, mis n\u00f5uab PHP-skriptilt teavet.<\/li>\n<\/ol>\n<p>Nii et \u00fclaltoodud n\u00e4ites on meil funktsioon, mis asub n\u00e4iteks failis <strong>functions.php<\/strong> ja seda kutsuv mallifail:<\/p>\n<p>On loogiline? Kuid see, kuidas see saavutatakse \u2013 selle \u00fche funktsioonik\u00f5ne taga oleva koodi osas \u2013 on postituse sisu omaette.<\/p>\n<p>Ja seda ma j\u00e4rgmises postituses vaatlen.<\/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>Kui m\u00f5istame, kuidas WordPressi mallid PHP-faile kutsuvad, v\u00f5ib see aidata meil kirjutada DRY-koodi, aga ka koodi, mida on lihtsam lugeda ja lihtsam hooldada.<\/p>\n","protected":false},"author":1,"featured_media":223984,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_wp_rev_ctl_limit":""},"categories":[833,894,916,842,863],"tags":[1165],"class_list":["post-229466","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-juhend-algajatele","category-kood","category-muud","category-opetused","category-wordpress-4","tag-affiai-et"],"_links":{"self":[{"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/posts\/229466","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=229466"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/posts\/229466\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/media\/223984"}],"wp:attachment":[{"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/media?parent=229466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/categories?post=229466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/tags?post=229466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}