{"id":6472,"date":"2021-11-15T08:33:00","date_gmt":"2021-11-15T08:33:00","guid":{"rendered":"https:\/\/musilda.cz\/?p=6472"},"modified":"2021-11-15T08:33:00","modified_gmt":"2021-11-15T08:33:00","slug":"detekce-existence-woocommerce","status":"publish","type":"post","link":"https:\/\/affinite.io\/cs\/detekce-existence-woocommerce\/","title":{"rendered":"Detekce existence WooCommerce"},"content":{"rendered":"\n<p>Kdykoliv p\u00ed\u0161ete n\u011bjak\u00fd k\u00f3d, u kter\u00e9ho jeho funk\u010dnost z\u00e1vis\u00ed na k\u00f3du t\u0159et\u00ed strany, mus\u00edte po\u010d\u00edtat s t\u00edm, \u017ee nebude dostupn\u00fd. Proto je nezbytn\u00e9 u k\u00f3du pro WooCommerce \u0159e\u0161it to, zda je v\u016fbec plugin nainstalov\u00e1n a aktivn\u00ed. <\/p>\n\n\n\n<p>Zase nejde o nic slo\u017eit\u00e9ho, ide\u00e1ln\u00ed pro uk\u00e1zku bude n\u011bjak\u00fd plugin, kter\u00fd si vyrob\u00edme. <\/p>\n\n\n\n<p>Vzal jsem k\u00f3d ze v\u010derej\u0161\u00edho n\u00e1vodu <a href=\"https:\/\/musilda.cz\/jak-vytvorit-vlastni-typ-produktu\/\" target=\"_blank\" rel=\"noopener\">https:\/\/musilda.cz\/jak-vytvorit-vlastni-typ-produktu\/<\/a> a ud\u011blal z n\u011bj mal\u00fd plugin. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/eventpress.png\"><img loading=\"lazy\" decoding=\"async\" width=\"869\" height=\"154\" src=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/eventpress.png\" alt=\"\" class=\"wp-image-6474\" srcset=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/eventpress.png 869w, https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/eventpress-300x53.png 300w, https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/eventpress-768x136.png 768w\" sizes=\"auto, (max-width: 869px) 100vw, 869px\" \/><\/a><\/figure>\n\n\n\n<p>Bohu\u017eel, je pln\u011b z\u00e1visl\u00fd na WooCommerce, tak\u017ee p\u0159i pokusu o aktivaci se stane toto:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/error.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"164\" src=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/error-1024x164.png\" alt=\"\" class=\"wp-image-6475\" srcset=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/error-1024x164.png 1024w, https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/error-300x48.png 300w, https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/error-768x123.png 768w, https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/error.png 1117w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>To je ta lep\u0161\u00ed varianta, plugin nejde aktivovat. V p\u0159\u00edpad\u011b, \u017ee je ji\u017e aktivn\u00ed a n\u011bkdo vypne WooCommerce, chyba sund\u00e1 cel\u00fd web. A tomu te\u010f zabr\u00e1n\u00edme. <\/p>\n\n\n\n<p>M\u016f\u017eeme pou\u017e\u00edt dv\u011b PHP funkce:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>class_exists<\/li><li>function_exists<\/li><\/ul>\n\n\n\n<p>Poka\u017ed\u00e9, kdy\u017e se WooCommerce na\u010dte, tak se pou\u017eije t\u0159\u00edda woocommerce a objekt je p\u0159\u00edstupn\u00fd p\u0159es funkci WC().<\/p>\n\n\n\n<p>Tak\u017ee m\u016f\u017eete ps\u00e1t:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if ( class_exists( 'woocommerce') ) {\n    \/\/v\u00e1\u0161 k\u00f3d\n} <\/code><\/pre>\n\n\n\n<p>nebo<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\nif ( function_exists( 'WC') ) {\n    \/\/v\u00e1\u0161 k\u00f3d\n}\n<\/code><\/pre>\n\n\n\n<p>Tento postup se ale hod\u00ed do \u0161ablon, nebo do funkc\u00ed, kter\u00e9 jsou zaregistrov\u00e1ny v n\u011bjak\u00e9m hooku. Nezaru\u010duj\u00ed v\u00e1m toti\u017e, \u017ee v tu chv\u00edli, kdy se WordPress dostane k va\u0161emu k\u00f3du, je ji\u017e WooCommerce na\u010dten\u00e1, i kdy\u017e je na webu aktivn\u00ed.<\/p>\n\n\n\n<p>Na webech dost \u010dasto najdeten\u00e1sleduj\u00edc\u00ed k\u00f3d:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>include_once(ABSPATH . 'wp-admin\/includes\/plugin.php');\nif (is_plugin_active('woocommerce\/woocommerce.php')) {\n    \/\/v\u00e1\u0161 k\u00f3d\n}<\/code><\/pre>\n\n\n\n<p>Co\u017e tak\u00e9 funguje (v\u0161imn\u011bte si vlo\u017een\u00ed souboru plugin.php, kter\u00fd je norm\u00e1ln\u011b dostupn\u00fd jen v adminu), ale op\u011bt, je zde jeden probl\u00e9m. <\/p>\n\n\n\n<p>Plugin sice m\u016f\u017ee b\u00fdt aktivn\u00ed, ale WordPress na\u010d\u00edt\u00e1 plugin podle abecedy a kdy\u017e m\u00e1te n\u011bjakou t\u0159\u00eddu, kter\u00e1 je extended t\u0159\u00eddy ve WooCommerce, nemus\u00ed b\u00fdt tato t\u0159\u00edda je\u0161t\u011b na\u010dten\u00e1. <\/p>\n\n\n\n<p>Tak\u017ee tuto funkci pou\u017eijeme pro zobrazen\u00ed notice v adminu, ale pro plugin pot\u0159ebujeme jin\u00fd postup.<\/p>\n\n\n\n<p>WooComerce m\u00e1 akci, kter\u00e1 se jmenuje woocommerce_loaded a tu pou\u017eijeme pro na\u010dten\u00ed na\u0161eho k\u00f3du. <\/p>\n\n\n\n<p>Nejprve tedy informaci o tom, \u017ee WooCommerce nen\u00ed aktivn\u00ed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>include_once( ABSPATH . 'wp-admin\/includes\/plugin.php' );\nif ( !is_plugin_active( 'woocommerce\/woocommerce.php' ) ) {\n\n\t\/**\n\t * Admin notice\n\t *\n\t * @since 1.0\n\t *\/\n\tadd_action('admin_notices', 'musilda_admin_notice_wc_not_active');\n  \tfunction musilda_admin_notice_wc_not_active() {\n    \t$class = 'notice notice-error';\n    \tprintf(\n      \t\t'&lt;div class=\"error\" style=\"background:red; color:#fff;\"&gt;&lt;p&gt;%s&lt;\/p&gt;&lt;\/div&gt;',\n      \t\t__( 'You Must Install WooCommerce Plugin before activating EventPress ', 'musilda-eventpress' )\n    \t);\n  \t}\n\n}<\/code><\/pre>\n\n\n\n<p>Plugin ji\u017e jde activovat, ale v adminu se n\u00e1m zobraz\u00ed kr\u00e1sn\u00e1 hl\u00e1\u0161ka:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/admin-notice.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"394\" src=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/admin-notice-1024x394.png\" alt=\"\" class=\"wp-image-6476\" srcset=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/admin-notice-1024x394.png 1024w, https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/admin-notice-300x116.png 300w, https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/admin-notice-768x296.png 768w, https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/admin-notice.png 1122w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>Tak\u017ee tohle m\u00e1me vy\u0159e\u0161en\u00e9, ale co s k\u00f3dem, kter\u00fd bude v pluginu?<\/p>\n\n\n\n<p>Uzav\u0159eme jej do takov\u00e9to funkce:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Load plugin code, when WooCommerce is loaded\n *\n * @since 1.0\n *\/\nadd_action( 'woocommerce_loaded', 'load_eventpress' );\nfunction load_eventpress() {\n     \/\/v\u00e1s k\u00f3d\n}<\/code><\/pre>\n\n\n\n<p>T\u00edm je zaji\u0161t\u011bno, \u017ee v\u0161e co je pot\u0159eba pro fungov\u00e1n\u00ed pluginu bude na\u010dteno a\u017e kdy\u017e bude na\u010dten k\u00f3d WooCommerce a v\u0161e n\u00e1m bude bezpe\u010dn\u011b fungovat:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/event-product.png\"><img loading=\"lazy\" decoding=\"async\" width=\"978\" height=\"249\" src=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/event-product.png\" alt=\"\" class=\"wp-image-6477\" srcset=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/event-product.png 978w, https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/event-product-300x76.png 300w, https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/event-product-768x196.png 768w\" sizes=\"auto, (max-width: 978px) 100vw, 978px\" \/><\/a><\/figure>\n\n\n\n<p>Proto\u017ee si s pluginem budeme trochu hr\u00e1t v dal\u0161\u00edch n\u00e1vodech, vytvo\u0159il jsem ve\u0159ejn\u00fd reposit\u00e1\u0159, kde bude cel\u00fd k\u00f3d dostupn\u00fd <a href=\"https:\/\/github.com\/Musilda\/musilda-eventpress\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/Musilda\/musilda-eventpress<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Kdykoliv p\u00ed\u0161ete n\u011bjak\u00fd k\u00f3d, u kter\u00e9ho jeho funk\u010dnost z\u00e1vis\u00ed na k\u00f3du t\u0159et\u00ed strany, mus\u00edte po\u010d\u00edtat s t\u00edm, \u017ee nebude dostupn\u00fd. Proto je nezbytn\u00e9 u k\u00f3du pro WooCommerce \u0159e\u0161it to, zda je v\u016fbec plugin nainstalov\u00e1n a aktivn\u00ed. Zase nejde o nic slo\u017eit\u00e9ho, ide\u00e1ln\u00ed pro uk\u00e1zku bude n\u011bjak\u00fd plugin, kter\u00fd si vyrob\u00edme. Vzal jsem k\u00f3d ze v\u010derej\u0161\u00edho<\/p>\n","protected":false},"author":1,"featured_media":5600,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","footnotes":""},"categories":[29],"tags":[],"class_list":["post-6472","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-woocommerce-snippety"],"acf":[],"uagb_featured_image_src":{"full":["https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2020\/05\/snippet.jpg",1200,765,false],"thumbnail":["https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2020\/05\/snippet-150x150.jpg",150,150,true],"medium":["https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2020\/05\/snippet-300x191.jpg",300,191,true],"medium_large":["https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2020\/05\/snippet-768x490.jpg",640,408,true],"large":["https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2020\/05\/snippet-1024x653.jpg",640,408,true],"1536x1536":["https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2020\/05\/snippet.jpg",1200,765,false],"2048x2048":["https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2020\/05\/snippet.jpg",1200,765,false],"archive-list":["https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2020\/05\/snippet-400x265.jpg",400,265,true]},"uagb_author_info":{"display_name":"Affinite","author_link":"https:\/\/affinite.io\/cs\/author\/affinite\/"},"uagb_comment_info":0,"uagb_excerpt":"Kdykoliv p\u00ed\u0161ete n\u011bjak\u00fd k\u00f3d, u kter\u00e9ho jeho funk\u010dnost z\u00e1vis\u00ed na k\u00f3du t\u0159et\u00ed strany, mus\u00edte po\u010d\u00edtat s t\u00edm, \u017ee nebude dostupn\u00fd. Proto je nezbytn\u00e9 u k\u00f3du pro WooCommerce \u0159e\u0161it to, zda je v\u016fbec plugin nainstalov\u00e1n a aktivn\u00ed. Zase nejde o nic slo\u017eit\u00e9ho, ide\u00e1ln\u00ed pro uk\u00e1zku bude n\u011bjak\u00fd plugin, kter\u00fd si vyrob\u00edme. Vzal jsem k\u00f3d ze v\u010derej\u0161\u00edho","_links":{"self":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/6472","targetHints":{"allow":["GET"]}}],"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=6472"}],"version-history":[{"count":0,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/6472\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media\/5600"}],"wp:attachment":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media?parent=6472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/categories?post=6472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/tags?post=6472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}