# Check if plugin is installed
- name: identify installation plugin
  shell: "wp plugin is-installed {{ item.1 }}"
  args:
    chdir: "{{ item.0.path }}"
  register: check_plugins
  failed_when: False
  changed_when: False
  with_subelements:
    - wordpress_installs
    - plugins
  when: item.1
  tags: [configuration, wordpress, wordpress-plugins, wordpress-is-installed-plugin, wordpress-update, wordpress-update-plugin]

# Install plugins
- name: install plugin
  shell: "wp --path='{{ item.item.0.path }}' plugin install {{ item.item.1 }}"
  with_items: check_plugins.results
  when: check_plugins is defined and item.item.1 and item.rc != 0
  tags: [configuration, wordpress, wordpress-plugins, wordpress-install-plugin]

- name: check install (plugin)
  shell: "wp --path='{{ item.0.path }}' plugin is-installed {{ item.1 }}"
  changed_when: False
  with_subelements:
    - wordpress_installs
    - plugins
  when: item.1
  tags: [configuration, wordpress, wordpress-plugins, wordpress-install-plugin-check]

# Update plugins
- name: update (plugin)
  shell: "wp --path='{{ item.item.0.path }}' plugin update {{ item.item.1 }}"
  with_items: check_plugins.results
  when: check_plugins is defined and item.item.1 and item.rc == 0 and item.item.0.plugins_update == 1
  tags: [configuration, wordpress, wordpress-plugins, wordpress-update, wordpress-update-plugin]
