{"id":2638,"date":"2015-12-13T08:12:12","date_gmt":"2015-12-13T08:12:12","guid":{"rendered":"http:\/\/musilda.cz\/?p=2638"},"modified":"2015-12-13T08:12:12","modified_gmt":"2015-12-13T08:12:12","slug":"admin-ajax-php-zpomaluje-stranky","status":"publish","type":"post","link":"https:\/\/affinite.io\/cs\/admin-ajax-php-zpomaluje-stranky\/","title":{"rendered":"Admin-ajax.php zpomaluje str\u00e1nky"},"content":{"rendered":"\n

Soubor admin-ajax.php, je sou\u010d\u00e1st\u00ed administrace a pou\u017e\u00edv\u00e1 jej nap\u0159\u00edklad Heartbeat API. D\u00edky snadnosti jeho pou\u017eit\u00ed, jej \u010dasto vyu\u017e\u00edvaj\u00ed pluginy, kter\u00e9 jej volaj\u00ed na frontendu. A to t\u0159eba i WooCommerce.<\/p>\n\n\n

\n
\"\"<\/figure><\/div>\n\n\n

Bohu\u017eel, snadnost, s jakou se d\u00e1 tento soubor pou\u017e\u00edt, pro pr\u00e1ci s Ajaxem, l\u00e1k\u00e1 tv\u016frce plugin\u016f, aby jej nadu\u017e\u00edvali.
Kdy\u017e jsem p\u00e1tral, po tom, pro\u010d je Musilda.cz tak pomal\u00fd, pou\u017eil jsem test rychlost na\u010d\u00edt\u00e1n\u00ed, na http:\/\/www.webpagetest.org\/<\/a>.
<\/p>\n\n\n\n

P\u0159i v\u00fdpisu testu, m\u00e1te k dispozici dva v\u00fdsledky, jeden pro prvn\u00ed na\u010dten\u00ed a druh\u00fd, pro opakovan\u00e9 na\u010dten\u00ed. Tento screen je z opakovan\u00e9ho na\u010dten\u00ed, kde je kr\u00e1sn\u011b vid\u011bt, \u017ee admin-ajax.php, je velkou brzdou na\u010d\u00edt\u00e1n\u00ed. P\u0159i opakovan\u00e9m na\u010dten\u00ed, jsou statick\u00e9 soubory ji\u017e nacacheov\u00e1ny, tak\u017ee se ve v\u00fdpise nezobrazuj\u00ed.<\/p>\n\n\n\n

Abych zjistil, kde m\u016f\u017ee b\u00fdt probl\u00e9m, vyu\u017eil jsem dal\u0161\u00ed test rychlosti, tentokr\u00e1t https:\/\/gtmetrix.com\/<\/a> a v\u00fdsledek byl stejn\u00fd.<\/p>\n\n\n

\n
\"\"<\/figure><\/div>\n\n\n

Jak je vid\u011bt, stejn\u00fd v\u00fdsledek. Proto\u017ee m\u00e1m ov\u011b\u0159eno ze dvou zdroj\u016f, \u017ee admin-ajax.php brzd\u00ed web, budu muset zjistit, co jej tolikr\u00e1t za sebou vol\u00e1.<\/p>\n\n\n

\n
\"\"<\/figure><\/div>\n\n\n

Nev\u00edm, zda to je dob\u0159e vid\u011bt na screenu, ale po rozkliknut\u00ed polo\u017eky, se zobraz\u00ed z\u00e1lo\u017eky a na z\u00e1lo\u017ece Post, je vid\u011bt, jak\u00e1 akce je volan\u00e1. V tomto p\u0159\u00edpad\u011b mom_post_views. A proto\u017ee se jedn\u00e1 o Ajax, WordPress m\u00e1 dva hooky, pomoc\u00ed kter\u00fd m\u016f\u017eete zav\u011b\u0161ovat funkce, pro zpracov\u00e1n\u00ed Ajax vol\u00e1n\u00ed.<\/p>\n\n\n\n

Tak\u017ee jsem v\u011bd\u011bl, \u017ee mus\u00edm hledat wp_ajax_mom_post_views a wp_ajax_nopriv_mom_post_views. N\u00e1zev hook\u016f se skl\u00e1d\u00e1 z p\u0159edpony a n\u00e1zvu samotn\u00e9 akce. Tak\u017ee pokud je volan\u00e1 akce mom_post_views, bude hledan\u00fd hook wp_ajax_ + mom_post_views.<\/p>\n\n\n\n

Proto\u017ee je v\u0161ak ajax.admin.php prim\u00e1rn\u011b ur\u010den pro administraci, mus\u00edm hledat je\u0161t\u011b wp_ajax_nopriv_ + mom_post_views. To proto, \u017ee tento hook, umo\u017en\u00ed p\u0159\u00edstup k vol\u00e1n\u00ed i pro nep\u0159ihl\u00e1\u0161en\u00e9 n\u00e1v\u0161t\u011bvn\u00edky.<\/p>\n\n\n\n

V jenom souboru jsem na\u0161el:<\/p>\n\n\n\n

add_action( ‚wp_ajax_mom_post_views‘, ‚getPostViews‘ );
add_action( ‚wp_ajax_nopriv_mom_post_views‘, ‚getPostViews‘);<\/p>\n\n\n\n

z \u010deho\u017e vypl\u00fdvalo, \u017ee probl\u00e9mem je zobrazov\u00e1n\u00ed po\u010dtu shl\u00e9dnut\u00ed, kter\u00e9, z n\u011bjak\u00e9ho d\u016fvodu, vol\u00e1 \u0161ablona pomoc\u00ed Ajaxu.<\/p>\n\n\n\n

Co\u017e na hlavn\u00ed stran\u011b je minim\u00e1ln\u011b 18x. N\u00e1prava u\u017e byla jednoduch\u00e1, odstranil jsem z js funkci, kter\u00e1 v\u00fdu\u017e\u00edvala Ajax, v souboru \u0161ablony jsem na\u0161el m\u00edsto, kde se z\u00edskan\u00e1 data zobrazovala a nahradil jsem tu \u010d\u00e1st, pomoc\u00ed get_post_meta.<\/p>\n\n\n\n

V\u00fdsledek u\u017e je daleko lep\u0161\u00ed:<\/p>\n\n\n

\n
\"\"<\/figure><\/div>\n\n\n

Kdy\u017e \u0159eknu, \u017ee jsem zrychlil web o 2 vte\u0159iny, nebude to tak zn\u00edt, jako kdy\u017e nap\u00ed\u0161u:<\/p>\n\n\n\n

\u00dapravou jedn\u00e9 funkce, jsem zrychlil web o 40%<\/span><\/strong><\/h2>\n","protected":false},"excerpt":{"rendered":"

Soubor admin-ajax.php, je sou\u010d\u00e1st\u00ed administrace a pou\u017e\u00edv\u00e1 jej nap\u0159\u00edklad Heartbeat API. D\u00edky snadnosti jeho pou\u017eit\u00ed, jej \u010dasto vyu\u017e\u00edvaj\u00ed pluginy, kter\u00e9 jej volaj\u00ed na frontendu. A to t\u0159eba i WooCommerce. Bohu\u017eel, snadnost, s jakou se d\u00e1 tento soubor pou\u017e\u00edt, pro pr\u00e1ci s Ajaxem, l\u00e1k\u00e1 tv\u016frce plugin\u016f, aby jej nadu\u017e\u00edvali.Kdy\u017e jsem p\u00e1tral, po tom, pro\u010d je Musilda.cz<\/p>\n","protected":false},"author":1,"featured_media":2643,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[65,213,564],"class_list":["post-2638","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-admin-ajax-php","tag-jak-zrychlit-wordpress","tag-zrychleni-webu"],"_links":{"self":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/2638"}],"collection":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/comments?post=2638"}],"version-history":[{"count":0,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/2638\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media\/2643"}],"wp:attachment":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media?parent=2638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/categories?post=2638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/tags?post=2638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}