There is a lot more involved than just this. Both plants and animals have millions of years of natural selection in which they compete and cooperate with each other. For example, a lot of animals have evolved so that they can use plant poisons against insects without harm.
Onions, garlic, hot peppers, tobacco, coffee, the list is enormous. All have evolved defenses that we now like, regard as desirable, and cultivate and improve on.
At the same time, many plants produce fruit that is *intended* to be eaten, but not the seeds within, which can pass through an animals digestion unharmed to be deposited elsewhere. Yet it is only supposed to be eaten when ripe, so changes colors to let animals know.
Examples of all the back and forth could go on for a long time, so the idea that plants can detect when they are being damaged and react to it is pretty basic.
Tree’s are at war..constantly.