# Check if themes are installed
- name: identify installation (theme)
  shell: "wp --no-color --path='{{ item.0.path }}' theme is-installed {{ item.1 }}"
  register: check_themes
  failed_when: False
  changed_when: False
  with_subelements:
    - wordpress_installs
    - themes
  when: item.1
  tags: [configuration, wordpress, wordpress-themes, wordpress-is-installed-theme, wordpress-update, wordpress-update-theme]

# Install themes
- name: install (theme)
  shell: "wp --no-color --path='{{ item.item.0.path }}' theme install {{ item.item.1 }}"
  with_items: check_themes.results
  when: check_themes is defined and item.item.1 and item.rc != 0
  tags: [configuration, wordpress, wordpress-themes, wordpress-install-theme]

- name: check install (theme)
  shell: "wp --no-color --path='{{ item.0.path }}' theme is-installed {{ item.1 }}"
  changed_when: False
  with_subelements:
    - wordpress_installs
    - themes
  when: item.1
  tags: [configuration, wordpress, wordpress-themes, wordpress-install-theme-check]
  
# Update themes
- name: update (theme)
  shell: "wp --no-color --path='{{ item.item.0.path }}' theme update {{ item.item.1 }}"
  with_items: check_themes.results
  when: check_themes is defined and item.item.1 and item.rc == 0 and themes_update == 1
  tags: [configuration, wordpress, wordpress-themes, wordpress-update, wordpress-update-theme]

