├── .gitignore ├── img └── spinner.gif ├── .github └── screenshots │ ├── 1.png │ ├── 2.png │ ├── 3.png │ └── 4.png ├── .jshintrc ├── languages ├── dynamic-featured-image-bs_BA.mo ├── dynamic-featured-image-de_DE.mo ├── dynamic-featured-image-he_IL.mo ├── dynamic-featured-image-hr_HR.mo ├── dynamic-featured-image-it_IT.mo ├── dynamic-featured-image-ne_NP.mo ├── dynamic-featured-image-pt_BR.mo ├── dynamic-featured-image-sr_RS.mo ├── dynamic-featured-image-sv_SE.mo ├── dynamic-featured-image-th_TH.mo ├── dynamic-featured-image-pt_BR.po ├── dynamic-featured-image-th_TH.po ├── dynamic-featured-image-he_IL.po ├── dynamic-featured-image-bs_BA.po ├── dynamic-featured-image-hr_HR.po ├── dynamic-featured-image-sr_RS.po ├── dynamic-featured-image-ne_NP.po ├── dynamic-featured-image-sv_SE.po ├── dynamic-featured-image-it_IT.po └── dynamic-featured-image-de_DE.po ├── .scrutinizer.yml ├── bower.json ├── uninstall.php ├── tests ├── bootstrap.php ├── test-dynamic-featured-image-ajax.php └── test-dynamic-featured-image.php ├── phpunit.xml ├── composer.json ├── .travis.yml ├── css └── style-dfi.css ├── phpcs.xml ├── bin └── install-wp-tests.sh ├── js └── script-dfi.js ├── sponsors.php ├── README.md ├── LICENSE └── dynamic-featured-image.php /.gitignore: -------------------------------------------------------------------------------- 1 | coverage 2 | vendor 3 | .idea 4 | 5 | --no-check-certificate 6 | .DS_Store 7 | -------------------------------------------------------------------------------- /img/spinner.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitpokhrel/Dynamic-Featured-Image/HEAD/img/spinner.gif -------------------------------------------------------------------------------- /.github/screenshots/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitpokhrel/Dynamic-Featured-Image/HEAD/.github/screenshots/1.png -------------------------------------------------------------------------------- /.github/screenshots/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitpokhrel/Dynamic-Featured-Image/HEAD/.github/screenshots/2.png -------------------------------------------------------------------------------- /.github/screenshots/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitpokhrel/Dynamic-Featured-Image/HEAD/.github/screenshots/3.png -------------------------------------------------------------------------------- /.github/screenshots/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitpokhrel/Dynamic-Featured-Image/HEAD/.github/screenshots/4.png -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "browser": true, 3 | "globals": { 4 | "wp": true, 5 | "DFI_SPECIFIC": true 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /languages/dynamic-featured-image-bs_BA.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitpokhrel/Dynamic-Featured-Image/HEAD/languages/dynamic-featured-image-bs_BA.mo -------------------------------------------------------------------------------- /languages/dynamic-featured-image-de_DE.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitpokhrel/Dynamic-Featured-Image/HEAD/languages/dynamic-featured-image-de_DE.mo -------------------------------------------------------------------------------- /languages/dynamic-featured-image-he_IL.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitpokhrel/Dynamic-Featured-Image/HEAD/languages/dynamic-featured-image-he_IL.mo -------------------------------------------------------------------------------- /languages/dynamic-featured-image-hr_HR.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitpokhrel/Dynamic-Featured-Image/HEAD/languages/dynamic-featured-image-hr_HR.mo -------------------------------------------------------------------------------- /languages/dynamic-featured-image-it_IT.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitpokhrel/Dynamic-Featured-Image/HEAD/languages/dynamic-featured-image-it_IT.mo -------------------------------------------------------------------------------- /languages/dynamic-featured-image-ne_NP.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitpokhrel/Dynamic-Featured-Image/HEAD/languages/dynamic-featured-image-ne_NP.mo -------------------------------------------------------------------------------- /languages/dynamic-featured-image-pt_BR.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitpokhrel/Dynamic-Featured-Image/HEAD/languages/dynamic-featured-image-pt_BR.mo -------------------------------------------------------------------------------- /languages/dynamic-featured-image-sr_RS.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitpokhrel/Dynamic-Featured-Image/HEAD/languages/dynamic-featured-image-sr_RS.mo -------------------------------------------------------------------------------- /languages/dynamic-featured-image-sv_SE.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitpokhrel/Dynamic-Featured-Image/HEAD/languages/dynamic-featured-image-sv_SE.mo -------------------------------------------------------------------------------- /languages/dynamic-featured-image-th_TH.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankitpokhrel/Dynamic-Featured-Image/HEAD/languages/dynamic-featured-image-th_TH.mo -------------------------------------------------------------------------------- /.scrutinizer.yml: -------------------------------------------------------------------------------- 1 | --- 2 | checks: 3 | php: 4 | code_rating: true 5 | 6 | filter: 7 | excluded_paths: 8 | - tests/* 9 | - uninstall.php 10 | 11 | tools: 12 | php_code_sniffer: 13 | config: { standard: WordPress } 14 | external_code_coverage: 15 | timeout: 1200 16 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dynamic-featured-image", 3 | "version": "3.7.0", 4 | "main": "dynamic-featured-image.php", 5 | "ignore": [ 6 | ".bowerrc", 7 | ".gitignore", 8 | ".jshintrc", 9 | "bower.json", 10 | "composer.json", 11 | ".scrutinizer.yml", 12 | ".travis.yml", 13 | "phpunit.xml", 14 | "phpcs.xml", 15 | "README.md" 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /uninstall.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | 10 | if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { 11 | exit(); 12 | } 13 | 14 | // cleanup plugin data. 15 | delete_post_meta_by_key( 'dfiFeatured' ); 16 | delete_post_meta_by_key( '_dfi_link_to_image' ); 17 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | ./tests/ 12 | 13 | 14 | 15 | 16 | ./dynamic-featured-image.php 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ankitpokhrel/dynamic-featured-image", 3 | "description": "Dynamically adds multiple featured image (post thumbnail) functionality to posts, pages and custom post types.", 4 | "keywords": [ 5 | "dynamic featured image", 6 | "multiple featured image", 7 | "multiple post thumbnails", 8 | "wordpress plugin" 9 | ], 10 | "license": "GPL-2.0-or-later", 11 | "authors": [ 12 | { 13 | "name": "Ankit Pokhrel", 14 | "email": "hello@ankit.pl", 15 | "homepage": "https://ankit.pl", 16 | "role": "Developer" 17 | } 18 | ], 19 | "require-dev": { 20 | "phpunit/phpunit": "~5.7" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: php 3 | dist: trusty 4 | 5 | php: 6 | - 5.6 7 | - 7.0 8 | - 7.2 9 | 10 | env: 11 | - WP_VERSION=latest WP_MULTISITE=0 12 | - WP_VERSION=latest WP_MULTISITE=1 13 | - WP_VERSION=4.9 WP_MULTISITE=0 14 | - WP_VERSION=4.9 WP_MULTISITE=1 15 | - WP_VERSION=4.7 WP_MULTISITE=0 16 | - WP_VERSION=4.7 WP_MULTISITE=1 17 | 18 | cache: 19 | directories: 20 | - vendor 21 | - $HOME/.composer/cache 22 | 23 | before_script: 24 | - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION 25 | 26 | script: 27 | - composer install 28 | - vendor/bin/phpunit --coverage-clover=coverage.clover 29 | 30 | after_script: 31 | - wget https://scrutinizer-ci.com/ocular.phar 32 | - php ocular.phar code-coverage:upload --format=php-clover coverage.clover 33 | 34 | sudo: false 35 | 36 | notifications: 37 | email: false 38 | -------------------------------------------------------------------------------- /languages/dynamic-featured-image-pt_BR.po: -------------------------------------------------------------------------------- 1 | msgid "" 2 | msgstr "" 3 | "Project-Id-Version: dynamic-featured-image\n" 4 | "POT-Creation-Date: 2015-02-28 09:09-0300\n" 5 | "PO-Revision-Date: 2018-02-26 21:05+0100\n" 6 | "Last-Translator: Matthias Wirtz \n" 7 | "Language-Team: Ankit Pokhrel \n" 8 | "MIME-Version: 1.0\n" 9 | "Content-Type: text/plain; charset=UTF-8\n" 10 | "Content-Transfer-Encoding: 8bit\n" 11 | "X-Generator: Poedit 2.0.6\n" 12 | "X-Poedit-KeywordsList: _;gettext;gettext_noop;__;_e\n" 13 | "X-Poedit-Basepath: .\n" 14 | "Language: pt\n" 15 | 16 | msgid "Dynamic Featured Image - Media Selector" 17 | msgstr "Dynamic Featured Image - Seletor de Mídia" 18 | 19 | msgid "Set Featured Image" 20 | msgstr "Definir Imagem Destacada" 21 | 22 | msgid "Featured Image" 23 | msgstr "Imagem Destacada" 24 | 25 | msgid "Add New" 26 | msgstr "Adicionar" 27 | 28 | msgid "Remove" 29 | msgstr "Remover" 30 | 31 | msgid "Link to Image" 32 | msgstr "Link para Imagem" 33 | 34 | msgid "" 35 | "ATTENTION! Please read the DOCUMENTATION properly before " 37 | "update." 38 | msgstr "" 39 | "ATENÇÃO! Por favor leia a DOCUMENTAÇÃO corretamente antes " 41 | "de atualizar." 42 | -------------------------------------------------------------------------------- /languages/dynamic-featured-image-th_TH.po: -------------------------------------------------------------------------------- 1 | msgid "" 2 | msgstr "" 3 | "Project-Id-Version: dynamic-featured-image\n" 4 | "POT-Creation-Date: 2017-10-21 16:43+0545\n" 5 | "PO-Revision-Date: 2018-02-26 21:07+0100\n" 6 | "Last-Translator: Matthias Wirtz \n" 7 | "Language-Team: Ankit Pokhrel \n" 8 | "MIME-Version: 1.0\n" 9 | "Content-Type: text/plain; charset=UTF-8\n" 10 | "Content-Transfer-Encoding: 8bit\n" 11 | "X-Generator: Poedit 2.0.6\n" 12 | "X-Poedit-KeywordsList: _;gettext;gettext_noop;__;_e\n" 13 | "X-Poedit-Basepath: .\n" 14 | "Language: th\n" 15 | 16 | msgid "Featured Image" 17 | msgstr "รูปภาพเด่น" 18 | 19 | msgid "Dynamic Featured Image - Media Selector" 20 | msgstr "รูปภาพเด่นแบบไดนามิก - ตัวเลือกรุปภาพ" 21 | 22 | msgid "Set Featured Image" 23 | msgstr "ตั้งค่ารูปภาพเด่น" 24 | 25 | msgid "Add New" 26 | msgstr "เพิ่มใหม่" 27 | 28 | msgid "Remove" 29 | msgstr "ลบ" 30 | 31 | msgid "" 32 | "ATTENTION! Please read the DOCUMENTATION properly before " 34 | "update." 35 | msgstr "" 36 | "โปรดทราบ!! กรุณาอ่าน เอกสาร ให้ถี่ถ้วนก่อนทำการอัพเดท." 38 | 39 | msgid "0" 40 | msgstr "0" 41 | 42 | msgid "1" 43 | msgstr "1" 44 | 45 | msgid "2" 46 | msgstr "2" 47 | 48 | msgid "3" 49 | msgstr "3" 50 | 51 | msgid "4" 52 | msgstr "4" 53 | 54 | msgid "5" 55 | msgstr "5" 56 | 57 | msgid "6" 58 | msgstr "6" 59 | 60 | msgid "7" 61 | msgstr "7" 62 | 63 | msgid "8" 64 | msgstr "8" 65 | 66 | msgid "9" 67 | msgstr "9" 68 | -------------------------------------------------------------------------------- /languages/dynamic-featured-image-he_IL.po: -------------------------------------------------------------------------------- 1 | msgid "" 2 | msgstr "" 3 | "Project-Id-Version: dynamic-featured-image\n" 4 | "POT-Creation-Date: 2014-07-19 16:43+0545\n" 5 | "PO-Revision-Date: 2018-02-26 21:04+0100\n" 6 | "Last-Translator: Matthias Wirtz \n" 7 | "Language-Team: Ankit Pokhrel \n" 8 | "MIME-Version: 1.0\n" 9 | "Content-Type: text/plain; charset=UTF-8\n" 10 | "Content-Transfer-Encoding: 8bit\n" 11 | "X-Generator: Poedit 2.0.6\n" 12 | "X-Poedit-KeywordsList: _;gettext;gettext_noop;__;_e\n" 13 | "X-Poedit-Basepath: .\n" 14 | "Language: he\n" 15 | 16 | msgid "Featured Image" 17 | msgstr "תמונה ראשית" 18 | 19 | msgid "Dynamic Featured Image - Media Selector" 20 | msgstr "תמונה ראשית דינאמית - בחירת מדיה" 21 | 22 | msgid "Set Featured Image" 23 | msgstr "קבע תמונה ראשית" 24 | 25 | msgid "Add New" 26 | msgstr "הוסף תמונה" 27 | 28 | msgid "Remove" 29 | msgstr "הסר" 30 | 31 | msgid "" 32 | "ATTENTION! Please read the DOCUMENTATION properly before " 34 | "update." 35 | msgstr "" 36 | "לתשומת לבך! אנא קרא את הדוקומנטציה היטב לפני שדרוג " 38 | "התוסף." 39 | 40 | msgid "0" 41 | msgstr "0" 42 | 43 | msgid "1" 44 | msgstr "1" 45 | 46 | msgid "2" 47 | msgstr "2" 48 | 49 | msgid "3" 50 | msgstr "3" 51 | 52 | msgid "4" 53 | msgstr "4" 54 | 55 | msgid "5" 56 | msgstr "5" 57 | 58 | msgid "6" 59 | msgstr "6" 60 | 61 | msgid "7" 62 | msgstr "7" 63 | 64 | msgid "8" 65 | msgstr "8" 66 | 67 | msgid "9" 68 | msgstr "9" 69 | -------------------------------------------------------------------------------- /languages/dynamic-featured-image-bs_BA.po: -------------------------------------------------------------------------------- 1 | msgid "" 2 | msgstr "" 3 | "Project-Id-Version: dynamic-featured-image\n" 4 | "POT-Creation-Date: 2014-07-19 16:43+0545\n" 5 | "PO-Revision-Date: 2018-02-26 21:03+0100\n" 6 | "Last-Translator: Matthias Wirtz \n" 7 | "Language-Team: Ankit Pokhrel \n" 8 | "MIME-Version: 1.0\n" 9 | "Content-Type: text/plain; charset=UTF-8\n" 10 | "Content-Transfer-Encoding: 8bit\n" 11 | "X-Generator: Poedit 2.0.6\n" 12 | "X-Poedit-KeywordsList: _;gettext;gettext_noop;__;_e\n" 13 | "X-Poedit-Basepath: .\n" 14 | "Language: bs\n" 15 | 16 | msgid "Featured Image" 17 | msgstr "Glavna slika" 18 | 19 | msgid "Dynamic Featured Image - Media Selector" 20 | msgstr "Dinamična Glavna Slika – Izbor Media" 21 | 22 | msgid "Set Featured Image" 23 | msgstr "Postavite glavnu sliku" 24 | 25 | msgid "Add New" 26 | msgstr "Dodoajte novu" 27 | 28 | msgid "Remove" 29 | msgstr "Uklonite" 30 | 31 | msgid "" 32 | "ATTENTION! Please read the DOCUMENTATION properly before " 34 | "update." 35 | msgstr "" 36 | "PAŽNJA! Molimo vas pažljivo pročitajte DOKUMENTACIJU pre ažuriranja." 39 | 40 | msgid "0" 41 | msgstr "0" 42 | 43 | msgid "1" 44 | msgstr "1" 45 | 46 | msgid "2" 47 | msgstr "2" 48 | 49 | msgid "3" 50 | msgstr "3" 51 | 52 | msgid "4" 53 | msgstr "4" 54 | 55 | msgid "5" 56 | msgstr "5" 57 | 58 | msgid "6" 59 | msgstr "6" 60 | 61 | msgid "7" 62 | msgstr "7" 63 | 64 | msgid "8" 65 | msgstr "8" 66 | 67 | msgid "9" 68 | msgstr "9" 69 | -------------------------------------------------------------------------------- /languages/dynamic-featured-image-hr_HR.po: -------------------------------------------------------------------------------- 1 | msgid "" 2 | msgstr "" 3 | "Project-Id-Version: dynamic-featured-image\n" 4 | "POT-Creation-Date: 2014-07-19 16:43+0545\n" 5 | "PO-Revision-Date: 2018-02-26 21:04+0100\n" 6 | "Last-Translator: Matthias Wirtz \n" 7 | "Language-Team: Ankit Pokhrel \n" 8 | "MIME-Version: 1.0\n" 9 | "Content-Type: text/plain; charset=UTF-8\n" 10 | "Content-Transfer-Encoding: 8bit\n" 11 | "X-Generator: Poedit 2.0.6\n" 12 | "X-Poedit-KeywordsList: _;gettext;gettext_noop;__;_e\n" 13 | "X-Poedit-Basepath: .\n" 14 | "Language: hr\n" 15 | 16 | msgid "Featured Image" 17 | msgstr "Glavna slika" 18 | 19 | msgid "Dynamic Featured Image - Media Selector" 20 | msgstr "Dinamična Glavna Slika – Izbor Media" 21 | 22 | msgid "Set Featured Image" 23 | msgstr "Postavite glavnu sliku" 24 | 25 | msgid "Add New" 26 | msgstr "Dodoajte novu" 27 | 28 | msgid "Remove" 29 | msgstr "Uklonite" 30 | 31 | msgid "" 32 | "ATTENTION! Please read the DOCUMENTATION properly before " 34 | "update." 35 | msgstr "" 36 | "PAŽNJA! Molimo vas pažljivo pročitajte DOKUMENTACIJU pre ažuriranja." 39 | 40 | msgid "0" 41 | msgstr "0" 42 | 43 | msgid "1" 44 | msgstr "1" 45 | 46 | msgid "2" 47 | msgstr "2" 48 | 49 | msgid "3" 50 | msgstr "3" 51 | 52 | msgid "4" 53 | msgstr "4" 54 | 55 | msgid "5" 56 | msgstr "5" 57 | 58 | msgid "6" 59 | msgstr "6" 60 | 61 | msgid "7" 62 | msgstr "7" 63 | 64 | msgid "8" 65 | msgstr "8" 66 | 67 | msgid "9" 68 | msgstr "9" 69 | -------------------------------------------------------------------------------- /languages/dynamic-featured-image-sr_RS.po: -------------------------------------------------------------------------------- 1 | msgid "" 2 | msgstr "" 3 | "Project-Id-Version: dynamic-featured-image\n" 4 | "POT-Creation-Date: 2014-07-19 16:43+0545\n" 5 | "PO-Revision-Date: 2018-02-26 21:07+0100\n" 6 | "Last-Translator: Matthias Wirtz \n" 7 | "Language-Team: Ankit Pokhrel \n" 8 | "MIME-Version: 1.0\n" 9 | "Content-Type: text/plain; charset=UTF-8\n" 10 | "Content-Transfer-Encoding: 8bit\n" 11 | "X-Generator: Poedit 2.0.6\n" 12 | "X-Poedit-KeywordsList: _;gettext;gettext_noop;__;_e\n" 13 | "X-Poedit-Basepath: .\n" 14 | "Language: sr\n" 15 | 16 | msgid "Featured Image" 17 | msgstr "Glavna slika" 18 | 19 | msgid "Dynamic Featured Image - Media Selector" 20 | msgstr "Dinamična Glavna Slika – Izbor Media" 21 | 22 | msgid "Set Featured Image" 23 | msgstr "Postavite glavnu sliku" 24 | 25 | msgid "Add New" 26 | msgstr "Dodoajte novu" 27 | 28 | msgid "Remove" 29 | msgstr "Uklonite" 30 | 31 | msgid "" 32 | "ATTENTION! Please read the DOCUMENTATION properly before " 34 | "update." 35 | msgstr "" 36 | "PAŽNJA! Molimo vas pažljivo pročitajte DOKUMENTACIJU pre ažuriranja." 39 | 40 | msgid "0" 41 | msgstr "0" 42 | 43 | msgid "1" 44 | msgstr "1" 45 | 46 | msgid "2" 47 | msgstr "2" 48 | 49 | msgid "3" 50 | msgstr "3" 51 | 52 | msgid "4" 53 | msgstr "4" 54 | 55 | msgid "5" 56 | msgstr "5" 57 | 58 | msgid "6" 59 | msgstr "6" 60 | 61 | msgid "7" 62 | msgstr "7" 63 | 64 | msgid "8" 65 | msgstr "8" 66 | 67 | msgid "9" 68 | msgstr "9" 69 | -------------------------------------------------------------------------------- /languages/dynamic-featured-image-ne_NP.po: -------------------------------------------------------------------------------- 1 | msgid "" 2 | msgstr "" 3 | "Project-Id-Version: dynamic-featured-image\n" 4 | "POT-Creation-Date: 2014-07-19 16:43+0545\n" 5 | "PO-Revision-Date: 2018-02-26 21:04+0100\n" 6 | "Last-Translator: Matthias Wirtz \n" 7 | "Language-Team: Ankit Pokhrel \n" 8 | "MIME-Version: 1.0\n" 9 | "Content-Type: text/plain; charset=UTF-8\n" 10 | "Content-Transfer-Encoding: 8bit\n" 11 | "X-Generator: Poedit 2.0.6\n" 12 | "X-Poedit-KeywordsList: _;gettext;gettext_noop;__;_e\n" 13 | "X-Poedit-Basepath: .\n" 14 | "Language: ne\n" 15 | 16 | msgid "Featured Image" 17 | msgstr "फीचर्ड इमेज" 18 | 19 | msgid "Dynamic Featured Image - Media Selector" 20 | msgstr "डाइनामिक फीचर्ड इमेज - मिडिया सेलेक्टर" 21 | 22 | msgid "Set Featured Image" 23 | msgstr "फीचर्ड इमेज राख्नुहोस्" 24 | 25 | msgid "Add New" 26 | msgstr "नयाँ" 27 | 28 | msgid "Remove" 29 | msgstr "हटाउनुहोस्" 30 | 31 | msgid "" 32 | "ATTENTION! Please read the DOCUMENTATION properly before update." 34 | msgstr "" 35 | "साबधान! कृपया अपडेट गर्नु अगाडि डकुमेन्टेसन राम्रोसँग पढ्नु होला |" 37 | 38 | msgid "Link to Image" 39 | msgstr "तस्बिरको लिंक" 40 | 41 | msgid "0" 42 | msgstr "०" 43 | 44 | msgid "1" 45 | msgstr "१" 46 | 47 | msgid "2" 48 | msgstr "२" 49 | 50 | msgid "3" 51 | msgstr "३" 52 | 53 | msgid "4" 54 | msgstr "४" 55 | 56 | msgid "5" 57 | msgstr "५" 58 | 59 | msgid "6" 60 | msgstr "६" 61 | 62 | msgid "7" 63 | msgstr "७" 64 | 65 | msgid "8" 66 | msgstr "८" 67 | 68 | msgid "9" 69 | msgstr "९" 70 | -------------------------------------------------------------------------------- /languages/dynamic-featured-image-sv_SE.po: -------------------------------------------------------------------------------- 1 | msgid "" 2 | msgstr "" 3 | "Project-Id-Version: dynamic-featured-image\n" 4 | "POT-Creation-Date: 2014-07-19 16:43+0545\n" 5 | "PO-Revision-Date: 2018-03-14 22:48+0000\n" 6 | "Last-Translator: Dennis Germundal \n" 7 | "Language-Team: Swedish\n" 8 | "MIME-Version: 1.0\n" 9 | "Content-Type: text/plain; charset=UTF-8\n" 10 | "Content-Transfer-Encoding: 8bit\n" 11 | "X-Generator: Loco https://localise.biz/\n" 12 | "X-Poedit-KeywordsList: _;gettext;gettext_noop;__;_e\n" 13 | "X-Poedit-Basepath: .\n" 14 | "Language: sv_SE\n" 15 | "Report-Msgid-Bugs-To: \n" 16 | "Plural-Forms: nplurals=2; plural=n != 1;" 17 | 18 | msgid "Featured Image" 19 | msgstr "Utvald Bild" 20 | 21 | msgid "Dynamic Featured Image - Media Selector" 22 | msgstr "Dynamic Featured Image - Välj bild" 23 | 24 | msgid "Set Featured Image" 25 | msgstr "Ange utvald bild" 26 | 27 | msgid "Add New" 28 | msgstr "Lägg till" 29 | 30 | msgid "Remove" 31 | msgstr "Ta bort" 32 | 33 | msgid "" 34 | "ATTENTION! Please read the DOCUMENTATION properly before " 36 | "update." 37 | msgstr "" 38 | "VIKTIGT! Vänligen läs DOKUMENTATIONEN innan " 40 | "uppdatering." 41 | 42 | msgid "0" 43 | msgstr "0" 44 | 45 | msgid "1" 46 | msgstr "1" 47 | 48 | msgid "2" 49 | msgstr "2" 50 | 51 | msgid "3" 52 | msgstr "3" 53 | 54 | msgid "4" 55 | msgstr "4" 56 | 57 | msgid "5" 58 | msgstr "5" 59 | 60 | msgid "6" 61 | msgstr "6" 62 | 63 | msgid "7" 64 | msgstr "7" 65 | 66 | msgid "8" 67 | msgstr "8" 68 | 69 | msgid "9" 70 | msgstr "9" 71 | -------------------------------------------------------------------------------- /languages/dynamic-featured-image-it_IT.po: -------------------------------------------------------------------------------- 1 | msgid "" 2 | msgstr "" 3 | "Project-Id-Version: dynamic-featured-image\n" 4 | "POT-Creation-Date: 2014-07-19 16:43+0545\n" 5 | "PO-Revision-Date: 2018-02-26 21:04+0100\n" 6 | "Last-Translator: Matthias Wirtz \n" 7 | "Language-Team: Ankit Pokhrel \n" 8 | "MIME-Version: 1.0\n" 9 | "Content-Type: text/plain; charset=UTF-8\n" 10 | "Content-Transfer-Encoding: 8bit\n" 11 | "X-Generator: Poedit 2.0.6\n" 12 | "X-Poedit-KeywordsList: _;gettext;gettext_noop;__;_e\n" 13 | "X-Poedit-Basepath: .\n" 14 | "Language: it\n" 15 | 16 | msgid "Featured Image" 17 | msgstr "Immagine in Primo Piano" 18 | 19 | msgid "Dynamic Featured Image - Media Selector" 20 | msgstr "Dynamic Featured Image - Selettore Multimediale" 21 | 22 | msgid "Set Featured Image" 23 | msgstr "Imposta Immagine in Primo Piano" 24 | 25 | msgid "Add New" 26 | msgstr "Aggiungi Nuova" 27 | 28 | msgid "Remove" 29 | msgstr "Rimuovere" 30 | 31 | msgid "" 32 | "ATTENTION! Please read the DOCUMENTATION properly before " 34 | "update." 35 | msgstr "" 36 | "ATTENZIONE! Si prega di leggere la DOCUMENTAZIONE prima di " 38 | "effettuare l'aggiornamento." 39 | 40 | msgid "0" 41 | msgstr "0" 42 | 43 | msgid "1" 44 | msgstr "1" 45 | 46 | msgid "2" 47 | msgstr "2" 48 | 49 | msgid "3" 50 | msgstr "3" 51 | 52 | msgid "4" 53 | msgstr "4" 54 | 55 | msgid "5" 56 | msgstr "5" 57 | 58 | msgid "6" 59 | msgstr "6" 60 | 61 | msgid "7" 62 | msgstr "7" 63 | 64 | msgid "8" 65 | msgstr "8" 66 | 67 | msgid "9" 68 | msgstr "9" 69 | -------------------------------------------------------------------------------- /css/style-dfi.css: -------------------------------------------------------------------------------- 1 | /** 2 | * @file style-dfi.css 3 | * 4 | * Style for dynamic featured image plugin 5 | * 6 | * Copyright (c) 2013, Ankit Pokhrel 7 | */ 8 | 9 | .dfiAddNew { 10 | float: left; 11 | margin-top: 3px; 12 | } 13 | 14 | .dfiRemove { 15 | float: right; 16 | } 17 | 18 | .dfiLinks{ 19 | padding: 2px 0; 20 | } 21 | 22 | .dfiClearFloat { 23 | clear: both; 24 | } 25 | 26 | .featured-meta-box a:not(.hasFeaturedImage):not(.dfiAddNew) { 27 | display: inline-block !important; 28 | margin: 3px 0; 29 | } 30 | 31 | img.dfiImg { 32 | max-width: 258px; 33 | } 34 | 35 | img.dfiImg[src=""]{ 36 | display: none; 37 | } 38 | 39 | img.dfiImgEmpty { 40 | display: none; 41 | } 42 | 43 | .dfiLoading { 44 | background: url("../img/spinner.gif") no-repeat; 45 | display: inline-block; 46 | height: 20px; 47 | position: relative; 48 | top: 2px; 49 | width: 20px; 50 | } 51 | 52 | .dfiFeaturedImage { 53 | border: 2px dashed #AAA; 54 | color: #CCC; 55 | height: 130px; 56 | margin: 10px 0 !important; 57 | padding: 10px; 58 | text-align: center; 59 | width: 235px; 60 | } 61 | 62 | .dfiFeaturedImage:hover{ 63 | border-color: #999; 64 | } 65 | 66 | .dfiFeaturedImage > span { 67 | display: inline-block; 68 | font-size: 54px; 69 | margin-top: 17%; 70 | margin-right: 11%; 71 | } 72 | 73 | .hasFeaturedImage { 74 | border: none; 75 | width: auto; 76 | height: auto; 77 | margin: 0 !important; 78 | position: absolute; 79 | top: 30%; 80 | left: 37%; 81 | display: none; 82 | z-index: 999; 83 | } 84 | 85 | .dashicons { 86 | text-decoration: none !important; 87 | } 88 | 89 | .dashicons:hover, .dfiFeaturedImage:hover { 90 | color: #2ea2cc !important; 91 | } 92 | -------------------------------------------------------------------------------- /languages/dynamic-featured-image-de_DE.po: -------------------------------------------------------------------------------- 1 | msgid "" 2 | msgstr "" 3 | "Project-Id-Version: dynamic-featured-image\n" 4 | "POT-Creation-Date: 2014-07-19 16:43+0545\n" 5 | "PO-Revision-Date: 2018-02-26 19:58+0100\n" 6 | "Language-Team: Ankit Pokhrel \n" 7 | "MIME-Version: 1.0\n" 8 | "Content-Type: text/plain; charset=UTF-8\n" 9 | "Content-Transfer-Encoding: 8bit\n" 10 | "X-Generator: Poedit 2.0.6\n" 11 | "X-Poedit-KeywordsList: _;gettext;gettext_noop;__;_e\n" 12 | "X-Poedit-Basepath: .\n" 13 | "Last-Translator: Matthias Wirtz \n" 14 | "Plural-Forms: nplurals=2; plural=(n != 1);\n" 15 | "Language: de_DE\n" 16 | 17 | msgid "Featured Image" 18 | msgstr "Beitragsbild" 19 | 20 | msgid "Dynamic Featured Image - Media Selector" 21 | msgstr "Dynamic Featured Image - Beitragsbild" 22 | 23 | msgid "Set Featured Image" 24 | msgstr "Beitragsbild festlegen" 25 | 26 | msgid "Add New" 27 | msgstr "Beitragsbild hinzufügen" 28 | 29 | msgid "Remove" 30 | msgstr "Beitragsbild entfernen" 31 | 32 | msgid "" 33 | "ATTENTION! Please read the DOCUMENTATION properly before " 35 | "update." 36 | msgstr "" 37 | "ACHTUNG! Bitte die DOKUMENTATION vor einem Update " 39 | "vollständig lesen." 40 | 41 | msgid "Link to Image" 42 | msgstr "Link zum Beitragsbild" 43 | 44 | msgid "0" 45 | msgstr "0" 46 | 47 | msgid "1" 48 | msgstr "1" 49 | 50 | msgid "2" 51 | msgstr "2" 52 | 53 | msgid "3" 54 | msgstr "3" 55 | 56 | msgid "4" 57 | msgstr "4" 58 | 59 | msgid "5" 60 | msgstr "5" 61 | 62 | msgid "6" 63 | msgstr "6" 64 | 65 | msgid "7" 66 | msgstr "7" 67 | 68 | msgid "8" 69 | msgstr "8" 70 | 71 | msgid "9" 72 | msgstr "9" 73 | -------------------------------------------------------------------------------- /phpcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | Coding Standard for Dynamic Featured Image Plugin 13 | 14 | 15 | vendor/* 16 | node_modules/* 17 | tests/* 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /tests/test-dynamic-featured-image-ajax.php: -------------------------------------------------------------------------------- 1 | post_id = $this->factory->post->create( [ 17 | 'post_title' => 'Dynamic Featured Image WordPress Plugin', 18 | ] ); 19 | 20 | $this->_setRole( 'administrator' ); 21 | } 22 | 23 | /** 24 | * @test 25 | * 26 | * @covers ::ajax_callback 27 | */ 28 | public function it_fails_on_referer_check() { 29 | try { 30 | $this->_handleAjax( 'dfiMetaBox_callback' ); 31 | } catch ( WPAjaxDieStopException $e ) { 32 | } 33 | 34 | // it should throw exception 35 | $this->assertTrue( isset( $e ) ); 36 | 37 | // exception message must be -1 38 | $this->assertEquals( '-1', $e->getMessage() ); 39 | } 40 | 41 | /** 42 | * @test 43 | * 44 | * @covers ::ajax_callback 45 | */ 46 | public function it_executes_ajax_callback() { 47 | 48 | $expectedOutput = '
49 | 50 | 54 |
55 | '; 56 | 57 | $expectedOutput = preg_replace( '/\s+/', '', $expectedOutput ); 58 | $plugin_folder = preg_replace( '/tests\/' . basename( __FILE__ ) . '/', '', plugin_basename( __FILE__ ) ); 59 | 60 | $_POST = [ 61 | 'id' => $this->post_id, 62 | 'security' => wp_create_nonce( $plugin_folder . 'dynamic-featured-image.php' ), 63 | ]; 64 | 65 | try { 66 | $this->_handleAjax( 'dfiMetaBox_callback' ); 67 | } catch ( WPAjaxDieContinueException $e ) { 68 | } 69 | 70 | // it should throw exception 71 | $this->assertTrue( isset( $e ) ); 72 | 73 | // exception message must be empty 74 | $this->assertEquals( '', $e->getMessage() ); 75 | 76 | $response = preg_replace( '/\s+/', '', $this->_last_response ); 77 | 78 | // should contain expected output 79 | $this->assertContains( $expectedOutput, $response ); 80 | } 81 | 82 | public function tearDown() { 83 | unset( $this->post_id ); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /bin/install-wp-tests.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [ $# -lt 3 ]; then 4 | echo "usage: $0 [db-host] [wp-version] [skip-database-creation]" 5 | exit 1 6 | fi 7 | 8 | DB_NAME=$1 9 | DB_USER=$2 10 | DB_PASS=$3 11 | DB_HOST=${4-localhost} 12 | WP_VERSION=${5-latest} 13 | SKIP_DB_CREATE=${6-false} 14 | 15 | TMPDIR=${TMPDIR-/tmp} 16 | TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//") 17 | WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib} 18 | WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress/} 19 | 20 | download() { 21 | if [ `which curl` ]; then 22 | curl -s "$1" > "$2"; 23 | elif [ `which wget` ]; then 24 | wget -nv -O "$2" "$1" 25 | fi 26 | } 27 | 28 | if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then 29 | WP_TESTS_TAG="branches/$WP_VERSION" 30 | elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then 31 | if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then 32 | # version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x 33 | WP_TESTS_TAG="tags/${WP_VERSION%??}" 34 | else 35 | WP_TESTS_TAG="tags/$WP_VERSION" 36 | fi 37 | elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then 38 | WP_TESTS_TAG="trunk" 39 | else 40 | # http serves a single offer, whereas https serves multiple. we only want one 41 | download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json 42 | grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json 43 | LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//') 44 | if [[ -z "$LATEST_VERSION" ]]; then 45 | echo "Latest WordPress version could not be found" 46 | exit 1 47 | fi 48 | WP_TESTS_TAG="tags/$LATEST_VERSION" 49 | fi 50 | 51 | set -ex 52 | 53 | install_wp() { 54 | 55 | if [ -d $WP_CORE_DIR ]; then 56 | return; 57 | fi 58 | 59 | mkdir -p $WP_CORE_DIR 60 | 61 | if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then 62 | mkdir -p $TMPDIR/wordpress-nightly 63 | download https://wordpress.org/nightly-builds/wordpress-latest.zip $TMPDIR/wordpress-nightly/wordpress-nightly.zip 64 | unzip -q $TMPDIR/wordpress-nightly/wordpress-nightly.zip -d $TMPDIR/wordpress-nightly/ 65 | mv $TMPDIR/wordpress-nightly/wordpress/* $WP_CORE_DIR 66 | else 67 | if [ $WP_VERSION == 'latest' ]; then 68 | local ARCHIVE_NAME='latest' 69 | elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+ ]]; then 70 | # https serves multiple offers, whereas http serves single. 71 | download https://api.wordpress.org/core/version-check/1.7/ $TMPDIR/wp-latest.json 72 | if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then 73 | # version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x 74 | LATEST_VERSION=${WP_VERSION%??} 75 | else 76 | # otherwise, scan the releases and get the most up to date minor version of the major release 77 | local VERSION_ESCAPED=`echo $WP_VERSION | sed 's/\./\\\\./g'` 78 | LATEST_VERSION=$(grep -o '"version":"'$VERSION_ESCAPED'[^"]*' $TMPDIR/wp-latest.json | sed 's/"version":"//' | head -1) 79 | fi 80 | if [[ -z "$LATEST_VERSION" ]]; then 81 | local ARCHIVE_NAME="wordpress-$WP_VERSION" 82 | else 83 | local ARCHIVE_NAME="wordpress-$LATEST_VERSION" 84 | fi 85 | else 86 | local ARCHIVE_NAME="wordpress-$WP_VERSION" 87 | fi 88 | download https://wordpress.org/${ARCHIVE_NAME}.tar.gz $TMPDIR/wordpress.tar.gz 89 | tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR 90 | fi 91 | 92 | download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php 93 | } 94 | 95 | install_test_suite() { 96 | # portable in-place argument for both GNU sed and Mac OSX sed 97 | if [[ $(uname -s) == 'Darwin' ]]; then 98 | local ioption='-i .bak' 99 | else 100 | local ioption='-i' 101 | fi 102 | 103 | # set up testing suite if it doesn't yet exist 104 | if [ ! -d $WP_TESTS_DIR ]; then 105 | # set up testing suite 106 | mkdir -p $WP_TESTS_DIR 107 | svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes 108 | svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data 109 | fi 110 | 111 | if [ ! -f wp-tests-config.php ]; then 112 | download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php 113 | # remove all forward slashes in the end 114 | WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::") 115 | sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php 116 | sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php 117 | sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php 118 | sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php 119 | sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php 120 | fi 121 | 122 | } 123 | 124 | install_db() { 125 | 126 | if [ ${SKIP_DB_CREATE} = "true" ]; then 127 | return 0 128 | fi 129 | 130 | # parse DB_HOST for port or socket references 131 | local PARTS=(${DB_HOST//\:/ }) 132 | local DB_HOSTNAME=${PARTS[0]}; 133 | local DB_SOCK_OR_PORT=${PARTS[1]}; 134 | local EXTRA="" 135 | 136 | if ! [ -z $DB_HOSTNAME ] ; then 137 | if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then 138 | EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp" 139 | elif ! [ -z $DB_SOCK_OR_PORT ] ; then 140 | EXTRA=" --socket=$DB_SOCK_OR_PORT" 141 | elif ! [ -z $DB_HOSTNAME ] ; then 142 | EXTRA=" --host=$DB_HOSTNAME --protocol=tcp" 143 | fi 144 | fi 145 | 146 | # create database 147 | mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA 148 | } 149 | 150 | install_wp 151 | install_test_suite 152 | install_db 153 | -------------------------------------------------------------------------------- /js/script-dfi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Script for dynamic featured image plugin. 3 | * 4 | * @package dynamic-featured-image 5 | * @subpackage js 6 | * 7 | * Copyright (c) 2013, Ankit Pokhrel 8 | */ 9 | 10 | jQuery( document ).ready( function ( $ ) { 11 | var current = null; 12 | 13 | // Add new meta box. 14 | $( document ).on( 'click', '.dfiAddNew', function () { 15 | 16 | var obj = $( this ), 17 | lastFeaturedMetaBox = $( '.featured-meta-box:last' ), 18 | id = parseInt( lastFeaturedMetaBox.find( '.dfiAddNew' ).data( 'id' ), 10 ), 19 | idLocal = lastFeaturedMetaBox.find( '.dfiAddNew' ).attr( 'data-id-local' ), 20 | newMetaBox = obj.closest( '.featured-meta-box' ).clone(); 21 | 22 | newMetaBox.find( '.hndle span' ).html( DFI_SPECIFIC.metabox_title + " " + idLocal ); 23 | newMetaBox.attr( 'id', 'dfiFeaturedMetaBox' + "-" + (++ id) ); 24 | newMetaBox.find( '.handlediv' ).addClass( 'dfiDynamicBox' ); 25 | 26 | var metaBoxContentObj = newMetaBox.find( '.inside' ); 27 | metaBoxContentObj.html( '' ); 28 | obj.hide(); 29 | obj.parent().append( '' ).hide().fadeIn( 200 ); 30 | 31 | $.ajax( { 32 | type: 'POST', 33 | url: 'admin-ajax.php', 34 | data: { 35 | action: 'dfiMetaBox_callback', 36 | security: DFI_SPECIFIC.ajax_nonce, 37 | id: id 38 | }, 39 | success: function ( response ) { 40 | metaBoxContentObj.append( response ); 41 | newMetaBox.appendTo( obj.closest( '.featured-meta-box' ).parent() ); 42 | 43 | // Add post id. 44 | newMetaBox.find( '.dfiFeaturedImage' ).attr( 'data-post-id', 45 | obj.parent().parent().find( '.dfiFeaturedImage' ).attr( 'data-post-id' ) ); 46 | 47 | var alias = obj; 48 | obj.parent().find( '.dfiLoading' ).fadeOut( 300, function () { 49 | $( this ).remove(); 50 | alias.fadeIn( 200 ); 51 | } ); 52 | } 53 | } ); 54 | 55 | } ); 56 | 57 | // Remove featured image meta box. 58 | $( document ).on( 'click', '.dfiRemove', function () { 59 | 60 | if ( confirm( 'Are you sure?' ) ) { 61 | 62 | var dfiMetaBox = $( this ).closest( '.featured-meta-box' ), 63 | totalMetaBox = $( '.featured-meta-box' ).length; 64 | 65 | if ( 1 === totalMetaBox ) { 66 | 67 | dfiMetaBox.find( '.dfiImg' ).attr( 'src', '' ); 68 | dfiMetaBox.find( '.dfiImageHolder' ).val( '' ); 69 | dfiMetaBox.find( '.dfiFeaturedImage' ) 70 | .removeClass( 'hasFeaturedImage' ) 71 | .show() 72 | .animate( { opacity: 1, display: 'inline-block' }, 600 ); 73 | 74 | } else { 75 | dfiMetaBox.fadeOut( 500, function () { 76 | $( this ).remove(); 77 | } ); 78 | } 79 | } 80 | } ); 81 | 82 | // Display custom media uploader and allow to select featured image from the media library. 83 | $( document ).on( 'click', '.dfiFeaturedImage', function () { 84 | 85 | current = $( this ); 86 | 87 | if ( null !== current ) { 88 | var dfi_uploader = wp.media( { 89 | title: DFI_SPECIFIC.mediaSelector_title, 90 | button: { 91 | text: DFI_SPECIFIC.mediaSelector_buttonText 92 | }, 93 | multiple: false, 94 | library: { 95 | type: [ 'image' ] 96 | } 97 | } ).on( 'select', function () { 98 | var attachment = dfi_uploader.state().get( 'selection' ).first().toJSON(), 99 | fullSize = attachment.url, 100 | imgUrl = (typeof attachment.sizes.thumbnail === "undefined") ? fullSize : attachment.sizes.thumbnail.url, 101 | imgUrlTrimmed, 102 | fullUrlTrimmed; 103 | 104 | imgUrlTrimmed = imgUrl.replace( DFI_SPECIFIC.upload_url, "" ); 105 | fullUrlTrimmed = fullSize.replace( DFI_SPECIFIC.upload_url, "" ); 106 | 107 | var featuredBox = current.parent(); 108 | 109 | featuredBox.find( '.fImg' ).attr( { 110 | 'src': imgUrl, 111 | 'data-src': fullSize 112 | } ); 113 | 114 | featuredBox.find( '.dfiFeaturedImage' ).addClass( 'hasFeaturedImage' ); 115 | 116 | var dfiFeaturedImages = [imgUrlTrimmed, fullUrlTrimmed]; 117 | 118 | /** 119 | * Check if medium sized image exists. 120 | * 121 | * @type object 122 | */ 123 | var medium = attachment.url; 124 | 125 | if ( typeof attachment.sizes.medium !== "undefined" ) { 126 | medium = attachment.sizes.medium.url; 127 | } 128 | 129 | featuredBox.find( 'img' ).attr( 'src', medium ).fadeIn( 200 ); 130 | featuredBox.find( 'input.dfiImageHolder' ).val( dfiFeaturedImages ); 131 | } ); 132 | 133 | dfi_uploader.on('open', function () { 134 | var attached = current.data('attachment-id'); 135 | var selection = dfi_uploader.state().get('selection'); 136 | 137 | if ( attached ) { 138 | selection.add(wp.media.attachment(attached)); 139 | } 140 | }); 141 | 142 | // Open media dialog. 143 | dfi_uploader.open(); 144 | } // End if(). 145 | 146 | return false; 147 | } ); 148 | 149 | // Enable toggle of dynamically generated featured box. 150 | $( document ).on( 'click', '.dfiDynamicBox', function () { 151 | $( this ).parent().toggleClass( 'closed' ); 152 | } ); 153 | 154 | // Add a hover animation in image. 155 | $( document ).on( { 156 | mouseenter: function () { 157 | var obj = $( this ).closest( '.featured-meta-box' ); 158 | 159 | obj.find( '.dfiImg' ).stop( true, true ).animate( { opacity: 0.3 }, 300 ); 160 | obj.find( '.hasFeaturedImage' ).fadeIn( 200 ); 161 | }, 162 | mouseleave: function () { 163 | var obj = $( this ); 164 | 165 | obj.find( '.dfiImg' ).stop( true, true ).animate( { opacity: 1 }, 300 ); 166 | obj.find( '.hasFeaturedImage' ).fadeOut( 100 ); 167 | } 168 | }, '.featured-meta-box .inside' ); 169 | 170 | } ); 171 | -------------------------------------------------------------------------------- /sponsors.php: -------------------------------------------------------------------------------- 1 | 'mailoptin/mailoptin.php', 22 | ); 23 | 24 | /** 25 | * PluginSponsor constructor. 26 | * 27 | * @since 3.6.8 28 | */ 29 | public function __construct() { 30 | // admin notices. 31 | add_action( 'admin_notices', array( $this, 'admin_notice' ) ); 32 | add_action( 'network_admin_notices', array( $this, 'admin_notice' ) ); 33 | 34 | add_action( 'admin_init', array( $this, 'dismiss_admin_notice' ) ); 35 | } 36 | 37 | /** 38 | * Dismiss admin notice. 39 | * 40 | * @since 3.6.8 41 | * @access public 42 | * 43 | * @return void 44 | */ 45 | public function dismiss_admin_notice() { 46 | if ( ! isset( $_GET['mo-adaction'] ) || $_GET['mo-adaction'] != 'mo_dismiss_adnotice' ) { 47 | return; 48 | } 49 | 50 | $url = admin_url(); 51 | update_option( 'mo_dismiss_adnotice', 'true' ); 52 | 53 | wp_redirect( $url ); 54 | exit; 55 | } 56 | 57 | /** 58 | * Add admin notices. 59 | * 60 | * @since 3.6.8 61 | * @access public 62 | * 63 | * @return void 64 | */ 65 | public function admin_notice() { 66 | if ( get_option( 'mo_dismiss_adnotice', 'false' ) == 'true' ) { 67 | return; 68 | } 69 | 70 | if ( $this->is_plugin_installed( 'mailoptin' ) && $this->is_plugin_active( 'mailoptin' ) ) { 71 | return; 72 | } 73 | 74 | $dismiss_url = esc_url_raw( 75 | add_query_arg( 76 | array( 77 | 'mo-adaction' => 'mo_dismiss_adnotice', 78 | ), 79 | admin_url() 80 | ) 81 | ); 82 | 83 | $this->notice_css(); 84 | 85 | $install_url = wp_nonce_url( 86 | admin_url( 'update.php?action=install-plugin&plugin=mailoptin' ), 87 | 'install-plugin_mailoptin' 88 | ); 89 | 90 | $activate_url = wp_nonce_url( admin_url( 'plugins.php?action=activate&plugin=mailoptin%2Fmailoptin.php' ), 91 | 'activate-plugin_mailoptin/mailoptin.php' ); 92 | ?> 93 |
94 |
95 |

96 | ', '' ); 100 | ?> 101 |

102 |

Recommended by Dynamic Featured Image plugin

103 |
104 |
105 | is_plugin_installed( 'mailoptin' ) ) : ?> 106 | 108 | 109 | 110 | 111 | is_plugin_installed( 'mailoptin' ) && ! $this->is_plugin_active( 'mailoptin' ) ) : ?> 112 | 114 | 115 | 116 | 117 |
118 | Learn more 119 |
120 |
121 | 122 | 125 | 126 |
127 | 161 | 210 | This project is no longer actively maintained. 9 | 10 | [![Download](https://img.shields.io/wordpress/plugin/dt/dynamic-featured-image.svg?style=flat-square)](https://wordpress.org/plugins/dynamic-featured-image) 11 | [![Build](https://img.shields.io/travis/ankitpokhrel/Dynamic-Featured-Image.svg?style=flat-square)](https://travis-ci.org/ankitpokhrel/Dynamic-Featured-Image) 12 | [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/ankitpokhrel/Dynamic-Featured-Image.svg?style=flat-square)](https://scrutinizer-ci.com/g/ankitpokhrel/Dynamic-Featured-Image/) 13 | 14 | _Dynamically adds multiple featured image (post thumbnail) functionality to posts, pages and custom post types._ 15 | 16 | ### Overview 17 | Why limit yourself to only one featured image if you can do some awesome stuffs with multiple featured image? Dynamic Featured Image enables the option to have MULTIPLE featured images within a post or page. It allows you to add different number of featured images to each post and page that can be collected by the various theme functions. This is especially helpful when you use other plugins, post thumbnails or sliders that use featured images. 18 | 19 | ### Installation 20 | 21 | 1. Unzip and upload the `dynamic-featured-images` directory to the plugin directory (`/wp-content/plugins/`) or install it from `Plugins->Add New->Upload` 22 | 2. Activate the plugin through the `Plugins` menu in WordPress. 23 | 3. If you don't see new featured image box, click `Screen Options` in the upper right corner of your wordpress admin and make sure that the `Featured Image 2` box is selected. 24 | 25 | ### Bower 26 | ``` 27 | bower install dynamic-featured-image 28 | ``` 29 | 30 | ### How it works? 31 | 1. After successful plugin activation go to `add` or `edit` page of posts or pages and you will notice a box for second featured image. 32 | 33 | ![New featured image box](.github/screenshots/1.png) 34 | 35 | 2. Click `Set featured image` icon, select required image from the "Dynamic Featured Image Media Selector" popup and click `Set Featured Image`. 36 | 37 | ![Dynamic Featured Image Media Selector](.github/screenshots/2.png) 38 | 39 | 3. Click on `Add New` to add new featured image or use `Remove` link to remove the featured image box. 40 | 41 | ![Featured Images](.github/screenshots/3.png) 42 | ![Featured Images](.github/screenshots/4.png) 43 | 44 | 4. After adding featured images click `publish` or `update` to save featured images. 45 | 46 | ###### _Note: The featured images are only saved when you publish or update the post._ 47 | 48 | ### Documentation 49 | * [Retrieving images in a theme](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/Retrieving-data-in-a-theme) 50 | * [Getting image title, alt and caption attributes](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API-Functions#wiki-getting-image-title-alt-and-caption-attributes) 51 | * [API](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API) 52 | 53 | ### Other Resources 54 | * [Blog](https://ankitpokhrel.com/explore/category/dynamic-featured-image/) 55 | * [FAQs](https://wordpress.org/plugins/dynamic-featured-image/faq/) 56 | * [StackOverflow Tag](https://stackoverflow.com/questions/tagged/dynamic-featured-image) 57 | 58 | #### List of Available Functions 59 | 1. [get_image_id( $image_url )](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API#wiki-1-get_image_id-image_url-) 60 | 2. [get_image_thumb( $image_url, $size = "thumbnail" )](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API#wiki-2-get_image_thumb-image_url-size--thumbnail-) 61 | 3. [get_image_url( $attachment_id, $size = "full" )](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API#wiki-3-get_image_url-attachment_id-size--full-) 62 | 4. [get_post_attachment_ids( $post_id )](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API#wiki-4-get_post_attachment_ids-post_id-) 63 | 5. [is_attached( $attachment_id, $post_id )](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API#wiki-5-is_attached-attachment_id-post_id-) 64 | 6. [get_image_title( $image_url )](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API#wiki-6-get_image_title-image_url-) 65 | 7. [get_image_title_by_id( $attachment_id )](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API#wiki-7-get_image_title_by_id-attachment_id-) 66 | 8. [get_image_alt( $image_url )](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API-Functions#wiki-8-get_image_alt-image_url-) 67 | 9. [get_image_alt_by_id( $attachment_id )](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API#wiki-9-get_image_alt_by_id-attachment_id-) 68 | 10. [get_image_caption( $image_url )](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API#wiki-10-get_image_caption-image_url-) 69 | 11. [get_image_caption_by_id( $attachment_id )](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API#wiki-11-get_image_caption_by_id-attachment_id-) 70 | 12. [get_image_description( $image_url )](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API#wiki-12-get_image_description-image_url-) 71 | 13. [get_image_description_by_id( $attachment_id )](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API#wiki-13-get_image_description_by_id-attachment_id-) 72 | 14. [get_nth_featured_image( $position, $post_id )](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API#wiki-14-get_nth_featured_image-position-post_id--null-) 73 | 15. [get_all_featured_images( $post_id )](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API#15-get_all_featured_images-post_id-) 74 | 16. [get_featured_images( $post_id )](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API#16-get_featured_images-post_id-) 75 | 17. [get_link_to_image( $attachment_id )](https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/API#17-get_link_to_image-attachment_id-) 76 | 77 | ### Allowing DFI only in specific post types 78 | You can use `dfi_post_types` filter to allow DFI only in a specific post types. 79 | ``` 80 | function allowed_post_types() { 81 | return array('post'); //show DFI only in post 82 | } 83 | add_filter('dfi_post_types', 'allowed_post_types'); 84 | ``` 85 | 86 | ### Blocking DFI 87 | Use `dfi_post_type_user_filter` filter to block DFI from post types. 88 | ``` 89 | function blocked_post_types() { 90 | return array('page'); //block DFI in page 91 | } 92 | add_filter('dfi_post_type_user_filter', 'blocked_post_types'); 93 | ``` 94 | 95 | ### Changing the metabox default text 96 | Use `dfi_set_metabox_title` filter to change the metabox default title (Featured Image) 97 | ``` 98 | function set_metabox_title( $title ) { 99 | return "My custom metabox title"; 100 | } 101 | add_filter('dfi_set_metabox_title', 'set_metabox_title'); 102 | ``` 103 | 104 | ### Translation Guidelines 105 | All translations live in the `languages` folder. 106 | 107 | If you are interested in translating the plugin in your language, first make sure if the translation is not already available. The name of the file is important because there’s a particular format you should follow for consistency. For example, if you’re translating Nepali for Nepal, the file should be `dynamic-featured-image-ne_NP.po` – `dynamic-featured-image` for the plugin itself, `ne` for the language and `NP` for the country. 108 | 109 | ### Development 110 | 1. Install [PHPUnit](https://phpunit.de/) and [composer](https://getcomposer.org/) if you haven't already. 111 | 2. Install required dependencies 112 | ```shell 113 | $ composer install 114 | ``` 115 | 3. Build test using installation script 116 | ```shell 117 | $ ./bin/install-wp-tests.sh [db-host] [wp-version] [skip-database-creation] 118 | ``` 119 | 4. Run tests with phpunit 120 | ```shell 121 | $ ./vendor/bin/phpunit 122 | ``` 123 | 5. Validate changes against [WordPress Coding Standards](https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards) 124 | ```shell 125 | $ phpcs 126 | ``` 127 | 128 | ### Questions about this project? 129 | Please feel free to report any bug found. Pull requests, issues, and plugin recommendations are more than welcome! 130 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | Dynamically adds multiple featured image (post thumbnail) functionality to posts, pages and custom post types. 294 | Copyright (C) 2013 Ankit Pokhrel 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /tests/test-dynamic-featured-image.php: -------------------------------------------------------------------------------- 1 | mock_builder = $this->getMockBuilder( 'Dynamic_Featured_Image' ); 21 | 22 | $this->dfi = new Dynamic_Featured_Image; 23 | 24 | $this->plugin_data = get_plugin_data( dirname( dirname( __FILE__ ) ) . '/dynamic-featured-image.php' ); 25 | 26 | $this->post_id = $this->factory->post->create( [ 27 | 'post_title' => 'Dynamic Featured Image WordPress Plugin', 28 | ] ); 29 | 30 | $this->upload_url = 'http://example.org/wp-content/uploads'; 31 | 32 | $this->attachment_id = self::create_attachment_image(); 33 | } 34 | 35 | protected function create_attachment_image() { 36 | $filename = 'wp-content/uploads/2015/03/dfi.jpg'; 37 | $filetype = wp_check_filetype( basename( $filename ), null ); 38 | $wp_upload_dir = wp_upload_dir(); 39 | $guid = $wp_upload_dir['url'] . '/' . basename( $filename ); 40 | 41 | // WordPress upload dir changes with year and month. 42 | // Make it same year and month for simplicity. 43 | $guid = str_replace( '/' . date( 'Y' ) . '/' . date( 'm' ) . '/', '/2015/03/', $guid ); 44 | 45 | $attachment = [ 46 | 'guid' => $guid, 47 | 'post_mime_type' => $filetype['type'], 48 | 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), 49 | 'post_content' => '', 50 | 'post_status' => 'inherit', 51 | ]; 52 | 53 | $attachment_id = wp_insert_attachment( $attachment, $filename, $this->post_id ); 54 | 55 | // add attachment image alt. 56 | add_post_meta( $attachment_id, '_wp_attachment_image_alt', 'Dynamic Featured Image' ); 57 | 58 | // add link to image. 59 | add_post_meta( $attachment_id, '_dfi_link_to_image', 'https://ankitpokhrel.com' ); 60 | 61 | // set default post thumbnail. 62 | set_post_thumbnail( $this->post_id, $attachment_id ); 63 | 64 | // insert featured images. 65 | $dfiFeatured = [ 66 | '/2015/03/dfi-150x150.jpg,/2015/03/dfi.jpg', 67 | '/2015/03/dfis-150x150.jpg,/2015/03/dfis.jpg', 68 | $this->upload_url . '/2015/03/dfi-pro-150x150.jpg,' . $this->upload_url . '/2015/03/dfi-pro.jpg', 69 | ]; 70 | 71 | add_post_meta( $this->post_id, 'dfiFeatured', $dfiFeatured ); 72 | 73 | return $attachment_id; 74 | } 75 | 76 | /** 77 | * @test 78 | * 79 | * @covers ::__construct 80 | * @covers ::load_plugin_textdomain 81 | */ 82 | public function it_adds_required_actions_and_filters() { 83 | $this->assertEquals( 10, has_action( 'admin_enqueue_scripts', [ $this->dfi, 'enqueue_admin_scripts' ] ) ); 84 | $this->assertEquals( 10, has_action( 'add_meta_boxes', [ $this->dfi, 'initialize_featured_box' ] ) ); 85 | $this->assertEquals( 10, has_action( 'save_post', [ $this->dfi, 'save_meta' ] ) ); 86 | $this->assertEquals( 10, has_action( 'plugins_loaded', [ $this->dfi, 'load_plugin_textdomain' ] ) ); 87 | $this->assertEquals( 10, has_action( 'wp_ajax_dfiMetaBox_callback', [ $this->dfi, 'ajax_callback' ] ) ); 88 | $this->assertEquals( 10, has_filter( 'attachment_fields_to_edit', [ $this->dfi, 'media_attachment_custom_fields' ] ) ); 89 | $this->assertEquals( 10, has_filter( 'attachment_fields_to_save', [ $this->dfi, 'media_attachment_custom_fields_save' ] ) ); 90 | } 91 | 92 | /** 93 | * @test 94 | * 95 | * @covers ::enqueue_admin_scripts 96 | */ 97 | public function it_enqueue_admin_scripts() { 98 | $this->dfi->enqueue_admin_scripts(); 99 | 100 | $this->assertTrue( wp_script_is( 'scripts-dfi' ) ); 101 | $this->assertTrue( wp_style_is( 'style-dfi' ) ); 102 | } 103 | 104 | /** 105 | * @test 106 | * 107 | * @coversNothing 108 | */ 109 | public function it_sets_plugin_properties() { 110 | $this->assertTrue( $this->plugin_data['Name'] == 'Dynamic Featured Image' ); 111 | $this->assertTrue( $this->plugin_data['TextDomain'] == 'dynamic-featured-image' ); 112 | $this->assertTrue( $this->plugin_data['DomainPath'] == '/languages' ); 113 | } 114 | 115 | /** 116 | * @test 117 | * 118 | * @covers ::update_notice 119 | */ 120 | public function it_sets_update_notice() { 121 | $expectedOutput = 'ATTENTION! Please read the DOCUMENTATION properly before update.'; 122 | 123 | $this->expectOutputString( $expectedOutput ); 124 | $this->dfi->update_notice(); 125 | } 126 | 127 | /** 128 | * @test 129 | * 130 | * @covers ::featured_meta_box 131 | * @covers ::get_image_thumb 132 | * @covers ::get_number_translation 133 | * @covers ::get_featured_box 134 | */ 135 | public function it_makes_featured_meta_box() { 136 | $post = get_post( $this->post_id ); 137 | $featured['args'] = [ '', 3 ]; 138 | 139 | $mock = $this->mock_builder 140 | ->setMethods( [ 'nonce_field' ] ) 141 | ->getMock(); 142 | 143 | $mock->expects( $this->once() ) 144 | ->method( 'nonce_field' ) 145 | ->with( 'dfi_fimageplug-2' ) 146 | ->will( $this->returnValue( "" ) ); 147 | 148 | $expectedOutput = "
149 | 150 | 154 |
155 | "; 156 | 157 | $this->expectOutputString( $expectedOutput ); 158 | $mock->featured_meta_box( $post, $featured ); 159 | } 160 | 161 | /** 162 | * @test 163 | * 164 | * @covers ::featured_meta_box 165 | * @covers ::get_image_thumb 166 | * @covers ::get_number_translation 167 | * @covers ::get_featured_box 168 | */ 169 | public function it_makes_meta_box_when_featured_id_is_greater_than_nine() { 170 | $post = get_post( $this->post_id ); 171 | $featured['args'] = [ '', 13 ]; 172 | 173 | $mock = $this->mock_builder 174 | ->setMethods( [ 'nonce_field' ] ) 175 | ->getMock(); 176 | 177 | $mock->expects( $this->once() ) 178 | ->method( 'nonce_field' ) 179 | ->with( 'dfi_fimageplug-12' ) 180 | ->will( $this->returnValue( "" ) ); 181 | 182 | $expectedOutput = "
183 | 184 | 188 |
189 | "; 190 | 191 | $this->expectOutputString( $expectedOutput ); 192 | $mock->featured_meta_box( $post, $featured ); 193 | } 194 | 195 | /** 196 | * @test 197 | * 198 | * @covers ::featured_meta_box 199 | * @covers ::get_image_thumb 200 | * @covers ::get_number_translation 201 | * @covers ::get_featured_box 202 | */ 203 | public function it_makes_featured_meta_box_with_value() { 204 | $post = get_post( $this->post_id ); 205 | $featured['args'] = [ '/2015/03/dfi-150x150.jpg,/2015/03/dfi.jpg', 3 ]; 206 | 207 | $mock = $this->mock_builder 208 | ->setMethods( [ 'nonce_field' ] ) 209 | ->getMock(); 210 | 211 | $mock->expects( $this->once() ) 212 | ->method( 'nonce_field' ) 213 | ->with( 'dfi_fimageplug-2' ) 214 | ->will( $this->returnValue( "" ) ); 215 | 216 | $expectedOutput = "
217 | 218 | 222 |
223 | "; 224 | 225 | $this->expectOutputString( $expectedOutput ); 226 | $mock->featured_meta_box( $post, $featured ); 227 | } 228 | 229 | /** 230 | * @test 231 | * 232 | * @covers ::get_image_url 233 | */ 234 | public function it_gets_image_url() { 235 | $fullSizeImage = wp_get_attachment_image_src( $this->attachment_id, 'full' ); 236 | 237 | $this->assertEquals( $this->dfi->get_image_url( $this->attachment_id, 'full' ), $fullSizeImage[0] ); 238 | } 239 | 240 | /** 241 | * @test 242 | * 243 | * @covers ::get_image_thumb_by_attachment_id 244 | */ 245 | public function it_gets_image_thumb_by_attachment_id() { 246 | $thumbImage = wp_get_attachment_image_src( $this->attachment_id, 'thumbnail' ); 247 | 248 | $this->assertEquals( $this->dfi->get_image_thumb_by_attachment_id( $this->attachment_id, 'thumbnail' ), $thumbImage[0] ); 249 | } 250 | 251 | /** 252 | * @test 253 | * 254 | * @covers ::get_image_thumb 255 | */ 256 | public function it_gets_image_thumb() { 257 | $fullSizeImage = wp_get_attachment_image_src( $this->attachment_id, 'full' ); 258 | $thumbImage = wp_get_attachment_image_src( $this->attachment_id, 'thumbnail' ); 259 | 260 | $mock = $this->mock_builder 261 | ->setMethods( [ 'get_image_id' ] ) 262 | ->getMock(); 263 | 264 | $mock->expects( $this->once() ) 265 | ->method( 'get_image_id' ) 266 | ->with( $fullSizeImage[0] ) 267 | ->will( $this->returnValue( $this->attachment_id ) ); 268 | 269 | $this->assertEquals( $mock->get_image_thumb( $fullSizeImage[0], 'thumbnail' ), $thumbImage[0] ); 270 | } 271 | 272 | /** 273 | * @test 274 | * 275 | * @covers ::get_image_id 276 | * @covers ::get_attachment_id 277 | */ 278 | public function it_gets_image_id() { 279 | $fullSizeImage = wp_get_attachment_image_src( $this->attachment_id, 'full' ); 280 | 281 | $this->assertEquals( $this->dfi->get_image_id( $fullSizeImage[0] ), $this->attachment_id ); 282 | } 283 | 284 | /** 285 | * @test 286 | * 287 | * @covers ::get_image_id 288 | */ 289 | public function it_gets_image_id_with_post_id_from_posts_meta_table() { 290 | $image = '2015/03/dfi.jpg'; 291 | 292 | $mock = $this->mock_builder 293 | ->setMethods( [ 'get_attachment_id' ] ) 294 | ->getMock(); 295 | 296 | $mock->expects( $this->exactly( 2 ) ) 297 | ->method( 'get_attachment_id' ) 298 | ->will( $this->returnValue( null ) ); 299 | 300 | add_post_meta( $this->post_id, '_wp_attached_file', $image ); 301 | 302 | $this->assertEquals( $mock->get_image_id( $image ), $this->post_id ); 303 | $this->assertEquals( $mock->get_image_id( '2015/03/dfis.jpg' ), null ); 304 | } 305 | 306 | /** 307 | * @test 308 | * 309 | * @covers ::get_image_title 310 | * @covers ::execute_query 311 | */ 312 | public function it_gets_image_title() { 313 | $post = get_post( $this->attachment_id ); 314 | $fullSizeImage = wp_get_attachment_image_src( $this->attachment_id, 'full' ); 315 | 316 | $this->assertEquals( $this->dfi->get_image_title( $fullSizeImage[0] ), $post->post_title ); 317 | } 318 | 319 | /** 320 | * @test 321 | * 322 | * @covers ::get_image_title_by_id 323 | * @covers ::execute_query 324 | */ 325 | public function it_gets_image_title_by_id() { 326 | $post = get_post( $this->attachment_id ); 327 | $this->assertEquals( $this->dfi->get_image_title_by_id( $this->attachment_id ), $post->post_title ); 328 | } 329 | 330 | /** 331 | * @test 332 | * 333 | * @covers ::get_image_caption 334 | * @covers ::execute_query 335 | */ 336 | public function it_gets_image_caption() { 337 | $post = get_post( $this->attachment_id ); 338 | $fullSizeImage = wp_get_attachment_image_src( $this->attachment_id, 'full' ); 339 | 340 | $this->assertEquals( $this->dfi->get_image_caption( $fullSizeImage[0] ), $post->post_excerpt ); 341 | } 342 | 343 | /** 344 | * @test 345 | * 346 | * @covers ::get_image_caption_by_id 347 | * @covers ::execute_query 348 | */ 349 | public function it_gets_image_caption_by_id() { 350 | $post = get_post( $this->attachment_id ); 351 | $this->assertEquals( $this->dfi->get_image_caption_by_id( $this->attachment_id ), $post->post_excerpt ); 352 | } 353 | 354 | /** 355 | * @test 356 | * 357 | * @covers ::get_image_alt 358 | */ 359 | public function it_gets_image_alt() { 360 | $fullSizeImage = wp_get_attachment_image_src( $this->attachment_id, 'full' ); 361 | $alt = get_post_meta( $this->attachment_id, '_wp_attachment_image_alt', true ); 362 | 363 | $this->assertEquals( $this->dfi->get_image_alt( $fullSizeImage[0] ), $alt ); 364 | } 365 | 366 | /** 367 | * @test 368 | * 369 | * @covers ::get_image_alt_by_id 370 | */ 371 | public function it_gets_image_alt_by_id() { 372 | $alt = get_post_meta( $this->attachment_id, '_wp_attachment_image_alt', true ); 373 | 374 | $this->assertEquals( $this->dfi->get_image_alt_by_id( $this->attachment_id ), $alt ); 375 | } 376 | 377 | /** 378 | * @test 379 | * 380 | * @covers ::get_image_description 381 | * @covers ::execute_query 382 | */ 383 | public function it_gets_image_description() { 384 | $post = get_post( $this->attachment_id ); 385 | $fullSizeImage = wp_get_attachment_image_src( $this->attachment_id, 'full' ); 386 | 387 | $this->assertEquals( $this->dfi->get_image_description( $fullSizeImage[0] ), $post->post_content ); 388 | } 389 | 390 | /** 391 | * @test 392 | * 393 | * @covers ::get_image_description_by_id 394 | * @covers ::execute_query 395 | */ 396 | public function it_gets_image_description_by_id() { 397 | $post = get_post( $this->attachment_id ); 398 | 399 | $this->assertEquals( $this->dfi->get_image_description_by_id( $this->attachment_id ), $post->post_content ); 400 | } 401 | 402 | /** 403 | * @test 404 | * 405 | * @covers ::get_link_to_image 406 | */ 407 | public function it_gets_link_to_image() { 408 | $this->assertEquals( $this->dfi->get_link_to_image( $this->attachment_id ), 'https://ankitpokhrel.com' ); 409 | } 410 | 411 | /** 412 | * @test 413 | * 414 | * @covers ::get_post_attachment_ids 415 | * @covers ::get_image_id 416 | * @covers ::separate 417 | */ 418 | public function it_gets_post_attachment_ids() { 419 | $expected = [ $this->attachment_id, null, null ]; 420 | 421 | $this->assertEquals( $expected, $this->dfi->get_post_attachment_ids( $this->post_id ) ); 422 | } 423 | 424 | /** 425 | * @test 426 | * 427 | * @covers ::get_nth_featured_image 428 | * @covers ::get_featured_images 429 | * @covers ::get_real_post_id 430 | */ 431 | public function it_gets_nth_featured_image() { 432 | $featuredImage2 = [ 433 | 'thumb' => $this->upload_url . '/2015/03/dfi-150x150.jpg', 434 | 'full' => $this->upload_url . '/2015/03/dfi.jpg', 435 | 'attachment_id' => $this->attachment_id, 436 | ]; 437 | 438 | $this->assertEquals( $featuredImage2, $this->dfi->get_nth_featured_image( 2, $this->post_id ) ); 439 | 440 | // no attachment id. 441 | $featuredImage3 = [ 442 | 'thumb' => $this->upload_url . '/2015/03/dfis-150x150.jpg', 443 | 'full' => $this->upload_url . '/2015/03/dfis.jpg', 444 | 'attachment_id' => null, 445 | ]; 446 | 447 | $this->assertEquals( $featuredImage3, $this->dfi->get_nth_featured_image( 3, $this->post_id ) ); 448 | 449 | // full image url and no attachment id. 450 | $featuredImage4 = [ 451 | 'thumb' => $this->upload_url . '/2015/03/dfi-pro-150x150.jpg', 452 | 'full' => $this->upload_url . '/2015/03/dfi-pro.jpg', 453 | 'attachment_id' => null, 454 | ]; 455 | 456 | $this->assertEquals( $featuredImage4, $this->dfi->get_nth_featured_image( 4, $this->post_id ) ); 457 | 458 | // doesn't exist. 459 | $this->assertNull( $this->dfi->get_nth_featured_image( 5, $this->post_id ) ); 460 | 461 | } 462 | 463 | /** 464 | * @test 465 | * 466 | * @covers ::get_nth_featured_image 467 | * @covers ::get_featured_images 468 | * @covers ::get_real_post_id 469 | */ 470 | public function it_gets_nth_featured_image_when_post_id_is_null() { 471 | $expected = [ 472 | 'thumb' => $this->upload_url . '/2015/03/dfi-150x150.jpg', 473 | 'full' => $this->upload_url . '/2015/03/dfi.jpg', 474 | 'attachment_id' => $this->attachment_id, 475 | ]; 476 | 477 | $actual = null; 478 | 479 | query_posts( 'post_type=post' ); 480 | if ( have_posts() ) { 481 | while ( have_posts() ) { 482 | the_post(); 483 | $actual = $this->dfi->get_nth_featured_image( 2 ); 484 | } 485 | } 486 | 487 | $this->assertEquals( $expected, $actual ); 488 | } 489 | 490 | /** 491 | * @test 492 | * 493 | * @covers ::is_attached 494 | * @covers ::get_post_attachment_ids 495 | */ 496 | public function it_checks_if_image_is_attached() { 497 | $this->assertTrue( $this->dfi->is_attached( $this->attachment_id, $this->post_id ) ); 498 | $this->assertFalse( $this->dfi->is_attached( null, $this->post_id ) ); 499 | } 500 | 501 | /** 502 | * @test 503 | * 504 | * @covers ::get_featured_images 505 | * @covers ::get_real_upload_path 506 | * @covers ::get_image_id 507 | * @covers ::separate 508 | * @covers ::get_real_post_id 509 | */ 510 | public function it_gets_featured_images() { 511 | $expected = [ 512 | [ 513 | 'thumb' => $this->upload_url . '/2015/03/dfi-150x150.jpg', 514 | 'full' => $this->upload_url . '/2015/03/dfi.jpg', 515 | 'attachment_id' => $this->attachment_id, 516 | ], 517 | [ 518 | 'thumb' => $this->upload_url . '/2015/03/dfis-150x150.jpg', 519 | 'full' => $this->upload_url . '/2015/03/dfis.jpg', 520 | 'attachment_id' => null, 521 | ], 522 | [ 523 | 'thumb' => $this->upload_url . '/2015/03/dfi-pro-150x150.jpg', 524 | 'full' => $this->upload_url . '/2015/03/dfi-pro.jpg', 525 | 'attachment_id' => null, 526 | ], 527 | ]; 528 | 529 | $actual = $this->dfi->get_featured_images( $this->post_id ); 530 | 531 | $this->assertEquals( $expected, $actual ); 532 | } 533 | 534 | /** 535 | * @test 536 | * 537 | * @covers ::get_featured_images 538 | * @covers ::get_real_upload_path 539 | * @covers ::get_image_id 540 | * @covers ::separate 541 | * @covers ::get_real_post_id 542 | */ 543 | public function it_gets_featured_images_when_post_id_is_null() { 544 | $expected = [ 545 | [ 546 | 'thumb' => $this->upload_url . '/2015/03/dfi-150x150.jpg', 547 | 'full' => $this->upload_url . '/2015/03/dfi.jpg', 548 | 'attachment_id' => $this->attachment_id, 549 | ], 550 | [ 551 | 'thumb' => $this->upload_url . '/2015/03/dfis-150x150.jpg', 552 | 'full' => $this->upload_url . '/2015/03/dfis.jpg', 553 | 'attachment_id' => null, 554 | ], 555 | [ 556 | 'thumb' => $this->upload_url . '/2015/03/dfi-pro-150x150.jpg', 557 | 'full' => $this->upload_url . '/2015/03/dfi-pro.jpg', 558 | 'attachment_id' => null, 559 | ], 560 | ]; 561 | 562 | $actual = null; 563 | 564 | query_posts( 'post_type=post' ); 565 | if ( have_posts() ) { 566 | while ( have_posts() ) { 567 | the_post(); 568 | $actual = $this->dfi->get_featured_images(); 569 | } 570 | } 571 | 572 | $this->assertEquals( $expected, $actual ); 573 | } 574 | 575 | /** 576 | * @test 577 | * 578 | * @covers ::get_all_featured_images 579 | * @covers ::get_featured_images 580 | * @covers ::get_real_post_id 581 | */ 582 | public function it_gets_all_featured_images() { 583 | $expected = [ 584 | [ 585 | 'thumb' => $this->upload_url . '/2015/03/dfi.jpg', 586 | 'full' => $this->upload_url . '/2015/03/dfi.jpg', 587 | 'attachment_id' => $this->attachment_id, 588 | ], 589 | [ 590 | 'thumb' => $this->upload_url . '/2015/03/dfi-150x150.jpg', 591 | 'full' => $this->upload_url . '/2015/03/dfi.jpg', 592 | 'attachment_id' => $this->attachment_id, 593 | ], 594 | [ 595 | 'thumb' => $this->upload_url . '/2015/03/dfis-150x150.jpg', 596 | 'full' => $this->upload_url . '/2015/03/dfis.jpg', 597 | 'attachment_id' => null, 598 | ], 599 | [ 600 | 'thumb' => $this->upload_url . '/2015/03/dfi-pro-150x150.jpg', 601 | 'full' => $this->upload_url . '/2015/03/dfi-pro.jpg', 602 | 'attachment_id' => null, 603 | ], 604 | ]; 605 | 606 | $actual = $this->dfi->get_all_featured_images( $this->post_id ); 607 | 608 | $this->assertEquals( $expected, $actual ); 609 | } 610 | 611 | /** 612 | * @test 613 | * 614 | * @covers ::get_all_featured_images 615 | * @covers ::get_featured_images 616 | * @covers ::get_real_post_id 617 | */ 618 | public function it_gets_all_featured_images_when_post_id_is_null() { 619 | $expected = [ 620 | [ 621 | 'thumb' => $this->upload_url . '/2015/03/dfi.jpg', 622 | 'full' => $this->upload_url . '/2015/03/dfi.jpg', 623 | 'attachment_id' => $this->attachment_id, 624 | ], 625 | [ 626 | 'thumb' => $this->upload_url . '/2015/03/dfi-150x150.jpg', 627 | 'full' => $this->upload_url . '/2015/03/dfi.jpg', 628 | 'attachment_id' => $this->attachment_id, 629 | ], 630 | [ 631 | 'thumb' => $this->upload_url . '/2015/03/dfis-150x150.jpg', 632 | 'full' => $this->upload_url . '/2015/03/dfis.jpg', 633 | 'attachment_id' => null, 634 | ], 635 | [ 636 | 'thumb' => $this->upload_url . '/2015/03/dfi-pro-150x150.jpg', 637 | 'full' => $this->upload_url . '/2015/03/dfi-pro.jpg', 638 | 'attachment_id' => null, 639 | ], 640 | ]; 641 | 642 | $actual = null; 643 | 644 | query_posts( 'post_type=post' ); 645 | if ( have_posts() ) { 646 | while ( have_posts() ) { 647 | the_post(); 648 | $actual = $this->dfi->get_all_featured_images(); 649 | } 650 | } 651 | 652 | $this->assertEquals( $expected, $actual ); 653 | } 654 | 655 | /** 656 | * @test 657 | * 658 | * @covers ::add_metabox_classes 659 | */ 660 | public function it_adds_metabox_classes() { 661 | $classes[] = 'metabox'; 662 | $expected = [ 'metabox', 'featured-meta-box' ]; 663 | $actual = $this->dfi->add_metabox_classes( $classes ); 664 | 665 | $this->assertEquals( $expected, $actual ); 666 | } 667 | 668 | /** 669 | * @test 670 | * 671 | * @covers ::media_attachment_custom_fields 672 | */ 673 | public function it_sets_media_attachment_custom_fields() { 674 | $post = get_post( $this->post_id ); 675 | $formFields = $this->dfi->media_attachment_custom_fields( [], $post ); 676 | 677 | $this->assertArrayHasKey( 'dfi-link-to-image', $formFields ); 678 | $this->assertArrayHasKey( 'label', $formFields['dfi-link-to-image'] ); 679 | $this->assertArrayHasKey( 'input', $formFields['dfi-link-to-image'] ); 680 | $this->assertArrayHasKey( 'value', $formFields['dfi-link-to-image'] ); 681 | 682 | $this->assertEquals( $formFields['dfi-link-to-image']['input'], 'text' ); 683 | } 684 | 685 | /** 686 | * @test 687 | * 688 | * @covers ::media_attachment_custom_fields_save 689 | */ 690 | public function it_saves_media_attachment_custom_fields() { 691 | $post['ID'] = $this->post_id; 692 | $attachment['dfi-link-to-image'] = 'https://ankitpokhrel.com'; 693 | 694 | $this->dfi->media_attachment_custom_fields_save( $post, $attachment ); 695 | 696 | $this->assertEquals( $attachment['dfi-link-to-image'], 697 | get_post_meta( $this->post_id, '_dfi_link_to_image', true ) ); 698 | } 699 | 700 | /** 701 | * @test 702 | * 703 | * @covers ::save_meta 704 | * @covers ::get_featured_images 705 | * @covers ::sanitize_array 706 | */ 707 | public function it_saves_meta() { 708 | $mock = $this->mock_builder 709 | ->setMethods( [ 'verify_nonces' ] ) 710 | ->getMock(); 711 | 712 | $mock->expects( $this->once() ) 713 | ->method( 'verify_nonces' ) 714 | ->will( $this->returnValue( true ) ); 715 | 716 | $user_id = $this->factory->user->create( [ 717 | 'role' => 'administrator', 718 | ] ); 719 | wp_set_current_user( $user_id ); 720 | 721 | $_POST['dfiFeatured'] = [ '/2015/03/featured-150x150.jpg,/2015/03/featured.jpg' ]; 722 | $mock->save_meta( $this->post_id ); 723 | 724 | $expected = [ 725 | [ 726 | 'thumb' => $this->upload_url . '/2015/03/featured-150x150.jpg', 727 | 'full' => $this->upload_url . '/2015/03/featured.jpg', 728 | 'attachment_id' => null, 729 | ], 730 | ]; 731 | 732 | $actual = $this->dfi->get_featured_images( $this->post_id ); 733 | 734 | $this->assertEquals( $expected, $actual ); 735 | } 736 | 737 | /** 738 | * @test 739 | * 740 | * @covers ::save_meta 741 | */ 742 | public function it_saves_meta_when_doing_autosave() { 743 | define( 'DOING_AUTOSAVE', true ); 744 | 745 | $this->assertFalse( $this->dfi->save_meta( $this->post_id ) ); 746 | } 747 | 748 | public function tearDown() { 749 | parent::tearDown(); 750 | 751 | unset( $this->mock_builder ); 752 | unset( $this->post_id ); 753 | unset( $this->attachment_id ); 754 | 755 | unset( $this->dfi ); 756 | unset( $this->plugin_data ); 757 | } 758 | } 759 | -------------------------------------------------------------------------------- /dynamic-featured-image.php: -------------------------------------------------------------------------------- 1 | 18 | * 19 | * This program is free software; you can redistribute it and/or modify 20 | * it under the terms of the GNU General Public License as published by 21 | * the Free Software Foundation; either version 3 of the License, or 22 | * (at your option) any later version. 23 | * 24 | * This program is distributed in the hope that it will be useful, 25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 27 | * GNU General Public License for more details. 28 | * 29 | * You should have received a copy of the GNU General Public License 30 | * along with this program. If not, see . 31 | */ 32 | 33 | // Avoid direct calls to this file. 34 | if ( ! defined( 'ABSPATH' ) ) { 35 | header( 'Status: 403 Forbidden' ); 36 | header( 'HTTP/1.1 403 Forbidden' ); 37 | exit(); 38 | } 39 | 40 | /** 41 | * Dynamic Featured Image plugin main class. 42 | * 43 | * @author Ankit Pokhrel 44 | * @version 3.7.0 45 | */ 46 | class Dynamic_Featured_Image { 47 | /** 48 | * Current version of the plugin. 49 | * 50 | * @since 3.0.0 51 | */ 52 | const VERSION = '3.7.0'; 53 | 54 | /** 55 | * Text domain. 56 | * 57 | * @since 3.6.0 58 | */ 59 | const TEXT_DOMAIN = 'dynamic-featured-image'; 60 | 61 | /** 62 | * Documentation Link. 63 | * 64 | * @since 3.6.0 65 | */ 66 | const WIKI_LINK = 'https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki/'; 67 | 68 | /** 69 | * Image upload directory. 70 | * 71 | * @var $upload_dir string 72 | */ 73 | private $upload_dir; 74 | 75 | /** 76 | * Image upload URL. 77 | * 78 | * @var $upload_url string 79 | */ 80 | private $upload_url; 81 | 82 | /** 83 | * Database object. 84 | * 85 | * @var $db wpdb 86 | */ 87 | private $db; 88 | 89 | /** 90 | * Title for dfi metabox. 91 | * 92 | * @var $metabox_title string 93 | */ 94 | protected $metabox_title; 95 | 96 | /** 97 | * Users post type filter for dfi metabox. 98 | * 99 | * @var $user_filter array 100 | */ 101 | protected $user_filter; 102 | 103 | /** 104 | * Constructor. Hooks all interactions to initialize the class. 105 | * 106 | * @since 1.0.0 107 | * @access public 108 | * @global object $wpdb 109 | * 110 | * @see add_action() 111 | */ 112 | public function __construct() { 113 | // plugin update warning. 114 | add_action( 'in_plugin_update_message-' . plugin_basename( __FILE__ ), array( $this, 'update_notice' ) ); 115 | 116 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); 117 | add_action( 'add_meta_boxes', array( $this, 'initialize_featured_box' ) ); 118 | add_action( 'save_post', array( $this, 'save_meta' ) ); 119 | add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) ); 120 | 121 | // handle ajax request. 122 | add_action( 'wp_ajax_dfiMetaBox_callback', array( $this, 'ajax_callback' ) ); 123 | 124 | // media uploader custom fields. 125 | add_filter( 'attachment_fields_to_edit', array( $this, 'media_attachment_custom_fields' ), 10, 2 ); 126 | add_filter( 'attachment_fields_to_save', array( $this, 'media_attachment_custom_fields_save' ), 10, 2 ); 127 | 128 | // plugin sponsors. 129 | new PluginSponsor(); 130 | 131 | // get the site protocol. 132 | $protocol = $this->get_protocol(); 133 | 134 | $this->upload_dir = wp_upload_dir(); 135 | $this->upload_url = preg_replace( '#^https?://#', '', $this->upload_dir['baseurl'] ); 136 | 137 | // add protocol to the upload url. 138 | $this->upload_url = $protocol . $this->upload_url; 139 | 140 | // post type filter added by user. 141 | $this->user_filter = array(); 142 | 143 | global $wpdb; 144 | $this->db = $wpdb; 145 | } 146 | 147 | /** 148 | * Return site protocol. 149 | * 150 | * @since 3.5.1 151 | * @access public 152 | * 153 | * @return string 154 | */ 155 | private function get_protocol() { 156 | return is_ssl() ? 'https://' : 'http://'; 157 | } 158 | 159 | /** 160 | * Add required admin scripts. 161 | * 162 | * @since 1.0.0 163 | * @access public 164 | * 165 | * @see wp_enqueue_style() 166 | * @see wp_register_script() 167 | * @see wp_enqueue_script() 168 | * 169 | * @return void 170 | */ 171 | public function enqueue_admin_scripts() { 172 | // enqueue styles. 173 | wp_enqueue_style( 'style-dfi', plugins_url( '/css/style-dfi.css', __FILE__ ), array(), self::VERSION ); 174 | 175 | // register script. 176 | wp_register_script( 'scripts-dfi', plugins_url( '/js/script-dfi.js', __FILE__ ), array( 'jquery' ), self::VERSION ); 177 | 178 | // localize the script with required data. 179 | wp_localize_script( 180 | 'scripts-dfi', 181 | 'DFI_SPECIFIC', 182 | array( 183 | 'upload_url' => $this->upload_url, 184 | 'metabox_title' => __( $this->metabox_title, self::TEXT_DOMAIN ), 185 | 'mediaSelector_title' => __( 'Dynamic Featured Image - Media Selector', self::TEXT_DOMAIN ), 186 | 'mediaSelector_buttonText' => __( 'Set Featured Image', self::TEXT_DOMAIN ), 187 | 'ajax_nonce' => wp_create_nonce( plugin_basename( __FILE__ ) ), 188 | ) 189 | ); 190 | 191 | // enqueue scripts. 192 | wp_enqueue_script( 'scripts-dfi' ); 193 | } 194 | 195 | /** 196 | * Add featured meta boxes dynamically. 197 | * 198 | * @since 1.0.0 199 | * @access public 200 | * @global object $post 201 | * 202 | * @see get_post_meta() 203 | * @see get_post_types() 204 | * @see add_meta_box() 205 | * @see add_filter() 206 | * 207 | * @return void 208 | */ 209 | public function initialize_featured_box() { 210 | global $post; 211 | 212 | // make metabox title dynamic. 213 | $this->metabox_title = apply_filters( 'dfi_set_metabox_title', __( 'Featured Image', self::TEXT_DOMAIN ) ); 214 | 215 | $featured_data = get_post_meta( $post->ID, 'dfiFeatured', true ); 216 | $total_featured = is_array( $featured_data ) ? count( $featured_data ) : 0; 217 | 218 | $default_filter = array( 'attachment', 'revision', 'nav_menu_item' ); 219 | $this->user_filter = apply_filters( 'dfi_post_type_user_filter', $this->user_filter ); 220 | 221 | $post_types = array_diff( get_post_types(), array_merge( $default_filter, $this->user_filter ) ); 222 | $post_types = apply_filters( 'dfi_post_types', $post_types ); 223 | 224 | if ( ! empty( $featured_data ) && $total_featured >= 1 ) { 225 | $i = 2; 226 | foreach ( $featured_data as $featured ) { 227 | $this->dfi_add_meta_box( $post_types, $featured, $i++ ); 228 | } 229 | } else { 230 | $this->dfi_add_meta_box( $post_types ); 231 | } 232 | } 233 | 234 | /** 235 | * Translates more than one digit number digit by digit. 236 | * 237 | * @param int $number Integer to be translated. 238 | * 239 | * @return string Translated number 240 | */ 241 | protected function get_number_translation( $number ) { 242 | if ( $number <= 9 ) { 243 | return __( $number, self::TEXT_DOMAIN ); 244 | } else { 245 | $pieces = str_split( $number, 1 ); 246 | $buffer = ''; 247 | foreach ( $pieces as $piece ) { 248 | $buffer .= __( $piece, self::TEXT_DOMAIN ); 249 | } 250 | 251 | return $buffer; 252 | } 253 | } 254 | 255 | /** 256 | * Adds meta boxes. 257 | * 258 | * @param array $post_types Post types to show featured image box. 259 | * @param object $featured Callback arguments. 260 | * @param int $i Index of the featured image. 261 | * 262 | * @return void 263 | */ 264 | private function dfi_add_meta_box( $post_types, $featured = null, $i = null ) { 265 | if ( ! is_null( $i ) ) { 266 | foreach ( $post_types as $type ) { 267 | add_meta_box( 268 | 'dfiFeaturedMetaBox-' . $i, 269 | __( $this->metabox_title, self::TEXT_DOMAIN ) . ' ' . $this->get_number_translation( $i ), 270 | array( $this, 'featured_meta_box' ), 271 | $type, 272 | apply_filters( 'dfi_metabox_context', 'side' ), 273 | apply_filters( 'dfi_metabox_priority', 'low' ), 274 | array( $featured, $i + 1 ) 275 | ); 276 | 277 | add_filter( "postbox_classes_{$type}_dfiFeaturedMetaBox-" . $i, array( $this, 'add_metabox_classes' ) ); 278 | } 279 | } else { 280 | foreach ( $post_types as $type ) { 281 | add_meta_box( 282 | 'dfiFeaturedMetaBox', 283 | __( $this->metabox_title, self::TEXT_DOMAIN ) . ' ' . __( 2, self::TEXT_DOMAIN ), 284 | array( $this, 'featured_meta_box' ), 285 | $type, 286 | apply_filters( 'dfi_metabox_context', 'side' ), 287 | apply_filters( 'dfi_metabox_priority', 'low' ), 288 | array( null, null ) 289 | ); 290 | 291 | add_filter( "postbox_classes_{$type}_dfiFeaturedMetaBox", array( $this, 'add_metabox_classes' ) ); 292 | } 293 | } 294 | } 295 | 296 | /** 297 | * Separate thumb and full image url from given URL string. 298 | * 299 | * @since 3.3.1 300 | * 301 | * @param string $url_string Url string. 302 | * @param string $state Thumb or full. 303 | * 304 | * @return string|null 305 | */ 306 | private function separate( $url_string, $state = 'thumb' ) { 307 | $image_piece = explode( ',', $url_string ); 308 | 309 | if ( 'thumb' === $state ) { 310 | return isset( $image_piece[0] ) ? $image_piece[0] : null; 311 | } 312 | 313 | return isset( $image_piece[1] ) ? $image_piece[1] : null; 314 | } 315 | 316 | /** 317 | * Create a nonce field. 318 | * 319 | * @since 3.5.0 320 | * 321 | * @see wp_nonce_field() 322 | * @see plugin_basename() 323 | * 324 | * @codeCoverageIgnore 325 | * 326 | * @param string $key Nonce key. 327 | * 328 | * @return string 329 | */ 330 | protected function nonce_field( $key ) { 331 | return wp_nonce_field( plugin_basename( __FILE__ ), $key, true, false ); 332 | } 333 | 334 | /** 335 | * Featured meta box as seen in the admin. 336 | * 337 | * @since 1.0.0 338 | * @access public 339 | * 340 | * @param object $post Global post object. 341 | * @param array $featured Array containing featured image count. 342 | * 343 | * @throws Exception Medium size image not found. 344 | * @return void 345 | */ 346 | public function featured_meta_box( $post, $featured ) { 347 | $featured_img = $featured['args'][0]; 348 | $featured_id = is_null( $featured['args'][1] ) ? 2 : --$featured['args'][1]; 349 | $featured_img_full = $featured_img; 350 | $featured_img_trimmed = $featured_img; 351 | 352 | if ( ! is_null( $featured_img ) ) { 353 | $featured_img_trimmed = $this->separate( $featured_img ); 354 | $featured_img_full = $this->separate( $featured_img, 'full' ); 355 | } 356 | 357 | $thumbnail = null; 358 | $attachment_id = null; 359 | if ( ! empty( $featured_img_full ) ) { 360 | $attachment_id = $this->get_image_id( $this->upload_url . $featured_img_full ); 361 | 362 | $thumbnail = $this->get_image_thumb_by_attachment_id( $attachment_id, 'medium' ); 363 | 364 | if ( empty( $thumbnail ) ) { 365 | // since medium sized thumbnail image is missing, 366 | // let's set full image url as thumbnail. 367 | $thumbnail = $featured_img_full; 368 | } 369 | } 370 | 371 | // Add a nonce field. 372 | echo $this->nonce_field( 'dfi_fimageplug-' . $featured_id ); // WPCS: XSS ok. 373 | echo $this->get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post->ID, $attachment_id ); // WPCS: XSS ok. 374 | } 375 | 376 | /** 377 | * Returns featured box html content. 378 | * 379 | * @since 3.1.0 380 | * @access private 381 | * 382 | * @param string $featured_img_trimmed Medium sized image. 383 | * @param string $featured_img Full sized image. 384 | * @param string $featured_id Featured id number for translation. 385 | * @param string $thumbnail Thumb sized image. 386 | * @param int $post_id Post id. 387 | * @param int $attachment_id Attachment id. 388 | * 389 | * @return string Html content 390 | */ 391 | private function get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post_id, $attachment_id ) { 392 | $has_featured_image = ! empty( $featured_img_trimmed ) ? ' hasFeaturedImage' : ''; 393 | $thumbnail = ! is_null( $thumbnail ) ? $thumbnail : ''; 394 | $dfi_empty = is_null( $featured_img_trimmed ) ? 'dfiImgEmpty' : ''; 395 | 396 | return "
397 | 398 | 402 |
403 | "; 404 | } 405 | 406 | /** 407 | * Load new featured meta box via ajax. 408 | * 409 | * @since 1.0.0 410 | * @access public 411 | * 412 | * @return void 413 | */ 414 | public function ajax_callback() { 415 | check_ajax_referer( plugin_basename( __FILE__ ), 'security' ); 416 | 417 | $featured_id = isset( $_POST['id'] ) ? intval( wp_unslash( $_POST['id'] ) ) : null; 418 | 419 | if ( ! is_numeric( $featured_id ) ) { 420 | return; 421 | } 422 | 423 | // @codingStandardsIgnoreStart 424 | echo $this->nonce_field( 'dfi_fimageplug-' . $featured_id ); 425 | ?> 426 |
429 | 430 | 437 |
438 | 439 | __( 'Link to Image', self::TEXT_DOMAIN ), 475 | 'input' => 'text', 476 | 'value' => get_post_meta( $post->ID, '_dfi_link_to_image', true ), 477 | ); 478 | 479 | return $form_fields; 480 | } 481 | 482 | /** 483 | * Save values of media uploader custom fields. 484 | * 485 | * @since 3.4.0 486 | * 487 | * @param array $post Post data for database. 488 | * @param array $attachment Attachment fields from $_POST form. 489 | * 490 | * @return array 491 | */ 492 | public function media_attachment_custom_fields_save( $post, $attachment ) { 493 | if ( isset( $attachment['dfi-link-to-image'] ) ) { 494 | update_post_meta( $post['ID'], '_dfi_link_to_image', $attachment['dfi-link-to-image'] ); 495 | } 496 | 497 | return $post; 498 | } 499 | 500 | /** 501 | * Update featured images in the database. 502 | * 503 | * @since 1.0.0 504 | * @access public 505 | * 506 | * @see plugin_basename() 507 | * @see update_post_meta() 508 | * @see current_user_can() 509 | * 510 | * @param int $post_id Current post id. 511 | * 512 | * @return bool|null 513 | */ 514 | public function save_meta( $post_id ) { 515 | // Check auto save. 516 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { 517 | return false; 518 | } 519 | 520 | if ( ! $this->verify_nonces() ) { 521 | return false; 522 | } 523 | 524 | // Check permission before saving data. 525 | if ( current_user_can( 'edit_posts', $post_id ) && isset( $_POST['dfiFeatured'] ) ) { // WPCS: CSRF ok. 526 | $featured_images = is_array( $_POST['dfiFeatured'] ) ? $_POST['dfiFeatured'] : array(); // WPCS: sanitization ok, CSRF ok. 527 | 528 | update_post_meta( $post_id, 'dfiFeatured', $this->sanitize_array( $featured_images ) ); 529 | } 530 | } 531 | 532 | /** 533 | * Sanitize array. 534 | * 535 | * @since 3.6.0 536 | * @access protected 537 | * 538 | * @param array $input_array Input array. 539 | * 540 | * @return array 541 | */ 542 | protected function sanitize_array( $input_array ) { 543 | $sanitized = array(); 544 | 545 | foreach ( $input_array as $value ) { 546 | $sanitized[] = sanitize_text_field( wp_unslash( $value ) ); 547 | } 548 | 549 | return $sanitized; 550 | } 551 | 552 | /** 553 | * Verify metabox nonces. 554 | * 555 | * @access protected 556 | * @see wp_verify_nonce() 557 | * 558 | * @return bool 559 | */ 560 | protected function verify_nonces() { 561 | $keys = preg_grep( '/dfi_fimageplug-\d+$/', array_keys( $_POST ) ); // WPCS: CSRF ok. 562 | 563 | if ( empty( $keys ) ) { 564 | return false; 565 | } 566 | 567 | foreach ( $keys as $key ) { 568 | // Verify nonce. 569 | if ( ! isset( $_POST[ $key ] ) || 570 | ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ $key ] ) ), plugin_basename( __FILE__ ) ) 571 | ) { 572 | return false; 573 | } 574 | } 575 | 576 | return true; 577 | } 578 | 579 | /** 580 | * Add update notice. Displayed in plugin update page. 581 | * 582 | * @since 2.0.0 583 | * @access public 584 | * 585 | * @return void 586 | */ 587 | public function update_notice() { 588 | $info = __( 'ATTENTION! Please read the DOCUMENTATION properly before update.', 589 | self::TEXT_DOMAIN ); 590 | 591 | echo '' . strip_tags( $info, '' ) . ''; // WPCS: XSS ok. 592 | } 593 | 594 | /** 595 | * Execute query. 596 | * 597 | * @param string $query Query to execute. 598 | * 599 | * @return null|string 600 | */ 601 | private function execute_query( $query ) { 602 | return $this->db->get_var( $query ); 603 | } 604 | 605 | /** 606 | * Get attachment id of the image by image url. 607 | * 608 | * @since 3.1.7 609 | * @access protected 610 | * @global object $wpdb 611 | * 612 | * @param string $image_url URL of an image. 613 | * 614 | * @return string 615 | */ 616 | protected function get_attachment_id( $image_url ) { 617 | return $this->execute_query( $this->db->prepare( 'SELECT ID FROM ' . $this->db->posts . ' WHERE guid = %s', $image_url ) ); 618 | } 619 | 620 | /** 621 | * Get image url of the image by attachment id. 622 | * 623 | * @since 2.0.0 624 | * @access public 625 | * 626 | * @see wp_get_attachment_image_src() 627 | * 628 | * @param int $attachment_id attachment id of an image. 629 | * @param string $size size of the image to fetch (thumbnail, medium, full). 630 | * 631 | * @return string 632 | */ 633 | public function get_image_url( $attachment_id, $size = 'full' ) { 634 | $image_thumb = wp_get_attachment_image_src( $attachment_id, $size ); 635 | 636 | return empty( $image_thumb ) ? null : $image_thumb[0]; 637 | } 638 | 639 | /** 640 | * Get image thumbnail url of specific size by attachment id. 641 | * 642 | * @since 3.7.0 643 | * @access public 644 | * 645 | * @see wp_get_attachment_image_src() 646 | * 647 | * @param int $attachment_id attachment id of an image. 648 | * @param string $size size of the image to fetch (thumbnail, medium, full). 649 | * 650 | * @return string|null 651 | */ 652 | public function get_image_thumb_by_attachment_id( $attachment_id, $size = 'thumbnail' ) { 653 | if ( empty( $attachment_id ) ) { 654 | return null; 655 | } 656 | 657 | $image_thumb = wp_get_attachment_image_src( $attachment_id, $size ); 658 | 659 | return empty( $image_thumb ) ? null : $image_thumb[0]; 660 | } 661 | 662 | /** 663 | * Get image thumbnail url of specific size by image url. 664 | * 665 | * @since 2.0.0 666 | * @access public 667 | * 668 | * @see get_image_id() 669 | * @see wp_get_attachment_image_src() 670 | * 671 | * @param string $image_url url of an image. 672 | * @param string $size size of the image to fetch (thumbnail, medium, full). 673 | * 674 | * @return string 675 | */ 676 | public function get_image_thumb( $image_url, $size = 'thumbnail' ) { 677 | $attachment_id = $this->get_image_id( $image_url ); 678 | $image_thumb = wp_get_attachment_image_src( $attachment_id, $size ); 679 | 680 | return empty( $image_thumb ) ? null : $image_thumb[0]; 681 | } 682 | 683 | /** 684 | * Gets attachment id from given image url. 685 | * 686 | * @param string $image_url url of an image. 687 | * 688 | * @since 2.0.0 689 | * @access public 690 | * 691 | * @return int|null attachment id of an image 692 | */ 693 | public function get_image_id( $image_url ) { 694 | $attachment_id = $this->get_attachment_id( $image_url ); 695 | 696 | if ( is_null( $attachment_id ) ) { 697 | /* 698 | * Check if the image is an edited image. 699 | * and try to get the attachment id. 700 | */ 701 | 702 | global $wp_version; 703 | 704 | if ( intval( $wp_version ) >= 4 ) { 705 | return attachment_url_to_postid( $image_url ); 706 | } 707 | 708 | // Fallback. 709 | $image_url = str_replace( $this->upload_url . '/', '', $image_url ); 710 | 711 | $row = $this->execute_query( $this->db->prepare( 'SELECT post_id FROM ' . $this->db->postmeta . ' WHERE meta_key = %s AND meta_value = %s', '_wp_attached_file', $image_url ) ); 712 | if ( ! is_null( $row ) ) { 713 | $attachment_id = $row; 714 | } 715 | } 716 | 717 | return $attachment_id; 718 | } 719 | 720 | /** 721 | * Get image title. 722 | * 723 | * @since 2.0.0 724 | * @access public 725 | * 726 | * @param string $image_url URL of an image. 727 | * 728 | * @return string 729 | */ 730 | public function get_image_title( $image_url ) { 731 | return $this->execute_query( $this->db->prepare( 'SELECT post_title FROM ' . $this->db->posts . ' WHERE guid = %s', $image_url ) ); 732 | } 733 | 734 | /** 735 | * Get image title by id. 736 | * 737 | * @since 2.0.0 738 | * @access public 739 | * 740 | * @param int $attachment_id Attachment id of an image. 741 | * 742 | * @return string 743 | */ 744 | public function get_image_title_by_id( $attachment_id ) { 745 | return $this->execute_query( $this->db->prepare( 'SELECT post_title FROM ' . $this->db->posts . ' WHERE ID = %d', $attachment_id ) ); 746 | } 747 | 748 | /** 749 | * Get image caption. 750 | * 751 | * @since 2.0.0 752 | * @access public 753 | * 754 | * @param string $image_url URL of an image. 755 | * 756 | * @return string 757 | */ 758 | public function get_image_caption( $image_url ) { 759 | return $this->execute_query( $this->db->prepare( 'SELECT post_excerpt FROM ' . $this->db->posts . ' WHERE guid = %s', $image_url ) ); 760 | } 761 | 762 | /** 763 | * Get image caption by id. 764 | * 765 | * @since 2.0.0 766 | * @access public 767 | * 768 | * @param int $attachment_id Attachment id of an image. 769 | * 770 | * @return string 771 | */ 772 | public function get_image_caption_by_id( $attachment_id ) { 773 | return $this->execute_query( $this->db->prepare( 'SELECT post_excerpt FROM ' . $this->db->posts . ' WHERE ID = %d', $attachment_id ) ); 774 | } 775 | 776 | /** 777 | * Get image alternate text. 778 | * 779 | * @since 2.0.0 780 | * @access public 781 | * 782 | * @see get_post_meta() 783 | * 784 | * @param string $image_url URL of an image. 785 | * 786 | * @return string 787 | */ 788 | public function get_image_alt( $image_url ) { 789 | $attachment = $this->db->get_col( $this->db->prepare( 'SELECT ID FROM ' . $this->db->posts . ' WHERE guid = %s', $image_url ) ); 790 | 791 | $alt = null; 792 | if ( ! empty( $attachment ) ) { 793 | $alt = get_post_meta( $attachment[0], '_wp_attachment_image_alt' ); 794 | } 795 | 796 | return ( is_null( $alt ) || empty( $alt ) ) ? null : $alt[0]; 797 | } 798 | 799 | /** 800 | * Get image alternate text by attachment id. 801 | * 802 | * @since 2.0.0 803 | * @access public 804 | * 805 | * @see get_post_meta() 806 | * 807 | * @param int $attachment_id Attachment id of an image. 808 | * 809 | * @return string 810 | */ 811 | public function get_image_alt_by_id( $attachment_id ) { 812 | $alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt' ); 813 | 814 | return empty( $alt ) ? null : $alt[0]; 815 | } 816 | 817 | /** 818 | * Get image description. 819 | * 820 | * @since 3.0.0 821 | * @access public 822 | * 823 | * @param string $image_url URL of an image. 824 | * 825 | * @return string 826 | */ 827 | public function get_image_description( $image_url ) { 828 | return $this->execute_query( $this->db->prepare( 'SELECT post_content FROM ' . $this->db->posts . ' WHERE guid = %s', $image_url ) ); 829 | } 830 | 831 | /** 832 | * Get image description by id. 833 | * 834 | * @since 3.0.0 835 | * @access public 836 | * 837 | * @param int $attachment_id attachment id of an image. 838 | * 839 | * @return string 840 | */ 841 | public function get_image_description_by_id( $attachment_id ) { 842 | return $this->execute_query( $this->db->prepare( 'SELECT post_content FROM ' . $this->db->posts . ' WHERE ID = %d', $attachment_id ) ); 843 | } 844 | 845 | /** 846 | * Get link to image. 847 | * 848 | * @since 3.4.0 849 | * @access public 850 | * 851 | * @param int $attachment_id Attachment id of an image. 852 | * 853 | * @return string|null 854 | */ 855 | public function get_link_to_image( $attachment_id ) { 856 | return get_post_meta( $attachment_id, '_dfi_link_to_image', true ); 857 | } 858 | 859 | /** 860 | * Get all attachment ids of the post. 861 | * 862 | * @since 2.0.0 863 | * @access public 864 | * 865 | * @see get_post_meta() 866 | * 867 | * @param int $post_id id of the current post. 868 | * 869 | * @return array 870 | */ 871 | public function get_post_attachment_ids( $post_id ) { 872 | $dfi_images = get_post_meta( $post_id, 'dfiFeatured', true ); 873 | $ret_val = array(); 874 | 875 | if ( ! empty( $dfi_images ) && is_array( $dfi_images ) ) { 876 | foreach ( $dfi_images as $dfi_image ) { 877 | $dfi_image_full = $this->separate( $dfi_image, 'full' ); 878 | $ret_val[] = (int) $this->get_image_id( $this->upload_url . $dfi_image_full ); 879 | } 880 | } 881 | 882 | return $ret_val; 883 | } 884 | 885 | /** 886 | * Get real post id. 887 | * 888 | * @since 3.6.0 889 | * @access protected 890 | * 891 | * @param int|null $post_id Post id. 892 | * 893 | * @return int|null 894 | */ 895 | protected function get_real_post_id( $post_id = null ) { 896 | if ( ! is_null( $post_id ) && is_numeric( $post_id ) ) { 897 | return $post_id; 898 | } 899 | 900 | global $post; 901 | 902 | return $post->ID; 903 | } 904 | 905 | /** 906 | * Fetches featured image data of nth position. 907 | * 908 | * @since 3.0.0 909 | * @access public 910 | * 911 | * @see get_featured_images() 912 | * 913 | * @param int $position Position of the featured image. 914 | * @param int $post_id Current post id. 915 | * 916 | * @return array if found, null otherwise. 917 | */ 918 | public function get_nth_featured_image( $position, $post_id = null ) { 919 | $post_id = $this->get_real_post_id( ( $post_id ) ); 920 | 921 | $featured_images = $this->get_featured_images( $post_id ); 922 | 923 | return isset( $featured_images[ $position - 2 ] ) ? $featured_images[ $position - 2 ] : null; 924 | } 925 | 926 | /** 927 | * Check if the image is attached with the particular post. 928 | * 929 | * @since 2.0.0 930 | * @access public 931 | * 932 | * @see get_post_attachment_ids() 933 | * 934 | * @param int $attachment_id Attachment id of an image. 935 | * @param int $post_id Current post id. 936 | * 937 | * @return bool 938 | */ 939 | public function is_attached( $attachment_id, $post_id ) { 940 | if ( empty( $attachment_id ) ) { 941 | return false; 942 | } 943 | 944 | $attachment_ids = $this->get_post_attachment_ids( $post_id ); 945 | 946 | return in_array( $attachment_id, $attachment_ids, true ) ? true : false; 947 | } 948 | 949 | /** 950 | * Retrieve featured images for specific post(s). 951 | * 952 | * @since 2.0.0 953 | * @access public 954 | * 955 | * @see get_post_meta() 956 | * 957 | * @param int $post_id id of the current post. 958 | * 959 | * @return array 960 | */ 961 | public function get_featured_images( $post_id = null ) { 962 | $post_id = $this->get_real_post_id( $post_id ); 963 | $dfi_images = get_post_meta( $post_id, 'dfiFeatured', true ); 964 | $ret_images = array(); 965 | 966 | if ( ! empty( $dfi_images ) && is_array( $dfi_images ) ) { 967 | $dfi_images = array_filter( $dfi_images ); 968 | 969 | $count = 0; 970 | foreach ( $dfi_images as $dfi_image ) { 971 | $dfi_image_trimmed = $this->separate( $dfi_image ); 972 | $dfi_image_full = $this->separate( $dfi_image, 'full' ); 973 | 974 | try { 975 | $ret_images[ $count ]['thumb'] = $this->get_real_upload_path( $dfi_image_trimmed ); 976 | $ret_images[ $count ]['full'] = $this->get_real_upload_path( $dfi_image_full ); 977 | $ret_images[ $count ]['attachment_id'] = $this->get_image_id( $ret_images[ $count ]['full'] ); 978 | } catch ( Exception $e ) { 979 | /* Ignore the exception and continue with other featured images */ 980 | } 981 | 982 | $count ++; 983 | } 984 | } 985 | 986 | return $ret_images; 987 | } 988 | 989 | /** 990 | * Check to see if the upload url is already available in path. 991 | * 992 | * @since 3.1.14 993 | * @access protected 994 | * 995 | * @param string $img Uploaded image. 996 | * 997 | * @return string 998 | */ 999 | protected function get_real_upload_path( $img ) { 1000 | // check if upload path is already attached. 1001 | if ( false !== strpos( $img, $this->upload_url ) || preg_match( '/https?:\/\//', $img ) ) { 1002 | return $img; 1003 | } 1004 | 1005 | return $this->upload_url . $img; 1006 | } 1007 | 1008 | /** 1009 | * Retrieve featured images for specific post(s) including the default Featured Image. 1010 | * 1011 | * @since 3.1.7 1012 | * @access public 1013 | * 1014 | * @see $this->get_featured_images() 1015 | * 1016 | * @param int $post_id Current post id. 1017 | * 1018 | * @return array An array of images or an empty array on failure 1019 | */ 1020 | public function get_all_featured_images( $post_id = null ) { 1021 | $post_id = $this->get_real_post_id( $post_id ); 1022 | $thumbnail_id = get_post_thumbnail_id( $post_id ); 1023 | $all_images = array(); 1024 | 1025 | if ( ! empty( $thumbnail_id ) ) { 1026 | $featured_image = array( 1027 | 'thumb' => wp_get_attachment_thumb_url( $thumbnail_id ), 1028 | 'full' => wp_get_attachment_url( $thumbnail_id ), 1029 | 'attachment_id' => $thumbnail_id, 1030 | ); 1031 | 1032 | $all_images[] = $featured_image; 1033 | } 1034 | 1035 | return array_merge( $all_images, $this->get_featured_images( $post_id ) ); 1036 | } 1037 | 1038 | /** 1039 | * Load the plugin's textdomain hooked to 'plugins_loaded'. 1040 | * 1041 | * @since 1.0.0 1042 | * @access public 1043 | * 1044 | * @see load_plugin_textdomain() 1045 | * @see plugin_basename() 1046 | * @action plugins_loaded 1047 | * 1048 | * @codeCoverageIgnore 1049 | * 1050 | * @return void 1051 | */ 1052 | public function load_plugin_textdomain() { 1053 | load_plugin_textdomain( 1054 | self::TEXT_DOMAIN, 1055 | false, 1056 | dirname( plugin_basename( __FILE__ ) ) . '/languages/' 1057 | ); 1058 | } 1059 | } 1060 | 1061 | // Sponsors who support this plugin. 1062 | include 'sponsors.php'; 1063 | 1064 | /** 1065 | * Instantiate the main class. 1066 | * 1067 | * @since 1.0.0 1068 | * @access public 1069 | * 1070 | * @var object $dynamic_featured_image holds the instantiated class {@uses Dynamic_Featured_Image} 1071 | */ 1072 | global $dynamic_featured_image; 1073 | $dynamic_featured_image = new Dynamic_Featured_Image(); 1074 | --------------------------------------------------------------------------------